/*************************************************************** ** ** ** D I F F E R E N T I A L E V O L U T I O N ** ** ** ** Program: de.c ** ** ** ** Authors: Dr. Rainer Storn ** ** c/o ICSI, 1947 Center Street, Suite 600 ** ** Berkeley, CA 94707 ** ** Tel.: 510-642-4274 (extension 192) ** ** Fax.: 510-643-7684 ** ** E-mail: storn@icsi.berkeley.edu ** ** WWW: http://http.icsi.berkeley.edu/~storn/ ** ** on leave from ** ** Siemens AG, ZFE T SN 2, Otto-Hahn Ring 6 ** ** D-81739 Muenchen, Germany ** ** Tel: 636-40502 ** ** Fax: 636-44577 ** ** E-mail: rainer.storn@zfe.siemens.de ** ** ** ** Kenneth Price ** ** 836 Owl Circle ** ** Vacaville, CA 95687 ** ** E-mail: kprice@solano.community.net ** ** ** ** This program implements some variants of Differential ** ** Evolution (DE) as described in part in the techreport ** ** tr-95-012.ps of ICSI. You can get this report either via ** ** ftp.icsi.berkeley.edu/pub/techreports/1995/tr-95-012.ps.Z ** ** or via WWW: http://http.icsi.berkeley.edu/~storn/litera.html* ** A more extended version of tr-95-012.ps is submitted for ** ** publication in the Journal Evolutionary Computation. ** ** ** ** You may use this program for any purpose, give it to any ** ** person or change it according to your needs as long as you ** ** are referring to Rainer Storn and Ken Price as the origi- ** ** nators of the the DE idea. ** ** If you have questions concerning DE feel free to contact ** ** us. We also will be happy to know about your experiences ** ** with DE and your suggestions of improvement. ** ** ** ***************************************************************/ #include "stdio.h" #include "stdlib.h" #include "math.h" #define MAXPOP 500 #define MAXDIM 35 /*------Constants for rnd_uni()--------------------------------------------*/ #define IM1 2147483563 #define IM2 2147483399 #define AM (1.0/IM1) #define IMM1 (IM1-1) #define IA1 40014 #define IA2 40692 #define IQ1 53668 #define IQ2 52774 #define IR1 12211 #define IR2 3791 #define NTAB 32 #define NDIV (1+IMM1/NTAB) #define EPS 1.2e-7 #define RNMX (1.0-EPS) /* # include "conio.h" */ /* conio.h is not available on all platforms */ /*------------------------Globals---------------------------------------*/ long rnd_uni_init; /* serves as a seed for rnd_uni() */ /*---------Function devlarations----------------------------------------*/ float rnd_uni(long *idum); /* uniform pseudo random number generator */ float extern evaluate(int D, float tmp[], long *nfeval); /* obj. funct. */ /*---------Function definitions-----------------------------------------*/ float rnd_uni(long *idum) { long j; long k; static long idum2=123456789; static long iy=0; static long iv[NTAB]; float temp; if (*idum <= 0) { if (-(*idum) < 1) *idum=1; else *idum = -(*idum); idum2=(*idum); for (j=NTAB+7;j>=0;j--) { k=(*idum)/IQ1; *idum=IA1*(*idum-k*IQ1)-k*IR1; if (*idum < 0) *idum += IM1; if (j < NTAB) iv[j] = *idum; } iy=iv[0]; } k=(*idum)/IQ1; *idum=IA1*(*idum-k*IQ1)-k*IR1; if (*idum < 0) *idum += IM1; k=idum2/IQ2; idum2=IA2*(idum2-k*IQ2)-k*IR2; if (idum2 < 0) idum2 += IM2; j=iy/NDIV; iy=iv[j]-idum2; iv[j] = *idum; if (iy < 1) iy += IMM1; if ((temp=AM*iy) > RNMX) return RNMX; else return temp; }/*------End of rnd_uni()--------------------------*/ main(int argc, char *argv[]) { char chr; /* y/n choice variable */ int i, j, k; /* counting variables */ int r1, r2, r3; /* placeholders for random indexes */ int D; /* Dimension of parameter vector */ int NP; /* number of population members */ int imin; /* index to member with lowest energy */ int refresh; /* refresh rate of screen output */ int strategy; /* choice parameter for screen output */ int gen, genmax, seed; long nfeval; /* number of function evaluations */ float trial_energy; /* buffer variable */ float initfac; /* initializier of population */ float c[MAXPOP][MAXDIM], d[MAXPOP][MAXDIM]; /* populations */ float tmp[MAXDIM], best[MAXDIM], bestit[MAXDIM]; /* members */ float energy[MAXPOP]; /* obj. funct. values */ float F,CR; /* control variables of DE */ float emin; /* help variables */ float (*pold)[MAXPOP][MAXDIM], (*pnew)[MAXPOP][MAXDIM], (*pswap)[MAXPOP][MAXDIM]; /* pointers to enable switching population arrays */ FILE *fpin_ptr; FILE *fpout_ptr; /*------Initializations----------------------------*/ if (argc != 3) /* number of arguments */ { printf("\nUsage : de \n"); exit(1); } /*-----Read input data------------------------------------------------*/ fpin_ptr = fopen(argv[1],"r"); if (fpin_ptr == NULL) { printf("\nCannot open input file\n"); exit(1); /* input file is necessary */ } fscanf(fpin_ptr,"%d",&strategy); /*---choice of strategy-----------------*/ fscanf(fpin_ptr,"%d",&genmax); /*---maximum number of generations------*/ fscanf(fpin_ptr,"%d",&refresh); /*---output refresh cycle---------------*/ fscanf(fpin_ptr,"%d",&D); /*---number of parameters---------------*/ fscanf(fpin_ptr,"%d",&NP); /*---population size.-------------------*/ fscanf(fpin_ptr,"%f",&initfac); /*---factor for initialization----------*/ fscanf(fpin_ptr,"%f",&F); /*---weight factor----------------------*/ fscanf(fpin_ptr,"%f",&CR); /*---crossing over factor---------------*/ fscanf(fpin_ptr,"%d",&seed); /*---random seed------------------------*/ fclose(fpin_ptr); /*-----Open output file-----------------------------------------------*/ fpout_ptr = fopen(argv[2],"w"); if (fpout_ptr == NULL) { printf("\nCannot open output file\n"); exit(1); } /*-----Initialize random number generator-----------------------------*/ rnd_uni_init = -(long)seed; /* initialization of rnd_uni() */ nfeval = 0; /* reset number of function evaluations */ /*------Initialization-----------------------------*/ for (i=0; i1) result+=(1-px)*(1-px); x+=dx; } px = tmp[0]; for (j=1;j