Migration from SVN
This commit is contained in:
parent
d7d6c3a217
commit
8cd56f37db
29069 changed files with 0 additions and 4096888 deletions
33
mo/tutorial/Lesson6/CMakeLists.txt
Normal file
33
mo/tutorial/Lesson6/CMakeLists.txt
Normal 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})
|
||||
210
mo/tutorial/Lesson6/adaptiveWalks.cpp
Normal file
210
mo/tutorial/Lesson6/adaptiveWalks.cpp
Normal 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;
|
||||
}
|
||||
13
mo/tutorial/Lesson6/adaptiveWalks.param
Normal file
13
mo/tutorial/Lesson6/adaptiveWalks.param
Normal 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
|
||||
225
mo/tutorial/Lesson6/autocorrelation.cpp
Normal file
225
mo/tutorial/Lesson6/autocorrelation.cpp
Normal 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;
|
||||
}
|
||||
13
mo/tutorial/Lesson6/autocorrelation.param
Normal file
13
mo/tutorial/Lesson6/autocorrelation.param
Normal 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
|
||||
193
mo/tutorial/Lesson6/densityOfStates.cpp
Normal file
193
mo/tutorial/Lesson6/densityOfStates.cpp
Normal 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;
|
||||
}
|
||||
13
mo/tutorial/Lesson6/densityOfStates.param
Normal file
13
mo/tutorial/Lesson6/densityOfStates.param
Normal 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
192
mo/tutorial/Lesson6/fdc.cpp
Normal 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;
|
||||
}
|
||||
13
mo/tutorial/Lesson6/fdc.param
Normal file
13
mo/tutorial/Lesson6/fdc.param
Normal 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
|
||||
217
mo/tutorial/Lesson6/fitnessCloud.cpp
Normal file
217
mo/tutorial/Lesson6/fitnessCloud.cpp
Normal 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;
|
||||
}
|
||||
13
mo/tutorial/Lesson6/fitnessCloud.param
Normal file
13
mo/tutorial/Lesson6/fitnessCloud.param
Normal 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
|
||||
215
mo/tutorial/Lesson6/neutralDegree.cpp
Normal file
215
mo/tutorial/Lesson6/neutralDegree.cpp
Normal 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;
|
||||
}
|
||||
14
mo/tutorial/Lesson6/neutralDegree.param
Normal file
14
mo/tutorial/Lesson6/neutralDegree.param
Normal 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
|
||||
251
mo/tutorial/Lesson6/neutralWalk.cpp
Normal file
251
mo/tutorial/Lesson6/neutralWalk.cpp
Normal 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;
|
||||
}
|
||||
14
mo/tutorial/Lesson6/neutralWalk.param
Normal file
14
mo/tutorial/Lesson6/neutralWalk.param
Normal 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
|
||||
256
mo/tutorial/Lesson6/sampling.cpp
Normal file
256
mo/tutorial/Lesson6/sampling.cpp
Normal 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;
|
||||
}
|
||||
13
mo/tutorial/Lesson6/sampling.param
Normal file
13
mo/tutorial/Lesson6/sampling.param
Normal 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
|
||||
201
mo/tutorial/Lesson6/testMetropolisHasting.cpp
Normal file
201
mo/tutorial/Lesson6/testMetropolisHasting.cpp
Normal 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;
|
||||
}
|
||||
12
mo/tutorial/Lesson6/testMetropolisHasting.param
Normal file
12
mo/tutorial/Lesson6/testMetropolisHasting.param
Normal 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
|
||||
278
mo/tutorial/Lesson6/testRandomNeutralWalk.cpp
Normal file
278
mo/tutorial/Lesson6/testRandomNeutralWalk.cpp
Normal 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;
|
||||
}
|
||||
13
mo/tutorial/Lesson6/testRandomNeutralWalk.param
Normal file
13
mo/tutorial/Lesson6/testRandomNeutralWalk.param
Normal 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
|
||||
243
mo/tutorial/Lesson6/testRandomWalk.cpp
Normal file
243
mo/tutorial/Lesson6/testRandomWalk.cpp
Normal 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;
|
||||
}
|
||||
12
mo/tutorial/Lesson6/testRandomWalk.param
Normal file
12
mo/tutorial/Lesson6/testRandomWalk.param
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue