📄 app.c
字号:
//应用修改处。end
/*----------------------------------------------------------------------------*/
/* app.c - application dependent routines, change these for different problem */
/*----------------------------------------------------------------------------*/
#include <math.h>
#include "external.h"
unsigned float x1,x2,x3;
/* this routine should contain any application-dependent computations */
/* that should be performed before each GA cycle. called by main() */
application()
{
}
/* application dependent data input, called by init_data() */
/* ask your input questions here, and put output in global variables */
app_data()
{
printf(" yutingchao: Hello! Please input your data.\n");
}
/* application dependent free() calls, called by freeall() */
app_free()
{
}
/* application dependent initialization routine called by intialize() */
app_init()
{
}
/* Application-dependent initial report called by initialize() */
app_initreport()
{
}
/* application dependent malloc() calls, called by initmalloc() */
app_malloc()
{
/* char *malloc(); */
}
/* Application-dependent report, called by report() */
app_report()
{
}
/* Application-dependent statistics calculations called by statistics() */
app_stats(pop)
struct individual *pop;
{
}
/* 此处为目标函数 */
/* objective function used in Goldberg's book */
/* fitness function is f(x) = x**n,
normalized to range between 0 and 1,
where x is the chromosome interpreted as an
integer, and n = 10 */
objfunc(critter)
struct individual *critter;
{
unsigned mask=1; /* mask for current bit */
unsigned bitpos; /* current bit position */
unsigned tp;
double pow(), bitpow, coef;
int j, k, stop;
int n = 10;
//以下程序计算x(从多位[如100位]二进制到十进制)并赋给critter->fitness。
critter->fitness = 0.0;
coef = pow(2.0,(double) lchrom) - 1.0;
coef = pow(coef, (double) n);
/* loop thru number of bytes holding chromosome */
for(k = 0; k < chromsize; k++)
{
if(k == (chromsize-1))
stop = lchrom-(k*UINTSIZE);
else
stop = UINTSIZE;
/* loop thru bits in current byte */
tp = critter->chrom[k];
for(j = 0; j < stop; j++)
{
bitpos = j + UINTSIZE*k;
/* test for current bit 0 or 1 */
if((tp&mask) == 1)
{
bitpow = pow(2.0,(double) bitpos);
critter->fitness = critter->fitness + bitpow;
}
tp = tp>>1;
}
}
/* At this point, fitness = x */
/* Now we must raise x to the n */
critter->fitness = pow(critter->fitness,(double) n);
/* normalize the fitness */
critter->fitness = critter->fitness/coef;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -