/* allocation simulation cc simpall.c -o bsimp -lm */ #include #include #include #define MAXGENS 200 #define NPLANTS 500 #define SEASONLEN 100 #define RRR 0.02 #define MUTATERATE 0.01 #define LZERO 1.0 typedef struct plant_def { double trait[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 PrintPlants(Plant *plnt); int main(void) { int ttt; double avefit, highfit; Plant *babes, *adults, *tmpy; adults = plants2; babes = plants1; InitSeed(); InitPlants(babes); 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; double growth, fitness=0.0; *high=0.0; for(i=0;isize; plnt->size += (1.0-plnt->trait[j])*growth; plnt->fitness += plnt->trait[j]*growth; } 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) { int j; double *mapnt, *papnt, *bapnt, xxx; mapnt = ma->trait; papnt = pa->trait; bapnt = babe->trait; for(j=0;jhifit) { biggie = i; hifit = plnt[i].fitness; } for(j=0;j