Commit a7c7d447 authored by kruger's avatar kruger

Mises a jour

parent a4156440
......@@ -632,15 +632,15 @@
// Evolution Engine:
Evaluator goal: minimize
Number of generations: 100
Population size: 100
Elite: 4
Number of generations: 5
Population size: 10
Elite: 5
Selection operator: Tournament 7
Offspring size: 50%
Offspring size: 100%
Reduce parents operator: Tournament 4
Surviving parents: 50
Surviving parents: 50%
Reduce offspring operator: Tournament 2
Surviving offspring: 96
Surviving offspring: 96%
Final reduce operator: Tournament 2
Elitism: Weak
......
......@@ -9,7 +9,6 @@
*/
#include "include/CEvolutionaryAlgorithm.h"
#include <string>
#ifndef WIN32
#include <sys/time.h>
#endif
......@@ -18,12 +17,16 @@
#endif
#include <time.h>
#include <math.h>
#include <string>
#include <string.h>
#include "include/CIndividual.h"
#include "include/Parameters.h"
#include "include/CGnuplot.h"
#include "include/global.h"
#include "include/CComUDPLayer.h"
#include "include/CRandomGenerator.h"
#include <stdio.h>
#include <sstream>
#include <fstream>
using namespace std;
......@@ -35,10 +38,11 @@ void EASEAGenerationFunctionBeforeReplacement(CEvolutionaryAlgorithm* evolutiona
extern void evale_pop_chunk(CIndividual** pop, int popSize);
extern bool INSTEAD_EVAL_STEP;
/**
* @DEPRECATED the next contructor has to be used instead of this one.
*/
CEvolutionaryAlgorithm::CEvolutionaryAlgorithm( size_t parentPopulationSize,
/*CEvolutionaryAlgorithm::CEvolutionaryAlgorithm( size_t parentPopulationSize,
size_t offspringPopulationSize,
float selectionPressure, float replacementPressure, float parentReductionPressure, float offspringReductionPressure,
CSelectionOperator* selectionOperator, CSelectionOperator* replacementOperator,
......@@ -62,8 +66,19 @@ CEvolutionaryAlgorithm::CEvolutionaryAlgorithm( size_t parentPopulationSize,
this->reduceParents = 0;
this->reduceOffsprings = 0;
}
// INITIALIZE SERVER OBJECT ISLAND MODEL
if(params->remoteIslandModel){
this->server = new CComUDPServer(2909,0);
this->treatedIndividuals = 0;
this->numberOfClients = 0;
this->initializeClients();
}
}*/
/*****
* REAL CONSTRUCTOR
*/
CEvolutionaryAlgorithm::CEvolutionaryAlgorithm(Parameters* params){
this->params = params;
......@@ -103,17 +118,35 @@ CEvolutionaryAlgorithm::CEvolutionaryAlgorithm(Parameters* params){
this->gnuplot = new CGnuplot((this->params->offspringPopulationSize*this->params->nbGen)+this->params->parentPopulationSize);
}
#endif
// INITIALIZE SERVER OBJECT ISLAND MODEL
if(params->remoteIslandModel){
server = new CComUDPServer(2909,0); //1 if debug
this->treatedIndividuals = 0;
this->numberOfClients = 0;
this->initializeClients();
}
}
/* DESTRUCTOR */
CEvolutionaryAlgorithm::~CEvolutionaryAlgorithm(){
delete population;
delete population;
if(this->params->remoteIslandModel){
delete this->server;
if(this->numberOfClients>1){
for(int i=0; (unsigned)i<this->numberOfClients; i++)
delete this->Clients[i];
delete this->Clients;
}
}
}
void CEvolutionaryAlgorithm::addStoppingCriterion(CStoppingCriterion* sc){
this->stoppingCriteria.push_back(sc);
}
/* MAIN FUNCTION TO RUN THE EVOLUTIONARY LOOP */
void CEvolutionaryAlgorithm::runEvolutionaryLoop(){
CIndividual** elitistPopulation;
......@@ -148,10 +181,15 @@ void CEvolutionaryAlgorithm::runEvolutionaryLoop(){
if(params->elitSize)
elitistPopulation = (CIndividual**)malloc(params->elitSize*sizeof(CIndividual*));
// EVOLUTIONARY LOOP
while( this->allCriteria() == false){
EASEABeginningGenerationFunction(this);
// Sending individuals if remote island model
if(params->remoteIslandModel && this->numberOfClients>0)
this->sendIndividual();
population->produceOffspringPopulation();
if(!INSTEAD_EVAL_STEP)
......@@ -194,6 +232,11 @@ void CEvolutionaryAlgorithm::runEvolutionaryLoop(){
bBest = population->Best;
EASEAEndGenerationFunction(this);
//Receiving individuals if cluster island model
if(params->remoteIslandModel){
this->receiveIndividuals();
}
currentGeneration += 1;
}
#ifdef __linux__
......@@ -208,6 +251,15 @@ void CEvolutionaryAlgorithm::runEvolutionaryLoop(){
std::cout << *population << std::endl;
}
//IF SAVING THE POPULATION, ERASE THE OLD FILE
if(params->savePopulation){
string fichier = params->outputFilename;
fichier.append(".pop");
remove(fichier.c_str());
population->serializePopulation();
}
if(this->params->generateGnuplotScript || !this->params->plotStats)
generateGnuplotScript();
......@@ -297,7 +349,7 @@ void CEvolutionaryAlgorithm::showPopulationStats(struct timeval beginTime){
#ifdef WIN32
fprintf(f,"%lu,%2.6f,%lu,%.15e,%.15e,%.15e\n",currentGeneration,duration,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
#else
fprintf(f,"%d,%ld.%06ld,%d,%f,%f,%f\n",currentGeneration,res.tv_sec,res.tv_usec,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
fprintf(f,"%d,%ld.%06ld,%d,%.15e,%.15e,%.15e\n",currentGeneration,res.tv_sec,res.tv_usec,population->currentEvaluationNb,population->Best->getFitness(),currentAverageFitness,currentSTDEV);
#endif
fclose(f);
}
......@@ -317,6 +369,76 @@ void CEvolutionaryAlgorithm::showPopulationStats(struct timeval beginTime){
params->timeCriterion->setElapsedTime(res.tv_sec);
}
//REMOTE ISLAND MODEL FUNCTIONS
void CEvolutionaryAlgorithm::initializeClients(){
char (*clients)[16] = (char(*)[16])calloc(1,sizeof(char)*16);
// string clients[256];
cout << "Reading IP address file" << endl;
ifstream IP_File("ip.txt");
string line;
while(getline(IP_File, line)){
if(!isLocalMachine(line.c_str())){
memmove(clients[this->numberOfClients],line.c_str(),sizeof(char)*16);
//clients[this->numberOfClients] = line;
this->numberOfClients++;
clients = (char(*)[16])realloc(clients,sizeof(char)*16*(this->numberOfClients*16));
}
}
this->Clients = (CComUDPClient**)malloc(this->numberOfClients*sizeof(CComUDPClient*));
for(int i=0; i<(signed)this->numberOfClients; i++){
//this->Clients[i] = new CComUDPClient(2909,clients[i].c_str(),0);
this->Clients[i] = new CComUDPClient(2909,(const char*)clients[i],0);
// cout << "Client " << i << " IP is " << this->Clients[i]->getIP() << endl;
}
free(clients);
}
void CEvolutionaryAlgorithm::sendIndividual(){
//Sending an individual every n generations
if(this->currentGeneration%10==0){
//cout << "I'm going to send an Individual now" << endl;
this->population->selectionOperator->initialize(this->population->parents, 7, this->population->actualParentPopulationSize);
size_t index = this->population->selectionOperator->selectNext(this->population->actualParentPopulationSize);
//cout << "Going to send individual " << index << " with fitness " << this->population->parents[index]->fitness << endl;
//selecting a client randomly
int client = globalRandomGenerator->getRandomIntMax(this->numberOfClients);
cout << "Going to send and individual to client " << client << endl;
cout << "His IP is " << this->Clients[client]->getIP() << endl;
//cout << "Sending individual " << index << " to client " << client << " nomw" << endl;
//cout << this->population->parents[index]->serialize() << endl;
this->Clients[client]->CComUDP_client_send((char*)this->population->parents[index]->serialize().c_str());
}
}
void CEvolutionaryAlgorithm::receiveIndividuals(){
//Checking every generation for received individuals
if(this->treatedIndividuals<(unsigned)this->server->nb_data){
//cout << "number of received individuals :" << this->server->nb_data << endl;
//cout << "number of treated individuals :" << this->treatedIndividuals << endl;
//Treating all the individuals before continuing
while(this->treatedIndividuals < (unsigned)this->server->nb_data){
//selecting the individual to erase
CSelectionOperator *antiTournament = getSelectionOperator("Tournament",!this->params->minimizing, globalRandomGenerator);
antiTournament->initialize(this->population->parents, 7, this->population->actualParentPopulationSize);
size_t index = antiTournament->selectNext(this->population->actualParentPopulationSize);
//cout << "old individual fitness :" << this->population->parents[index]->fitness << endl;
//cout << "old Individual :" << this->population->parents[index]->serialize() << endl;
this->server->read_data_lock();
string line = this->server->parm->data[this->treatedIndividuals].data;
this->population->parents[index]->deserialize(line);
this->server->read_data_unlock();
//cout << "new Individual :" << this->population->parents[index]->serialize() << endl;
this->treatedIndividuals++;
}
}
}
void CEvolutionaryAlgorithm::outputGraph(){
fprintf(this->gnuplot->fWrit,"set term png\n");
fprintf(this->gnuplot->fWrit,"set output \"%s\"\n",params->plotOutputFilename);
......@@ -361,35 +483,35 @@ void CEvolutionaryAlgorithm::generateRScript(){
bool CEvolutionaryAlgorithm::allCriteria(){
for( size_t i=0 ; i<stoppingCriteria.size(); i++ ){
if( stoppingCriteria.at(i)->reached() ){
std::cout << "Stopping criterion reached : " << i << std::endl;
return true;
}
}
return false;
for( size_t i=0 ; i<stoppingCriteria.size(); i++ ){
if( stoppingCriteria.at(i)->reached() ){
std::cout << "Stopping criterion reached : " << i << std::endl;
return true;
}
}
return false;
}
#ifdef WIN32
int gettimeofday
(struct timeval* tp, void* tzp) {
DWORD t;
t = timeGetTime();
tp->tv_sec = t / 1000;
tp->tv_usec = t % 1000;
/* 0 indicates success. */
return 0;
}
void timersub( const timeval * tvp, const timeval * uvp, timeval* vvp )
{
vvp->tv_sec = tvp->tv_sec - uvp->tv_sec;
vvp->tv_usec = tvp->tv_usec - uvp->tv_usec;
if( vvp->tv_usec < 0 )
{
--vvp->tv_sec;
vvp->tv_usec += 1000000;
}
}
int gettimeofday
(struct timeval* tp, void* tzp) {
DWORD t;
t = timeGetTime();
tp->tv_sec = t / 1000;
tp->tv_usec = t % 1000;
/* 0 indicates success. */
return 0;
}
void timersub( const timeval * tvp, const timeval * uvp, timeval* vvp )
{
vvp->tv_sec = tvp->tv_sec - uvp->tv_sec;
vvp->tv_usec = tvp->tv_usec - uvp->tv_usec;
if( vvp->tv_usec < 0 )
{
--vvp->tv_sec;
vvp->tv_usec += 1000000;
}
}
#endif
......@@ -140,6 +140,7 @@ void parseArguments(const char* parametersFileName, int ac, char** av,
("reduceFinalPressure",po::value<float>(),"Set the Final Reducing Pressure (default : 2.0)")
("optimiseIterations",po::value<int>(),"Set the number of optimisation iterations (default : 100)")
("baldwinism",po::value<int>(),"Only keep fitness (default : 0)")
("remoteIslandModel",po::value<int>(),"Boolean to activate the individual exachange with remote islands (default : 0)")
("outputfile",po::value<string>(),"Set an output file for the final population (default : none)")
("inputfile",po::value<string>(),"Set an input file for the initial population (default : none)")
("printStats",po::value<int>(),"Print the Stats (default : 1)")
......@@ -150,6 +151,8 @@ void parseArguments(const char* parametersFileName, int ac, char** av,
// ("printStatsFile",po::value<int>(),"Print the Stats to a File (Filename: ProjectName.dat) (default : 0)")
("printInitialPopulation",po::value<int>(),"Prints the initial population (default : 0)")
("printFinalPopulation",po::value<int>(),"Prints the final population (default : 0)")
("savePopulation",po::value<int>(),"Saves population at the end (default : 0)")
("startFromFile",po::value<int>(),"Loads the population from a .pop file (default : 0")
("u1",po::value<string>(),"User defined parameter 1")
("u2",po::value<string>(),"User defined parameter 2")
("u3",po::value<int>(),"User defined parameter 3")
......
......@@ -6,11 +6,15 @@
*/
#include "include/CPopulation.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include "include/CRandomGenerator.h"
#include "include/CIndividual.h"
#include "include/Parameters.h"
using namespace std;
CSelectionOperator* CPopulation::selectionOperator;
CSelectionOperator* CPopulation::replacementOperator;
CSelectionOperator* CPopulation::parentReductionOperator;
......@@ -234,6 +238,19 @@ void CPopulation::sortRPopulation(CIndividual** population, size_t populationSiz
qsort(population,populationSize,sizeof(CIndividual*),CIndividualRCompare);
}
/* Fonction qui va serializer la population */
void CPopulation::serializePopulation(){
ofstream EASEA_File;
std::string fichier = params->outputFilename;
fichier.append(".pop");
EASEA_File.open(fichier.c_str(), ios::app);
for(int i=0; (unsigned)i<parentPopulationSize; i++){
EASEA_File << parents[i]->serialize() << endl;
}
EASEA_File.close();
}
/**
Reduit les populations en faisant l'operation de remplacement.
......@@ -381,35 +398,39 @@ void CPopulation::weakElitism(size_t elitismSize, CIndividual** parentsPopulatio
}
for(int i = 0 ; (unsigned)i<elitismSize ; i++ ){
if((!params->minimizing && bestParentFitness > bestOffspringFitness) || (params->minimizing && bestParentFitness<bestOffspringFitness)){
if(((!params->minimizing && bestParentFitness > bestOffspringFitness) || (params->minimizing && bestParentFitness<bestOffspringFitness) || (*offPopSize)==0) && (*parentPopSize)>0){
outPopulation[i] = parentsPopulation[bestParentIndiv];
parentsPopulation[bestParentIndiv] = parentsPopulation[(*parentPopSize)-1];
parentsPopulation[(*parentPopSize)-1] = NULL;
(*parentPopSize)-=1;
bestParentFitness = parentsPopulation[0]->getFitness();
bestParentIndiv=0;
for(int j=1; (unsigned)j<(*parentPopSize); j++){
if( (params->minimizing && bestParentFitness > parentsPopulation[j]->getFitness() ) ||
( !params->minimizing && bestParentFitness < parentsPopulation[j]->getFitness() )){
bestParentFitness = parentsPopulation[j]->getFitness();
bestParentIndiv = j;
}
}
if((*parentPopSize)>0){
bestParentFitness = parentsPopulation[0]->getFitness();
bestParentIndiv=0;
for(int j=1; (unsigned)j<(*parentPopSize); j++){
if( (params->minimizing && bestParentFitness > parentsPopulation[j]->getFitness() ) ||
( !params->minimizing && bestParentFitness < parentsPopulation[j]->getFitness() )){
bestParentFitness = parentsPopulation[j]->getFitness();
bestParentIndiv = j;
}
}
}
}
else{
outPopulation[i] = offspringPopulation[bestOffspringIndiv];
offspringPopulation[bestOffspringIndiv] = offspringPopulation[(*offPopSize)-1];
offspringPopulation[(*offPopSize)-1] = NULL;
(*offPopSize)-=1;
bestOffspringFitness = offspringPopulation[0]->getFitness();
bestOffspringIndiv = 0;
for(int j=1; (unsigned)j<(*offPopSize); j++){
if( (params->minimizing && bestOffspringFitness > offspringPopulation[j]->getFitness() ) ||
( !params->minimizing && bestOffspringFitness < offspringPopulation[j]->getFitness() )){
bestOffspringFitness = offspringPopulation[j]->getFitness();
bestOffspringIndiv = j;
}
}
if((*offPopSize)>0){
bestOffspringFitness = offspringPopulation[0]->getFitness();
bestOffspringIndiv = 0;
for(int j=1; (unsigned)j<(*offPopSize); j++){
if( (params->minimizing && bestOffspringFitness > offspringPopulation[j]->getFitness() ) ||
( !params->minimizing && bestOffspringFitness < offspringPopulation[j]->getFitness() )){
bestOffspringFitness = offspringPopulation[j]->getFitness();
bestOffspringIndiv = j;
}
}
}
}
}
}
......
......@@ -52,6 +52,7 @@ CSelectionOperator* getSelectionOperator(std::string selectop, int minimizing, C
}
}
/* ****************************************
SelectionOperator class
****************************************/
......@@ -62,13 +63,15 @@ void CSelectionOperator::initialize(CIndividual** population, float selectionPre
size_t CSelectionOperator::selectNext(size_t populationSize){ return 0; }
/* ****************************************
MaxDeterministic class
****************************************/
void MaxDeterministic::initialize(CIndividual** population, float selectionPressure,size_t populationSize){
CSelectionOperator::initialize(population,selectionPressure,populationSize);
CPopulation::sortPopulation(population,populationSize);
populationSize = populationSize;
}
size_t MaxDeterministic::selectNext(size_t populationSize){
return populationSize-1;
}
......@@ -77,14 +80,15 @@ float MaxDeterministic::getExtremum(){
return -FLT_MAX;
}
/* ****************************************
MinDeterministic class
****************************************/
void MinDeterministic::initialize(CIndividual** population, float selectionPressure,size_t populationSize){
CSelectionOperator::initialize(population,selectionPressure,populationSize);
CPopulation::sortRPopulation(population,populationSize);
populationSize = populationSize;
}
size_t MinDeterministic::selectNext(size_t populationSize){
return populationSize-1;
}
......@@ -93,6 +97,9 @@ float MinDeterministic::getExtremum(){
return FLT_MAX;
}
/* ****************************************
MaxRandom class
****************************************/
MaxRandom::MaxRandom(CRandomGenerator* globalRandomGenerator){
rg = globalRandomGenerator;
}
......@@ -109,6 +116,9 @@ float MaxRandom::getExtremum(){
return -FLT_MAX;
}
/* ****************************************
MinRandom class
****************************************/
MinRandom::MinRandom(CRandomGenerator* globalRandomGenerator){
rg = globalRandomGenerator;
}
......@@ -126,7 +136,9 @@ float MinRandom::getExtremum(){
}
/* ****************************************
MinTournament class
****************************************/
void MinTournament::initialize(CIndividual** population, float selectionPressure, size_t populationSize) {
CSelectionOperator::initialize(population,selectionPressure,populationSize);
}
......@@ -135,7 +147,6 @@ float MinTournament::getExtremum(){
return FLT_MAX;
}
size_t MinTournament::selectNext(size_t populationSize){
size_t bestIndex = 0;
float bestFitness = FLT_MAX;
......@@ -178,6 +189,10 @@ size_t MinTournament::selectNext(size_t populationSize){
return bestIndex;
}
/* ****************************************
MaxTournament class
****************************************/
void MaxTournament::initialize(CIndividual** population, float selectionPressure, size_t populationSize) {
CSelectionOperator::initialize(population,selectionPressure,populationSize);
}
......@@ -186,10 +201,9 @@ float MaxTournament::getExtremum(){
return -FLT_MAX;
}
size_t MaxTournament::selectNext(size_t populationSize){
size_t bestIndex = 0;
float bestFitness = FLT_MAX;
float bestFitness = -FLT_MAX;
//std::cout << "MinTournament selection " ;
if( currentSelectionPressure >= 2 ){
......
......@@ -8,7 +8,8 @@ endif #OS
OBJS = CRandomGenerator.o CSelectionOperator.o CEvolutionaryAlgorithm.o\
CStoppingCriterion.o COptionParser.o CPopulation.o CIndividual.o\
CGnuplot.o CCmaes.o CCuda.o CCmaesCuda.o Parameters.o CGPNode.o
CGnuplot.o CCmaes.o CCuda.o CCmaesCuda.o Parameters.o CGPNode.o\
CComUDPLayer.o
ifeq ($(UNAME),Darwin)
LIBS =
......
......@@ -14,6 +14,7 @@
#include "CSelectionOperator.h"
#include "CPopulation.h"
#include "CStoppingCriterion.h"
#include "CComUDPLayer.h"
#ifdef WIN32
#include <windows.h>
#endif
......@@ -24,13 +25,13 @@ class CGnuplot;
class CEvolutionaryAlgorithm {
public:
CEvolutionaryAlgorithm( size_t parentPopulationSize,
/* CEvolutionaryAlgorithm( size_t parentPopulationSize,
size_t offspringPopulationSize,
float selectionPressure, float replacementPressure, float parentReductionPressure, float offspringReductionPressure,
CSelectionOperator* selectionOperator, CSelectionOperator* replacementOperator,
CSelectionOperator* parentReductionOperator, CSelectionOperator* offspringReductionOperator,
float pCrossover, float pMutation,
float pMutationPerGene);
float pMutationPerGene);*/
CEvolutionaryAlgorithm( Parameters* params );
virtual void initializeParentPopulation() = 0;
......@@ -47,6 +48,15 @@ public:
size_t reduceParents;
size_t reduceOffsprings;
//methods and variables for remote island model
size_t treatedIndividuals;
size_t numberOfClients;
CComUDPServer *server;
CComUDPClient **Clients;
void initializeClients();
void receiveIndividuals();
void sendIndividual();
#ifdef WIN32
void showPopulationStats(clock_t beginTime);
#else
......
......@@ -26,6 +26,8 @@ public:
virtual CIndividual* crossover(CIndividual** p2) = 0;
virtual CIndividual* clone() = 0;
virtual std::string serialize() = 0;
virtual void deserialize(std::string EASEA_Line) = 0;
static size_t getCrossoverArrity(){ return 2; }
float getFitness(){ return this->fitness; }
......
......@@ -98,6 +98,7 @@ public:
static void sortRPopulation(CIndividual** population, size_t populationSize);
void serializePopulation();
void sortParentPopulation(){ CPopulation::sortPopulation(parents,actualParentPopulationSize);}
......
......@@ -31,6 +31,7 @@ public:
float parentReductionPressure;
float offspringReductionPressure;
//Genetic operators parameters
float pCrossover;
float pMutation;
float pMutationPerGene;
......@@ -47,13 +48,16 @@ public:
bool offspringReduction;
bool parentReduction ;
//Elitism parameters
bool strongElitism;
unsigned int elitSize;
//Parameters for memetic algorithm
bool optimise;
int optimiseIterations;
bool baldwinism;
//Miscalleous parameters
int printStats;
int generateCSVFile;
int generateGnuplotScript;
......@@ -62,6 +66,12 @@ public:
int printInitialPopulation;
int printFinalPopulation;
bool savePopulation;
bool startFromFile;
//Parameters for the Island Model
bool remoteIslandModel;
char* outputFilename;
char* plotOutputFilename;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment