📄 sga.c
字号:
rndx2 = sin(t);
rndcalcflag = 0;
return(rndx1 * cos(t));
}
else
{
rndcalcflag = 1;
return(rndx2 * rndx1);
}
}
advance_random()
/* Create next batch of 55 random numbers */
{
int j1;
double new_random;
for(j1 = 0; j1 < 24; j1++)
{
new_random = oldrand[j1] - oldrand[j1+31];
if(new_random < 0.0) new_random = new_random + 1.0;
oldrand[j1] = new_random;
}
for(j1 = 24; j1 < 55; j1++)
{
new_random = oldrand [j1] - oldrand [j1-24];
if(new_random < 0.0) new_random = new_random + 1.0;
oldrand[j1] = new_random;
}
}
int flip(prob)
/* Flip a biased coin - true if heads */
float prob;
{
float randomperc();
if(randomperc() <= prob)
return(1);
else
return(0);
}
randomize()
/* Get seed number for random and start it up */
{
int j1;
for(j1=0; j1<=54; j1++) oldrand[j1] = 0.0;
jrand=0;
warmup_random(seed);
initrandomnormaldeviate();
}
float randomperc()
/* Fetch a single random number between 0.0 and 1.0 - */
/* Subtractive Method . See Knuth, D. (1969), v. 2 for */
/* details.Name changed from random() to avoid library */
/* conflicts on some machines */
{
jrand++;
if(jrand >= 55)
{
jrand = 1;
advance_random();
}
return((float) oldrand[jrand]);
}
int rnd(low, high)
/* Pick a random integer between low and high */
int low,high;
{
int i;
float randomperc();
if(low >= high)
i = low;
else
{
i = (randomperc() * (high - low + 1)) + low;
if(i > high) i = high;
}
return(i);
}
float rndreal(lo ,hi)
/* real random number between specified limits */
float lo, hi;
{
return((randomperc() * (hi - lo)) + lo);
}
warmup_random(random_seed)
/* Get random off and running */
float random_seed;
{
int j1, ii;
double new_random, prev_random;
oldrand[54] = random_seed;
new_random = 0.000000001;
prev_random = random_seed;
for(j1 = 1 ; j1 <= 54; j1++)
{
ii = (21*j1)%54;
oldrand[ii] = new_random;
new_random = prev_random-new_random;
if(new_random<0.0) new_random = new_random + 1.0;
prev_random = oldrand[ii];
}
advance_random();
advance_random();
advance_random();
jrand = 0;
}
/*----------------------------------------------------------*/
/* Files for tournament selection : */
/* Source : sga.c (c) E.Goldberg */
/*----------------------------------------------------------*/
select_memory()
{
unsigned nbytes;
if(tourneysize > pop_size)
{
printf("FATAL: Tournament size (%d) > pop_size (%d)\n",
tourneysize,pop_size);
exit(-1);
} ;
}
preselect_tour()
{
reset1();
tourneypos = 0;
}
int tour_select()
{
int pick, winner, i;
/* If remaining members not enough for a tournament, then reset list */
start_select :
if((pop_size - tourneypos) < tourneysize)
{
reset1();
tourneypos = 0;
}
/* Select tourneysize structures at random and conduct a tournament */
winner=tourneylist[tourneypos];
/* Added by RBA */
if( winner < 0 || winner > pop_size-1) {
printf("\n Warning !! ERROR1");
printf(" tourpos = %d",tourneypos);
printf(" winner = %d",winner);
preselect_tour();
goto start_select; }
for(i=1; i<tourneysize; i++)
{
pick=tourneylist[i+tourneypos];
/* Added by RBA */
if (pick < 0 || pick > pop_size-1) { preselect_tour();
printf("\n Warning !! ERROR2");
goto start_select; }
if(MINM * oldpop[pick].mod_obj < MINM * oldpop[winner].mod_obj) winner=pick;
}
/* Update tourneypos */
tourneypos += tourneysize;
return(winner);
}
/* Selection routines */
/* Roulette Wheel selection */
select_memory_rw()
{
}
select_free_rw()
{
}
preselect_rw()
{
int j;
sum_obj = 0;
for(j = 0; j < pop_size; j++) sum_obj += oldpop[j].obj;
}
int rw_select()
/* roulette-wheel selection */
{
extern float randomperc();
float sum, pick;
int i;
pick = randomperc();
sum = 0;
if(sum_obj != 0)
{
for(i = 0; (sum < pick) && (i < pop_size); i++)
sum += oldpop[i].obj/sum_obj;
}
else
i = rnd(0,pop_size-1);
return(i-1);
}
/* Stochastic Remainder Roulette Wheel */
static int *choices, nremain;
static float *fraction;
select_memory_sr()
{
/* allocates auxiliary memory for stochastic remainder selection */
unsigned nbytes;
int j;
nbytes = pop_size*sizeof(int);
if((choices = (int *) malloc(nbytes)) == NULL)
nomemory(stderr,"choices");
nbytes = pop_size*sizeof(float);
if((fraction = (float *) malloc(nbytes)) == NULL)
nomemory(stderr,"fraction");
}
select_free_sr()
{
/* frees auxiliary memory for stochastic remainder selection */
free(choices);
free(fraction);
}
preselect_sr()
/* preselection for stochastic remainder method */
{
int j, jassign, k;
float expected;
if(avg_obj == 0)
{
for(j = 0; j < pop_size; j++) choices[j] = j;
}
else
{
j = 0;
k = 0;
/* Assign whole numbers */
do
{
expected = ((oldpop[j].obj)/avg_obj);
jassign = expected;
/* note that expected is automatically truncated */
fraction[j] = expected - jassign;
while(jassign > 0)
{
jassign--;
choices[k] = j;
k++;
}
j++;
}
while(j < pop_size);
j = 0;
/* Assign fractional parts */
while(k < pop_size)
{
if(j >= pop_size) j = 0;
if(fraction[j] > 0.0)
{
/* A winner if true */
if(flip(fraction[j]))
{
choices[k] = j;
fraction[j] = fraction[j] - 1.0;
k++;
}
}
j++;
}
}
nremain = pop_size - 1;
}
int sr_select()
/* selection using remainder method */
{
int jpick, slect;
jpick = rnd(0, nremain);
slect = choices[jpick];
choices[jpick] = choices[nremain];
nremain--;
return(slect);
}
reset1()
/* Name changed from reset because of clash with lib. function - RBA */
/* Shuffles the tourneylist at random */
{
int i, rand1, rand2, temp_site;
for(i=0; i<pop_size; i++) tourneylist[i] = i;
for(i=0; i < pop_size; i++)
{
rand1= rnd(0,pop_size-1);
rand2= rnd(0,pop_size-1);
temp_site = tourneylist[rand1];
tourneylist[rand1]=tourneylist[rand2];
tourneylist[rand2]=temp_site;
}
}
/******************* APPLICATION ORIENTED ROUTINES ***************/
/**** Change these routines for your particular application ******/
input_app_parameters()
/* Input your application dependent parameters here and put the
output in global variables */
{
}
app_computation()
/* this routine should contain any application-dependent computations */
/* that should be performed before each GA cycle.
called by generate_new_pop */
{
}
app_free()
/* application dependent free() calls, called by free_all() */
{
}
app_initialize()
/* application dependent initialization routine called by intialize() */
{
}
app_initreport()
/* Application-dependent initial report called by initreport() */
{
}
app_report()
/* Application-dependent report, called by report() */
{
}
app_statistics()
/* Application-dependent statistics calculations called by statistics() */
{
}
app_closure()
/* Application-dependent work which should be done before closure of
the main program. called by main() */
{
}
/*====================================================================
OBJECTIVE FUNCTION ( Supposed to be minimized) :
Change it for different applications
====================================================================*/
float objective(x)
float *x;
{
float term1,term2, term3, pi;
float g, penalty_coef;
if (x == NULL) error_ptr_null("x in objective()");
#ifdef prob1
MINM = -1;
term1 = (x[0]*x[0]+x[1]-11.0)*(x[0]*x[0]+x[1]-11.0);
term2 = (x[0]+x[1]*x[1]- 7.0)*(x[0]+x[1]*x[1]- 7.0);
term3 = term1+term2;
penalty_coef = 0.0;
g = (square(x[0]-5.0) + square(x[1]))/26.0 - 1.0;
if (g < 0.0) term3 = term3 + penalty_coef * g * g;
return(1.0/(1.+term3));
#endif
#ifdef can
MINM = 1;
pi = 4.0 * atan(1.0);
term3 = pi * x[0] * x[0]/2.0 + pi * x[0] * x[1];
penalty_coef = 1.0e4;
g = (pi * x[0] * x[0] * x[1]/4.0 - 400.0)/400.0;
if (g < 0.0) term3 = term3 + penalty_coef * g * g;
return(term3);
#endif
}
/********************** END OF FILE **************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -