Tutos updated
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@1739 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
d9674919b3
commit
070fdd01cc
2 changed files with 77 additions and 48 deletions
|
|
@ -44,9 +44,11 @@ using namespace std;
|
||||||
// Define types of the representation solution, different neighbors and neighborhoods
|
// Define types of the representation solution, different neighbors and neighborhoods
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
typedef eoInt<unsigned int> Queen; //Permutation (Queen's problem representation)
|
typedef eoInt<unsigned int> Queen; //Permutation (Queen's problem representation)
|
||||||
|
|
||||||
typedef moSwapNeighbor<Queen> swapNeighbor ; //swap Neighbor
|
typedef moSwapNeighbor<Queen> swapNeighbor ; //swap Neighbor
|
||||||
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
|
|
||||||
typedef moSwapNeighborhood<Queen> swapNeighborhood; //classical swap Neighborhood
|
typedef moSwapNeighborhood<Queen> swapNeighborhood; //classical swap Neighborhood
|
||||||
|
|
||||||
|
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
|
||||||
typedef moOrderNeighborhood<shiftNeighbor> orderShiftNeighborhood; //order shift Neighborhood (Indexed)
|
typedef moOrderNeighborhood<shiftNeighbor> orderShiftNeighborhood; //order shift Neighborhood (Indexed)
|
||||||
typedef moRndWithoutReplNeighborhood<shiftNeighbor> rndWithoutReplShiftNeighborhood; //random without replacement shift Neighborhood (Indexed)
|
typedef moRndWithoutReplNeighborhood<shiftNeighbor> rndWithoutReplShiftNeighborhood; //random without replacement shift Neighborhood (Indexed)
|
||||||
typedef moRndWithReplNeighborhood<shiftNeighbor> rndWithReplShiftNeighborhood; //random with replacement shift Neighborhood (Indexed)
|
typedef moRndWithReplNeighborhood<shiftNeighbor> rndWithReplShiftNeighborhood; //random with replacement shift Neighborhood (Indexed)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
/** testSimulatedAnnealing.cpp
|
/** testSimulatedAnnealing.cpp
|
||||||
*
|
*
|
||||||
* SV - 29/03/10
|
* SV - 29/03/10
|
||||||
*
|
* JH - 20/04/10
|
||||||
*/
|
*/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -22,26 +22,42 @@
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// fitness function
|
//Representation and initializer
|
||||||
#include <problems/eval/oneMaxFullEval.h>
|
|
||||||
#include <problems/bitString/moBitNeighbor.h>
|
|
||||||
#include <eoInt.h>
|
#include <eoInt.h>
|
||||||
#include <neighborhood/moRndWithReplNeighborhood.h>
|
#include <eoInit.h>
|
||||||
|
#include <eoScalarFitness.h>
|
||||||
|
|
||||||
|
// fitness function
|
||||||
|
#include <problems/eval/queenFullEval.h>
|
||||||
#include <eval/moFullEvalByModif.h>
|
#include <eval/moFullEvalByModif.h>
|
||||||
#include <eval/moFullEvalByCopy.h>
|
#include <eval/moFullEvalByCopy.h>
|
||||||
#include <comparator/moNeighborComparator.h>
|
|
||||||
#include <comparator/moSolNeighborComparator.h>
|
|
||||||
#include <continuator/moTrueContinuator.h>
|
|
||||||
#include <algo/moLocalSearch.h>
|
|
||||||
#include <explorer/moSAexplorer.h>
|
|
||||||
#include <coolingSchedule/moSimpleCoolingSchedule.h>
|
|
||||||
|
|
||||||
// REPRESENTATION
|
//Neighbors and Neighborhoods
|
||||||
|
#include <problems/permutation/moShiftNeighbor.h>
|
||||||
|
#include <neighborhood/moRndWithReplNeighborhood.h>
|
||||||
|
|
||||||
|
//Algorithm and its components
|
||||||
|
#include <coolingSchedule/moCoolingSchedule.h>
|
||||||
|
#include <algo/moSA.h>
|
||||||
|
|
||||||
|
//comparator
|
||||||
|
#include <comparator/moSolNeighborComparator.h>
|
||||||
|
|
||||||
|
//continuators
|
||||||
|
#include <continuator/moTrueContinuator.h>
|
||||||
|
#include <continuator/moCheckpoint.h>
|
||||||
|
#include <continuator/moFitnessStat.h>
|
||||||
|
#include <utils/eoFileMonitor.h>
|
||||||
|
#include <continuator/moCounterMonitorSaver.h>
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
typedef eoBit<unsigned> Indi;
|
// Define types of the representation solution, different neighbors and neighborhoods
|
||||||
typedef moBitNeighbor<unsigned int> Neighbor ; // incremental evaluation
|
//-----------------------------------------------------------------------------
|
||||||
typedef moRndWithReplNeighborhood<Neighbor> Neighborhood ;
|
typedef eoInt<eoMinimizingFitness> Queen; //Permutation (Queen's problem representation)
|
||||||
|
|
||||||
|
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
|
||||||
|
typedef moRndWithReplNeighborhood<shiftNeighbor> rndShiftNeighborhood; //rnd shift Neighborhood (Indexed)
|
||||||
|
|
||||||
void main_function(int argc, char **argv)
|
void main_function(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
|
@ -89,7 +105,7 @@ void main_function(int argc, char **argv)
|
||||||
* ========================================================= */
|
* ========================================================= */
|
||||||
|
|
||||||
//reproducible random seed: if you don't change SEED above,
|
//reproducible random seed: if you don't change SEED above,
|
||||||
// you'll aways get the same result, NOT a random run
|
// you'll always get the same result, NOT a random run
|
||||||
rng.reseed(seed);
|
rng.reseed(seed);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -99,7 +115,7 @@ void main_function(int argc, char **argv)
|
||||||
*
|
*
|
||||||
* ========================================================= */
|
* ========================================================= */
|
||||||
|
|
||||||
oneMaxFullEval<Indi> eval;
|
queenFullEval<Queen> fullEval;
|
||||||
|
|
||||||
|
|
||||||
/* =========================================================
|
/* =========================================================
|
||||||
|
|
@ -108,10 +124,7 @@ void main_function(int argc, char **argv)
|
||||||
*
|
*
|
||||||
* ========================================================= */
|
* ========================================================= */
|
||||||
|
|
||||||
// a Indi random initializer
|
eoInitPermutation<Queen> init(vecSize);
|
||||||
eoUniformGenerator<bool> uGen;
|
|
||||||
eoInitFixedLength<Indi> random(vecSize, uGen);
|
|
||||||
|
|
||||||
|
|
||||||
/* =========================================================
|
/* =========================================================
|
||||||
*
|
*
|
||||||
|
|
@ -119,20 +132,7 @@ void main_function(int argc, char **argv)
|
||||||
*
|
*
|
||||||
* ========================================================= */
|
* ========================================================= */
|
||||||
|
|
||||||
moFullEvalByModif<Neighbor> fulleval(eval);
|
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
|
||||||
|
|
||||||
//An eval by copy can be used instead of the eval by modif
|
|
||||||
//moFullEvalByCopy<Neighbor> fulleval(eval);
|
|
||||||
|
|
||||||
|
|
||||||
/* =========================================================
|
|
||||||
*
|
|
||||||
* Comparator of neighbors
|
|
||||||
*
|
|
||||||
* ========================================================= */
|
|
||||||
|
|
||||||
moSolNeighborComparator<Neighbor> solComparator;
|
|
||||||
|
|
||||||
|
|
||||||
/* =========================================================
|
/* =========================================================
|
||||||
*
|
*
|
||||||
|
|
@ -140,7 +140,35 @@ void main_function(int argc, char **argv)
|
||||||
*
|
*
|
||||||
* ========================================================= */
|
* ========================================================= */
|
||||||
|
|
||||||
Neighborhood neighborhood(vecSize);
|
rndShiftNeighborhood rndShiftNH(pow(vecSize-1, 2));
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* the local search algorithm
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
moSA<shiftNeighbor> localSearch1(rndShiftNH, fullEval, shiftEval);
|
||||||
|
|
||||||
|
/* =========================================================
|
||||||
|
*
|
||||||
|
* execute the local search from random solution
|
||||||
|
*
|
||||||
|
* ========================================================= */
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
/* =========================================================
|
/* =========================================================
|
||||||
|
|
@ -150,45 +178,44 @@ void main_function(int argc, char **argv)
|
||||||
* ========================================================= */
|
* ========================================================= */
|
||||||
|
|
||||||
// initial temp, factor of decrease, number of steps without decrease, final temp.
|
// initial temp, factor of decrease, number of steps without decrease, final temp.
|
||||||
moSimpleCoolingSchedule<Indi> coolingSchedule(10, 0.9, 1, 0.01);
|
moSimpleCoolingSchedule<Queen> coolingSchedule(1, 0.9, 100, 0.01);
|
||||||
|
|
||||||
/* =========================================================
|
/* =========================================================
|
||||||
*
|
*
|
||||||
* a neighborhood explorer solution
|
* Comparator of neighbors
|
||||||
*
|
*
|
||||||
* ========================================================= */
|
* ========================================================= */
|
||||||
|
|
||||||
moSAexplorer<Neighbor> explorer(neighborhood, fulleval, solComparator, coolingSchedule);
|
moSolNeighborComparator<shiftNeighbor> solComparator;
|
||||||
|
|
||||||
/* =========================================================
|
/* =========================================================
|
||||||
*
|
*
|
||||||
* the local search algorithm
|
* Example of Checkpointing
|
||||||
*
|
*
|
||||||
* ========================================================= */
|
* ========================================================= */
|
||||||
|
|
||||||
moTrueContinuator<Neighbor> continuator;//always continue
|
moTrueContinuator<shiftNeighbor> continuator;//always continue
|
||||||
|
moCheckpoint<shiftNeighbor> checkpoint(continuator);
|
||||||
|
moFitnessStat<Queen> fitStat;
|
||||||
|
checkpoint.add(fitStat);
|
||||||
|
eoFileMonitor monitor("fitness.out", "");
|
||||||
|
moCounterMonitorSaver countMon(100, monitor);
|
||||||
|
checkpoint.add(countMon);
|
||||||
|
monitor.add(fitStat);
|
||||||
|
|
||||||
moLocalSearch<Neighbor> localSearch(explorer, continuator, eval);
|
moSA<shiftNeighbor> localSearch2(rndShiftNH, fullEval, shiftEval, coolingSchedule, solComparator, checkpoint);
|
||||||
|
|
||||||
/* =========================================================
|
init(solution2);
|
||||||
*
|
|
||||||
* execute the local search from random sollution
|
|
||||||
*
|
|
||||||
* ========================================================= */
|
|
||||||
|
|
||||||
Indi solution;
|
fullEval(solution2);
|
||||||
|
|
||||||
random(solution);
|
std::cout << "#########################################" << std::endl;
|
||||||
|
std::cout << "initial solution2: " << solution2 << std::endl ;
|
||||||
|
|
||||||
//Can be eval here, else it will be done at the beginning of the localSearch
|
localSearch2(solution2);
|
||||||
//eval(solution);
|
|
||||||
|
|
||||||
std::cout << "initial: " << solution << std::endl ;
|
|
||||||
|
|
||||||
localSearch(solution);
|
|
||||||
|
|
||||||
std::cout << "final: " << solution << std::endl ;
|
|
||||||
|
|
||||||
|
std::cout << "final solution2: " << solution2 << std::endl ;
|
||||||
|
std::cout << "#########################################" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A main that catches the exceptions
|
// A main that catches the exceptions
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue