📄 de36.c
字号:
/***************************************************************
** **
** D I F F E R E N T I A L E V O L U T I O N **
** **
** Program: de.c **
** Version: 3.6 **
** **
** 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. **
** **
***************************************************************/
/**H*O*C**************************************************************
** **
** No.!Version! Date ! Request ! Modification ! Author **
** ---+-------+------+---------+---------------------------+------- **
** 1 + 3.1 +5/18/95+ - + strategy DE/rand-to-best/1+ Storn **
** + + + + included + **
** 1 + 3.2 +6/06/95+C.Fleiner+ change loops into memcpy + Storn **
** 2 + 3.2 +6/06/95+ - + update comments + Storn **
** 1 + 3.3 +6/15/95+ K.Price + strategy DE/best/2 incl. + Storn **
** 2 + 3.3 +6/16/95+ - + comments and beautifying + Storn **
** 3 + 3.3 +7/13/95+ - + upper and lower bound for + Storn **
** + + + + initialization + **
** 1 + 3.4 +2/12/96+ - + increased printout prec. + Storn **
** 1 + 3.5 +5/28/96+ - + strategies revisited + Storn **
** 2 + 3.5 +5/28/96+ - + strategy DE/rand/2 incl. + Storn **
** 1 + 3.6 +8/06/96+ K.Price + Binomial Crossover added + Storn **
** 2 + 3.6 +9/30/96+ K.Price + cost variance output + Storn **
** 3 + 3.6 +9/30/96+ - + alternative to ASSIGND + Storn **
** 4 + 3.6 +10/1/96+ - + variable checking inserted + Storn **
** 5 + 3.6 +10/1/96+ - + strategy indic. improved + Storn **
** **
***H*O*C*E***********************************************************/
# include "de.h"
/*------------------------Macros----------------------------------------*/
/*#define ASSIGND(a,b) memcpy((a),(b),sizeof(double)*D) */ /* quick copy by Claudio */
/* works only for small */
/* arrays, but is faster.*/
/*------------------------Globals---------------------------------------*/
long rnd_uni_init; /* serves as a seed for rnd_uni() */
double c[MAXPOP][MAXDIM], d[MAXPOP][MAXDIM];
double (*pold)[MAXPOP][MAXDIM], (*pnew)[MAXPOP][MAXDIM], (*pswap)[MAXPOP][MAXDIM];
/*---------Function declarations----------------------------------------*/
void assignd(int D, double a[], double b[]);
double rnd_uni(long *idum); /* uniform pseudo random number generator */
double extern evaluate(int D, double tmp[], long *nfeval); /* obj. funct. */
/*---------Function definitions-----------------------------------------*/
void assignd(int D, double a[], double b[])
/**C*F****************************************************************
** **
** Assigns D-dimensional vector b to vector a. **
** You might encounter problems with the macro ASSIGND on some **
** machines. If yes, better use this function although it's slower. **
** **
***C*F*E*************************************************************/
{
int j;
for (j=0; j<D; j++)
{
a[j] = b[j];
}
}
double rnd_uni(long *idum)
/**C*F****************************************************************
** **
** SRC-FUNCTION :rnd_uni() **
** LONG_NAME :random_uniform **
** AUTHOR :(see below) **
** **
** DESCRIPTION :rnd_uni() generates an equally distributed ran- **
** dom number in the interval [0,1]. For further **
** reference see Press, W.H. et alii, Numerical **
** Recipes in C, Cambridge University Press, 1992. **
** **
** FUNCTIONS :none **
** **
** GLOBALS :none **
** **
** PARAMETERS :*idum serves as a seed value **
** **
** PRECONDITIONS :*idum must be negative on the first call. **
** **
** POSTCONDITIONS :*idum will be changed **
** **
***C*F*E*************************************************************/
{
long j;
long k;
static long idum2=123456789;
static long iy=0;
static long iv[NTAB];
double 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[])
/**C*F****************************************************************
** **
** SRC-FUNCTION :main() **
** LONG_NAME :main program **
** AUTHOR :Rainer Storn, Kenneth Price **
** **
** DESCRIPTION :driver program for differential evolution. **
** **
** FUNCTIONS :rnd_uni(), evaluate(), printf(), fprintf(), **
** fopen(), fclose(), fscanf(). **
** **
** GLOBALS :rnd_uni_init input variable for rnd_uni() **
** **
** PARAMETERS :argc #arguments = 3 **
** argv pointer to argument strings **
** **
** PRECONDITIONS :main must be called with three parameters **
** e.g. like de1 <input-file> <output-file>, if **
** the executable file is called de1. **
** The input file must contain valid inputs accor- **
** ding to the fscanf() section of main(). **
** **
** POSTCONDITIONS :main() produces consecutive console outputs and **
** writes the final results in an output file if **
** the program terminates without an error. **
** **
***C*F*E*************************************************************/
{
char chr; /* y/n choice variable */
char *strat[] = /* strategy-indicator */
{
"",
"DE/best/1/exp",
"DE/rand/1/exp",
"DE/rand-to-best/1/exp",
"DE/best/2/exp",
"DE/rand/2/exp",
"DE/best/1/bin",
"DE/rand/1/bin",
"DE/rand-to-best/1/bin",
"DE/best/2/bin",
"DE/rand/2/bin"
};
int i, j, L, n; /* counting variables */
int r1, r2, r3, r4; /* placeholders for random indexes */
int r5; /* 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 */
double trial_cost; /* buffer variable */
double inibound_h; /* upper parameter bound */
double inibound_l; /* lower parameter bound */
double tmp[MAXDIM], best[MAXDIM], bestit[MAXDIM]; /* members */
double cost[MAXPOP]; /* obj. funct. values */
double cvar; /* computes the cost variance */
double cmean; /* mean cost */
double F,CR; /* control variables of DE */
double cmin; /* help variables */
FILE *fpin_ptr;
FILE *fpout_ptr;
/*------Initializations----------------------------*/
if (argc != 3) /* number of arguments */
{
printf("\nUsage : de <input-file> <output-file>\n");
exit(1);
}
fpout_ptr = fopen(argv[2],"r"); /* open output file for reading, */
/* to see whether it already exists */
if ( fpout_ptr != NULL )
{
printf("\nOutput file %s does already exist, \ntype y if you ",argv[2]);
printf("want to overwrite it, \nanything else if you want to exit.\n");
chr = (char)getchar();
if ((chr != 'y') && (chr != 'Y'))
{
exit(1);
}
fclose(fpout_ptr);
}
/*-----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,"%lf",&inibound_h); /*---upper parameter bound for init-----*/
fscanf(fpin_ptr,"%lf",&inibound_l); /*---lower parameter bound for init-----*/
fscanf(fpin_ptr,"%lf",&F); /*---weight factor----------------------*/
fscanf(fpin_ptr,"%lf",&CR); /*---crossing over factor---------------*/
fscanf(fpin_ptr,"%d",&seed); /*---random seed------------------------*/
fclose(fpin_ptr);
/*-----Checking input variables for proper range----------------------------*/
if (D > MAXDIM)
{
printf("\nError! D=%d > MAXDIM=%d\n",D,MAXDIM);
exit(1);
}
if (D <= 0)
{
printf("\nError! D=%d, should be > 0\n",D);
exit(1);
}
if (NP > MAXPOP)
{
printf("\nError! NP=%d > MAXPOP=%d\n",NP,MAXPOP);
exit(1);
}
if (NP <= 0)
{
printf("\nError! NP=%d, should be > 0\n",NP);
exit(1);
}
if ((CR < 0) || (CR > 1.0))
{
printf("\nError! CR=%f, should be ex [0,1]\n",CR);
exit(1);
}
if (seed <= 0)
{
printf("\nError! seed=%d, should be > 0\n",seed);
exit(1);
}
if (refresh <= 0)
{
printf("\nError! refresh=%d, should be > 0\n",refresh);
exit(1);
}
if (genmax <= 0)
{
printf("\nError! genmax=%d, should be > 0\n",genmax);
exit(1);
}
if ((strategy < 0) || (strategy > 10))
{
printf("\nError! strategy=%d, should be ex {1,2,3,4,5,6,7,8,9,10}\n",strategy);
exit(1);
}
if (inibound_h < inibound_l)
{
printf("\nError! inibound_h=%f < inibound_l=%f\n",inibound_h, inibound_l);
exit(1);
}
/*-----Open output file-----------------------------------------------*/
fpout_ptr = fopen(argv[2],"w"); /* open output file for writing */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -