/* allocation simulation cc ga2.c -o bga2 -lm */ #include #include #include #include #define MAXGENS 200 #define NPLANTS 500 #define SEASONLEN 100 #define HERBFREQ 1.0 #define HERBRATE 0.05 #define HERBSTART 30 #define HERBEND 40 #define RRR 0.02 #define MUTATERATE 0.01 #define LZERO 1.0 typedef struct plant_def { double trait[SEASONLEN]; double sizet[SEASONLEN]; double size; double fitness; } Plant; Plant plants1[NPLANTS], plants2[NPLANTS]; void InitSeed(void); void InitPlants(Plant *plnt); void ZeroPlants(Plant *plnt); void GrowPlants(Plant *plnt, double *ave, double *high); void ReproducePlants(double avefit, Plant *parents, Plant *babies); void MakeBrakes(double avefit, double *totr, double *breaks, Plant *parents); Plant *GetParent(double totr, double *breaks, Plant *parents); void Mate(Plant *ma, Plant *pa, Plant *babe); void Xover(double *mapnt, double *papnt, double *bapnt); void PrintPlants(Plant *plnt); FILE *fopen(const char *, const char *); int main(void) { int ttt; double avefit, highfit; Plant *babes, *adults, *tmpy; FILE *fileid; adults = plants2; babes = plants1; InitSeed(); InitPlants(babes); fileid = fopen("time.dat","w"); for(ttt=0;ttttm_sec+1)*(preztime->tm_min+1)* (preztime->tm_hour+1)*(preztime->tm_year)*(preztime->tm_year)); if(seed%2==0) seed++; srand48(seed); } void InitPlants(Plant *plnt) { int i, j; for(i=0;itrait[j] = drand48(); } } } void ZeroPlants(Plant *plnt) { int i; for(i=0;isize = LZERO; plnt->fitness = 0.0; } } void GrowPlants(Plant *plnt, double *ave, double *high) { int i, j, herbbegin, herbend; double growth, fitness=0.0; *high=0.0; for(i=0;isize; plnt->size += (1.0-plnt->trait[j])*growth; plnt->sizet[j] = plnt->size; plnt->fitness += plnt->trait[j]*growth; if(j>=herbbegin && j<=herbend) plnt->size *= (1.0-HERBRATE); } fitness += plnt->fitness; if(plnt->fitness>*high) *high = plnt->fitness; } *ave = fitness/(double)NPLANTS; } void ReproducePlants(double avefit, Plant *parents, Plant *babies) { int i; double totrep; double brakes[NPLANTS]; Plant *mom, *dad; /* measure the total reproduction in the habitat */ MakeBrakes(avefit, &totrep, brakes, parents); /* Reproduction */ for(i=0;ifitness>avefit) *totr += parents->fitness; breaks[i] = *totr; } } Plant *GetParent(double totr, double *breaks, Plant *parents) { int j, rnum; rnum = drand48() * totr; j = (int)((rnum*NPLANTS)/totr); if(rnum breaks[j]) j++; else { while(rnum < breaks[j]) j--; j++; } return(&parents[j]); } void Mate(Plant *ma, Plant *pa, Plant *babe) { double *mapnt, *papnt, *bapnt; mapnt = ma->trait; papnt = pa->trait; bapnt = babe->trait; Xover(mapnt,papnt,bapnt); } void Xover(double *mapnt, double *papnt, double *bapnt) { int j; double xxx; for(j=0;jhifit) { biggie = i; hifit = plnt[i].fitness; } } for(j=0;j