diff --git a/trunk/paradiseo-mo/src/moSimpleSolutionTabuList.h b/trunk/paradiseo-mo/src/moSimpleSolutionTabuList.h index 9b5a7797f..6cc1459e1 100755 --- a/trunk/paradiseo-mo/src/moSimpleSolutionTabuList.h +++ b/trunk/paradiseo-mo/src/moSimpleSolutionTabuList.h @@ -91,7 +91,7 @@ class moSimpleSolutionTabuList: public moTabuList < M > M move=(M)_move; EOT solution=(EOT) _solution; - _move(_solution); + move(solution); if (memory_size!=0) { diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/hill_climbing.cpp b/trunk/paradiseo-mo/tutorial/Lesson1/hill_climbing.cpp index 2b3833b7b..c34cecc51 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson1/hill_climbing.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson1/hill_climbing.cpp @@ -33,22 +33,27 @@ Contact: paradiseo-help@lists.gforge.inria.fr */ +#include #include #include +void manage_configuration_file(eoParser & _parser); + int main (int _argc, char* _argv []) { - if (_argc != 2) - { + std::string instancePath; + unsigned int seed; - std :: cerr << "Usage : ./hill_climbing [instance]" << std :: endl; - return EXIT_FAILURE; - } + eoParser parser(_argc, _argv); - srand (1000); + manage_configuration_file(parser); - Graph::load(_argv [1]); + seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() ); + instancePath=parser.getParamWithLongName("instancePath")->getValue(); + + srand (seed); + Graph::load(instancePath.c_str()); Route solution; @@ -82,3 +87,26 @@ main (int _argc, char* _argv []) return EXIT_SUCCESS; } +void +manage_configuration_file(eoParser & _parser) +{ + std::ofstream os; + + _parser.getORcreateParam(std::string("../examples/tsp/benchs/berlin52.tsp"), "instancePath", "Path to the instance.", + 0, "Configuration", false); + _parser.getORcreateParam((unsigned int)time(0), "seed", "Seed for rand.", 0, "Configuration", false); + + if (_parser.userNeedsHelp()) + { + _parser.printHelp(std::cout); + exit(EXIT_FAILURE); + } + + os.open("current_param"); + if(!os.is_open()) + { + throw std::runtime_error("[hill_climbing.cpp]: the file current_param cannot be created."); + } + os <<_parser; + os.close(); +} diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/lesson_1.pdf b/trunk/paradiseo-mo/tutorial/Lesson1/lesson_1.pdf deleted file mode 100644 index 7b9e4d80b..000000000 Binary files a/trunk/paradiseo-mo/tutorial/Lesson1/lesson_1.pdf and /dev/null differ diff --git a/trunk/paradiseo-mo/tutorial/Lesson1/param b/trunk/paradiseo-mo/tutorial/Lesson1/param new file mode 100644 index 000000000..766f1adf5 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson1/param @@ -0,0 +1,8 @@ + +###### General ###### +# --help=0 # -h : Prints this message +# --stopOnUnknownParam=1 # Stop if unkown param entered + +###### Configuration ###### +# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance +# --seed=1202916317 # Seed for rand diff --git a/trunk/paradiseo-mo/tutorial/Lesson2/lesson_2.pdf b/trunk/paradiseo-mo/tutorial/Lesson2/lesson_2.pdf deleted file mode 100644 index 06b152206..000000000 Binary files a/trunk/paradiseo-mo/tutorial/Lesson2/lesson_2.pdf and /dev/null differ diff --git a/trunk/paradiseo-mo/tutorial/Lesson2/param b/trunk/paradiseo-mo/tutorial/Lesson2/param new file mode 100644 index 000000000..53d46bed9 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson2/param @@ -0,0 +1,11 @@ + +###### General ###### +# --help=0 # -h : Prints this message +# --stopOnUnknownParam=1 # Stop if unkown param entered + +###### Configuration ###### +# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance +# --seed=1202917905 # Seed for rand +# --tabuListSize=10 # Size of the tabu list +# --maxIter=1000 # Maximum number of iterations +# --tabuListType=TwoOpt # Type of the tabu list: 'TwoOpt', 'SimpleMove' or 'SimpleSolution' diff --git a/trunk/paradiseo-mo/tutorial/Lesson2/tabu_search.cpp b/trunk/paradiseo-mo/tutorial/Lesson2/tabu_search.cpp index 6cc7e1a5c..df8e6af24 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson2/tabu_search.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson2/tabu_search.cpp @@ -34,19 +34,30 @@ */ +#include #include #include +void manage_configuration_file(eoParser & _parser); + int main (int _argc, char* _argv []) { - if (_argc != 2) - { - std :: cerr << "Usage : ./tabu_search [instance]" << std :: endl ; - return 1 ; - } + std::string instancePath, value; + unsigned int seed, maxIterations, tabuListSize; - Graph::load (_argv [1]); + eoParser parser(_argc, _argv); + + manage_configuration_file(parser); + + seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() ); + maxIterations=atoi( (parser.getParamWithLongName("maxIter")->getValue()).c_str() ); + tabuListSize=atoi( (parser.getParamWithLongName("tabuListSize")->getValue()).c_str() ); + instancePath=parser.getParamWithLongName("instancePath")->getValue(); + value=parser.getParamWithLongName("tabuListType")->getValue(); + + srand (seed); + Graph::load(instancePath.c_str()); Route solution; @@ -67,20 +78,67 @@ main (int _argc, char* _argv []) TwoOptIncrEval two_opt_incremental_evaluation; - TwoOptTabuList tabu_list; - //moSimpleMoveTabuList tabu_list(10); - //moSimpleSolutionTabuList tabu_list(10); + moTabuList *tabuList; + + if(value.compare("TwoOpt")==0) + { + tabuList=new TwoOptTabuList(); + } + else if (value.compare("SimpleMove")==0) + { + tabuList=new moSimpleMoveTabuList(tabuListSize); + } + else if (value.compare("SimpleSolution")==0) + { + tabuList=new moSimpleSolutionTabuList(tabuListSize); + } + else + { + throw std::runtime_error("[tabu_search.cpp]: the type of tabu list '"+value+"' is not correct."); + } moNoAspirCrit aspiration_criterion; - moGenSolContinue continu (10000); + moGenSolContinue continu (maxIterations); moTS tabu_search (two_opt_initializer, two_opt_next_move_generator, - two_opt_incremental_evaluation, tabu_list, aspiration_criterion, continu, full_evaluation); + two_opt_incremental_evaluation, *tabuList, aspiration_criterion, continu, full_evaluation); tabu_search(solution); std :: cout << "[To] " << solution << std :: endl; + delete(tabuList); + return EXIT_SUCCESS; } +void +manage_configuration_file(eoParser & _parser) +{ + std::ofstream os; + + _parser.getORcreateParam(std::string("../examples/tsp/benchs/berlin52.tsp"), "instancePath", "Path to the instance.", + 0, "Configuration", false); + _parser.getORcreateParam((unsigned int)time(0), "seed", "Seed for rand.", 0, "Configuration", false); + + _parser.getORcreateParam((unsigned int)10, "tabuListSize", "Size of the tabu list.", 0, "Configuration", false); + + _parser.getORcreateParam((unsigned int)1000, "maxIter", "Maximum number of iterations.", 0, "Configuration", false); + + _parser.getORcreateParam(std::string("TwoOpt"), "tabuListType", "Type of the tabu list: 'TwoOpt', 'SimpleMove' or 'SimpleSolution'.", + 0, "Configuration", false); + + if (_parser.userNeedsHelp()) + { + _parser.printHelp(std::cout); + exit(EXIT_FAILURE); + } + + os.open("current_param"); + if(!os.is_open()) + { + throw std::runtime_error("[tabu_search.cpp]: the file current_param cannot be created."); + } + os <<_parser; + os.close(); +} diff --git a/trunk/paradiseo-mo/tutorial/Lesson3/lesson_3.pdf b/trunk/paradiseo-mo/tutorial/Lesson3/lesson_3.pdf deleted file mode 100644 index e9f5bd0a1..000000000 Binary files a/trunk/paradiseo-mo/tutorial/Lesson3/lesson_3.pdf and /dev/null differ diff --git a/trunk/paradiseo-mo/tutorial/Lesson3/param b/trunk/paradiseo-mo/tutorial/Lesson3/param new file mode 100644 index 000000000..d80626b17 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson3/param @@ -0,0 +1,14 @@ + +###### General ###### +# --help=0 # -h : Prints this message +# --stopOnUnknownParam=1 # Stop if unkown param entered + +###### Configuration ###### +# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance. +# --seed=1202919978 # Seed for rand. +# --maxIter=1000 # Maximum number of iterations. +# --initialTemp=1000 # Initial temperature. +# --threshold=0.1 # Minimum temperature allowed. +# --expoRatio=0.98 # Ratio used if exponential cooling schedule is chosen. +# --lineaRatio=0.5 # Ratio used if linear cooling schedule is chosen. +# --coolSchedType=Expo # Type the cooling schedule: 'Expo' or 'Linear'. diff --git a/trunk/paradiseo-mo/tutorial/Lesson3/simulated_annealing.cpp b/trunk/paradiseo-mo/tutorial/Lesson3/simulated_annealing.cpp index 3a32ff509..93d393047 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson3/simulated_annealing.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson3/simulated_annealing.cpp @@ -33,19 +33,34 @@ Contact: paradiseo-help@lists.gforge.inria.fr */ +#include #include #include +void manage_configuration_file(eoParser & _parser); + int main (int _argc, char* _argv []) { - if (_argc != 2) - { - std :: cerr << "Usage : ./simulated_annealing [instance]" << std :: endl; - return EXIT_FAILURE; - } + std::string instancePath, value; + unsigned int seed, maxIterations; + double threshold, exponentialRatio, linearRatio, initialTemperature; - Graph::load (_argv [1]); + eoParser parser(_argc, _argv); + + manage_configuration_file(parser); + + seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() ); + instancePath=parser.getParamWithLongName("instancePath")->getValue(); + maxIterations=atoi( (parser.getParamWithLongName("maxIter")->getValue()).c_str() ); + initialTemperature=atof( (parser.getParamWithLongName("initialTemp")->getValue()).c_str() ); + threshold=atof( (parser.getParamWithLongName("threshold")->getValue()).c_str() ); + exponentialRatio=atof( (parser.getParamWithLongName("expoRatio")->getValue()).c_str() ); + linearRatio=atof( (parser.getParamWithLongName("lineaRatio")->getValue()).c_str() ); + value=parser.getParamWithLongName("coolSchedType")->getValue(); + + srand (seed); + Graph::load(instancePath.c_str()); Route solution; @@ -66,19 +81,67 @@ main (int _argc, char* _argv []) TwoOpt move; - moExponentialCoolingSchedule cooling_schedule (0.1, 0.98); - //moLinearCoolingSchedule cooling_schedule (0.1, 0.5); + moCoolingSchedule* coolingSchedule; - moGenSolContinue continu (1000); /* Temperature Descreasing - will occur each 1000 - iterations */ + if(value.compare("Expo")==0) + { + coolingSchedule=new moExponentialCoolingSchedule(threshold, exponentialRatio); + } + else if (value.compare("Linear")==0) + { + coolingSchedule=new moLinearCoolingSchedule(threshold, linearRatio); + } + else + { + throw std::runtime_error("[simulated_annealing.cpp]: the type of cooling schedule '"+value+"' is not correct."); + } + + moGenSolContinue continu (maxIterations); moSA simulated_annealing (two_opt_random_move_generator, two_opt_incremental_evaluation, - continu, 1000, cooling_schedule, full_evaluation); + continu, initialTemperature, *coolingSchedule, full_evaluation); simulated_annealing (solution); std :: cout << "[To] " << solution << std :: endl; + delete(coolingSchedule); + return EXIT_SUCCESS ; } +void +manage_configuration_file(eoParser & _parser) +{ + std::ofstream os; + + _parser.getORcreateParam(std::string("../examples/tsp/benchs/berlin52.tsp"), "instancePath", "Path to the instance.", + 0, "Configuration", false); + _parser.getORcreateParam((unsigned int)time(0), "seed", "Seed for rand.", 0, "Configuration", false); + + _parser.getORcreateParam((unsigned int)1000, "maxIter", "Maximum number of iterations.", 0, "Configuration", false); + + _parser.getORcreateParam((double)1000, "initialTemp", "Initial temperature.", 0, "Configuration", false); + + _parser.getORcreateParam((double)0.1, "threshold", "Minimum temperature allowed.", 0, "Configuration", false); + + _parser.getORcreateParam((double)0.98, "expoRatio", "Ratio used if exponential cooling schedule is chosen.", 0, "Configuration", false); + + _parser.getORcreateParam((double)0.5, "lineaRatio", "Ratio used if linear cooling schedule is chosen.", 0, "Configuration", false); + + _parser.getORcreateParam(std::string("Expo"), "coolSchedType", "Type the cooling schedule: 'Expo' or 'Linear'.", + 0, "Configuration", false); + + if (_parser.userNeedsHelp()) + { + _parser.printHelp(std::cout); + exit(EXIT_FAILURE); + } + + os.open("current_param"); + if(!os.is_open()) + { + throw std::runtime_error("[simulated_annealing.cpp]: the file current_param cannot be created."); + } + os <<_parser; + os.close(); +} diff --git a/trunk/paradiseo-mo/tutorial/Lesson4/iterated_local_search.cpp b/trunk/paradiseo-mo/tutorial/Lesson4/iterated_local_search.cpp index 0ff328049..fbf6073bf 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson4/iterated_local_search.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson4/iterated_local_search.cpp @@ -33,19 +33,28 @@ Contact: paradiseo-help@lists.gforge.inria.fr */ +#include #include #include +void manage_configuration_file(eoParser & _parser); + int main (int _argc, char* _argv []) { - if (_argc != 2) - { - std :: cerr << "Usage : ./iterated_local_search [instance]" << std :: endl ; - return EXIT_FAILURE; - } + std::string instancePath; + unsigned int seed, maxIterations; - Graph::load (_argv [1]); + eoParser parser(_argc, _argv); + + manage_configuration_file(parser); + + seed=atoi( (parser.getParamWithLongName("seed")->getValue()).c_str() ); + instancePath=parser.getParamWithLongName("instancePath")->getValue(); + maxIterations=atoi( (parser.getParamWithLongName("maxIter")->getValue()).c_str() ); + + srand(seed); + Graph::load (instancePath.c_str()); Route solution; @@ -65,7 +74,7 @@ main (int _argc, char* _argv []) moBestImprSelect two_opt_selection; - moGenSolContinue continu(1000); + moGenSolContinue continu(maxIterations); moFitComparator comparator; @@ -80,3 +89,28 @@ main (int _argc, char* _argv []) return EXIT_SUCCESS; } +void +manage_configuration_file(eoParser & _parser) +{ + std::ofstream os; + + _parser.getORcreateParam(std::string("../examples/tsp/benchs/berlin52.tsp"), "instancePath", "Path to the instance.", + 0, "Configuration", false); + _parser.getORcreateParam((unsigned int)time(0), "seed", "Seed for rand.", 0, "Configuration", false); + + _parser.getORcreateParam((unsigned int)1000, "maxIter", "Maximum number of iterations.", 0, "Configuration", false); + + if (_parser.userNeedsHelp()) + { + _parser.printHelp(std::cout); + exit(EXIT_FAILURE); + } + + os.open("current_param"); + if(!os.is_open()) + { + throw std::runtime_error("[iterated_local_search.cpp]: the file current_param cannot be created."); + } + os <<_parser; + os.close(); +} diff --git a/trunk/paradiseo-mo/tutorial/Lesson4/lesson_4.pdf b/trunk/paradiseo-mo/tutorial/Lesson4/lesson_4.pdf deleted file mode 100644 index 2e5e51c55..000000000 Binary files a/trunk/paradiseo-mo/tutorial/Lesson4/lesson_4.pdf and /dev/null differ diff --git a/trunk/paradiseo-mo/tutorial/Lesson4/param b/trunk/paradiseo-mo/tutorial/Lesson4/param new file mode 100644 index 000000000..91a5284c8 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson4/param @@ -0,0 +1,9 @@ + +###### General ###### +# --help=0 # -h : Prints this message +# --stopOnUnknownParam=1 # Stop if unkown param entered + +###### Configuration ###### +# --instancePath=../examples/tsp/benchs/berlin52.tsp # Path to the instance. +# --seed=1203080388 # Seed for rand. +# --maxIter=1000 # Maximum number of iterations. diff --git a/trunk/paradiseo-mo/tutorial/examples/tsp/graph.cpp b/trunk/paradiseo-mo/tutorial/examples/tsp/graph.cpp index 92e185704..fee995d12 100644 --- a/trunk/paradiseo-mo/tutorial/examples/tsp/graph.cpp +++ b/trunk/paradiseo-mo/tutorial/examples/tsp/graph.cpp @@ -40,6 +40,9 @@ #include "graph.h" +using std::cout; +using std::endl; + namespace Graph { @@ -70,103 +73,103 @@ namespace Graph { double distX = (double)(vectCoord [i].first - vectCoord [j].first) ; double distY = (double)(vectCoord [i].second - vectCoord [j].second) ; - dist [i] [j] = dist [j] [i] = (unsigned) (sqrt ((float) (distX * distX + distY * distY)) + 0.5) ; + dist [i] [j] = dist [j] [i] = (unsigned int) (sqrt ((float) (distX * distX + distY * distY)) + 0.5) ; } } } - void load (const char * __fileName) + void load (const char * _fileName) { unsigned int i, dimension; std::string string_read, buffer; - std :: ifstream file (__fileName) ; + std :: ifstream file (_fileName) ; - std :: cout << ">> Loading [" << __fileName << "]" << std :: endl ; + cout << endl << "\tLoading [" << _fileName << "]" << endl << endl; - if (file) + if( file.is_open() ) { // Read NAME: file >> string_read; if (string_read.compare("NAME:")!=0) { - std::cout << "ERROR: \'NAME:\' espected, \'" << string_read << "\' found" << std::endl; - exit(1); + cout << "ERROR: \'NAME:\' espected, \'" << string_read << "\' found" << endl; + exit(EXIT_FAILURE); } // Read instance name file >> string_read; - std::cout << "\t Instance Name = " << string_read << std::endl; + cout << "\t\tInstance Name = " << string_read << endl; // Read TYPE: file >> string_read; if (string_read.compare("TYPE:")!=0) { - std::cout << "ERROR: \'TYPE:\' espected, \'" << string_read << "\' found" << std::endl; - exit(1); + cout << "ERROR: \'TYPE:\' espected, \'" << string_read << "\' found" << endl; + exit(EXIT_FAILURE); } // Read instance type; file >> string_read; - std::cout << "\t Instance type = " << string_read << std::endl; + cout << "\t\tInstance type = " << string_read << endl; if (string_read.compare("TSP")!=0) { - std::cout << "ERROR: only TSP type instance can be loaded" << std::endl; - exit(1); + cout << "ERROR: only TSP type instance can be loaded" << endl; + exit(EXIT_FAILURE); } // Read COMMENT: file >> string_read; if (string_read.compare("COMMENT:")!=0) { - std::cout << "ERROR: \'COMMENT:\' espected, \'" << string_read << "\' found" << std::endl; - exit(1); + cout << "ERROR: \'COMMENT:\' espected, \'" << string_read << "\' found" << endl; + exit(EXIT_FAILURE); } // Read comments - std::cout << "\t Instance comments = "; + cout << "\t\tInstance comments = "; file >> string_read; buffer = string_read+"_first"; while((string_read.compare("DIMENSION:")!=0) && (string_read.compare(buffer)!=0)) { if(string_read.compare("COMMENT:")!=0) { - std::cout << string_read << " "; + cout << string_read << " "; } else { - std::cout << std::endl << "\t "; + cout << endl << "\t "; } buffer = string_read; file >> string_read; } - std::cout << std::endl; + cout << endl; // Read dimension; file >> dimension ; - std::cout << "\t Instance dimension = " << dimension << std::endl; + cout << "\t\tInstance dimension = " << dimension << endl; vectCoord.resize (dimension) ; // Read EDGE_WEIGHT_TYPE file >> string_read; if (string_read.compare("EDGE_WEIGHT_TYPE:")!=0) { - std::cout << "ERROR: \'EDGE_WEIGHT_TYPE:\' espected, \'" << string_read << "\' found" << std::endl; - exit(1); + cout << "ERROR: \'EDGE_WEIGHT_TYPE:\' espected, \'" << string_read << "\' found" << endl; + exit(EXIT_FAILURE); } // Read edge weight type file >> string_read; - std::cout << "\t Instance edge weight type = " << string_read << std::endl; + cout << "\t\tInstance edge weight type = " << string_read << endl; if (string_read.compare("EUC_2D")!=0) { - std::cout << "ERROR: only EUC_2D edge weight type instance can be loaded" << std::endl; - exit(1); + cout << "ERROR: only EUC_2D edge weight type instance can be loaded" << endl; + exit(EXIT_FAILURE); } // Read NODE_COORD_SECTION file >> string_read; if (string_read.compare("NODE_COORD_SECTION")!=0) { - std::cout << "ERROR: \'NODE_COORD_SECTION\' espected, \'" << string_read << "\' found" << std::endl; - exit(1); + cout << "ERROR: \'NODE_COORD_SECTION\' espected, \'" << string_read << "\' found" << endl; + exit(EXIT_FAILURE); } // Read coordonates. @@ -182,11 +185,11 @@ namespace Graph file >> string_read; if(string_read.compare("EOF")!=0) { - std::cout << "ERROR: \'EOF\' espected, \'" << string_read << "\' found" << std::endl; - exit(1); + cout << "ERROR: \'EOF\' espected, \'" << string_read << "\' found" << endl; + exit(EXIT_FAILURE); } - std::cout << std::endl; + cout << endl; file.close () ; @@ -194,16 +197,14 @@ namespace Graph } else { - - std :: cout << __fileName << " does not exist !!!" << std :: endl ; - // Bye !!! - exit (1) ; + cout << _fileName << " does not exist !!!" << endl ; + exit(EXIT_FAILURE) ; } } - float distance (unsigned int __from, unsigned int __to) + float distance (unsigned int _from, unsigned int _to) { - return (float)(dist [__from] [__to]) ; + return (float)(dist [_from] [_to]) ; } } diff --git a/trunk/paradiseo-mo/tutorial/examples/tsp/graph.h b/trunk/paradiseo-mo/tutorial/examples/tsp/graph.h index bea04b225..2fff03688 100644 --- a/trunk/paradiseo-mo/tutorial/examples/tsp/graph.h +++ b/trunk/paradiseo-mo/tutorial/examples/tsp/graph.h @@ -41,14 +41,14 @@ #include namespace Graph - { - void load (const char * __file_name) ; +{ + void load (const char * _file_name) ; /* Loading cities (expressed by their coordinates) from the given file name */ - - float distance (unsigned int __from, unsigned int __to) ; - + + float distance (unsigned int _from, unsigned int _to) ; + unsigned int size () ; // How many cities ? }