Commit 516b76b8 authored by Frédéric Krüger's avatar Frédéric Krüger

Mise a jour de la libeasea

parent 7fe05bf9
This diff is collapsed.
This diff is collapsed.
......@@ -204,74 +204,7 @@ GPNode* RAMPED_H_H(unsigned INIT_TREE_DEPTH_MIN, unsigned INIT_TREE_DEPTH_MAX, u
return construction_method( VAR_LEN+1, OPCODE_SIZE , 1, currentDepth ,full, opArity, OP_ERC);
}
/**
* Return the root of the nth node in a tree rooted at root.
*
* @arg root: the root of the tree.
* @arg N: the node number to return.
* @arg childId: id of the child corresponding to the selected node.
* @arg tree_depth_max: the maximum possible depth in trees.
* @arg max_arity: the size of the arity array.
*
* @return: a pointer to the parent of the nth node.
*/
GPNode* pickNthNode(GPNode* root, int N, int* childId, unsigned tree_depth_max, unsigned max_arity){
GPNode** stack = new GPNode*[tree_depth_max*max_arity];
GPNode** parentStack = new GPNode*[tree_depth_max*max_arity];
int stackPointer = 0;
parentStack[stackPointer] = NULL;
stack[stackPointer++] = root;
for( int i=0 ; i<N ; i++ ){
GPNode* currentNode = stack[stackPointer-1];
//cout << currentNode << endl;
stackPointer--;
for( int j=opArity[(int)currentNode->opCode] ; j>0 ; j--){
parentStack[stackPointer] = currentNode;
stack[stackPointer++] = currentNode->children[j-1];
}
}
if( stackPointer )
stackPointer--;
for( int i=0 ; i<(int)opArity[(int)parentStack[stackPointer]->opCode] ; i++ ){
if( parentStack[stackPointer]->children[i]==stack[stackPointer] ){
(*childId)=i;
break;
}
}
GPNode* ret = parentStack[stackPointer];
delete[] stack;
delete[] parentStack;
return ret;
}
/**
* Flatten a tree inside a buffer using RPN notation.
*
* @arg root: the root of the tree to flatten.
* @arg buf: the buffer where to flatten the tree.
* @arg index: the filling counter of the buffer. Is is modified by the function in order to
* reflect the new size after flattening the current individual.
* @arg max_prog_size: the size of the buffer.
* @arg op_erc_id: the id of the ERC opcode.
*
* @return: nothing important.
*/
int flattening_tree_rpn( GPNode* root, float* buf, int* index,int max_prog_size, int op_erc_id){
int i;
for( i=0 ; i<opArity[(int)root->opCode] ; i++ ){
flattening_tree_rpn(root->children[i],buf,index,max_prog_size,op_erc_id);
}
if( (*index)+2>max_prog_size )return 0;
buf[(*index)++] = root->opCode;
if( root->opCode == op_erc_id ) buf[(*index)++] = root->erc_value;
return 1;
}
#include "include/CGnuplot.h"
#include "include/Parameters.h"
#include <stdio.h>
#include <string.h>
CGnuplot::CGnuplot(int nbEval){
#ifdef __linux__
CGnuplot::CGnuplot(Parameters* param, char* title){
#ifndef WIN32
int toFils[2];
int toPere[2];
int sonPid;
......@@ -35,11 +37,21 @@ CGnuplot::CGnuplot(int nbEval){
this->valid=0;
abort();
}
char *arg[2];
arg[0] = (char*)"-persist";
arg[1] = 0;
if(execvp("gnuplot",arg)<0){
perror("gnuplot not installed, please change plotStats parameter\n");
char* pPath;
pPath = getenv("EZ_PATH");
if(pPath != NULL){
pPath = strcat(pPath, "EaseaGrapher.jar");
}
else{
pPath = (char*)"../../EaseaGrapher.jar";
}
char *arg[4];
arg[0] = (char*)"java";
arg[1] = (char*)"-jar";
arg[2] = pPath;
arg[3] = (char*)0;
if(execvp("java",arg)<0){
perror("java not installed, please change plotStats parameter\n");
abort();
this->valid=0;
}
......@@ -49,11 +61,18 @@ CGnuplot::CGnuplot(int nbEval){
this->fWrit = (FILE *)fdopen(toFils[1],"w");
this->fRead = (FILE *)fdopen(toPere[0],"r");
this->pid = sonPid;
fprintf(this->fWrit,"set term wxt persist\n");
/*fprintf(this->fWrit,"set term wxt persist\n");
fprintf(this->fWrit,"set grid\n");
fprintf(this->fWrit,"set xrange[0:%d]\n",nbEval);
fprintf(this->fWrit,"set xlabel \"Number of Evaluations\"\n");
fprintf(this->fWrit,"set ylabel \"Fitness\"\n");
fprintf(this->fWrit,"set ylabel \"Fitness\"\n");*/
int nbEval = param->offspringPopulationSize*param->nbGen + param->parentPopulationSize;
fprintf(this->fWrit,"set max eval:%d\n",nbEval);
fprintf(this->fWrit,"set title:%s\n",title);
if(param->remoteIslandModel){
fprintf(this->fWrit,"set island model\n");
fprintf(this->fWrit,"set max generation:%d\n",param->nbGen);
}
fflush(this->fWrit);
}
}
......@@ -61,8 +80,8 @@ CGnuplot::CGnuplot(int nbEval){
}
CGnuplot::~CGnuplot(){
#ifdef __linux__
fprintf(this->fWrit,"quit\n");
#ifndef WIN32
//fprintf(this->fWrit,"quit\n");
fclose(this->fRead);
fclose(this->fWrit);
#endif
......
......@@ -143,6 +143,7 @@ void parseArguments(const char* parametersFileName, int ac, char** av,
("remoteIslandModel",po::value<int>(),"Boolean to activate the individual exachange with remote islands (default : 0)")
("ipFile",po::value<string>(),"File containing all the IPs of the remote islands)")
("migrationProbability", po::value<float>(),"Probability to send an individual each generation")
("serverPort", po::value<int>(),"Port of the Server")
("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)")
......@@ -155,7 +156,6 @@ void parseArguments(const char* parametersFileName, int ac, char** av,
("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")
("fcSize",po::value<int>(),"Number of learning point for genetic programming (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")
......
......@@ -12,6 +12,7 @@
#include "include/CRandomGenerator.h"
#include "include/CIndividual.h"
#include "include/Parameters.h"
#include "include/CStats.h"
using namespace std;
......@@ -37,7 +38,7 @@ CPopulation::CPopulation(){
CPopulation::CPopulation(unsigned parentPopulationSize, unsigned offspringPopulationSize,
float pCrossover, float pMutation, float pMutationPerGene,
CRandomGenerator* rg, Parameters* params){
CRandomGenerator* rg, Parameters* params, CStats* cstats){
this->parents = new CIndividual*[parentPopulationSize];
this->offsprings = new CIndividual*[offspringPopulationSize];
......@@ -60,6 +61,7 @@ CPopulation::CPopulation(unsigned parentPopulationSize, unsigned offspringPopula
this->currentEvaluationNb = 0;
this->params = params;
this->cstats = cstats;
}
void CPopulation::syncInVector(){
......@@ -323,10 +325,18 @@ void CPopulation::produceOffspringPopulation(){
unsigned index = selectionOperator->selectNext(parentPopulationSize);
p1 = parents[index];
//Check if Any Immigrants will reproduce
if( this->params->remoteIslandModel && parents[index]->isImmigrant ){
this->cstats->currentNumberOfImmigrantReproductions++;
}
if( rg->tossCoin(pCrossover) ){
for( unsigned j=0 ; j<crossoverArrity-1 ; j++ ){
index = selectionOperator->selectNext(parentPopulationSize);
ps[j] = parents[index];
index = selectionOperator->selectNext(parentPopulationSize);
ps[j] = parents[index];
if( this->params->remoteIslandModel && parents[index]->isImmigrant ){
this->cstats->currentNumberOfImmigrantReproductions++;
}
}
child = p1->crossover(ps);
}
......
......@@ -172,16 +172,12 @@ size_t MinTournament::selectNext(size_t populationSize){
if( rg->tossCoin(currentSelectionPressure) ){
if( population[i1]->getFitness() < population[i2]->getFitness() ){
bestIndex = i1;
} else {
bestIndex = i2;
}
}
}
else{
if( population[i1]->getFitness() < population[i2]->getFitness() ){
bestIndex = i2;
} else {
bestIndex = i1;
}
}
}
}
else{
......@@ -230,16 +226,12 @@ size_t MaxTournament::selectNext(size_t populationSize){
if( rg->tossCoin(currentSelectionPressure) ){
if( population[i1]->getFitness() > population[i2]->getFitness() ){
bestIndex = i1;
} else {
bestIndex = i2;
}
}
}
else{
if( population[i1]->getFitness() > population[i2]->getFitness() ){
bestIndex = i2;
} else {
bestIndex = i1;
}
}
}
}
else{
......
#include "include/CStats.h"
CStats::CStats(){
this->totalNumberOfImmigrants=0;
this->currentNumberOfImmigrants=0;
this->totalNumberOfImmigrantReproductions=0;
this->currentNumberOfImmigrantReproductions=0;
this->currentAverageFitness=0.0;
this->currentStdDev=0.0;
}
CStats::~CStats(){
}
void CStats::resetCurrentStats(){
this->totalNumberOfImmigrants += this->currentNumberOfImmigrants;
this->totalNumberOfImmigrantReproductions += this->currentNumberOfImmigrantReproductions;
this->currentNumberOfImmigrants=0;
this->currentNumberOfImmigrantReproductions=0;
this->currentAverageFitness=0.0;
this->currentStdDev=0.0;
}
UNAME := $(shell uname)
UNAME = $(shell uname)
ifeq ($(shell uname -o 2>/dev/null),Msys)
OS := MINGW
endif
CXXFLAGS = -g -Wall -fmessage-length=0 -I../boost/
CXXFLAGS = -g -Wall -fmessage-length=0 #-I../boost/
OBJS = CRandomGenerator.o CSelectionOperator.o CEvolutionaryAlgorithm.o\
CStoppingCriterion.o COptionParser.o CPopulation.o CIndividual.o\
CGnuplot.o CCmaes.o CCmaesCuda.o Parameters.o CGPNode.o\
CComUDPLayer.o CCuda.o
CComUDPLayer.o CCuda.o CStats.o
ifneq ("$(OS)","")
OBJS += inet_pton.o
endif
ifeq ($(EZ_CUDA),1)
OBJS += CCuda.o
endif
#ifeq ($(UNAME),Darwin)
LIBS =
#else ifeq ($(UNAME),Linux)
#LIBS = -lboost_program_options
#endif #OS
ifneq ("$(OS)","")
LIBS = -L"C:\MinGW\lib" -lws2_32 -lwinmm
endif
TARGET = libeasea.a
CPPFLAGS=-I/usr/local/cuda/include/ -I../boost/ -pg
TARGET = libeasea.a
ifneq ("$(OS)","")
CPPFLAGS=-I..\boost\ #-pg
else
CPPFLAGS=-I/usr/local/cuda/include/ -I../boost/ #-pg
endif
$(TARGET): $(OBJS)
ar rcs $@ $^ #../boost/program_options.a
ar rcs $@ $^
%.o:%.cpp
$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) $^
$(CXX) $(LIBS) -c $(CXXFLAGS) $(CPPFLAGS) $^
all: $(TARGET)
clean:
ifneq ("$(OS)","")
-del $(OBJS) $(TARGET) CCuda.o
else
rm -f $(OBJS) $(TARGET) CCuda.o
endif
......@@ -6,12 +6,16 @@
#ifndef CCOMUDPLAYER_H_
#define CCOMUDPLAYER_H_
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h> /* for socket(), bind(), and connect() */
#include <netdb.h> /* for gethostbyname */
#include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */
#include <netinet/in.h> /* for IP Socket data types */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
......@@ -60,13 +64,18 @@ public:
int debug;
void CComUDP_client_send(char *individual);
CComUDPClient(unsigned short port, const char *ip,int dg);
CComUDPClient(struct sockaddr_in* addr, int dg);
~CComUDPClient();
std::string getIP();
int getPort();
private:
struct sockaddr_in ServAddr;
int Socket;
};
bool isLocalMachine(const char* address);
bool checkValidLine(std::string line);
struct sockaddr_in parse_addr_string(const char* line);
CComUDPClient** parse_file(const char* file_name, unsigned* p_no_client, int serverPort);
#endif /* CCOMUDPLAYER_H_ */
......@@ -15,6 +15,7 @@
#include "CPopulation.h"
#include "CStoppingCriterion.h"
#include "CComUDPLayer.h"
#include "CStats.h"
#ifdef WIN32
#include <windows.h>
#endif
......@@ -57,6 +58,7 @@ public:
void initializeClients();
void receiveIndividuals();
void sendIndividual();
void refreshClient();
#ifdef WIN32
void showPopulationStats(clock_t beginTime);
......@@ -70,8 +72,8 @@ public:
Parameters* params;
CGnuplot* gnuplot;
double currentAverageFitness;
double currentSTDEV;
CStats* cstats;
virtual ~CEvolutionaryAlgorithm();
......
......@@ -82,6 +82,5 @@ GPNode* RAMPED_H_H(unsigned iINIT_TREE_DEPTH_MIN, unsigned iINIT_TREE_DEPTH_MAX,
void flattenDatas2D( float** inputs, int length, int width, float** flat_inputs);
GPNode* construction_method( const int constLen, const int totalLen , const int currentDepth, const int maxDepth, const bool full, const unsigned* opArity, const int OP_ERC);
GPNode* pickNthNode(GPNode* root, int N, int* childId, unsigned tree_depth_max,unsigned max_arity);
int flattening_tree_rpn( GPNode* root, float* buf, int* index,int max_prog_size, int op_erc_id);
#endif // __C_GPNODE__
......@@ -11,6 +11,7 @@
class CRandomGenerator;
#include <iostream>
#include <stdlib.h>
#include "Parameters.h"
class CGnuplot {
public:
......@@ -19,7 +20,7 @@ public:
int pid;
int valid;
public:
CGnuplot(int nbEval);
CGnuplot(Parameters* param, char* title);
~CGnuplot();
};
......
......@@ -14,6 +14,7 @@ class CRandomGenerator;
class CIndividual {
public:
bool valid;
bool isImmigrant;
float fitness;
static CRandomGenerator* rg;
public:
......
......@@ -23,6 +23,7 @@
#include "CSelectionOperator.h"
class Parameters;
class CStats;
class CPopulation {
......@@ -56,11 +57,12 @@ public:
std::vector<CIndividual*> pop_vect;
Parameters* params;
CStats* cstats;
public:
CPopulation();
CPopulation(unsigned parentPopulationSize, unsigned offspringPopulationSize,
float pCrossover, float pMutation, float pMutationPerGene, CRandomGenerator* rg, Parameters* params);
float pCrossover, float pMutation, float pMutationPerGene, CRandomGenerator* rg, Parameters* params, CStats* cstats);
virtual ~CPopulation();
//virtual void initializeParentPopulation() = 0;
......
#ifndef CSTATS_H_
#define CSTATS_H_
class CStats{
public:
int totalNumberOfImmigrants;
int currentNumberOfImmigrants;
int totalNumberOfImmigrantReproductions;
int currentNumberOfImmigrantReproductions;
double currentAverageFitness;
double currentStdDev;
public:
CStats();
~CStats();
void resetCurrentStats();
};
#endif /* CSTATS_H_ */
......@@ -73,6 +73,7 @@ public:
bool remoteIslandModel;
char* ipFile;
float migrationProbability;
int serverPort;
char* outputFilename;
char* plotOutputFilename;
......@@ -81,8 +82,9 @@ public:
#ifdef WIN32
Parameters();
~Parameters();
#endif
#else
virtual ~Parameters(){;}
#endif
virtual void setDefaultParameters(int argc, char** argv) = 0;
virtual CEvolutionaryAlgorithm* newEvolutionaryAlgorithm() = 0;
int setReductionSizes(int popSize, float popReducSize);
......
/* Convert internet address from text to binary format.
Copyright (C) 2005, 2006 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <sys/types.h>
#include <winsock2.h>
#include <windows.h>
#if !HAVE_DECL_INET_PTON
extern int inet_pton (int af, const char *src, void *dst);
#endif
/* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form
Copyright (C) 2006 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/*
* Copyright (c) 1996,1999 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
//#include <config.h>
/* Specification. */
#include "include/inet_pton.h"
#include <ctype.h>
#include <string.h>
#include <errno.h>
#ifndef EAFNOSUPPORT
# define EAFNOSUPPORT EINVAL
#endif
#define NS_INADDRSZ 4
#define NS_IN6ADDRSZ 16
#define NS_INT16SZ 2
/*
* WARNING: Don't even consider trying to compile this on a system where
* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
*/
static int inet_pton4 (const char *src, unsigned char *dst);
#if HAVE_IPV6
static int inet_pton6 (const char *src, unsigned char *dst);
#endif
/* int
* inet_pton(af, src, dst)
* convert from presentation format (which usually means ASCII printable)
* to network format (which is usually some kind of binary format).
* return:
* 1 if the address was valid for the specified address family
* 0 if the address wasn't valid (`dst' is untouched in this case)
* -1 if some other error occurred (`dst' is untouched in this case, too)
* author:
* Paul Vixie, 1996.
*/
int inet_pton (int af, const char *src, void *dst)
{
switch (af)
{
case AF_INET:
return (inet_pton4 (src, (unsigned char*)dst));
#if HAVE_IPV6
case AF_INET6:
return (inet_pton6 (src, dst));
#endif
default:
errno = EAFNOSUPPORT;
return (-1);
}
/* NOTREACHED */
}
/* int
* inet_pton4(src, dst)
* like inet_aton() but without all the hexadecimal, octal (with the
* exception of 0) and shorthand.
* return:
* 1 if `src' is a valid dotted quad, else 0.
* notice:
* does not touch `dst' unless it's returning 1.
* author:
* Paul Vixie, 1996.
*/
static int
inet_pton4 (const char *src, unsigned char *dst)
{
int saw_digit, octets, ch;
unsigned char tmp[NS_INADDRSZ], *tp;
saw_digit = 0;
octets = 0;
*(tp = tmp) = 0;
while ((ch = *src++) != '\0')
{
if (ch >= '0' && ch <= '9')
{
unsigned new1 = *tp * 10 + (ch - '0');
if (saw_digit && *tp == 0)
return (0);
if (new1 > 255)
return (0);
*tp = new1;
if (!saw_digit)
{
if (++octets > 4)
return (0);
saw_digit = 1;
}
}
else if (ch == '.' && saw_digit)
{
if (octets == 4)
return (0);
*++tp = 0;
saw_digit = 0;
}
else
return (0);
}
if (octets < 4)
return (0);
memcpy (dst, tmp, NS_INADDRSZ);
return (1);
}
#if HAVE_IPV6
/* int
* inet_pton6(src, dst)
* convert presentation level address to network order binary form.
* return:
* 1 if `src' is a valid [RFC1884 2.2] address, else 0.
* notice:
* (1) does not touch `dst' unless it's returning 1.
* (2) :: in a full address is silently ignored.
* credit:
* inspired by Mark Andrews.
* author:
* Paul Vixie, 1996.
*/
static int
inet_pton6 (const char *restrict src, unsigned char *restrict dst)
{
static const char xdigits[] = "0123456789abcdef";
unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
const char *curtok;
int ch, saw_xdigit;
unsigned val;
tp = memset (tmp, '\0', NS_IN6ADDRSZ);
endp = tp + NS_IN6ADDRSZ;
colonp = NULL;
/* Leading :: requires some special handling. */
if (*src == ':')
if (*++src != ':')
return (0);
curtok = src;
saw_xdigit = 0;
val = 0;
while ((ch = tolower (*src++)) != '\0')
{
const char *pch;
pch = strchr (xdigits, ch);
if (pch != NULL)
{
val <<= 4;
val |= (pch - xdigits);
if (val > 0xffff)
return (0);
saw_xdigit = 1;
continue;
}
if (ch == ':')
{
curtok = src;
if (!saw_xdigit)
{
if (colonp)
return (0);
colonp = tp;
continue;
}
else if (*src == '\0')
{
return (0);
}
if (tp + NS_INT16SZ > endp)
return (0);
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
saw_xdigit = 0;
val = 0;
continue;
}
if (ch == '.'