From 747be27d1a9e231df631c75c33cd6055dfb03fd4 Mon Sep 17 00:00:00 2001 From: jboisson Date: Wed, 20 Feb 2008 14:41:23 +0000 Subject: [PATCH] hill_climbing.cpp and moRandImprSelect.h have been updated git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@977 331e1502-861f-0410-8da2-ba01fb791d7f --- trunk/paradiseo-mo/src/moRandImprSelect.h | 15 ++++++--- .../tutorial/Lesson1/hill_climbing.cpp | 31 ++++++++++++++++--- trunk/paradiseo-mo/tutorial/Lesson1/param | 5 +-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/trunk/paradiseo-mo/src/moRandImprSelect.h b/trunk/paradiseo-mo/src/moRandImprSelect.h index 71e2fd51f..7d379ac3c 100755 --- a/trunk/paradiseo-mo/src/moRandImprSelect.h +++ b/trunk/paradiseo-mo/src/moRandImprSelect.h @@ -65,6 +65,7 @@ class moRandImprSelect: public moMoveSelect < M > initial_fitness = _fitness; better_fitnesses.clear(); better_moves.clear(); + firstTime=true; } //! Function that updates the fitness and move vectors @@ -78,14 +79,13 @@ class moRandImprSelect: public moMoveSelect < M > */ bool update (const M & _move, const Fitness & _fitness) { + firstTime=false; if (_fitness > initial_fitness) { better_fitnesses.push_back(_fitness); better_moves.push_back(_move); } - - return true; } //! The move selection @@ -100,10 +100,14 @@ class moRandImprSelect: public moMoveSelect < M > unsigned int index; index=0; - + if( (better_fitnesses.size()==0) || (better_moves.size()==0) ) { - throw std::runtime_error("[moRandImprSelect.h]: no move or/and no fitness already saved, update has to be called first."); + if(firstTime) + { + throw std::runtime_error("[moRandImprSelect.h]: no move or/and no fitness already saved, update has to be called first."); + } + return; } index = rng.random (better_fitnesses.size ()); @@ -122,6 +126,9 @@ class moRandImprSelect: public moMoveSelect < M > //! Candidate move vector. std::vector < M > better_moves; + + //! Indicate if update has been called or not. + bool firstTime; }; #endif diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/hill_climbing.cpp b/trunk/paradiseo-mo/tutorial/Lesson1/hill_climbing.cpp index c34cecc51..62828d4ed 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson1/hill_climbing.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson1/hill_climbing.cpp @@ -42,7 +42,7 @@ void manage_configuration_file(eoParser & _parser); int main (int _argc, char* _argv []) { - std::string instancePath; + std::string instancePath, selectionType; unsigned int seed; eoParser parser(_argc, _argv); @@ -51,6 +51,7 @@ main (int _argc, char* _argv []) seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() ); instancePath=parser.getParamWithLongName("instancePath")->getValue(); + selectionType=parser.getParamWithLongName("selectionType")->getValue(); srand (seed); Graph::load(instancePath.c_str()); @@ -74,16 +75,33 @@ main (int _argc, char* _argv []) TwoOptIncrEval two_opt_incremental_evaluation; - //moFirstImprSelect two_opt_selection; - moBestImprSelect two_opt_selection; - //moRandImprSelect two_opt_selection; + moMoveSelect* two_opt_selection; + + if(selectionType.compare("Best")==0) + { + two_opt_selection= new moBestImprSelect(); + } + else if (selectionType.compare("First")==0) + { + two_opt_selection= new moFirstImprSelect(); + } + else if (selectionType.compare("Random")==0) + { + two_opt_selection= new moRandImprSelect(); + } + else + { + throw std::runtime_error("[hill_climbing.cpp]: the type of selection '"+selectionType+"' is not correct."); + } moHC hill_climbing (two_opt_initializer, two_opt_next_move_generator, two_opt_incremental_evaluation, - two_opt_selection, full_evaluation); + *two_opt_selection, full_evaluation); hill_climbing (solution) ; std :: cout << "[To] " << solution << std :: endl; + delete(two_opt_selection); + return EXIT_SUCCESS; } @@ -96,6 +114,9 @@ manage_configuration_file(eoParser & _parser) 0, "Configuration", false); _parser.getORcreateParam((unsigned int)time(0), "seed", "Seed for rand.", 0, "Configuration", false); + _parser.getORcreateParam(std::string("Best"), "selectionType", "Type of the selection: 'Best', 'First' or 'Random'.", + 0, "Configuration", false); + if (_parser.userNeedsHelp()) { _parser.printHelp(std::cout); diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/param b/trunk/paradiseo-mo/tutorial/Lesson1/param index 766f1adf5..8bdb97f37 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson1/param +++ b/trunk/paradiseo-mo/tutorial/Lesson1/param @@ -4,5 +4,6 @@ # --stopOnUnknownParam=1 # Stop if unkown param entered ###### Configuration ###### -# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance -# --seed=1202916317 # Seed for rand +# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance. +# --seed=1203517190 # Seed for rand. +# --selectionType=Best # Type of the selection: 'Best', 'First' or 'Random'.