add hybrid lesson and test of ILS

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1799 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jhumeau 2010-05-07 12:11:00 +00:00
commit 5f85276ea5
9 changed files with 344 additions and 20 deletions

View file

@ -84,12 +84,11 @@ public:
* General constructor for Iterated Local Search
* @param _ls the local search to iterates
* @param _fullEval the full evaluation function
* @param _op the operator used to perturb solution
* @param _cont a continuator
* @param _perturb a perturbation operator
* @param _accept a acceptance criteria
*/
moILS(moLocalSearch<Neighbor>& _ls, eoEvalFunc<EOT>& _fullEval, eoMonOp<EOT>& _op, moContinuator<moDummyNeighbor<EOT> >& _cont, moMonOpPerturb<Neighbor>& _perturb, moAcceptanceCriterion<Neighbor>& _accept):
moILS(moLocalSearch<Neighbor>& _ls, eoEvalFunc<EOT>& _fullEval, moContinuator<moDummyNeighbor<EOT> >& _cont, moMonOpPerturb<Neighbor>& _perturb, moAcceptanceCriterion<Neighbor>& _accept):
moLocalSearch<moDummyNeighbor<EOT> >(explorer, _cont, _fullEval),
iterCont(0),
defaultPerturb(dummyOp, _fullEval),

View file

@ -44,7 +44,7 @@ public:
/**
* @param _maxIter number maximum of iterations
*/
moIterContinuator(unsigned int _maxIter): maxIter(_maxIter){}
moIterContinuator(unsigned int _maxIter, bool _verbose=true): maxIter(_maxIter), verbose(_verbose){}
/**
*@param _solution a solution
@ -54,9 +54,8 @@ public:
bool res;
cpt++;
res = (cpt < maxIter);
if(!res){
if(!res && verbose)
std::cout << "STOP in moIterContinuator: Reached maximum number of iterations [" << cpt << "/" << maxIter << "]" << std::endl;
}
return res;
}
@ -79,6 +78,7 @@ public:
private:
unsigned int maxIter;
unsigned int cpt;
bool verbose;
};
#endif

View file

@ -46,7 +46,7 @@ public:
* Ctor.
* @param _max maximum running time
*/
moTimeContinuator(time_t _max): max(_max){
moTimeContinuator(time_t _max, bool _verbose=true): max(_max), verbose(_verbose){
start = time(NULL);
}
@ -57,13 +57,12 @@ public:
*/
virtual bool operator() (EOT& _sol)
{
bool res;
time_t elapsed = (time_t) difftime(time(NULL), start);
if (elapsed >= max)
{
std::cout << "STOP in moTimeContinuator: Reached maximum time [" << elapsed << "/" << max << "]" << std::endl;
return false;
}
return true;
res = (elapsed < max);
if(!res && verbose)
std::cout << "STOP in moTimeContinuator: Reached maximum time [" << elapsed << "/" << max << "]" << std::endl;
return res;
}
/**
@ -90,6 +89,8 @@ private:
time_t max;
/** starting time */
time_t start;
/** verbose mode */
bool verbose;
};

View file

@ -81,6 +81,7 @@ SET (TEST_LIST
t-moNeighborVectorTabuList
t-moMonOpDiversification
t-moTS
t-moILS
)
FOREACH (test ${TEST_LIST})

View file

@ -78,14 +78,14 @@ int main() {
test1.init(s);
test1(s);
assert(a==3 && b==16 && c==11 && d==48);
assert(a==3 && b==16 && c==12 && d==49);
assert(stat.value()[0]);
assert(stat.value()[1]);
assert(!stat.value()[2]);
assert(stat.value().fitness()==17);
test1(s);
assert(a==4 && b==17 && c==12 && d==49);
assert(a==4 && b==17 && c==13 && d==50);
assert(stat.value()[0]);
assert(stat.value()[1]);
assert(!stat.value()[2]);
@ -95,7 +95,7 @@ int main() {
test2.init(s);
test2(s);
assert(a==5 && b==18 && c==13 && d==50);
assert(a==5 && b==18 && c==15 && d==52);
assert(stat.value()[0]);
assert(stat.value()[1]);
assert(!stat.value()[2]);
@ -105,14 +105,14 @@ int main() {
test2(s);
assert(stat.value().fitness()==4);
test2(s);
assert(stat.value().fitness()==4);
assert(stat.value().fitness()==6);
test2(s);
assert(stat.value().fitness()==6);
test1.lastCall(s);
assert(a==9 && b==22 && c==17 && d==54);
assert(a==9 && b==22 && c==19 && d==56);
test2.lastCall(s);
assert(a==10 && b==23 && c==18 && d==55);
assert(a==10 && b==23 && c==20 && d==57);
assert(test1.className()=="moCheckpoint");
std::cout << "[t-moCheckpoint] => OK" << std::endl;

View file

@ -0,0 +1,77 @@
/*
<t-moILS.cpp>
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
*/
#include <iostream>
#include <cstdlib>
#include <cassert>
#include <algo/moTS.h>
#include <algo/moILS.h>
#include "moTestClass.h"
#include <eval/oneMaxEval.h>
#include <continuator/moTrueContinuator.h>
#include <comparator/moSolNeighborComparator.h>
#include <comparator/moNeighborComparator.h>
#include <memory/moSolVectorTabuList.h>
#include <neighborhood/moDummyNeighbor.h>
class dummyMonOp: public eoMonOp<bitVector>{
public:
bool operator()(bitVector&){return false;}
};
int main(){
std::cout << "[t-moILS] => START" << std::endl;
bitNeighborhood nh(4);
oneMaxEval<bitVector> fullEval;
evalOneMax eval(4);
moTS<bitNeighbor> ts(nh, fullEval, eval, 1, 7);
dummyMonOp op;
//basic constructor
moILS<bitNeighbor> test1(ts, fullEval, op, 3);
//simple constructor
moTrueContinuator<moDummyNeighbor<bitVector> > cont;
moILS<bitNeighbor> test2(ts, fullEval, op, cont);
//general constructor
moMonOpPerturb<bitNeighbor> perturb(op, fullEval);
moAlwaysAcceptCrit<bitNeighbor> accept;
moILS<bitNeighbor> test3(ts, fullEval, cont, perturb, accept);
std::cout << "[t-moILS] => OK" << std::endl;
return EXIT_SUCCESS;
}

View file

@ -175,7 +175,7 @@ void main_function(int argc, char **argv)
* ========================================================= */
//Basic Constructor of the Tabu Search
moTS<shiftNeighbor> ts(orderShiftNH, fullEval, shiftEval, 5, 7);
moTS<shiftNeighbor> ts(orderShiftNH, fullEval, shiftEval, 1, 7);
eoSwapMutation<Queen> mut;
@ -185,9 +185,17 @@ void main_function(int argc, char **argv)
//Simple Constructor of the Iterated Local Search
//Be carefull, template of the continuator must be a dummyNeighbor!!!
moIterContinuator<moDummyNeighbor<Queen> > cont(4);
moIterContinuator<moDummyNeighbor<Queen> > cont(4, false);
moILS<shiftNeighbor> localSearch2(ts, fullEval, mut, cont);
//General Constructor of the Iterated Local Search
moMonOpPerturb<shiftNeighbor> perturb(mut, fullEval);
moSolComparator<Queen> solComp;
moBetterAcceptCrit<shiftNeighbor> accept(solComp);
moILS<shiftNeighbor> localSearch3(ts, fullEval, cont, perturb, accept);
std::cout << "Iterated Local Search 1:" << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "initial: " << sol1 << std::endl ;
@ -200,6 +208,12 @@ void main_function(int argc, char **argv)
localSearch2(sol2);
std::cout << "final: " << sol2 << std::endl << std::endl;
std::cout << "Iterated Local Search 3:" << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "initial: " << sol3 << std::endl ;
localSearch3(sol3);
std::cout << "final: " << sol3 << std::endl << std::endl;
}
// A main that catches the exceptions

View file

@ -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(hybridAlgo hybridAlgo.cpp)
TARGET_LINK_LIBRARIES(hybridAlgo eoutils ga eo)

View file

@ -0,0 +1,222 @@
//-----------------------------------------------------------------------------
/** testILS.cpp
*
* SV - 12/01/10
* JH - 06/05/10
*
*/
//-----------------------------------------------------------------------------
// standard includes
#define HAVE_SSTREAM
#include <stdexcept> // runtime_error
#include <iostream> // cout
#include <sstream> // ostrstream, istrstream
#include <fstream>
#include <string.h>
// the general include for eo
#include <eo>
#include <ga.h>
#include <ga/eoBitOp.h>
using namespace std;
//-----------------------------------------------------------------------------
//Representation and initializer
#include <eoInt.h>
#include <eoInit.h>
#include <eoScalarFitness.h>
// fitness function
#include <eval/queenEval.h>
#include <eval/moFullEvalByModif.h>
#include <eval/moFullEvalByCopy.h>
//Neighbors and Neighborhoods
#include <problems/permutation/moShiftNeighbor.h>
#include <neighborhood/moOrderNeighborhood.h>
//Mutation
#include <eoSwapMutation.h>
#include <eoOrderXover.h>
//Algorithm and its components
#include <algo/moFirstImprHC.h>
//mo eval
#include <eval/moFullEvalByCopy.h>
#include <continuator/moTrueContinuator.h>
// REPRESENTATION
//-----------------------------------------------------------------------------
typedef eoInt<eoMinimizingFitness> Queen; //Permutation (Queen's problem representation)
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
typedef moOrderNeighborhood<shiftNeighbor> orderShiftNeighborhood; //order 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<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// description of genotype
eoValueParam<unsigned int> vecSizeParam(8, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value
eoValueParam<string> 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 aways get the same result, NOT a random run
rng.reseed(seed);
/* =========================================================
*
* Full evaluation fitness function
*
* ========================================================= */
queenEval<Queen> fullEval;
/* =========================================================
*
* Initializer of a solution
*
* ========================================================= */
eoInitPermutation<Queen> init(vecSize);
/* =========================================================
*
* Declare and init a population
*
* ========================================================= */
eoPop<Queen> pop;
Queen tmp;
for(unsigned int i=0; i<20; i++){
init(tmp);
fullEval(tmp);
pop.push_back(tmp);
}
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
orderShiftNeighborhood orderShiftNH(pow(vecSize-1, 2));
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
//Basic Constructor a first improvement hill climber
moFirstImprHC<shiftNeighbor> hc(orderShiftNH, fullEval, shiftEval);
/* =========================================================
*
* the evolutionary algorithm
*
* ========================================================= */
//continuator
eoGenContinue<Queen> EAcont(50);
//selection
eoDetTournamentSelect<Queen> selectOne(2);
eoSelectMany<Queen> select(selectOne, 1);
//crossover
eoOrderXover<Queen> cross;
//transform operator (the hill climber replace the mutation operator)
eoSGATransform<Queen> transform(cross, 0.3, hc, 0.7);
//replacement
eoGenerationalReplacement<Queen> repl;
//easyEA
eoEasyEA<Queen> hybridAlgo(EAcont, fullEval, select, transform, repl);
std::cout << "INITIAL POPULATION:" << std::endl;
std::cout << "-------------------" << std::endl;
for(unsigned int i=0; i<pop.size(); i++)
std::cout << pop[i] << std::endl;
hybridAlgo(pop);
std::cout << std::endl;
std::cout << "FINAL POPULATION:" << std::endl;
std::cout << "-------------------" << std::endl;
for(unsigned int i=0; i<pop.size(); i++)
std::cout << pop[i] << 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;
}