opti.cc listing

/*
	Code adapted from Dr. Dobbs Journal, April 1997 with modifications
	Article: Differential Evolution by Kenneth Price and Rainer Storn
	Source Code: page 78
*/
#include "opti.h"

Strand& Opti::Optimize(){
 int a, b, c, j;
 const double CR = 0.8, frac = 1.4;
 double rfrac;

 for(int i=0; i<population; i++) strand[i].cost = f(strand[i].gene);

 int numgen = generations;
 while(numgen--){
  for(int i=0; i<population; i++){
   do {a = RandInt(population);} while(a==i);
   do {b = RandInt(population);} while(b==i || b==a);
   do {c = RandInt(population);} while(c==i || c==b || c==a);
   j = RandInt(length);
   rfrac = frac * UniRand();
   for(int k=1; k<=length; k++){
     if(UniRand() < CR || k == length){
      trial.gene[j] = strand[a].gene[j] +
	rfrac * (strand[b].gene[j] - strand[c].gene[j]);
     }
     else trial.gene[j] = strand[a].gene[j];
     j = (j+1)%length;
   }
   trial.cost = f(trial.gene);
   if( Better(i) ) strand[i] = trial;
  }
 }
 return strand[Best()];
}



double MiniOff(double solution[], const int& gen, const int& pop,
	const int& len, double (*func)(double *), const double& offset,
	const double& delta){

 Mini xxx(gen, pop, len, func);
 xxx.Init(offset, delta);
 Strand sol = xxx.Optimize();
 for(int index=0; index<len; index++) solution[index] = sol(index);
 return sol.Cost();
}


double MiniVec(double solution[], const int& gen, const int& pop, 
	const int& len, double (*func)(double *), double v0[], 
	const double& delta){

 Mini xxx(gen, pop, len, func);
 xxx.Init(v0, delta);
 Strand sol = xxx.Optimize();
 for(int index=0; index<len; index++) solution[index] = sol(index);
 return sol.Cost();
}


extern "C" double minidel_(double solution[], const int& gen, const int& pop, 
	const int& len, double (*func)(double *), double v0[], 
	double vdel[]){

 Mini xxx(gen, pop, len, func);
 xxx.Init(v0, vdel);
 Strand sol = xxx.Optimize();
 for(int index=0; index<len; index++) solution[index] = sol(index);
 return sol.Cost();
}


double MaxiVec(double solution[], const int& gen, const int& pop, 
	const int& len, double (*func)(double *), double v0[], 
	const double& delta){

 Maxi xxx(gen, pop, len, func);
 xxx.Init(v0, delta);
 Strand sol = xxx.Optimize();
 for(int index=0; index<len; index++) solution[index] = sol(index);
 return sol.Cost();
}


double MaxiDel(double solution[], const int& gen, const int& pop, 
	const int& len, double (*func)(double *), double v0[], 
	double vdel[]){

 Maxi xxx(gen, pop, len, func);
 xxx.Init(v0, vdel);
 Strand sol = xxx.Optimize();
 for(int index=0; index<len; index++) solution[index] = sol(index);
 return sol.Cost();
}




Mike Forster
Last modified: Wed Apr 2 11:55:28 EST 2003