Migration from SVN

This commit is contained in:
quemy 2012-08-30 11:30:11 +02:00
commit 8cd56f37db
29069 changed files with 0 additions and 4096888 deletions

View file

@ -0,0 +1,13 @@
######################################################################################
### 0) Include lessons subdirectories
######################################################################################
add_subdirectory(Lesson1)
add_subdirectory(Lesson2)
add_subdirectory(Lesson3)
add_subdirectory(Lesson4)
add_subdirectory(Lesson5)
add_subdirectory(Lesson6)
add_subdirectory(Lesson7)
add_subdirectory(Lesson9)

View file

@ -0,0 +1,33 @@
# Lesson 1
######################################################################################
### 0) Define files
######################################################################################
set(files
lesson1_simpleHC
lesson1_firstImprHC
lesson1_randomBestHC
lesson1_neutralHC
lesson1_iterContinuator
lesson1_fitContinuator
lesson1_fullEvalContinuator
lesson1_evalContinuator
lesson1_combinedContinuator
firstImprHC_maxSAT
)
######################################################################################
### 1) Create the lesson
######################################################################################
add_lesson(mo Lesson1 "${files}")
######################################################################################
### 2) Include dependencies
######################################################################################
include_directories(${EO_SRC_DIR}/src
${MO_SRC_DIR}/src
${PROBLEMS_SRC_DIR})

View file

@ -0,0 +1,222 @@
//-----------------------------------------------------------------------------
/** firstImprHC_maxSAT.cpp
*
* SV - 05/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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/maxSATeval.h>
#include <problems/eval/moMaxSATincrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moRndWithoutReplNeighborhood.h> // visit all neighbors in random order without repeating any neighbor
//-----------------------------------------------------------------------------
// the first improvement Hill-Climbing local search
#include <algo/moFirstImprHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// Number of clauses of the max SAT problem
eoValueParam<unsigned int> ncParam(10, "nbClauses", "Number of clauses", 'm');
parser.processParam( ncParam, "Representation" );
unsigned nbClause = ncParam.value();
// Number of litteral by clauses
eoValueParam<unsigned int> kParam(3, "nbLitt", "Number of litteral by clauses", 'k');
parser.processParam( kParam, "Representation" );
unsigned nbLitteral = kParam.value();
// the name of the instance file
string str_in = "" ; // default value
eoValueParam<string> inParam(str_in.c_str(), "in", "Input file of the file in ncf format", 'f');
parser.processParam(inParam, "Persistence" );
str_in = inParam.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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the max SAT evaluation
MaxSATeval<Indi> * fullEval;
if (str_in.compare("") == 0)
fullEval = new MaxSATeval<Indi>(vecSize, nbClause, nbLitteral);
else {
fullEval = new MaxSATeval<Indi>(str_in);
vecSize = fullEval->nbVar ;
}
// string out = "cnf.dat";
// fullEval->save(out);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(*fullEval);
// Incremental evaluation of the neighbor:
moMaxSATincrEval<Neighbor> neighborEval(*fullEval);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in random order of the neigbor's index:
// each neighbor is visited only once
moRndWithoutReplNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moFirstImprHC<Neighbor> hc(neighborhood, *fullEval, neighborEval);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
(*fullEval)(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,14 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179091 # -S : Random number seed
###### Persistence ######
# --in= # -f : Input file of the file in ncf format
# --status=./firstImprHC_maxSAT.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --nbClauses=10 # -m : Number of clauses
# --nbLitt=3 # -k : Number of litteral by clauses

View file

@ -0,0 +1,251 @@
//-----------------------------------------------------------------------------
/** lesson1_combinedContinuator.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbors in increasing order of bit index
//-----------------------------------------------------------------------------
// the continuators
#include <eoEvalFuncCounter.h> // to import the fitness counter
#include <eval/moEvalCounter.h> // to import the neighbor evaluation counter
#include <continuator/moIterContinuator.h>
#include <continuator/moFitContinuator.h>
#include <continuator/moFullEvalContinuator.h>
#include <continuator/moNeighborEvalContinuator.h>
#include <continuator/moCombinedContinuator.h>
//-----------------------------------------------------------------------------
// the simple Hill-Climbing local search
#include <algo/moSimpleHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// maximum number of full evaluation
eoValueParam<unsigned int> fevalParam(2, "fulleval", "Maximum number of full evaluation");
parser.processParam( fevalParam, "Representation" );
unsigned fullevalMax = fevalParam.value();
// maximum number of full evaluation
eoValueParam<unsigned int> evalParam(30, "eval", "Maximum number of neighbor evaluation", 'e');
parser.processParam( evalParam, "Representation" );
unsigned evalMax = evalParam.value();
// maximum fitness to reach
eoValueParam<unsigned int> fitParam(16, "fitness", "Maximum fitness value to reach", 'f');
parser.processParam( fitParam, "Representation" );
unsigned fitnessMax = fitParam.value();
// maximum number of iterations
eoValueParam<unsigned int> iterParam(10, "iter", "Maximum number of iterations", 'i');
parser.processParam( iterParam, "Representation" );
unsigned iterMax = iterParam.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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEvalTmp;
// to count the number of full evaluation
eoEvalFuncCounter<Indi> fullEval(fullEvalTmp);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEvalTmp;
// to count the number of neighbor evaluation
moEvalCounter<Neighbor> neighborEval(neighborEvalTmp);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the external continuators
*
* ========================================================= */
moIterContinuator<Neighbor> iterCont(iterMax);
moFitContinuator<Neighbor> fitCont(fitnessMax);
moFullEvalContinuator<Neighbor> fullevalCont(fullEval, fullevalMax);
moNeighborEvalContinuator<Neighbor> evalCont(neighborEval, evalMax);
moCombinedContinuator<Neighbor> continuator(iterCont);
continuator.add(fitCont);
continuator.add(fullevalCont);
continuator.add(evalCont);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moSimpleHC<Neighbor> hc(neighborhood, fullEval, neighborEval, continuator);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << std::endl ;
std::cout << "number of iteration: " << iterCont.value() << std::endl ;
std::cout << "Number of full evaluations during the local search: " << fullevalCont.value() << std::endl ;
std::cout << "Number of neighbor evaluations during the local search: " << evalCont.value() << 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;
}

View file

@ -0,0 +1,15 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276174177 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_combinedContinuator.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --fulleval=2 # Maximum number of full evaluation
# --eval=30 # -e : Maximum number of neighbor evaluation
# --fitness=16 # -f : Maximum fitness value to reach
# --iter=10 # -i : Maximum number of iterations

View file

@ -0,0 +1,218 @@
//-----------------------------------------------------------------------------
/** lesson1_continuator.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbors in increasing order of bit index
//-----------------------------------------------------------------------------
// the continuator based on the number of neighbor evaluations
#include <eval/moEvalCounter.h> // to import the neighbor evaluation counter
#include <continuator/moNeighborEvalContinuator.h>
//-----------------------------------------------------------------------------
// the simple Hill-Climbing local search
#include <algo/moSimpleHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// maximum number of full evaluation
eoValueParam<unsigned int> evalParam(30, "eval", "Maximum number of neighbor evaluation", 'e');
parser.processParam( evalParam, "Representation" );
unsigned evalMax = evalParam.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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEvalTmp;
// to count the number of neighbor evaluation
moEvalCounter<Neighbor> neighborEval(neighborEvalTmp);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the external continuators
*
* ========================================================= */
moNeighborEvalContinuator<Neighbor> continuator(neighborEval, evalMax);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moSimpleHC<Neighbor> hc(neighborhood, fullEval, neighborEval, continuator);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << std::endl ;
std::cout << "Number of neighbor evaluations during the local search: " << continuator.value() << 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;
}

View file

@ -0,0 +1,12 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179018 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_evalContinuator.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --eval=30 # -e : Maximum number of neighbor evaluation

View file

@ -0,0 +1,196 @@
//-----------------------------------------------------------------------------
/** lesson1_firstImprHC.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moRndWithoutReplNeighborhood.h> // visit all neighbors in random order without repeating any neighbor
//-----------------------------------------------------------------------------
// the first improvement Hill-Climbing local search
#include <algo/moFirstImprHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in random order of the neigbor's index:
// each neighbor is visited only once
moRndWithoutReplNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moFirstImprHC<Neighbor> hc(neighborhood, fullEval, neighborEval);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,11 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179046 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_firstImprHC.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size

View file

@ -0,0 +1,213 @@
//-----------------------------------------------------------------------------
/** lesson1_fitContinuator.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbors in increasing order of bit index
//-----------------------------------------------------------------------------
// the continuator based on fitness
#include <continuator/moFitContinuator.h>
//-----------------------------------------------------------------------------
// the simple Hill-Climbing local search
#include <algo/moSimpleHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// maximum fitness to reach
eoValueParam<unsigned int> fitParam(16, "fitness", "Maximum fitness value to reach", 'f');
parser.processParam( fitParam, "Representation" );
unsigned fitnessMax = fitParam.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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the external continuators
*
* ========================================================= */
moFitContinuator<Neighbor> continuator(fitnessMax);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moSimpleHC<Neighbor> hc(neighborhood, fullEval, neighborEval, continuator);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,12 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179024 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_fitContinuator.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --fitness=16 # -f : Maximum fitness value to reach

View file

@ -0,0 +1,218 @@
//-----------------------------------------------------------------------------
/** lesson1_fullEvalContinuator.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbors in increasing order of bit index
//-----------------------------------------------------------------------------
// the continuator based on full eval number
#include <eoEvalFuncCounter.h> // to import the fitness counter
#include <continuator/moFullEvalContinuator.h>
//-----------------------------------------------------------------------------
// the simple Hill-Climbing local search
#include <algo/moSimpleHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// maximum number of full evaluation
eoValueParam<unsigned int> evalParam(2, "fulleval", "Maximum number of full evaluation", 'e');
parser.processParam( evalParam, "Representation" );
unsigned fullevalMax = evalParam.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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEvalTmp;
// to count the number of full evaluation
eoEvalFuncCounter<Indi> fullEval(fullEvalTmp);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the external continuators
*
* ========================================================= */
moFullEvalContinuator<Neighbor> continuator(fullEval, fullevalMax);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moSimpleHC<Neighbor> hc(neighborhood, fullEval, neighborEval, continuator);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << std::endl ;
std::cout << "Number of full evaluations during the local search: " << continuator.value() << 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;
}

View file

@ -0,0 +1,12 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179068 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_fullEvalContinuator.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --fulleval=2 # -e : Maximum number of full evaluation

View file

@ -0,0 +1,215 @@
//-----------------------------------------------------------------------------
/** lesson1_iterContinuator.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbors in increasing order of bit index
//-----------------------------------------------------------------------------
// the iteration continuators
#include <continuator/moIterContinuator.h>
//-----------------------------------------------------------------------------
// the simple Hill-Climbing local search
#include <algo/moSimpleHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// maximum number of iterations
eoValueParam<unsigned int> iterParam(10, "iter", "Maximum number of iterations", 'i');
parser.processParam( iterParam, "Representation" );
unsigned iterMax = iterParam.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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the external continuators
*
* ========================================================= */
moIterContinuator<Neighbor> continuator(iterMax);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moSimpleHC<Neighbor> hc(neighborhood, fullEval, neighborEval, continuator);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << std::endl ;
std::cout << "number of iteration: " << continuator.value() << 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;
}

View file

@ -0,0 +1,12 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179030 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_iterContinuator.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --iter=10 # -i : Maximum number of iterations

View file

@ -0,0 +1,200 @@
//-----------------------------------------------------------------------------
/** lesson1_neutralHC.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbors in increasing order of bit index
//-----------------------------------------------------------------------------
// the neutral Hill-Climbing local search: move one of random best solution even it is the same fitness
#include <algo/moNeutralHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
eoValueParam<unsigned int> stepParam(10, "nbStepMax", "Number of steps of the random walk", 'n');
parser.processParam( stepParam, "Representation" );
unsigned nbStepMax = stepParam.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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moNeutralHC<Neighbor> hc(neighborhood, fullEval, neighborEval, nbStepMax);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,12 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179072 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_neutralHC.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --nbStepMax=10 # -n : Number of steps of the random walk

View file

@ -0,0 +1,196 @@
//-----------------------------------------------------------------------------
/** first_randomBestHC.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbors in increasing order of bit index
//-----------------------------------------------------------------------------
// the Hill-Climbing local search which randomly one of the best solution
#include <algo/moRandomBestHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moRandomBestHC<Neighbor> hc(neighborhood, fullEval, neighborEval);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,11 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179038 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_randomBestHC.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size

View file

@ -0,0 +1,196 @@
//-----------------------------------------------------------------------------
/** lesson1_simpleHC.cpp
*
* SV - 27/04/10 - version 1
*
*/
//-----------------------------------------------------------------------------
// 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbors in increasing order of bit index
//-----------------------------------------------------------------------------
// the simple Hill-Climbing local search
#include <algo/moSimpleHC.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
// Main function
//-----------------------------------------------------------------------------
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters from parser
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moSimpleHC<Neighbor> hc(neighborhood, fullEval, neighborEval);
/* =========================================================
*
* executes the local search from a random solution
*
* ========================================================= */
// The current solution
Indi solution;
// Apply random initialization
random(solution);
// Evaluation of the initial solution:
// can be evaluated here, or else it will be done at the beginning of the local search
fullEval(solution);
// Output: the intial solution
std::cout << "initial: " << solution << std::endl ;
// Apply the local search on the solution !
hc(solution);
// Output: the final solution
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,11 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179075 # -S : Random number seed
###### Persistence ######
# --status=./lesson1_simpleHC.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size

View file

@ -0,0 +1,21 @@
# Lesson 2
######################################################################################
### 0) Define files
######################################################################################
set(files testNeighborhood)
######################################################################################
### 1) Create the lesson
######################################################################################
add_lesson(mo Lesson2 "${files}")
######################################################################################
### 2) Include dependencies
######################################################################################
include_directories(${EO_SRC_DIR}/src
${MO_SRC_DIR}/src
${PROBLEMS_SRC_DIR})

View file

@ -0,0 +1,241 @@
//-----------------------------------------------------------------------------
/** testNeighborhood.cpp
*
* JH - 09/04/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>
using namespace std;
//----------------------------------------------------------------------------
//Representation and initializer
#include <eoInt.h>
#include <eoInit.h>
// fitness function
#include <eval/queenEval.h>
#include <eval/moFullEvalByModif.h>
#include <eval/moFullEvalByCopy.h>
//Neighbors and Neighborhoods
#include <problems/permutation/moShiftNeighbor.h>
#include <problems/permutation/moSwapNeighbor.h>
#include <problems/permutation/moSwapNeighborhood.h>
#include <neighborhood/moRndWithReplNeighborhood.h>
#include <neighborhood/moRndWithoutReplNeighborhood.h>
#include <neighborhood/moOrderNeighborhood.h>
// Define types of the representation solution, different neighbors and neighborhoods
//-----------------------------------------------------------------------------
typedef eoInt<unsigned int> Queen; //Permutation (Queen's problem representation)
typedef moSwapNeighbor<Queen> swapNeighbor ; //swap Neighbor
typedef moSwapNeighborhood<Queen> swapNeighborhood; //classical swap Neighborhood
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
typedef moOrderNeighborhood<shiftNeighbor> orderShiftNeighborhood; //order shift Neighborhood (Indexed)
typedef moRndWithoutReplNeighborhood<shiftNeighbor> rndWithoutReplShiftNeighborhood; //random without replacement shift Neighborhood (Indexed)
typedef moRndWithReplNeighborhood<shiftNeighbor> rndWithReplShiftNeighborhood; //random with replacement 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);
/* =========================================================
*
* Eval fitness function
*
* ========================================================= */
queenEval<Queen> fullEval;
/* =========================================================
*
* Initializer of the solution
*
* ========================================================= */
eoInitPermutation<Queen> init(vecSize);
/* =========================================================
*
* evaluation operators of a neighbor solution
*
* ========================================================= */
moFullEvalByModif<swapNeighbor> swapEval(fullEval);
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
/* =========================================================
*
* Neighbors and Neighborhoods
*
* ========================================================= */
swapNeighborhood swapNH;
orderShiftNeighborhood orderShiftNH((vecSize-1) * (vecSize-1));
rndWithoutReplShiftNeighborhood rndNoReplShiftNH((vecSize-1) * (vecSize-1));
rndWithReplShiftNeighborhood rndReplShiftNH((vecSize-1) * (vecSize-1));
/* =========================================================
*
* Init and eval a Queen
*
* ========================================================= */
Queen solution;
init(solution);
fullEval(solution);
std::cout << "Initial Solution:" << std::endl;
std::cout << solution << std::endl << std::endl;
/* =========================================================
*
* Use classical Neighbor and Neighborhood (swap)
*
* ========================================================= */
std::cout << "SWAP NEIGHBORHOOD" << std::endl;
std::cout << "-----------------" << std::endl;
std::cout << "Neighbors List: (Neighbor -> fitness)" << std::endl;
swapNeighbor n1;
swapNH.init(solution, n1);
swapEval(solution,n1);
n1.print();
while (swapNH.cont(solution)) {
swapNH.next(solution, n1);
swapEval(solution,n1);
n1.print();
}
/* =========================================================
*
* Use indexed Neighborhood with shift operator
*
* ========================================================= */
std::cout << "\nSHIFT ORDER NEIGHBORHOOD" << std::endl;
std::cout << "------------------------" << std::endl;
std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl;
shiftNeighbor n2;
orderShiftNH.init(solution, n2);
shiftEval(solution,n2);
n2.print();
while (orderShiftNH.cont(solution)) {
orderShiftNH.next(solution, n2);
shiftEval(solution,n2);
n2.print();
}
std::cout << "\nSHIFT RANDOM WITHOUT REPLACEMENT NEIGHBORHOOD" << std::endl;
std::cout << "---------------------------------------------" << std::endl;
std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl;
rndNoReplShiftNH.init(solution, n2);
shiftEval(solution,n2);
n2.print();
while (rndNoReplShiftNH.cont(solution)) {
rndNoReplShiftNH.next(solution, n2);
shiftEval(solution,n2);
n2.print();
}
std::cout << "\nSHIFT RANDOM WITH REPLACEMENT NEIGHBORHOOD" << std::endl;
std::cout << "---------------------------------------------" << std::endl;
std::cout << "Neighbors List: (key: Neighbor -> fitness)" << std::endl;
rndReplShiftNH.init(solution, n2);
shiftEval(solution,n2);
n2.print();
for (unsigned int i=0; i<100; i++) {
rndReplShiftNH.next(solution, n2);
shiftEval(solution,n2);
n2.print();
}
}
// 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;
}

View file

@ -0,0 +1,11 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276172025 # -S : Random number seed
###### Persistence ######
# --status=./testNeighborhood.status # Status file
###### Representation ######
# --vecSize=8 # -V : Genotype size

View file

@ -0,0 +1,23 @@
# Lesson 3
######################################################################################
### 0) Define files
######################################################################################
set(files testSimulatedAnnealing)
######################################################################################
### 1) Create the lesson
######################################################################################
add_lesson(mo Lesson3 "${files}")
######################################################################################
### 2) Include dependencies
######################################################################################
include_directories(${EO_SRC_DIR}/src
${MO_SRC_DIR}/src
${PROBLEMS_SRC_DIR})

View file

@ -0,0 +1,232 @@
//-----------------------------------------------------------------------------
/** testSimulatedAnnealing.cpp
*
* SV - 29/03/10
* JH - 20/04/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>
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/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>
//-----------------------------------------------------------------------------
// Define types of the representation solution, different neighbors and neighborhoods
//-----------------------------------------------------------------------------
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)
{
/* =========================================================
*
* 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 always get the same result, NOT a random run
rng.reseed(seed);
/* =========================================================
*
* Eval fitness function
*
* ========================================================= */
queenEval<Queen> fullEval;
/* =========================================================
*
* Initilisation of the solution
*
* ========================================================= */
eoInitPermutation<Queen> init(vecSize);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
rndShiftNeighborhood rndShiftNH((vecSize-1) * (vecSize-1));
/* =========================================================
*
* 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;
/* =========================================================
*
* the cooling schedule of the process
*
* ========================================================= */
// initial temp, factor of decrease, number of steps without decrease, final temp.
moSimpleCoolingSchedule<Queen> coolingSchedule(1, 0.9, 100, 0.01);
/* =========================================================
*
* Comparator of neighbors
*
* ========================================================= */
moSolNeighborComparator<shiftNeighbor> solComparator;
/* =========================================================
*
* Example of Checkpointing
*
* ========================================================= */
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);
moSA<shiftNeighbor> 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;
}

View file

@ -0,0 +1,11 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276172057 # -S : Random number seed
###### Persistence ######
# --status=./testSimulatedAnnealing.status # Status file
###### Representation ######
# --vecSize=8 # -V : Genotype size

View file

@ -0,0 +1,22 @@
# Lesson 4
######################################################################################
### 0) Define files
######################################################################################
set(files testSimpleTS)
######################################################################################
### 1) Create the lesson
######################################################################################
add_lesson(mo Lesson4 "${files}")
######################################################################################
### 2) Include dependencies
######################################################################################
include_directories(${EO_SRC_DIR}/src
${MO_SRC_DIR}/src
${PROBLEMS_SRC_DIR})

View file

@ -0,0 +1,266 @@
//-----------------------------------------------------------------------------
/** testSimpleHC.cpp
*
* SV - 12/01/10
* JH - 03/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>
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>
//Algorithm and its components
#include <algo/moTS.h>
//mo eval
#include <eval/moFullEvalByModif.h>
#include <eval/moFullEvalByCopy.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
// seed
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();
// size tabu list
eoValueParam<unsigned int> sizeTabuListParam(7, "sizeTabuList", "size of the tabu list", 'T');
parser.processParam( sizeTabuListParam, "Search Parameters" );
unsigned sizeTabuList = sizeTabuListParam.value();
// time Limit
eoValueParam<unsigned int> timeLimitParam(1, "timeLimit", "time limits", 'T');
parser.processParam( timeLimitParam, "Search Parameters" );
unsigned timeLimit = timeLimitParam.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 solutions
*
* ========================================================= */
Queen sol1;
Queen sol2;
Queen sol3;
//random initialization
init(sol1);
init(sol2);
init(sol3);
//evaluation
fullEval(sol1);
fullEval(sol2);
fullEval(sol3);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
orderShiftNeighborhood orderShiftNH((vecSize-1) * (vecSize-1));
/* =========================================================
*
* Comparator of neighbors and solutions
*
* ========================================================= */
moSolNeighborComparator<shiftNeighbor> solComparator;
moNeighborComparator<shiftNeighbor> comparator;
/* =========================================================
*
* tabu list
*
* ========================================================= */
moNeighborVectorTabuList<shiftNeighbor> tl(sizeTabuList,0);
/* =========================================================
*
* Memories
*
* ========================================================= */
moDummyIntensification<shiftNeighbor> inten;
moDummyDiversification<shiftNeighbor> div;
moBestImprAspiration<shiftNeighbor> asp;
/* =========================================================
*
* continuator
*
* ========================================================= */
moTimeContinuator<shiftNeighbor> continuator(timeLimit);
/* =========================================================
*
* the local search algorithms
*
* ========================================================= */
//Basic Constructor
moTS<shiftNeighbor> localSearch1(orderShiftNH, fullEval, shiftEval, 2, 7);
//Simple Constructor
moTS<shiftNeighbor> localSearch2(orderShiftNH, fullEval, shiftEval, 3, tl);
//General Constructor
moTS<shiftNeighbor> localSearch3(orderShiftNH, fullEval, shiftEval, comparator, solComparator, continuator, tl, inten, div, asp);
/* =========================================================
*
* execute the local search from random solution
*
* ========================================================= */
//Can be eval here, else it will be done at the beginning of the localSearch
//fullEval(solution);
//Run the three Tabu Search and print initial and final solutions
std::cout << "Tabu Search 1:" << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "initial: " << sol1 << std::endl ;
localSearch1(sol1);
std::cout << "final: " << sol1 << std::endl << std::endl;
std::cout << "Tabu Search 2:" << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "initial: " << sol2 << std::endl ;
localSearch2(sol2);
std::cout << "final: " << sol2 << std::endl << std::endl;
std::cout << "Tabu Search 3:" << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "initial: " << sol3 << std::endl ;
localSearch3(sol3);
std::cout << "final: " << sol3 << 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;
}

View file

@ -0,0 +1,15 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276172081 # -S : Random number seed
###### Persistence ######
# --status=./testSimpleTS.status # Status file
###### Representation ######
# --vecSize=8 # -V : Genotype size
###### Search Parameters ######
# --sizeTabuList=7 # -T : size of the tabu list
# --timeLimit=1 # -T : time limits

View file

@ -0,0 +1,23 @@
# Lesson 5
######################################################################################
### 0) Define files
######################################################################################
set(files testILS)
######################################################################################
### 1) Create the lesson
######################################################################################
add_lesson(mo Lesson5 "${files}")
######################################################################################
### 2) Include dependencies
######################################################################################
include_directories(${EO_SRC_DIR}/src
${MO_SRC_DIR}/src
${PROBLEMS_SRC_DIR})

View file

@ -0,0 +1,230 @@
//-----------------------------------------------------------------------------
/** testILS.cpp
*
* SV - 12/01/10
* JH - 04/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>
//Algorithm and its components
#include <algo/moTS.h>
#include <algo/moILS.h>
//mo eval
#include <eval/moFullEvalByCopy.h>
#include <perturb/moMonOpPerturb.h>
#include <perturb/moRestartPerturb.h>
#include <perturb/moNeighborhoodPerturb.h>
#include <acceptCrit/moAlwaysAcceptCrit.h>
#include <acceptCrit/moBetterAcceptCrit.h>
#include <continuator/moIterContinuator.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 solutions
*
* ========================================================= */
Queen sol1;
Queen sol2;
Queen sol3;
//random initialization
init(sol1);
init(sol2);
init(sol3);
//evaluation
fullEval(sol1);
fullEval(sol2);
fullEval(sol3);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
orderShiftNeighborhood orderShiftNH((vecSize-1) * (vecSize-1));
/* =========================================================
*
* the local search algorithms
*
* ========================================================= */
//Basic Constructor of the Tabu Search
moTS<shiftNeighbor> ts(orderShiftNH, fullEval, shiftEval, 1, 7);
eoSwapMutation<Queen> mut;
//Basic Constructor of the Iterated Local Search
moILS<shiftNeighbor> localSearch1(ts, fullEval, mut, 3);
//Simple Constructor of the Iterated Local Search
//Be carefull, template of the continuator must be a dummyNeighbor!!!
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 ;
localSearch1(sol1);
std::cout << "final: " << sol1 << std::endl << std::endl;
std::cout << "Iterated Local Search 2:" << std::endl;
std::cout << "--------------" << std::endl;
std::cout << "initial: " << sol2 << std::endl ;
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
int main(int argc, char **argv)
{
try {
main_function(argc, argv);
}
catch (exception& e) {
cout << "Exception: " << e.what() << '\n';
}
return 1;
}

View file

@ -0,0 +1,11 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276172246 # -S : Random number seed
###### Persistence ######
# --status=./testILS.status # Status file
###### Representation ######
# --vecSize=8 # -V : Genotype size

View file

@ -0,0 +1,33 @@
# Lesson 6
######################################################################################
### 0) Define files
######################################################################################
set(files
adaptiveWalks
autocorrelation
densityOfStates
fdc
fitnessCloud
neutralDegree
neutralWalk
sampling
testMetropolisHasting
testRandomNeutralWalk
testRandomWalk
)
######################################################################################
### 1) Create the lesson
######################################################################################
add_lesson(mo Lesson6 "${files}")
######################################################################################
### 2) Include dependencies
######################################################################################
include_directories(${EO_SRC_DIR}/src
${MO_SRC_DIR}/src
${PROBLEMS_SRC_DIR})

View file

@ -0,0 +1,210 @@
//-----------------------------------------------------------------------------
/** adaptiveWalks.cpp
*
* SV - 05/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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all the neighbors
//-----------------------------------------------------------------------------
// the sampling class
#include <sampling/moHillClimberSampling.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// the number of adaptive walks
eoValueParam<unsigned int> solParam(100, "nbSol", "Number of adaptive walks", 'n');
parser.processParam( solParam, "Representation" );
unsigned nbSol = solParam.value();
// the name of the output file
string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" );
// 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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in order
// from bit 0 to bit vecSize-1
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* The sampling of the search space
*
* ========================================================= */
// sampling object :
// - random initialization
// - local search to sample the search space
// - one statistic to compute
moHillClimberSampling<Neighbor> sampling(random, neighborhood, fullEval, neighborEval, nbSol);
/* =========================================================
*
* execute the sampling
*
* ========================================================= */
sampling();
/* =========================================================
*
* export the sampling
*
* ========================================================= */
// to export the statistics into file
sampling.fileExport(str_out);
// to get the values of statistics
// so, you can compute some statistics in c++ from the data
const std::vector<double> & lengthValues = sampling.getValues(0);
std::cout << "First values:" << std::endl;
std::cout << "Length " << lengthValues[0] << std::endl;
std::cout << "Last values:" << std::endl;
std::cout << "Length " << lengthValues[lengthValues.size() - 1] << 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;
}

View file

@ -0,0 +1,13 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179853 # -S : Random number seed
###### Persistence ######
# --out=out.dat # -o : Output file of the sampling
# --status=./adaptiveWalks.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --nbSol=100 # -n : Number of adaptive walks

View file

@ -0,0 +1,225 @@
//-----------------------------------------------------------------------------
/** autocorrelation.cpp
*
* SV - 03/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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moRndWithReplNeighborhood.h> // visit one random neighbor possibly the same one several times
//-----------------------------------------------------------------------------
// the sampling class
#include <sampling/moAutocorrelationSampling.h>
//-----------------------------------------------------------------------------
// the statistics class
#include <sampling/moStatistics.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// the number of steps of the random walk
eoValueParam<unsigned int> stepParam(100, "nbStep", "Number of steps of the random walk", 'n');
parser.processParam( stepParam, "Representation" );
unsigned nbStep = stepParam.value();
// the name of the output file
string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" );
// 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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in random order
// at each step one bit is randomly generated
moRndWithReplNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* The sampling of the search space
*
* ========================================================= */
// sampling object :
// - random initialization
// - neighborhood to compute the next step
// - fitness function
// - neighbor evaluation
// - number of steps of the walk
moAutocorrelationSampling<Neighbor> sampling(random, neighborhood, fullEval, neighborEval, nbStep);
/* =========================================================
*
* execute the sampling
*
* ========================================================= */
sampling();
/* =========================================================
*
* export the sampling
*
* ========================================================= */
// to export the statistics into file
sampling.fileExport(str_out);
// to get the values of statistics
// so, you can compute some statistics in c++ from the data
const std::vector<double> & fitnessValues = sampling.getValues(0);
std::cout << "First values:" << std::endl;
std::cout << "Fitness " << fitnessValues[0] << std::endl;
std::cout << "Last values:" << std::endl;
std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl;
// more basic statistics on the distribution:
moStatistics statistics;
vector<double> rho, phi;
statistics.autocorrelation(fitnessValues, 10, rho, phi);
for (unsigned s = 0; s < rho.size(); s++)
std::cout << s << " " << "rho=" << rho[s] << ", phi=" << phi[s] << 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;
}

View file

@ -0,0 +1,13 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179858 # -S : Random number seed
###### Persistence ######
# --out=out.dat # -o : Output file of the sampling
# --status=./autocorrelation.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --nbStep=100 # -n : Number of steps of the random walk

View file

@ -0,0 +1,193 @@
//-----------------------------------------------------------------------------
/** densityOfStates.cpp
*
* SV - 03/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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
//-----------------------------------------------------------------------------
// the sampling class
#include <sampling/moDensityOfStatesSampling.h>
//-----------------------------------------------------------------------------
// the statistics class
#include <sampling/moStatistics.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// the number of solution sampled
eoValueParam<unsigned int> solParam(100, "nbSol", "Number of random solution", 'n');
parser.processParam( solParam, "Representation" );
unsigned nbSol = solParam.value();
// the name of the output file
string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" );
// 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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* The sampling of the search space
*
* ========================================================= */
// sampling object :
// - random initialization
// - fitness function
// - number of solutions to sample
moDensityOfStatesSampling<Neighbor> sampling(random, fullEval, nbSol);
/* =========================================================
*
* execute the sampling
*
* ========================================================= */
sampling();
/* =========================================================
*
* export the sampling
*
* ========================================================= */
// to export the statistics into file
sampling.fileExport(str_out);
// to get the values of statistics
// so, you can compute some statistics in c++ from the data
const std::vector<double> & fitnessValues = sampling.getValues(0);
std::cout << "First values:" << std::endl;
std::cout << "Fitness " << fitnessValues[0] << std::endl;
std::cout << "Last values:" << std::endl;
std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl;
// more basic statistics on the distribution:
double min, max, avg, std;
moStatistics statistics;
statistics.basic(fitnessValues, min, max, avg, std);
std::cout << "min=" << min << ", max=" << max << ", average=" << avg << ", std dev=" << std << 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;
}

View file

@ -0,0 +1,13 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179907 # -S : Random number seed
###### Persistence ######
# --out=out.dat # -o : Output file of the sampling
# --status=./densityOfStates.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --nbSol=100 # -n : Number of random solution

192
mo/tutorial/Lesson6/fdc.cpp Normal file
View file

@ -0,0 +1,192 @@
//-----------------------------------------------------------------------------
/** fdc.cpp
*
* SV - 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
//-----------------------------------------------------------------------------
// the distance defined over the search space
#include <utils/eoDistance.h>
//-----------------------------------------------------------------------------
// the sampling class
#include <sampling/moFDCsampling.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// the number of solution sampled
eoValueParam<unsigned int> solParam(100, "nbSol", "Number of random solution", 'n');
parser.processParam( solParam, "Representation" );
unsigned nbSol = solParam.value();
// the name of the output file
string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" );
// 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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* The sampling of the search space
*
* ========================================================= */
// Hamming distance to the global optimum
eoHammingDistance<Indi> distance; // Hamming distance
Indi bestSolution(vecSize, true); // global optimum
// sampling object :
// - random initialization
// - fitness function
// - number of solutions to sample
moFDCsampling<Neighbor> sampling(random, fullEval, distance, bestSolution, nbSol);
/* =========================================================
*
* execute the sampling
*
* ========================================================= */
sampling();
/* =========================================================
*
* export the sampling
*
* ========================================================= */
// to export the statistics into file
sampling.fileExport(str_out);
// to get the values of statistics
// so, you can compute some statistics in c++ from the data
const std::vector<double> & fitnessValues = sampling.getValues(0);
const std::vector<double> & distValues = sampling.getValues(1);
std::cout << "First values:" << std::endl;
std::cout << "Fitness " << fitnessValues[0] << std::endl;
std::cout << "Distance " << distValues[0] << std::endl;
std::cout << "Last values:" << std::endl;
std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl;
std::cout << "Distance " << distValues[distValues.size() - 1] << 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;
}

View file

@ -0,0 +1,13 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179837 # -S : Random number seed
###### Persistence ######
# --out=out.dat # -o : Output file of the sampling
# --status=./fdc.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --nbSol=100 # -n : Number of random solution

View file

@ -0,0 +1,217 @@
//-----------------------------------------------------------------------------
/** fitnessCloud.cpp
*
* SV - 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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moRndWithoutReplNeighborhood.h> // visit one random neighbor possibly the same one several times
//-----------------------------------------------------------------------------
// the sampling class
#include <sampling/moRndRndFitnessCloudSampling.h>
#include <sampling/moMHRndFitnessCloudSampling.h>
#include <sampling/moRndBestFitnessCloudSampling.h>
#include <sampling/moMHBestFitnessCloudSampling.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// the number of solution sampled
eoValueParam<unsigned int> solParam(100, "nbSol", "Number of random solution", 'n');
parser.processParam( solParam, "Representation" );
unsigned nbSol = solParam.value();
// the name of the output file
string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" );
// 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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in random order
// at each step one bit is randomly generated
moRndWithoutReplNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* The sampling of the search space
*
* ========================================================= */
// sampling object :
// - random initialization
// - neighborhood to compute one random neighbor
// - fitness function
// - neighbor evaluation
// - number of solutions to sample
// moRndRndFitnessCloudSampling<Neighbor> sampling(random, neighborhood, fullEval, neighborEval, nbSol);
// moMHRndFitnessCloudSampling<Neighbor> sampling(random, neighborhood, fullEval, neighborEval, nbSol);
// moRndBestFitnessCloudSampling<Neighbor> sampling(random, neighborhood, fullEval, neighborEval, nbSol);
moMHBestFitnessCloudSampling<Neighbor> sampling(random, neighborhood, fullEval, neighborEval, nbSol);
/* =========================================================
*
* execute the sampling
*
* ========================================================= */
sampling();
/* =========================================================
*
* export the sampling
*
* ========================================================= */
// to export the statistics into file
sampling.fileExport(str_out);
// to get the values of statistics
// so, you can compute some statistics in c++ from the data
const std::vector<double> & fitnessValues = sampling.getValues(0);
const std::vector<double> & neighborFitnessValues = sampling.getValues(1);
std::cout << "First values:" << std::endl;
std::cout << "Fitness " << fitnessValues[0] << std::endl;
std::cout << "Neighbor Fitness " << neighborFitnessValues[0] << std::endl;
std::cout << "Last values:" << std::endl;
std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl;
std::cout << "Neighbor Fitness " << neighborFitnessValues[neighborFitnessValues.size() - 1] << 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;
}

View file

@ -0,0 +1,13 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179868 # -S : Random number seed
###### Persistence ######
# --out=out.dat # -o : Output file of the sampling
# --status=./fitnessCloud.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --nbSol=100 # -n : Number of random solution

View file

@ -0,0 +1,215 @@
//-----------------------------------------------------------------------------
/** neutralDegree.cpp
*
* SV - 03/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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/royalRoadEval.h>
#include <problems/eval/moRoyalRoadIncrEval.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moOrderNeighborhood.h> // visit all neighbor in order of their bit-flip
//-----------------------------------------------------------------------------
// the sampling class
#include <sampling/moNeutralDegreeSampling.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// size of the block
eoValueParam<unsigned int> blockSizeParam(4, "blockSize", "Block size of the Royal Road", 'k');
parser.processParam( blockSizeParam, "Representation" );
unsigned blockSize = blockSizeParam.value();
// the number of solution sampled
eoValueParam<unsigned int> solParam(100, "nbSol", "Number of random solution", 'n');
parser.processParam( solParam, "Representation" );
unsigned nbSol = solParam.value();
// the name of the output file
string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" );
// 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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is the royal function (oneMax is a Royal Road with block of 1)
RoyalRoadEval<Indi> fullEval(blockSize);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Incremental evaluation of the neighbor: fitness is modified by +1 , 0 or -1
moRoyalRoadIncrEval<Neighbor> neighborEval(fullEval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in increasing order of the neigbor's index:
// bit-flip from bit 0 to bit (vecSize - 1)
moOrderNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* The sampling of the search space
*
* ========================================================= */
// sampling object :
// - random initialization
// - neighborhood to compute the neutral degree
// - fitness function
// - neighbor evaluation
// - number of solutions to sample
moNeutralDegreeSampling<Neighbor> sampling(random, neighborhood, fullEval, neighborEval, nbSol);
/* =========================================================
*
* execute the sampling
*
* ========================================================= */
sampling();
/* =========================================================
*
* export the sampling
*
* ========================================================= */
// to export the statistics into file
sampling.fileExport(str_out);
// to get the values of statistics
// so, you can compute some statistics in c++ from the data
const std::vector<double> & fitnessValues = sampling.getValues(0);
const std::vector<double> & ndValues = sampling.getValues(1);
std::cout << "First values:" << std::endl;
std::cout << "Fitness " << fitnessValues[0] << std::endl;
std::cout << "N. Degree " << ndValues[0] << std::endl;
std::cout << "Last values:" << std::endl;
std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl;
std::cout << "N. Degree " << ndValues[fitnessValues.size() - 1] << 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;
}

View file

@ -0,0 +1,14 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179874 # -S : Random number seed
###### Persistence ######
# --out=out.dat # -o : Output file of the sampling
# --status=./neutralDegree.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --blockSize=4 # -k : Block size of the Royal Road
# --nbSol=100 # -n : Number of random solution

View file

@ -0,0 +1,251 @@
//-----------------------------------------------------------------------------
/** neutralWalk.cpp
*
* SV - 07/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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/royalRoadEval.h>
#include <problems/eval/moRoyalRoadIncrEval.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moRndWithoutReplNeighborhood.h> // visit one random neighbor possibly the same one several times
//-----------------------------------------------------------------------------
// the sampling class
#include <utils/eoDistance.h>
#include <sampling/moNeutralWalkSampling.h>
//-----------------------------------------------------------------------------
// the statistics class
#include <sampling/moStatistics.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// size of the block
eoValueParam<unsigned int> blockSizeParam(4, "blockSize", "Block size of the Royal Road", 'k');
parser.processParam( blockSizeParam, "Representation" );
unsigned blockSize = blockSizeParam.value();
// the number of steps of the random walk
eoValueParam<unsigned int> stepParam(100, "nbStep", "Number of steps of the random walk", 'n');
parser.processParam( stepParam, "Representation" );
unsigned nbStep = stepParam.value();
// the name of the output file
string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" );
// 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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is the royal function (oneMax is a Royal Road with block of 1)
RoyalRoadEval<Indi> fullEval(blockSize);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Incremental evaluation of the neighbor: fitness is modified by +1 , 0 or -1
moRoyalRoadIncrEval<Neighbor> neighborEval(fullEval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in random order
// at each step one bit is randomly generated
moRndWithoutReplNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* The sampling of the search space
*
* ========================================================= */
// Initial Solution of the random neutral walk
Indi initialSol(vecSize, false);
// nearly 2 blocks are complete
for (unsigned i = 0; i < blockSize - 1; i++) {
initialSol[i] = true;
initialSol[blockSize + i] = true;
initialSol[2 * blockSize + i] = true;
}
// first block is complete
initialSol[blockSize - 1] = true;
// evaluation of the initial solution
fullEval(initialSol);
// Hamming distance
eoHammingDistance<Indi> distance;
// sampling object :
// - random initialization
// - neighborhood to compute the next step
// - fitness function
// - neighbor evaluation
// - number of steps of the walk
moNeutralWalkSampling<Neighbor> sampling(initialSol, neighborhood, fullEval, neighborEval, distance, nbStep);
/* =========================================================
*
* execute the sampling
*
* ========================================================= */
std::cout << "Initial Solution: " << initialSol << std::endl;
// the sampling
sampling();
/* =========================================================
*
* export the sampling
*
* ========================================================= */
// to export the statistics into file
sampling.fileExport(str_out);
// to get the values of statistics
// so, you can compute some statistics in c++ from the data
const std::vector<Indi> & solutions = sampling.getSolutions(0);
std::cout << "First values:" << std::endl;
std::cout << "Solution " << solutions[0] << std::endl;
std::cout << "Last values:" << std::endl;
std::cout << "Solution " << solutions[solutions.size() - 1] << std::endl;
// export only the solution into file
sampling.fileExport(0, str_out + "_sol");
// more basic statistics on the distribution:
moStatistics statistics;
vector< vector<double> > dist;
vector<double> v;
statistics.distances(solutions, distance, dist);
for (unsigned i = 0; i < dist.size(); i++) {
for (unsigned j = 0; j < dist.size(); j++) {
std::cout << dist[i][j] << " " ;
if (j < i)
v.push_back(dist[i][j]);
}
std::cout << std::endl;
}
double min, max, avg, std;
statistics.basic(v, min, max, avg, std);
std::cout << "min=" << min << ", max=" << max << ", average=" << avg << ", std dev=" << std << 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;
}

View file

@ -0,0 +1,14 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179843 # -S : Random number seed
###### Persistence ######
# --out=out.dat # -o : Output file of the sampling
# --status=./neutralWalk.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --blockSize=4 # -k : Block size of the Royal Road
# --nbStep=100 # -n : Number of steps of the random walk

View file

@ -0,0 +1,256 @@
//-----------------------------------------------------------------------------
/** sampling.cpp
*
* SV - 03/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>
// declaration of the namespace
using namespace std;
//-----------------------------------------------------------------------------
// representation of solutions, and neighbors
#include <ga/eoBit.h> // bit string : see also EO tutorial lesson 1: FirstBitGA.cpp
#include <problems/bitString/moBitNeighbor.h> // neighbor of bit string
//-----------------------------------------------------------------------------
// fitness function, and evaluation of neighbors
#include <eval/oneMaxEval.h>
#include <problems/eval/moOneMaxIncrEval.h>
#include <eval/moFullEvalByModif.h>
//-----------------------------------------------------------------------------
// neighborhood description
#include <neighborhood/moRndWithReplNeighborhood.h> // visit one random neighbor possibly the same one several times
//-----------------------------------------------------------------------------
// the random walk local search: heuristic to sample the search space
#include <algo/moRandomWalk.h>
//-----------------------------------------------------------------------------
// the statistics to compute during the sampling
#include <continuator/moFitnessStat.h>
#include <utils/eoDistance.h>
#include <continuator/moDistanceStat.h>
#include <continuator/moSolutionStat.h>
//-----------------------------------------------------------------------------
// the sampling class
#include <sampling/moSampling.h>
// Declaration of types
//-----------------------------------------------------------------------------
// Indi is the typedef of the solution type like in paradisEO-eo
typedef eoBit<unsigned int> Indi; // bit string with unsigned fitness type
// Neighbor is the typedef of the neighbor type,
// Neighbor = How to compute the neighbor from the solution + information on it (i.e. fitness)
// all classes from paradisEO-mo use this template type
typedef moBitNeighbor<unsigned int> Neighbor ; // bit string neighbor with unsigned fitness type
void main_function(int argc, char **argv)
{
/* =========================================================
*
* Parameters
*
* ========================================================= */
// more information on the input parameters: see EO tutorial lesson 3
// but don't care at first it just read the parameters of the bit string size and the random seed.
// 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
// random seed parameter
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// length of the bit string
eoValueParam<unsigned int> vecSizeParam(20, "vecSize", "Genotype size", 'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// the number of steps of the random walk
eoValueParam<unsigned int> stepParam(100, "nbStep", "Number of steps of the random walk", 'n');
parser.processParam( stepParam, "Representation" );
unsigned nbStep = stepParam.value();
// the name of the output file
string str_out = "out.dat"; // default value
eoValueParam<string> outParam(str_out.c_str(), "out", "Output file of the sampling", 'o');
parser.processParam(outParam, "Persistence" );
// 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
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
rng.reseed(seed);
/* =========================================================
*
* Initialization of the solution
*
* ========================================================= */
// a Indi random initializer: each bit is random
// more information: see EO tutorial lesson 1 (FirstBitGA.cpp)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Eval fitness function (full evaluation)
*
* ========================================================= */
// the fitness function is just the number of 1 in the bit string
oneMaxEval<Indi> fullEval;
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
// Use it if there is no incremental evaluation: a neighbor is evaluated by the full evaluation of a solution
// moFullEvalByModif<Neighbor> neighborEval(fullEval);
// Incremental evaluation of the neighbor: fitness is modified by +/- 1
moOneMaxIncrEval<Neighbor> neighborEval;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
// Exploration of the neighborhood in random order
// at each step one bit is randomly generated
moRndWithReplNeighborhood<Neighbor> neighborhood(vecSize);
/* =========================================================
*
* the local search algorithm to sample the search space
*
* ========================================================= */
moRandomWalk<Neighbor> walk(neighborhood, fullEval, neighborEval, nbStep);
/* =========================================================
*
* the statistics to compute
*
* ========================================================= */
// fitness of the solution at each step
moFitnessStat<Indi> fStat;
// Hamming distance to the global optimum
eoHammingDistance<Indi> distance; // Hamming distance
Indi bestSolution(vecSize, true); // global optimum
moDistanceStat<Indi, double> distStat(distance, bestSolution); // statistic
// "statistic" of the solution
moSolutionStat<Indi> solStat;
/* =========================================================
*
* The sampling of the search space
*
* ========================================================= */
// sampling object :
// - random initialization
// - local search to sample the search space
// - one statistic to compute
moSampling<Neighbor> sampling(random, walk, fStat);
// to add another statistics
sampling.add(distStat); // distance
sampling.add(solStat); // solutions
/* =========================================================
*
* execute the sampling
*
* ========================================================= */
sampling();
/* =========================================================
*
* export the sampling
*
* ========================================================= */
// to export the statistics into file
sampling.fileExport(str_out);
// to get the values of statistics
// so, you can compute some statistics in c++ from the data
const std::vector<double> & fitnessValues = sampling.getValues(0);
const std::vector<double> & distValues = sampling.getValues(1);
const std::vector<Indi> & solutions = sampling.getSolutions(2);
std::cout << "First values:" << std::endl;
std::cout << "Fitness " << fitnessValues[0] << std::endl;
std::cout << "Distance " << distValues[0] << std::endl;
std::cout << "Solution " << solutions[0] << std::endl << std::endl;
std::cout << "Last values:" << std::endl;
std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl;
std::cout << "Distance " << distValues[distValues.size() - 1] << std::endl;
std::cout << "Solution " << solutions[solutions.size() - 1] << 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;
}

View file

@ -0,0 +1,13 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179904 # -S : Random number seed
###### Persistence ######
# --out=out.dat # -o : Output file of the sampling
# --status=./sampling.status # Status file
###### Representation ######
# --vecSize=20 # -V : Genotype size
# --nbStep=100 # -n : Number of steps of the random walk

View file

@ -0,0 +1,201 @@
//-----------------------------------------------------------------------------
/** testMetropolisHasting.cpp
*
* SV - 22/01/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>
using namespace std;
//-----------------------------------------------------------------------------
// fitness function
#include <eval/oneMaxEval.h>
#include <problems/bitString/moBitNeighbor.h>
#include <eoInt.h>
#include <neighborhood/moRndWithReplNeighborhood.h>
#include <eval/moFullEvalByModif.h>
#include <eval/moFullEvalByCopy.h>
#include <comparator/moNeighborComparator.h>
#include <comparator/moSolNeighborComparator.h>
#include <continuator/moTrueContinuator.h>
#include <algo/moLocalSearch.h>
#include <explorer/moMetropolisHastingExplorer.h>
// REPRESENTATION
//-----------------------------------------------------------------------------
typedef eoBit<unsigned> Indi;
typedef moBitNeighbor<unsigned int> Neighbor ; // incremental evaluation
typedef moRndWithReplNeighborhood<Neighbor> Neighborhood ;
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();
eoValueParam<unsigned int> stepParam(10, "nbStep", "Number of steps of the random walk", 'n');
parser.processParam( stepParam, "Representation" );
unsigned nbStep = stepParam.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);
/* =========================================================
*
* Eval fitness function
*
* ========================================================= */
oneMaxEval<Indi> eval;
/* =========================================================
*
* Initilisation of the solution
*
* ========================================================= */
// a Indi random initializer
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByModif<Neighbor> fulleval(eval);
//An eval by copy can be used instead of the eval by modif
//moFullEvalByCopy<Neighbor> fulleval(eval);
/* =========================================================
*
* Comparator of neighbors
*
* ========================================================= */
moNeighborComparator<Neighbor> comparator;
moSolNeighborComparator<Neighbor> solComparator;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
Neighborhood neighborhood(vecSize);
/* =========================================================
*
* a neighborhood explorer solution
*
* ========================================================= */
moMetropolisHastingExplorer<Neighbor> explorer(neighborhood, fulleval, comparator, solComparator, nbStep);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moTrueContinuator<Neighbor> continuator;//always continue
moLocalSearch<Neighbor> localSearch(explorer, continuator, eval);
/* =========================================================
*
* execute the local search from random sollution
*
* ========================================================= */
Indi solution;
random(solution);
//Can be eval here, else it will be done at the beginning of the localSearch
//eval(solution);
std::cout << "initial: " << solution << std::endl ;
localSearch(solution);
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,12 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179886 # -S : Random number seed
###### Persistence ######
# --status=./testMetropolisHasting.status # Status file
###### Representation ######
# --vecSize=8 # -V : Genotype size
# --nbStep=10 # -n : Number of steps of the random walk

View file

@ -0,0 +1,278 @@
//-----------------------------------------------------------------------------
/** testRandomNeutralWalk.cpp
*
* SV - 22/02/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>
using namespace std;
//-----------------------------------------------------------------------------
// fitness function
#include <eval/royalRoadEval.h>
#include <eoInt.h>
#include <neighborhood/moRndWithoutReplNeighborhood.h>
#include <problems/bitString/moBitNeighbor.h>
#include <eval/moFullEvalByModif.h>
#include <eval/moFullEvalByCopy.h>
#include <comparator/moNeighborComparator.h>
#include <comparator/moSolNeighborComparator.h>
#include <continuator/moTrueContinuator.h>
#include <algo/moLocalSearch.h>
#include <explorer/moRandomNeutralWalkExplorer.h>
#include <continuator/moCheckpoint.h>
#include <continuator/moFitnessStat.h>
#include <utils/eoDistance.h>
#include <continuator/moDistanceStat.h>
#include <neighborhood/moOrderNeighborhood.h>
#include <continuator/moNeighborhoodStat.h>
#include <continuator/moMinNeighborStat.h>
#include <continuator/moMaxNeighborStat.h>
#include <continuator/moSecondMomentNeighborStat.h>
#include <continuator/moNbInfNeighborStat.h>
#include <continuator/moNbSupNeighborStat.h>
#include <continuator/moNeutralDegreeNeighborStat.h>
#include <continuator/moSizeNeighborStat.h>
// REPRESENTATION
//-----------------------------------------------------------------------------
typedef eoBit<unsigned> Indi;
typedef moBitNeighbor<unsigned int> Neighbor ; // incremental evaluation
typedef moRndWithoutReplNeighborhood<Neighbor> Neighborhood ;
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();
eoValueParam<unsigned int> blockSizeParam(2, "blockSize", "Size of block in the royal road", 'k');
parser.processParam( blockSizeParam, "Representation" );
unsigned blockSize = blockSizeParam.value();
eoValueParam<unsigned int> stepParam(10, "nbStep", "Number of steps of the random walk", 'n');
parser.processParam( stepParam, "Representation" );
unsigned nbStep = stepParam.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);
/* =========================================================
*
* Eval fitness function
*
* ========================================================= */
RoyalRoadEval<Indi> eval(blockSize);
/* =========================================================
*
* Initilisazor of the solution
*
* ========================================================= */
// a Indi random initializer
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* Evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByModif<Neighbor> nhEval(eval);
//An eval by copy can be used instead of the eval by modif
//moFullEvalByCopy<Neighbor> nhEval(eval);
/* =========================================================
*
* Comparator of neighbors
*
* ========================================================= */
moNeighborComparator<Neighbor> comparator;
moSolNeighborComparator<Neighbor> solComparator;
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
Neighborhood neighborhood(vecSize);
/* =========================================================
*
* a neighborhood explorer solution
*
* ========================================================= */
moRandomNeutralWalkExplorer<Neighbor> explorer(neighborhood, nhEval, solComparator, nbStep);
/* =========================================================
*
* initial random solution
*
* ========================================================= */
Indi solution;
random(solution);
//Can be eval here, else it will be done at the beginning of the localSearch
eval(solution);
/* =========================================================
*
* the continuator and the checkpoint
*
* ========================================================= */
moTrueContinuator<Neighbor> continuator;//always continue
moCheckpoint<Neighbor> checkpoint(continuator);
moFitnessStat<Indi> fStat;
eoHammingDistance<Indi> distance;
moDistanceStat<Indi, unsigned> distStat(distance, solution); // distance from the intial solution
moOrderNeighborhood<Neighbor> nh(vecSize);
moNeighborhoodStat< Neighbor > neighborhoodStat(nh, nhEval, comparator, solComparator);
moMinNeighborStat< Neighbor > minStat(neighborhoodStat);
moSecondMomentNeighborStat< Neighbor > secondMomentStat(neighborhoodStat);
moMaxNeighborStat< Neighbor > maxStat(neighborhoodStat);
moNbSupNeighborStat< Neighbor > nbSupStat(neighborhoodStat);
moNbInfNeighborStat< Neighbor > nbInfStat(neighborhoodStat);
moNeutralDegreeNeighborStat< Neighbor > ndStat(neighborhoodStat);
moSizeNeighborStat< Neighbor > sizeStat(neighborhoodStat);
eoValueParam<unsigned int> genCounter(-1,"Gen");
eoIncrementor<unsigned int> increm(genCounter.value());
checkpoint.add(fStat);
checkpoint.add(distStat);
checkpoint.add(neighborhoodStat);
checkpoint.add(minStat);
checkpoint.add(secondMomentStat);
checkpoint.add(maxStat);
checkpoint.add(nbInfStat);
checkpoint.add(ndStat);
checkpoint.add(nbSupStat);
checkpoint.add(sizeStat);
checkpoint.add(increm);
eoFileMonitor outputfile("out.dat", " ");
checkpoint.add(outputfile);
outputfile.add(genCounter);
outputfile.add(fStat);
outputfile.add(distStat);
outputfile.add(minStat);
outputfile.add(secondMomentStat);
outputfile.add(maxStat);
outputfile.add(nbInfStat);
outputfile.add(ndStat);
outputfile.add(nbSupStat);
outputfile.add(sizeStat);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moLocalSearch<Neighbor> localSearch(explorer, checkpoint, eval);
/* =========================================================
*
* execute the local search from random sollution
*
* ========================================================= */
std::cout << "initial: " << solution << std::endl ;
localSearch(solution);
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,13 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179899 # -S : Random number seed
###### Persistence ######
# --status=./testRandomNeutralWalk.status # Status file
###### Representation ######
# --vecSize=8 # -V : Genotype size
# --blockSize=2 # -k : Size of block in the royal road
# --nbStep=10 # -n : Number of steps of the random walk

View file

@ -0,0 +1,243 @@
//-----------------------------------------------------------------------------
/** testRandomWalk.cpp
*
* SV - 22/01/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>
using namespace std;
//-----------------------------------------------------------------------------
// fitness function
#include <eval/oneMaxEval.h>
#include <problems/bitString/moBitNeighbor.h>
#include <eoInt.h>
#include <neighborhood/moRndWithReplNeighborhood.h>
#include <eval/moFullEvalByModif.h>
#include <eval/moFullEvalByCopy.h>
#include <continuator/moIterContinuator.h>
#include <algo/moLocalSearch.h>
#include <explorer/moRandomWalkExplorer.h>
#include <continuator/moCheckpoint.h>
#include <continuator/moFitnessStat.h>
#include <continuator/moSolutionStat.h>
#include <utils/eoDistance.h>
#include <continuator/moDistanceStat.h>
#include <utils/eoFileMonitor.h>
#include <utils/eoUpdater.h>
// REPRESENTATION
//-----------------------------------------------------------------------------
typedef eoBit<unsigned> Indi;
typedef moBitNeighbor<unsigned int> Neighbor ; // incremental evaluation
typedef moRndWithReplNeighborhood<Neighbor> Neighborhood ;
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();
eoValueParam<unsigned int> stepParam(10, "nbStep", "Number of steps of the random walk", 'n');
parser.processParam( stepParam, "Representation" );
unsigned nbStep = stepParam.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);
/* =========================================================
*
* Eval fitness function
*
* ========================================================= */
oneMaxEval<Indi> eval;
/* =========================================================
*
* Initilisation of the solution
*
* ========================================================= */
// a Indi random initializer
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByModif<Neighbor> nhEval(eval);
//An eval by copy can be used instead of the eval by modif
//moFullEvalByCopy<Neighbor> nhEval(eval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
Neighborhood neighborhood(vecSize);
/* =========================================================
*
* a neighborhood explorer solution
*
* ========================================================= */
moRandomWalkExplorer<Neighbor> explorer(neighborhood, nhEval);
/* =========================================================
*
* the continuator and the checkpoint
*
* ========================================================= */
moIterContinuator<Neighbor> continuator(nbStep);
moCheckpoint<Neighbor> checkpoint(continuator);
moFitnessStat<Indi> fStat;
eoHammingDistance<Indi> distance;
Indi bestSolution(vecSize, true);
moDistanceStat<Indi, unsigned> distStat(distance, bestSolution);
// moSolutionStat<Indi> solStat;
checkpoint.add(fStat);
checkpoint.add(distStat);
// checkpoint.add(solStat);
eoValueParam<int> genCounter(-1,"Gen");
eoIncrementor<int> increm(genCounter.value());
checkpoint.add(increm);
eoFileMonitor outputfile("out.dat", " ");
checkpoint.add(outputfile);
outputfile.add(genCounter);
outputfile.add(fStat);
outputfile.add(distStat);
// outputfile.add(solStat);
Indi solution; // current solution of the search process
/*
// to save the solution at each iteration
eoState outState;
// Register the algorithm into the state (so it has something to save!!
outState.registerObject(solution);
// and feed the state to state savers
// save state every 10th iteration
eoCountedStateSaver stateSaver(10, outState, "iteration");
// Don't forget to add the two savers to the checkpoint
checkpoint.add(stateSaver);
*/
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moLocalSearch<Neighbor> localSearch(explorer, checkpoint, eval);
/* =========================================================
*
* execute the local search from random sollution
*
* ========================================================= */
random(solution);
//Can be eval here, else it will be done at the beginning of the localSearch
//eval(solution);
std::cout << "initial: " << solution << std::endl ;
localSearch(solution);
std::cout << "final: " << solution << 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;
}

View file

@ -0,0 +1,12 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276179894 # -S : Random number seed
###### Persistence ######
# --status=./testRandomWalk.status # Status file
###### Representation ######
# --vecSize=8 # -V : Genotype size
# --nbStep=10 # -n : Number of steps of the random walk

View file

@ -0,0 +1,22 @@
# Lesson 7
######################################################################################
### 0) Define files
######################################################################################
set(files hybridAlgo)
######################################################################################
### 1) Create the lesson
######################################################################################
add_lesson(mo Lesson7 "${files}")
######################################################################################
### 2) Include dependencies
######################################################################################
include_directories(${EO_SRC_DIR}/src
${MO_SRC_DIR}/src
${PROBLEMS_SRC_DIR})

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((vecSize-1) * (vecSize-1));
/* =========================================================
*
* 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;
}

View file

@ -0,0 +1,11 @@
###### General ######
# --help=0 # -h : Prints this message
# --stopOnUnknownParam=1 # Stop if unkown param entered
# --seed=1276172440 # -S : Random number seed
###### Persistence ######
# --status=./hybridAlgo.status # Status file
###### Representation ######
# --vecSize=8 # -V : Genotype size

View file

@ -0,0 +1,22 @@
# Lesson 9
######################################################################################
### 0) Define files
######################################################################################
set(files VNS)
######################################################################################
### 1) Create the lesson
######################################################################################
add_lesson(mo Lesson9 "${files}")
######################################################################################
### 2) Include dependencies
######################################################################################
include_directories(${EO_SRC_DIR}/src
${MO_SRC_DIR}/src
${PROBLEMS_SRC_DIR})

288
mo/tutorial/Lesson9/VNS.cpp Normal file
View file

@ -0,0 +1,288 @@
//-----------------------------------------------------------------------------
/** VNS.cpp
*
* SV - 20/08/10
* JH - 20/08/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>
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 <problems/permutation/moIndexedSwapNeighbor.h>
#include <neighborhood/moIndexNeighbor.h>
#include <neighborhood/moRndWithoutReplNeighborhood.h>
#include <neighborhood/moOrderNeighborhood.h>
#include <explorer/moVNSexplorer.h>
#include <neighborhood/moBackwardVectorVNSelection.h>
#include <neighborhood/moForwardVectorVNSelection.h>
#include <neighborhood/moRndVectorVNSelection.h>
//Algorithm and its components
#include <coolingSchedule/moCoolingSchedule.h>
#include <algo/moSimpleHC.h>
#include <algo/moLocalSearch.h>
#include <algo/moVNS.h>
//#include <algo/moSimpleVNS.h>
#include <continuator/moTimeContinuator.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>
#include <eoSwapMutation.h>
#include <eoShiftMutation.h>
#include <acceptCrit/moBetterAcceptCrit.h>
#include <acceptCrit/moAlwaysAcceptCrit.h>
//-----------------------------------------------------------------------------
// Define types of the representation solution, different neighbors and neighborhoods
//-----------------------------------------------------------------------------
typedef eoInt<eoMinimizingFitness> Queen; //Permutation (Queen's problem representation)
typedef moShiftNeighbor<Queen> shiftNeighbor; //shift Neighbor
typedef moIndexedSwapNeighbor <Queen> swapNeighbor;
typedef moIndexNeighbor<Queen> indexNeighbor;
typedef moRndWithoutReplNeighborhood<shiftNeighbor> shiftNeighborhood; //rnd shift Neighborhood (Indexed)
typedef moRndWithoutReplNeighborhood<swapNeighbor> swapNeighborhood;
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 always get the same result, NOT a random run
rng.reseed(seed);
/* =========================================================
*
* Eval fitness function
*
* ========================================================= */
queenEval<Queen> fullEval;
/* =========================================================
*
* Initilisation of the solution
*
* ========================================================= */
eoInitPermutation<Queen> init(vecSize);
/* =========================================================
*
* evaluation of a neighbor solution
*
* ========================================================= */
moFullEvalByCopy<shiftNeighbor> shiftEval(fullEval);
moFullEvalByCopy<swapNeighbor> swapEval(fullEval);
/* =========================================================
*
* the neighborhood of a solution
*
* ========================================================= */
shiftNeighborhood shiftNH((vecSize-1) * (vecSize-1));
swapNeighborhood swapNH(vecSize * (vecSize-1) / 2);
/* =========================================================
*
* the local search algorithm
*
* ========================================================= */
moSimpleHC<shiftNeighbor> ls1(shiftNH, fullEval, shiftEval);
moSimpleHC<swapNeighbor> ls2(swapNH, fullEval, swapEval);
eoSwapMutation<Queen> swapMut;
eoShiftMutation<Queen> shiftMut;
// moForwardVectorVNSelection<Queen> selectNH(ls1, shiftMut, true);
// moBackwardVectorVNSelection<Queen> selectNH(ls1, shiftMut, true);
moRndVectorVNSelection<Queen> selectNH(ls1, shiftMut, true);
selectNH.add(ls2, swapMut);
moAlwaysAcceptCrit<shiftNeighbor> acceptCrit;
// moVNSexplorer<shiftNeighbor> explorer(selectNH, acceptCrit);
moTimeContinuator<shiftNeighbor> cont(3);
// moLocalSearch<shiftNeighbor> vns(explorer, cont, fullEval);
moVNS<shiftNeighbor> vns(selectNH, acceptCrit, fullEval, cont);
/* moSimpleVNS<shiftNeighbor> svns(ls1, shiftMut, fullEval, cont);
svns.add(ls2, swapMut);*/
/* =========================================================
*
* execute the local search from random solution
*
* ========================================================= */
Queen sol;
init(sol);
fullEval(sol);
std::cout << "#########################################" << std::endl;
std::cout << "initial sol: " << sol << std::endl ;
vns(sol);
std::cout << "final sol: " << sol << std::endl ;
std::cout << "#########################################" << std::endl;
init(sol);
fullEval(sol);
std::cout << "#########################################" << std::endl;
std::cout << "initial sol: " << sol << std::endl ;
//svns(sol);
std::cout << "final sol: " << sol << std::endl ;
std::cout << "#########################################" << std::endl;
//
//
// /* =========================================================
// *
// * the cooling schedule of the process
// *
// * ========================================================= */
//
// // initial temp, factor of decrease, number of steps without decrease, final temp.
// moSimpleCoolingSchedule<Queen> coolingSchedule(1, 0.9, 100, 0.01);
//
// /* =========================================================
// *
// * Comparator of neighbors
// *
// * ========================================================= */
//
// moSolNeighborComparator<shiftNeighbor> solComparator;
//
// /* =========================================================
// *
// * Example of Checkpointing
// *
// * ========================================================= */
//
// 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);
//
// //moSA<shiftNeighbor> 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;
}