diff --git a/trunk/paradiseo-mo/tutorial/Lesson8/CMakeLists.txt b/trunk/paradiseo-mo/tutorial/Lesson8/CMakeLists.txt new file mode 100644 index 000000000..982173989 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson8/CMakeLists.txt @@ -0,0 +1,10 @@ +INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src + ${MO_SRC_DIR}/src + ${PROBLEMS_SRC_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../src) + +LINK_DIRECTORIES(${EO_BIN_DIR}/lib) + +ADD_EXECUTABLE(testPopLS testPopLS.cpp) + +TARGET_LINK_LIBRARIES(testPopLS eoutils ga eo) diff --git a/trunk/paradiseo-mo/tutorial/Lesson8/testPopLS.cpp b/trunk/paradiseo-mo/tutorial/Lesson8/testPopLS.cpp new file mode 100644 index 000000000..5afb4e7d1 --- /dev/null +++ b/trunk/paradiseo-mo/tutorial/Lesson8/testPopLS.cpp @@ -0,0 +1,294 @@ +/* + +Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + +Sébastien Verel, Arnaud Liefooghe, Jérémie Humeau + +This software is governed by the CeCILL license under French law and +abiding by the rules of distribution of free software. You can ue, +modify and/ or redistribute the software under the terms of the CeCILL +license as circulated by CEA, CNRS and INRIA at the following URL +"http://www.cecill.info". + +In this respect, the user's attention is drawn to the risks associated +with loading, using, modifying and/or developing or reproducing the +software by the user in light of its specific status of free software, +that may mean that it is complicated to manipulate, and that also +therefore means that it is reserved for developers and experienced +professionals having in-depth computer knowledge. Users are therefore +encouraged to load and test the software's suitability as regards their +requirements in conditions enabling the security of their systems and/or +data to be ensured and, more generally, to use and operate it in the +same conditions as regards security. +The fact that you are presently reading this means that you have had +knowledge of the CeCILL license and that you accept its terms. + +ParadisEO WebSite : http://paradiseo.gforge.inria.fr +Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +// standard includes +#define HAVE_SSTREAM + +#include // runtime_error +#include // cout +#include // ostrstream, istrstream +#include +#include + +// the general include for eo +#include +#include + +using namespace std; + +//----------------------------------------------------------------------------- +//Representation and initializer +#include +#include +#include +#include +#include + +// fitness function +#include +#include + +//Neighbors and Neighborhoods +#include +#include + +//Algorithm and its components +#include +#include + +//comparator +#include + +//continuators +#include +#include +#include +#include +#include + + +//----------------------------------------------------------------------------- +// Define types of the representation solution, different neighbors and neighborhoods +//----------------------------------------------------------------------------- +typedef moPopSol > Solution; //Permutation (Queen's problem representation) + +typedef moPopBitNeighbor Neighbor; //shift Neighbor +typedef moOrderNeighborhood Neighborhood; //rnd shift Neighborhood (Indexed) + +void main_function(int argc, char **argv) +{ + /* ========================================================= + * + * Parameters + * + * ========================================================= */ + + // First define a parser from the command-line arguments + eoParser parser(argc, argv); + + // For each parameter, define Parameter, read it through the parser, + // and assign the value to the variable + + eoValueParam seedParam(time(0), "seed", "Random number seed", 'S'); + parser.processParam( seedParam ); + unsigned seed = seedParam.value(); + + // description of genotype + eoValueParam vecSizeParam(4, "vecSize", "Genotype size", 'V'); + parser.processParam( vecSizeParam, "Representation" ); + unsigned vecSize = vecSizeParam.value(); + + // description of genotype + eoValueParam popSizeParam(10, "popSize", "population size", 'V'); + parser.processParam( popSizeParam, "Representation" ); + unsigned popSize = popSizeParam.value(); + + // the name of the "status" file where all actual parameter values will be saved + string str_status = parser.ProgramName() + ".status"; // default value + eoValueParam statusParam(str_status.c_str(), "status", "Status file"); + parser.processParam( statusParam, "Persistence" ); + + // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED + // i.e. in case you need parameters somewhere else, postpone these + if (parser.userNeedsHelp()) { + parser.printHelp(cout); + exit(1); + } + if (statusParam.value() != "") { + ofstream os(statusParam.value().c_str()); + os << parser;// and you can use that file as parameter file + } + + /* ========================================================= + * + * Random seed + * + * ========================================================= */ + + //reproducible random seed: if you don't change SEED above, + // you'll always get the same result, NOT a random run + rng.reseed(seed); + + + /* ========================================================= + * + * Eval fitness function + * + * ========================================================= */ + + oneMaxEval< eoBit > eval; + oneMaxPopEval< eoBit > popEval(eval, 2); + + + /* ========================================================= + * + * Initilisation of the solution + * + * ========================================================= */ + eoUniformGenerator uGen; + eoInitFixedLength > random(vecSize, uGen); + + moPopSol > sol; + eoBit tmp(vecSize); + + for(unsigned int i=0; i moEval(popEval); + +// Neighbor n; +// +// n.index(3); +// moEval(sol, n); +// n.move(sol); +// popEval(sol); +// sol.printOn(std::cout); +// std::cout << "fit neighor: " << n.fitness() << std::endl; + + + + + /* ========================================================= + * + * the neighborhood of a solution + * + * ========================================================= */ + + Neighborhood neighborhood(vecSize*popSize); + + + /* ========================================================= + * + * the local search algorithm + * + * ========================================================= */ + + moSimpleHC ls(neighborhood, popEval, moEval); + + /* ========================================================= + * + * execute the local search from random solution + * + * ========================================================= */ + + ls(sol); + + std::cout << "final solution:" << std::endl; + sol.printOn(std::cout); + std::cout << std::endl; + +// Queen solution1, solution2; +// +// init(solution1); +// +// fullEval(solution1); +// +// std::cout << "#########################################" << std::endl; +// std::cout << "initial solution1: " << solution1 << std::endl ; +// +// localSearch1(solution1); +// +// std::cout << "final solution1: " << solution1 << std::endl ; +// std::cout << "#########################################" << std::endl; + + + /* ========================================================= + * + * the cooling schedule of the process + * + * ========================================================= */ + + // initial temp, factor of decrease, number of steps without decrease, final temp. + + + /* ========================================================= + * + * Comparator of neighbors + * + * ========================================================= */ + + + + /* ========================================================= + * + * Example of Checkpointing + * + * ========================================================= */ + +// moTrueContinuator continuator;//always continue +// moCheckpoint checkpoint(continuator); +// moFitnessStat fitStat; +// checkpoint.add(fitStat); +// eoFileMonitor monitor("fitness.out", ""); +// moCounterMonitorSaver countMon(100, monitor); +// checkpoint.add(countMon); +// monitor.add(fitStat); +// +// moSA localSearch2(rndShiftNH, fullEval, shiftEval, coolingSchedule, solComparator, checkpoint); +// +// init(solution2); +// +// fullEval(solution2); +// +// std::cout << "#########################################" << std::endl; +// std::cout << "initial solution2: " << solution2 << std::endl ; +// +// localSearch2(solution2); +// +// std::cout << "final solution2: " << solution2 << std::endl ; +// std::cout << "#########################################" << std::endl; +} + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ + try { + main_function(argc, argv); + } + catch (exception& e) { + cout << "Exception: " << e.what() << '\n'; + } + return 1; +} +