Commit a4156440 authored by kruger's avatar kruger

Premiere mises a jour de Fred comprenant notamment le model en ilot

Il manque encore certain parametres qui seront commité par la suite
parent 8f509b48
/*_________________________________________________________
This file was automatically created by GUIDE v0.1c
User: collet
Date: Tue May 06 10:02:37 CEST 2003
File name: D:\travail\easea\listsort.ez
Target library: EO
Operating System: Windows XP
_________________________________________________________*/
\User declarations:
int SIZE=10;
double pMutPerGene=0.4;
inline void swap(int& a,int& b) {int c=a; a=b; b=c;}
\end
\User functions:
void cross(GenomeClass *pChild, GenomeClass *pParent, int locus){
Element *p, *pChildList, *pParentList;
pChildList=pChild->pList; pParentList=pParent->pList;
for (int i=0;i<SIZE;i++){
if (i>=locus){
for(p=pChild->pList;pParentList->Value!=p->Value;p=p->pNext);
swap(p->Value, pChildList->Value);
}
pChildList=pChildList->pNext;
pParentList=pParentList->pNext;
}
}
\end
\Before everything else function:
if ((argc>1)&&(!strcmp(argv[1],"size"))) SIZE=atoi(argv[2]);
\end
\User classes:
Element { int Value;
Element *pNext; }
GenomeClass { Element *pList;
int Size; }
\end
\After everything else function:
std::cout << (*population) ;
\end
\GenomeClass::initialiser:
Element *pElt;
Genome.Size=0;
Genome.pList=NULL;
for (int i=0;i<SIZE;i++){ // creation of a linked list of SIZE elements
pElt=new Element; // with the decreasing values:
pElt->Value=i+1; // (SIZE, SIZE-1, ... , 3, 2, 1)
pElt->pNext=Genome.pList;
Genome.pList=pElt;
Genome.Size++;
}
\end
\GenomeClass::crossover:
int locus=random(0,SIZE-1);
cross(&child, &parent2, locus);
\end
\GenomeClass::mutator:
int NbMut=0;
Element *p=Genome.pList;
while (p->pNext){
if (tossCoin(pMutPerGene)){ //We swap the current value with the next
swap(p->Value,p->pNext->Value);
NbMut++;
}
p=p->pNext;
}
return NbMut;
\end
\GenomeClass::evaluator:
int i=0,eval=0;
Element *p=Genome.pList;
while(p->pNext){
if (p->Value==++i) eval+=10;
if (p->Value<p->pNext->Value) eval+=4;
else eval-=2;
p=p->pNext;
}
if (p->Value==SIZE) eval+=10;
return (eval<0 ? 0 : eval);
\end
\GenomeClass::display:
os << "Size:" << Genome.Size << "\n";
os << "pList:" << *(Genome.pList) << "\n";
os << "This was MY display function !\n";
\end
\At the beginning of each generation function:
if (NB_GEN-currentGeneration==10) pMutPerGene=0.1;
\end
\At the end of each generation function:
if (NB_GEN-currentGeneration==10) pMutPerGene=0.1;
\end
\User Makefile options:
CPPFLAGS+=
\end
\Default run parameters:
Mutation probability: 1.0
Crossover probability: 0.9
// Evolution Engine:
Evaluator goal: maximize
Number of generations: 5
Population size: 5
Elite: 5
Selection operator: Tournament 2 // ou 0,7 // deterministic // random // roulette
Offspring size: 100%
Reduce offspring operator: Roulette
Surviving offspring: 5
Reduce parents operator: Tournament 2
Surviving parents: 5
Final reduce operator: Tournament 2
Elitism: Weak
\end
#include "include/CComUDPLayer.h"
#include <sys/ioctl.h>
#include <net/if.h>
pthread_mutex_t server_mutex = PTHREAD_MUTEX_INITIALIZER;
/* UDP SERVER*/
CComUDPServer::~CComUDPServer() {
pthread_cancel(thread);
};
void * CComUDPServer::UDP_server_thread(void *parm) {
UDP_server_thread_parm_t *p = (UDP_server_thread_parm_t*)parm;
struct sockaddr_in cliaddr; /* Client address */
socklen_t len = sizeof(cliaddr);
char buffer[MAXINDSIZE];
unsigned int recvMsgSize;
for(;;) {/*forever loop*/
/*receive UDP datagrams from client*/
if ((recvMsgSize = recvfrom(p->Socket,buffer,MAXINDSIZE,0,(struct sockaddr *)&cliaddr,&len)) < 0) {
printf("\nError recvfrom()\n"); exit(1);
}
if(p->debug) {
buffer[recvMsgSize] = 0;
printf("\nData entry[%i]\n",*p->nb_data);
printf("Received the following:\n");
printf("%s\n",buffer);
}
printf("Received packet from %s:%d\n\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
pthread_mutex_lock(&server_mutex);
/*process received data */
memmove(p->data[(*p->nb_data)].data,buffer,sizeof(char)*MAXINDSIZE);
(*p->nb_data)++;
// printf("address %p\n",(p->data));
p->data = (RECV_DATA*)realloc(p->data,sizeof(RECV_DATA)*((*p->nb_data)+1));
// printf("address %p\n",(p->data));
pthread_mutex_unlock(&server_mutex);
/*reset receiving buffer*/
memset(buffer,0,MAXINDSIZE);
}
};
CComUDPServer::CComUDPServer(unsigned short port, int dg) {
struct sockaddr_in ServAddr; /* Local address */
debug = dg;
this->nb_data = 0;
this->data = (RECV_DATA*)calloc(1,sizeof(RECV_DATA));
/* Create socket for incoming connections */
if ((this->ServerSocket = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
printf("Socket create problem.\n"); exit(1);
}
/* Construct local address structure */
memset(&ServAddr, 0, sizeof(ServAddr)); /* Zero out structure */
ServAddr.sin_family = AF_INET; /* Internet address family */
ServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
ServAddr.sin_port = htons(port); /* Local port */
/* Bind to the local address */
if (bind(ServerSocket, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0) {
printf("Can't bind to given port number. Try a different one.\n"); exit(1);
}
//UDP_server_thread_parm_t *parm;
this->parm = (UDP_server_thread_parm_t*)malloc(sizeof(UDP_server_thread_parm_t));
this->parm->Socket = ServerSocket;
this->parm->ServAddr = ServAddr;
this->parm->nb_data = &this->nb_data;
this->parm->data = this->data;
this->parm->debug = this->debug;
if(pthread_create(&thread, NULL, &CComUDPServer::UDP_server_thread, (void *)this->parm) != 0) {
printf("pthread create failed. exiting\n"); exit(1);
}
};
void CComUDPServer::read_data_lock() {
pthread_mutex_lock(&server_mutex);
};
void CComUDPServer::read_data_unlock() {
pthread_mutex_unlock(&server_mutex);
};
/*UDP SERVER*/
/*UDP CLIENT*/
CComUDPClient::~CComUDPClient() {};
CComUDPClient::CComUDPClient(unsigned short port, const char *ip,int dg){
this->debug = dg;
/* Construct local address structure */
memset(&ServAddr, 0, sizeof(ServAddr)); /* Zero out structure */
ServAddr.sin_family = AF_INET; /* Internet address family */
ServAddr.sin_addr.s_addr = inet_addr(ip); /* Any incoming interface */
ServAddr.sin_port = htons(port); /* Local port */
};
void CComUDPClient::CComUDP_client_send(char *individual) {
if ((this->Socket = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
printf("Socket create problem."); exit(1);
}
if(strlen(individual) < MAXINDSIZE ) {
//printf("Sending message...\n");
sendto(this->Socket,individual,MAXINDSIZE,0,(struct sockaddr *)&this->ServAddr,sizeof(this->ServAddr));
}
else {fprintf(stderr,"Not sending individual with strlen(): %i\n",(int)strlen(individual));}
};
std::string CComUDPClient::getIP(){
return inet_ntoa(this->ServAddr.sin_addr);
}
/*UDP CLIENT*/
bool isLocalMachine(const char* address){
char hostname[128];
struct in_addr ipv4addr;
struct hostent *he;
int size;
//retrieve local host name
gethostname(hostname, sizeof(hostname));
//printf("Local host name %s\n",hostname);
//retrieve ip's host name
inet_pton(AF_INET, address, &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
//printf("Host name:%s\n",he->h_name);
if(strlen(hostname)<strlen(he->h_name))
size = strlen(hostname);
else
size = strlen(he->h_name);
if(strncmp(hostname,he->h_name,size)==0)
return true;
else
return false;
}
/* C UDP Communication Layer using Server-Client Modeli
for implementation of distributed evolutionary computing
@author: Pascal Comte, June 2010
*/
#ifndef CCOMUDPLAYER_H_
#define CCOMUDPLAYER_H_
#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() */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <typeinfo>
#include <float.h>
#include <limits.h>
#include <omp.h>
#include <string>
#define _MULTI_THREADED
#define MAXINDSIZE 5000 /*maximum size of an individual in number of characters*/
typedef struct {
char data[MAXINDSIZE];
}RECV_DATA;
typedef struct {
int Socket; /* Socket descriptor for server */
struct sockaddr_in ServAddr; /* Local address */
int debug;
RECV_DATA *data;
int *nb_data;
}UDP_server_thread_parm_t;
class CComUDPServer {
public:
int debug;
RECV_DATA *data;
int nb_data;
UDP_server_thread_parm_t *parm;
CComUDPServer(unsigned short port, int dg);
static void * UDP_server_thread(void *parm);
~CComUDPServer();
void read_data_lock();
void read_data_unlock();
private:
int ServerSocket;
pthread_t thread;
int Socket;
};
class CComUDPClient {
public:
int debug;
void CComUDP_client_send(char *individual);
CComUDPClient(unsigned short port, const char *ip,int dg);
~CComUDPClient();
std::string getIP();
private:
struct sockaddr_in ServAddr;
int Socket;
};
bool isLocalMachine(const char* address);
#endif /* CCOMUDPLAYER_H_ */
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