3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
20 #ifndef LEMON_TABU_SEARCH_H
21 #define LEMON_TABU_SEARCH_H
25 /// \brief TabuSearch algorithm.
27 /// \author Szabadkai Mark
29 #include <lemon/bits/utility.h>
30 #include <lemon/error.h>
31 #include <lemon/time_measure.h>
38 /// \brief Default Traits for TabuSearch class.
40 /// This template defines the needed types for the \ref TabuSearch class.
41 /// Is main purpos is to simplify the main class's template interface,
42 /// but it provides the EdgeIt type, passing to the concrete graph wheter
43 /// it is directed or undirected.
45 template< typename GRAPH, typename VALUE,
46 typename HEIGHTMAP, typename BETTER, bool UNDIR >
48 template< typename GRAPH, typename VALUE,
49 typename HEIGHTMAP = typename GRAPH::template NodeMap<VALUE>,
50 typename BETTER = std::less<VALUE>,
51 bool UNDIR = UndirectedTagIndicator<GRAPH>::value >
53 struct TabuSearchDefaultTraits {
55 typedef BETTER Better;
58 typedef typename GRAPH::Node Node;
59 typedef HEIGHTMAP HeightMap;
61 typedef typename GRAPH::IncEdgeIt EdgeIt;
64 template< typename GRAPH, typename VALUE,
65 typename HEIGHTMAP, typename BETTER >
66 struct TabuSearchDefaultTraits< GRAPH, VALUE, HEIGHTMAP, BETTER, false > {
68 typedef BETTER Better;
71 typedef typename GRAPH::Node Node;
72 typedef HEIGHTMAP HeightMap;
74 typedef typename GRAPH::OutEdgeIt EdgeIt;
79 /// \brief Policy hierarchy to controll the search algorithm.
81 /// The fallowing template hierarchy offers a clean interface to define own
82 /// policies, and combine existing ones.
83 template< typename TS >
84 struct TabuSearchPolicyConcept {
85 void target( TS *ts ) {}
88 bool onStep() { return false; }
89 bool onStick() { return false; }
90 bool onImprove( const typename TS::Value &best ) { return false; }
93 template< typename TS >
95 void target( TS *ts ) {}
98 bool onStep() { return true; }
99 bool onStick() { return true; }
100 bool onImprove( const typename TS::Value &best ) { return true; }
103 template< typename TS >
104 struct NoPolicy : public TabuSearchPolicyConcept<TS> {};
106 /// \brief Some basic methode, how tow Policies can be combined
107 struct PolicyAndCombination {
108 static bool evaluate( const bool r1, const bool r2 ) {
113 struct PolicyOrCombination {
114 static bool evaluate( const bool r1, const bool r2 ) {
119 /// \brief CombinePolicies
121 /// It combines tow policies using the given combination methode (mainly
122 /// some of the basic logical methodes) to create a new one.
124 template< template<typename> class CP1, template<typename> class CP2,
125 typename COMBINATION >
127 template< template<typename> class CP1, template<typename> class CP2,
128 typename COMBINATION = PolicyAndCombination >
130 struct CombinePolicies {
131 template< typename TS >
133 typedef CP1<TS> Policy1;
134 typedef CP2<TS> Policy2;
139 inline Policy() : policy1(), policy2() {}
140 inline Policy( const Policy1 &cp1, const Policy2 &cp2 )
141 : policy1(cp1), policy2(cp2) {}
143 void target( TS *ts ) {
144 policy1.target(ts), policy2.target(ts);
148 policy1.reset(), policy2.reset();
152 return cmb.evaluate( policy1.onStep(), policy2.onStep() );
156 return cmb.evaluate( policy1.onStick(), policy2.onStick() );
159 bool onImprove( const typename TS::Value &best ) {
160 return cmb.evaluate( policy1.onImprove(best),
161 policy2.onImprove(best) );
170 /// \brief IterationPolicy limits the number of iterations and the
171 /// number of iterations without improvement
172 template< typename TS >
173 struct IterationPolicy {
174 IterationPolicy() : _it_lim(100000), _noimpr_it_lim(5000) {}
175 IterationPolicy( const long int itl, const long int noimpritl )
176 : _it_lim(itl), _noimpr_it_lim(noimpritl)
179 void target( TS *ts ) {}
182 _it = _noimpr_it = 0;
187 return (_it <= _it_lim) && (_noimpr_it <= _noimpr_it_lim);
194 bool onImprove( const typename TS::Value &best ) {
199 long int iterationLimit() const {
203 void iterationLimit( const long int itl ) {
207 long int noImprovementIterationLimit() const {
208 return _noimpr_it_lim;
211 void noImprovementIterationLimit( const long int noimpritl ) {
212 _noimpr_it_lim = noimpritl;
216 long int _it_lim, _noimpr_it_lim;
217 long int _it, _noimpr_it;
220 /// \brief HeightPolicy stops the search when a given height is reached or
222 template< typename TS >
223 struct HeightPolicy {
224 typedef typename TS::Value Value;
226 HeightPolicy() : _height_lim(), _found(false) {}
227 HeightPolicy( const Value &hl ) : _height_lim(hl), _found(false) {}
229 void target( TS *ts ) {}
243 bool onImprove( const Value &best ) {
244 typename TS::Better better;
245 _found = better(best, _height_lim) || (best == _height_lim);
249 Value heightLimi() const {
253 void heightLimi( const Value &hl ) {
262 /// \brief TimePolicy limits the time for searching.
263 template< typename TS >
265 TimePolicy() : _time_lim(60.0), _timeisup(false) {}
266 TimePolicy( const double tl ) : _time_lim(tl), _timeisup(false) {}
268 void target( TS *ts ) {}
284 bool onImprove( const typename TS::Value &best ) {
289 double timeLimit() const {
293 void setTimeLimit( const double tl ) {
303 inline void update() {
304 _timeisup = _t.realTime() > _time_lim;
312 /// \brief TabuSearch main class
314 /// This class offers the implementation of tabu-search algorithm. The
315 /// tabu-serach is a local-search. It starts from a specified point of the
316 /// problem's graph representation, and in every step it goes to the localy
317 /// best next Node except those in tabu set. The maximum size of this tabu
318 /// set defines how many Node will be remembered. The best Node ever found
319 /// will also stored, so we wont lose it, even is the search continues.
320 /// The class can be used on any kind of Graph and with any kind of Value
321 /// with a total-settlement on it.
323 /// \param _Graph The graph type the algorithm runs on.
324 /// \param _Value The values' type associated to the nodes.
325 /// \param _Policy Controlls the search. Determinates when to stop, or how
326 /// manage stuck search. Default value is \ref IterationPolicy .
327 /// \param _Traits Collection of needed types. Default value is
328 /// \ref TabuSearchDefaultTraits .
330 /// \author Szabadkai Mark
332 template< typename GRAPH, typename VALUE, template<typename> class POLICY, typename TRAITS >
334 template< typename GRAPH, typename VALUE,
335 template<typename> class POLICY = IterationPolicy,
336 typename TRAITS = TabuSearchDefaultTraits<GRAPH, VALUE> >
342 /// \brief Thrown by setting the size of the tabu-set and the given size
344 class BadParameterError : public lemon::LogicError {
346 virtual const char* what() const throw() {
347 return "lemon::TabuSearch::BadParameterError";
352 typedef TabuSearch<GRAPH,VALUE,POLICY,TRAITS> SelfType;
354 typedef typename TRAITS::Graph Graph;
355 typedef typename TRAITS::Node Node;
356 typedef typename TRAITS::Value Value;
357 typedef typename TRAITS::HeightMap HeightMap;
358 typedef typename TRAITS::Better Better;
359 typedef typename std::deque< Node >::const_iterator TabuIterator;
361 typedef POLICY<SelfType> Policy;
364 typedef typename TRAITS::EdgeIt EdgeIt;
367 const HeightMap &height;
368 /// The tabu set. Teh current node is the first
369 std::deque< Node > tabu;
370 /// Maximal tabu size
372 /// The best Node found
379 /// \brief Constructor
381 /// \param graph the graph the algorithm will run on.
382 /// \param hm the height map used by the algorithm.
383 /// \param tabusz the maximal size of the tabu set. Default value is 3
384 /// \param p the Policy controlling the search.
385 TabuSearch( const Graph &graph, const HeightMap &hm,
386 const int tabusz = 3, Policy p = Policy() )
387 : gr(graph), height(hm), mts(tabusz), pol(p)
392 /// \brief Destructor
397 /// Set/Get the size of the tabu set
398 void tabuSize( const unsigned int size )
401 throw BadParameterError( "Tabu size must be at least 2!" );
403 while( mts < tabu.size() )
407 unsigned int tabuSize() const {
412 void policy( Policy p ) {
422 /// \name Execution control
423 /// The simplest way to execute the algorithm is to use the member
424 /// functions called \c run( 'startnode' ).
427 /// \brief Initializes the internal data.
429 /// \param startn The start node where the search begins.
430 void init( const Node startn ) {
432 tabu.push_front( startn );
437 /// \brief Does one iteration
439 /// If the Policy allows it searches for the best next node, then steps
441 /// \return %False if one Policy condition wants to stop the search.
444 ///Request premmision from ControllPolicy
448 ///Find the best next potential node
449 Node n; bool found = false;
450 for( EdgeIt e(gr,tabu[0]); e != INVALID; ++e )
452 Node m = (gr.source(e) == tabu[0]) ? gr.target(e) : gr.source(e);
454 for( int i = 1; i != (signed int)tabu.size(); ++i )
466 if( better(height[m], height[n]) ) {
471 ///Handle stuck search
473 return pol.onStick();
478 while( mts < tabu.size() ) {
481 if( better(height[n], height[b]) ) {
483 if( !pol.onImprove(height[b]) )
490 /// \brief Runs a search while the Policy stops it.
492 /// \param startn The start node where the search begins.
493 inline void run( const Node startn ) {
494 std::cin.unsetf( std::ios_base::skipws );
499 std::cin.setf( std::ios_base::skipws );
504 /// \name Query Functions
505 /// The result of the TabuSearch algorithm can be obtained using these
509 /// \brief The node, the search is standing on.
510 inline Node current() const {
514 /// \brief The best node found until now.
515 inline Node best() const {
519 /// \brief Beginning to iterate on the current tabu set.
520 inline TabuIterator tabu_begin() const {
524 /// \brief Ending to iterate on the current tabu set.
525 inline TabuIterator tabu_end() const {