* New tree configuration of the project:
.../
... + -- EO
| |
| |
+-- src ----- + -- EDO
| |
| |
+-- test + -- MO
| |
| |
+-- tutorial + -- MOEO
| |
| |
+-- doc + -- SMP
| |
| |
... + -- EOMPI
|
|
+ -- EOSERIAL
Question for current maintainers: ./README: new release?
Also:
* Moving out eompi & eoserial modules (issue #2).
* Correction of the errors when executing "make doc" command.
* Adding a solution for the conflicting headers problem (see the two CMake Cache
Values: PROJECT_TAG & PROJECT_HRS_INSTALL_SUBPATH) (issue #1)
* Header inclusions:
** src: changing absolute paths into relative paths ('#include <...>' -> '#include "..."')
** test, tutorial: changing relative paths into absolute paths ('#include "..."' -> '#include <...>')
* Moving out some scripts from EDO -> to the root
* Add a new script for compilation and installation (see build_gcc_linux_install)
* Compilation with uBLAS library or EDO module: now ok
* Minor modifications on README & INSTALL files
* Comment eompi failed tests with no end
*** TODO: CPack (debian (DEB) & RedHat (RPM) packages) (issues #6 & #7) ***
This commit is contained in:
parent
515bd5943d
commit
490e837f7a
2359 changed files with 7688 additions and 16329 deletions
38
tutorial/eo/app/mastermind/CMakeLists.txt
Executable file
38
tutorial/eo/app/mastermind/CMakeLists.txt
Executable file
|
|
@ -0,0 +1,38 @@
|
|||
######################################################################################
|
||||
### 1) Include the sources
|
||||
######################################################################################
|
||||
|
||||
#INCLUDE_DIRECTORIES(${EO_SRC_DIR}/src)
|
||||
#INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
######################################################################################
|
||||
### 2) Specify where CMake can find the libraries (mandatory: before 3) )
|
||||
######################################################################################
|
||||
|
||||
LINK_DIRECTORIES(${EO_BIN_DIR}/${LIB})
|
||||
|
||||
######################################################################################
|
||||
### 3) Define your target(s): just an executable here
|
||||
######################################################################################
|
||||
|
||||
SET (MASTERMIND_SOURCES mastermind.cpp)
|
||||
|
||||
# no matter what is the OS, hopefully
|
||||
ADD_EXECUTABLE(mastermind ${MASTERMIND_SOURCES})
|
||||
|
||||
ADD_DEPENDENCIES(mastermind eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
### 4) Optionnal: define your target(s)'s version: no effect for windows
|
||||
######################################################################################
|
||||
|
||||
SET(MASTERMIND_VERSION ${GLOBAL_VERSION})
|
||||
SET_TARGET_PROPERTIES(mastermind PROPERTIES VERSION "${MASTERMIND_VERSION}")
|
||||
|
||||
######################################################################################
|
||||
### 5) Link the librairies for your target(s)
|
||||
######################################################################################
|
||||
|
||||
TARGET_LINK_LIBRARIES(mastermind eo eoutils)
|
||||
|
||||
######################################################################################
|
||||
139
tutorial/eo/app/mastermind/mastermind.cpp
Executable file
139
tutorial/eo/app/mastermind/mastermind.cpp
Executable file
|
|
@ -0,0 +1,139 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// mastermind
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <stdlib.h> // EXIT_SUCCESS EXIT_FAILURE
|
||||
#include <stdexcept> // exception
|
||||
#include <iostream> // cerr cout
|
||||
#include <fstream> // ifstream
|
||||
#include <string> // string
|
||||
#include <paradiseo/eo.h> // all usefull eo stuff
|
||||
|
||||
#include "mastermind.h" // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator
|
||||
|
||||
using namespace std;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// global variables
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
unsigned in, out, hidden;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// parameters
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
eoValueParam<unsigned> pop_size(16, "pop_size", "population size", 'p');
|
||||
eoValueParam<unsigned> generations(100, "generations", "number of generation", 'g');
|
||||
eoValueParam<double> mut_rate(0.1, "mut_rate", "mutation rate", 'm');
|
||||
eoValueParam<double> xover_rate(0.5, "xover_rate", "default crossover rate", 'x');
|
||||
eoValueParam<unsigned> col_p(default_colors, "colors", "number of colors", 'c');
|
||||
eoValueParam<unsigned> len_p(default_length, "legth", "solution legth", 'l');
|
||||
eoValueParam<string> sol_p(default_solution, "solution", "problem solution", 's');
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// auxiliar functions
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void arg(int argc, char** argv);
|
||||
void ga();
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// main
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
arg(argc, argv);
|
||||
ga();
|
||||
}
|
||||
catch (exception& e)
|
||||
{
|
||||
cerr << argv[0] << ": " << e.what() << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// implementation
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void arg(int argc, char** argv)
|
||||
{
|
||||
eoParser parser(argc, argv);
|
||||
|
||||
parser.processParam(pop_size, "genetic operators");
|
||||
parser.processParam(generations, "genetic operators");
|
||||
parser.processParam(mut_rate, "genetic operators");
|
||||
parser.processParam(xover_rate, "genetic operators");
|
||||
parser.processParam(col_p, "problem");
|
||||
parser.processParam(len_p, "problem");
|
||||
parser.processParam(sol_p, "problem");
|
||||
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(cout);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
init_eoChromEvaluator(col_p.value(), len_p.value(), sol_p.value());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void ga()
|
||||
{
|
||||
// create population
|
||||
eoInitChrom init;
|
||||
eoPop<Chrom> pop(pop_size.value(), init);
|
||||
|
||||
// evaluate population
|
||||
eoEvalFuncPtr<Chrom> evaluator(eoChromEvaluator);
|
||||
apply<Chrom>(evaluator, pop);
|
||||
|
||||
// selector
|
||||
eoProportionalSelect<Chrom> select(pop);
|
||||
|
||||
// genetic operators
|
||||
eoChromMutation mutation;
|
||||
eoChromXover xover;
|
||||
|
||||
// stop condition
|
||||
eoGenContinue<Chrom> continuator1(generations.value());
|
||||
eoFitContinue<Chrom> continuator2(solution.fitness());
|
||||
eoCombinedContinue<Chrom> continuator(continuator1);
|
||||
continuator.add(continuator2);
|
||||
|
||||
// checkpoint
|
||||
eoCheckPoint<Chrom> checkpoint(continuator);
|
||||
|
||||
// monitor
|
||||
eoStdoutMonitor monitor;
|
||||
checkpoint.add(monitor);
|
||||
|
||||
// statistics
|
||||
eoBestFitnessStat<Chrom> stats;
|
||||
checkpoint.add(stats);
|
||||
monitor.add(stats);
|
||||
|
||||
// genetic algorithm
|
||||
eoSGA<Chrom> sga(select,
|
||||
xover, xover_rate.value(),
|
||||
mutation, mut_rate.value(),
|
||||
evaluator,
|
||||
checkpoint);
|
||||
sga(pop);
|
||||
|
||||
cout << "solution = " << solution << endl
|
||||
<< "best = " << *max_element(pop.begin(), pop.end()) << endl;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// End:
|
||||
199
tutorial/eo/app/mastermind/mastermind.h
Executable file
199
tutorial/eo/app/mastermind/mastermind.h
Executable file
|
|
@ -0,0 +1,199 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// mastermind.h
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef mastermind_h
|
||||
#define mastermind_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <stdlib.h> // exit EXIT_FAILURE
|
||||
#include <paradiseo/eo/eoVector.h> // eoVectorLength
|
||||
#include <paradiseo/eo/eoOp.h> // eoMonOp eoQuadraticOp
|
||||
#include <paradiseo/eo/eoInit.h> // eoInit
|
||||
#include <paradiseo/eo/utils/rnd_generators.h> // uniform_generator
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// phenotype
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef float phenotype;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// genotype
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef std::vector<int> genotype;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Chrom
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
typedef eoVector<phenotype, int> Chrom;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoChromEvaluator
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// const unsigned points_per_black = 3, points_per_white = 1;
|
||||
Chrom solution;
|
||||
|
||||
phenotype eoChromEvaluator(const Chrom& chrom)
|
||||
{
|
||||
Chrom tmp = solution;
|
||||
unsigned black = 0, white = 0;
|
||||
|
||||
// look for blacks
|
||||
for (unsigned i = 0; i < chrom.size(); ++i)
|
||||
if (chrom[i] == tmp[i])
|
||||
{
|
||||
++black;
|
||||
tmp[i] = -1;
|
||||
}
|
||||
|
||||
// look for whites
|
||||
for (unsigned i = 0; i < chrom.size(); ++i)
|
||||
for (unsigned j = 0; j < tmp.size(); ++j)
|
||||
if (chrom[i] == tmp[j])
|
||||
{
|
||||
++white;
|
||||
tmp[j] = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
// return black * points_per_black + white * points_per_white;
|
||||
return black * chrom.size() + white;
|
||||
}
|
||||
|
||||
const unsigned default_length = 8;
|
||||
const unsigned default_colors = 8;
|
||||
const std::string default_solution = "01234567";
|
||||
|
||||
|
||||
unsigned num_colors;
|
||||
|
||||
void init_eoChromEvaluator(const unsigned& c, const unsigned& l, std::string s)
|
||||
{
|
||||
num_colors = c;
|
||||
|
||||
// check consistency between parameters
|
||||
if (s != default_solution)
|
||||
{
|
||||
// check length
|
||||
if (l != default_length && s.size() != l)
|
||||
{
|
||||
std::cerr << "solution length != length" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// check number of colors
|
||||
if ((c != default_colors) && (c < unsigned(*max_element(s.begin(), s.end()) - '0')))
|
||||
{
|
||||
std::cerr << "too high color number found!" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else
|
||||
if (l != default_length || c != default_colors )
|
||||
// generate a random solution
|
||||
if(num_colors <= 10)
|
||||
{
|
||||
uniform_generator<char> color('0', static_cast<char>('0' + c));
|
||||
s.resize(l);
|
||||
generate(s.begin(), s.end(), color);
|
||||
}
|
||||
|
||||
// put the solution parameter on the solution chromosome
|
||||
if (num_colors <= 10)
|
||||
{
|
||||
solution.resize(s.size());
|
||||
for (unsigned i = 0; i < solution.size(); ++i)
|
||||
solution[i] = s[i] - '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
solution.resize(l);
|
||||
uniform_generator<int> color(0, num_colors);
|
||||
generate(solution.begin(), solution.end(), color);
|
||||
}
|
||||
|
||||
solution.fitness(eoChromEvaluator(solution));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoChromInit
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoInitChrom: public eoInit<Chrom>
|
||||
{
|
||||
public:
|
||||
void operator()(Chrom& chrom)
|
||||
{
|
||||
uniform_generator<int> color(0, num_colors);
|
||||
chrom.resize(solution.size());
|
||||
generate(chrom.begin(), chrom.end(), color);
|
||||
chrom.invalidate();
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoChromMutation
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoChromMutation: public eoMonOp<Chrom>
|
||||
{
|
||||
// many operators in one :(
|
||||
bool operator()(Chrom& chrom)
|
||||
{
|
||||
uniform_generator<unsigned> what(0, 2);
|
||||
uniform_generator<unsigned> position(0, chrom.size());
|
||||
|
||||
switch(what())
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
// mutation
|
||||
uniform_generator<int> color(0, num_colors);
|
||||
chrom[position()] = color();
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
// transposition
|
||||
std::swap(chrom[position()], chrom[position()]);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
std::cerr << "unknown operator!" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoChromXover
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
class eoChromXover: public eoQuadOp<Chrom>
|
||||
{
|
||||
public:
|
||||
bool operator()(Chrom& chrom1, Chrom& chrom2)
|
||||
{
|
||||
uniform_generator<unsigned> position(0, chrom1.size());
|
||||
swap_ranges(chrom1.begin(), chrom1.begin() + position(), chrom2.begin());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#endif // mastermind_h
|
||||
|
||||
// Local Variables:
|
||||
// mode:C++
|
||||
// End:
|
||||
Loading…
Add table
Add a link
Reference in a new issue