From a41564404aa270ddf55c57eef1098b49e31cbb01 Mon Sep 17 00:00:00 2001 From: kruger Date: Fri, 17 Sep 2010 16:17:48 +0200 Subject: [PATCH] =?UTF-8?q?Premiere=20mises=20a=20jour=20de=20Fred=20compr?= =?UTF-8?q?enant=20notamment=20le=20model=20en=20ilot=20Il=20manque=20enco?= =?UTF-8?q?re=20certain=20parametres=20qui=20seront=20commit=C3=A9=20par?= =?UTF-8?q?=20la=20suite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/listsort/listsort.ez | 130 +++++++++++++++++++++++++++++ libeasea/CComUDPLayer.cpp | 139 ++++++++++++++++++++++++++++++++ libeasea/include/CComUDPLayer.h | 72 +++++++++++++++++ 3 files changed, 341 insertions(+) create mode 100644 examples/listsort/listsort.ez create mode 100644 libeasea/CComUDPLayer.cpp create mode 100755 libeasea/include/CComUDPLayer.h diff --git a/examples/listsort/listsort.ez b/examples/listsort/listsort.ez new file mode 100644 index 0000000..d2958b7 --- /dev/null +++ b/examples/listsort/listsort.ez @@ -0,0 +1,130 @@ +/*_________________________________________________________ + +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=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;iValue=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->ValuepNext->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 diff --git a/libeasea/CComUDPLayer.cpp b/libeasea/CComUDPLayer.cpp new file mode 100644 index 0000000..3bcceac --- /dev/null +++ b/libeasea/CComUDPLayer.cpp @@ -0,0 +1,139 @@ +#include "include/CComUDPLayer.h" +#include +#include + +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)h_name)) + size = strlen(hostname); + else + size = strlen(he->h_name); + + if(strncmp(hostname,he->h_name,size)==0) + return true; + else + return false; + +} diff --git a/libeasea/include/CComUDPLayer.h b/libeasea/include/CComUDPLayer.h new file mode 100755 index 0000000..1ea3795 --- /dev/null +++ b/libeasea/include/CComUDPLayer.h @@ -0,0 +1,72 @@ +/* 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 /* for socket(), bind(), and connect() */ +#include /* for gethostbyname */ +#include /* for sockaddr_in and inet_ntoa() */ +#include /* for IP Socket data types */ +#include /* for memset() */ +#include /* for close() */ +#include +#include +#include +#include +#include +#include +#include +#include + +#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_ */ -- GitLab