home *** CD-ROM | disk | FTP | other *** search
- /* bookeep.c 4-10-91 bookeeping functions for the Tierra simulator */
- /** Tierra Simulator V3.0: Copyright (c) 1991 Thomas S. Ray **/
-
- #include "license.h"
-
- #ifndef lint
- static char sccsid[] = "@(#)bookeep.c 2.7 10/6/91";
- #endif
-
- #include "tierra.h"
- #include "extern.h"
-
- void DivideBookeep(ci, ni)
- I32s ci, ni;
- {
- Pcells nc = cells + ni;
- Pcells ce = cells + ci;
- Pgl tgl, tcgl;
- float max;
- I8s same = 0;
-
- LastDiv = InstExe;
- if (!ce->d.fecundity && !ce->d.mut && !ce->d.flaw) {
- ce->d.d1.flags = ce->d.flags; /* record metabolic data 1st replicat */
- ce->d.d1.inst = ce->d.inst + 1;
- ce->d.d1.mov_daught = ce->d.mov_daught;
- }
- ce->d.fecundity++;
- nc->d.gen.size = nc->mm.s;
- if (GeneBnker) {
- if (ce->mm.s == nc->mm.s && /* if cell breeds true */
- IsSameGen(nc->mm.s, soup + nc->mm.p, soup + ce->mm.p)) {
- if (ce->d.fecundity == 1)
- nc->d.d1.BreedTrue = ce->d.d1.BreedTrue = 1;
- nc->d.parent = ce->d.parent;
- nc->d.gen = ce->d.gen;
- nc->d.gli = ce->d.gli;
- same = 1;
- }
- else { /* if daughter is a new genotype (same = 0) */
- nc->d.parent = ce->d.gen;
- CheckGenotype(ni,&nc->d.gli); /* this will assign a gen.label */
- }
- tgl = (sl + nc->d.gli.si)->g + nc->d.gli.gi; /* new cell g_list */
- tcgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi; /* mother cell g_list */
- if (ce->d.fecundity == 1 && !ce->d.mut && !ce->d.flaw)
- tcgl->d1 = ce->d.d1;
- else if (ce->d.fecundity == 2 && !ce->d.mut && !ce->d.flaw) {
- tcgl->d2.inst = ce->d.inst + 1 - ce->d.d1.inst;
- tcgl->d2.flags = ce->d.flags - ce->d.d1.flags;
- tcgl->d2.mov_daught = ce->d.mov_daught;
- if (same) tcgl->d2.BreedTrue = same;
- }
- tgl->pop++;
- /* this might be a good place to keep track of multiple parental genotypes. */
- if (reaped) {
- max = (float) tgl->pop / (float) NumCells;
- if (max > tgl->MaxPropPop) tgl->MaxPropPop = max;
- max = (float) tgl->pop * nc->d.gen.size / (float) SoupSize;
- if (max > tgl->MaxPropInst) tgl->MaxPropInst = max;
- }
- /* criteria for saving genotype to disk */
- if (reaped && !IsBit(tgl->bits,0) && tgl->pop > 3 &&
- (tgl->MaxPropPop > SavThrPop || tgl->MaxPropInst > SavThrMem * .5))
- { if (!(sl + nc->d.gli.si)->num_s) NumSiz++;
- SetBit(&tgl->bits, 0, 1); SetBit(&tgl->bits, 1, 1);
- extract(ni);
- (sl + nc->d.gli.si)->num_s++;
- num_gen++;
- }
- }
- ce->d.mov_daught = ce->d.mut = 0;
- OutDisk('b',nc);
- }
-
- void OutDisk(bd, nc)
- I8s bd;
- Pcells nc;
- {
- I32s ttime;
- I8s path[80], label[4];
-
- if(DiskOut)
- {
- if (FirstOutDisk) {
- FirstOutDisk = 0; BrkupCum = 0; BrkupCou = 1;
- #ifdef IBM3090
- if (BrkupSiz) sprintf(path,"break.1.d");
- else sprintf(path,"tierra.run.d");
- oufr = fopen(path,"w");
- #else
- if (BrkupSiz) sprintf(path,"%sbreak.1", OutPath);
- else sprintf(path,"%stierra.run", OutPath);
- oufr = fopen(path,"w");
- #endif
- if (oufr == NULL)
- { sprintf(mes[0],"OutDisk 1: file %s not opened, exiting", path);
- FEMessage(1);
- while (hangup) ;
- exit(0);
- }
- sprintf(label,nc->d.gen.label);
- #ifdef IBM3090
- Ascii2Ebcdic(label);
- #endif
- BrkupCum += fprintf(oufr,"%lx %c %ld", InstExe.i, bd,
- nc->d.gen.size);
- if (GeneBnker) BrkupCum += 1 + fprintf(oufr," %s\n", label);
- else BrkupCum += 1 + fprintf(oufr,"\n");
- }
- else {
- ttime = InstExe.i - lo.time;
- if (ttime < 0) ttime += 1000000L;
- BrkupCum += fprintf(oufr,"%lx", ttime);
- if (lo.bd != bd) BrkupCum += fprintf(oufr," %c", bd);
- if (lo.size != nc->d.gen.size)
- BrkupCum += fprintf(oufr," %ld", nc->d.gen.size);
- if (GeneBnker && strcmp(lo.label,nc->d.gen.label)) {
- sprintf(label,nc->d.gen.label);
- #ifdef IBM3090
- Ascii2Ebcdic(label);
- #endif
- BrkupCum += fprintf(oufr," %s", label);
- }
- BrkupCum += 1 + fprintf(oufr,"\n");
- if (BrkupSiz && BrkupCum > BrkupSiz * 1024L) {
- fclose(oufr); BrkupCum = 0; BrkupCou++;
- #ifdef IBM3090
- sprintf(path,"break.%ld.d", BrkupCou);
- oufr = fopen(path,"w");
- #else
- sprintf(path,"%sbreak.%ld", OutPath, BrkupCou);
- oufr = fopen(path,"w");
- #endif
- if (oufr == NULL)
- { sprintf(mes[0],"OutDisk 2: file %s not opened, exiting", path);
- if (!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"system being saved to disk");
- FEMessage(2);
- }
- while (hangup) ;
- WriteSoup(1);
- exit(0);
- }
- }
- }
- }
- else
- { if(FirstOutDisk) FirstOutDisk = 0;
- else
- { ttime = InstExe.i - lo.time;
- if(ttime < 0) ttime += 1000000L;
- }
- }
- lo.bd = bd; lo.size = nc->d.gen.size; lo.time = InstExe.i;
- strcpy(lo.label,nc->d.gen.label);
- TimePop += (double) ttime * (double) NumCells;
- if (bd == 'b') TimeBirth++;
- else TimeDeath++;
- }
-
- void ReapBookeep(ci)
- I32s ci;
- {
- Pgl tgl;
- Pcells ce = cells + ci;
-
- OutDisk('d',ce);
- if (GeneBnker) {
- tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
- tgl->pop--;
- /* don't do this any more? -tom
- if (tgl->pop <= 0 && IsBit(tgl->bits,1) && !IsBit(tgl->bits,0))
- DelGenFile(tgl);
- */
- }
- InitCell(ci);
- NumCells--;
- reaped = 1;
- }
-
- void MutBookeep(i)
- Ind i;
- {
- I32s ci;
- I8s md;
- Pcells ce;
- Pgl tgl;
-
- if (!GeneBnker || IsFree(i)) return ;
- WhichCell(i, &ci, &md);
- if (md == 'm') {
- ce = cells + ci;
- ((sl + ce->d.gli.si)->g + ce->d.gli.gi)->pop--;
- OutDisk('d',ce);
- ce->d.parent = ce->d.gen; /* assign new genotype */
- CheckGenotype(ci,&ce->d.gli);
- tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
- tgl->pop++;
- OutDisk('b',ce);
- ce->d.d1.flags = ce->d.d1.mov_daught = 0L;
- ce->d.fecundity = ce->d.flags = 0L;
- ce->d.d1.inst = ce->d.inst = 0L;
- ce->d.mut++;
- }
- }
-
- void plan()
- { I32s i, n = 0L, upper_cell = 0L, indiv_gen_time, pop_gen_time;
- I32s MaxPop = 0L, MaxMem = 0L, pop = 0L, mem = 0L;
- struct genotype MaxGenPop, MaxGenMem;
- double prob_of_hit;
- Pcells ce;
- I8s buf[80];
-
- AverageSize = 0L;
- for (i = 2; i < CellsSize; i++)
- { ce = cells + i;
- if (ce->ld)
- { n++;
- AverageSize += ce->d.gen.size;
- upper_cell = i;
- if (GeneBnker && InstExe.m)
- { pop = ((sl + ce->d.gli.si)->g + ce->d.gli.gi)->pop;
- mem = pop * ce->d.gen.size;
- if (pop > MaxPop)
- { MaxPop = pop;
- MaxGenPop = ce->d.gen;
- }
- if (mem > MaxMem)
- { MaxMem = mem;
- MaxGenMem = ce->d.gen;
- }
- }
- }
- }
- #ifdef ERROR
- if (n != NumCells)
- { sprintf(mes[0],"ERROR: NumCells = %ld count of cells = %ld",
- NumCells, n);
- FEMessage(1);
- }
- #endif
- if (((double) CellsSize > 1.8 * (double) (upper_cell + 1)) && reaped &&
- DistFreq < 0)
- { sprintf(mes[0],"lbookeep: realloc, NumCells = %ld upper_cell = %ld",
- NumCells, upper_cell);
- sprintf(buf," old CellsSize = %ld ", CellsSize);
- CellsSize = (I32s) (1.2 * (double) (upper_cell + 1));
- if (CellsSize < upper_cell) CellsSize = upper_cell + 10;
- if (CellsSize < NumCells + 2) CellsSize = NumCells + 12;
- cells = (Pcells) threalloc((I8s Fp) cells,
- (I32u) CellsSize * sizeof(struct cell));
- sprintf(mes[1],"%s new CellsSize = %ld", buf, CellsSize);
- FEMessage(2);
- }
- AverageSize /= n;
- if (GenPerMovMut) RateMovMut = (I32s) 2L * GenPerMovMut * AverageSize;
- indiv_gen_time = 10L * AverageSize;
- if (InstExe.m) pop_gen_time = NumCells * indiv_gen_time;
- else pop_gen_time = indiv_gen_time * (SoupSize / (4L * AverageSize));
- prob_of_hit = (double) AverageSize / (double) SoupSize;
- if(GenPerBkgMut) RateMut=(I32s)(pop_gen_time*2L*GenPerBkgMut*prob_of_hit);
- if (GenPerFlaw) RateFlaw = (I32s) indiv_gen_time * GenPerFlaw * 2L;
- DropDead = 1L + AverageSize / 80L;
- Search_limit = SearchLimit * (I32s) AverageSize;
- if (InstExe.m) {
- TimePop /= 1000000.;
- Generations += (double) (TimeBirth + TimeDeath) / (2. * TimePop);
- }
- FEPlan(MaxPop, MaxMem, &MaxGenPop, &MaxGenMem);
- TimePop = 0.; TimeBirth = TimeDeath = 0L;
- }
-
- void GenExTemp(adrt, ci, tsize)
- Ind adrt; /* address of beginning of template */
- I32s ci, tsize; /* ci = index of cell executing instruction */
- {
- I32s i, ti;
- I32u who; /* 0 same cell; 1 daughter cell; 2 other cell; */
- /* 3 free memory; 4 daughter of other cell */
- Ind dist;
- Pgl tgl, ogl;
- Pcells ce = cells + ci, ct;
-
- tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
- for (i = 0; i < tsize; i++)
- { ti = ci;
- who = WhoIs(&ti, ad(ce->c.ip + 1 + i)); /* who has template pattern */
- if (who < 4) tgl->bits |= (I32u) (ONE << (I32u) (12 + who));
- else tgl->bits |= (I32u) (ONE << (I32u) (12 + 2));
- if (!who) {
- dist = ad(ce->c.ip + 1 + i) - ce->mm.p;
- dist = ad(dist);
- #ifdef ERROR
- if (tgl->genome == NULL || dist < 0 || dist >= tgl->gen.size)
- { sprintf(mes[0],"GenExTemp error 0");
- if(!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"core being saved");
- FEMessage(2);
- }
- while (hangup) ;
- WriteSoup(1);
- exit(0);
- }
- #endif
- /* *(tgl->genome + dist) |= (1 << 5); */
- tgl->gbits[dist][ce->c.tr] |= 1;
- }
- if (who == 2) {
- ct = cells + ti;
- ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
- if (IsBit(ogl->bits,0)) {
- ogl->bits |= (I32u) (ONE << (I32u) (12 + 4));
- dist = ad(ce->c.ip + 1 + i) - ct->mm.p;
- dist = ad(dist);
- #ifdef ERROR
- if (ogl->genome == NULL || dist < 0 || dist >= ogl->gen.size)
- { sprintf(mes[0],"GenExTemp error 1");
- if (!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"core being saved");
- FEMessage(2);
- }
- while (hangup) ;
- WriteSoup(1);
- exit(0);
- }
- #endif
- /* *(ogl->genome + dist) |= (1 << 6); */
- ogl->gbits[dist][ce->c.tr] |= (1 << 1);
- }
- }
- ti = ci;
- who = WhoIs(&ti, ad(adrt + i)); /* who has complementary template */
- if (who < 4) tgl->bits |= (I32u) (ONE << (I32u) (7 + who));
- else tgl->bits |= (I32u) (ONE << (I32u) (7 + 2));
- if (!who) {
- dist = ad(adrt + i) - ce->mm.p;
- dist = ad(dist);
- #ifdef ERROR
- if (tgl->genome == NULL || dist < 0 || dist >= tgl->gen.size)
- { sprintf(mes[0],"GenExTemp error 2");
- if (!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"core being saved");
- FEMessage(2);
- }
- while (hangup) ;
- WriteSoup(1);
- exit(0);
- }
- #endif
- /* *(tgl->genome + dist) |= (1 << 5); */
- tgl->gbits[dist][ce->c.tr] |= 1;
- }
- if (who == 2)
- { ct = cells + ti;
- ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
- if (IsBit(ogl->bits,0))
- { ogl->bits |= (I32u) (ONE << (I32u) (7 + 4));
- dist = ad(adrt + i) - ct->mm.p;
- dist = ad(dist);
- #ifdef ERROR
- if (ogl->genome == NULL || dist < 0 || dist >= ogl->gen.size)
- { sprintf(mes[0],"GenExTemp error 3");
- if (!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"core being saved");
- FEMessage(2);
- }
- while (hangup) ;
- WriteSoup(1);
- exit(0);
- }
- #endif
- /* *(ogl->genome + dist) |= (1 << 6); */
- ogl->gbits[dist][ce->c.tr] |= (1 << 1);
- }
- }
- }
- }
-
- void GenExMov(ci, to, from)
- I32s ci, to, from;
- { Pcells ce = (cells + ci), ct;
- Pgl tgl, ogl;
- I32s ti;
- I32u who; /* 0 same cell; 1 daughter cell; 2 other cell; */
- /* 3 free memory; 4 daughter of other cell */
-
- tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
- if(ce->d.flaw || ce->d.mut || !IsBit(tgl->bits,0))
- return;
- if(ce->mm.p <= ce->c.ip && ce->c.ip < (ce->mm.p + ce->mm.s))
- /* the mov instruction being executed is within your own genome */
- { ti = ci;
- who = WhoIs(&ti, from); /* who is it moved from */
- if(who < 4) tgl->bits |= (I32u) (ONE << (I32u) (17 + who));
- else tgl->bits |= (I32u) (ONE << (I32u) (17 + 2));
- if(who == 2)
- { ct = cells + ti;
- ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
- if(IsBit(ogl->bits,0))
- ogl->bits |= (I32u) (ONE << (I32u) (17 + 4));
- }
- ti = ci;
- who = WhoIs(&ti, to); /* who is it moved to */
- if(who < 4) tgl->bits |= (I32u) (ONE << (I32u) (22 + who));
- else tgl->bits |= (I32u) (ONE << (I32u) (22 + 2));
- if(who == 2)
- { ct = cells + ti;
- ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
- if(IsBit(ogl->bits,0))
- ogl->bits |= (I32u) (ONE << (I32u) (22 + 4));
- }
- }
- else /* these are moved from while executing instructions that */
- { ti = ci; /* are not your own */
- who = WhoIs(&ti, from); /* who is it moved from */
- if(who < 4) tgl->bits |= (I32u) (ONE << (I32u) (27 + who));
- else tgl->bits |= (I32u) (ONE << (I32u) (27 + 2));
- if(who == 2)
- { ct = cells + ti; /* index of cell from which inst is moved */
- ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
- if(IsBit(ogl->bits,0))
- ogl->bits |= (I32u) (ONE << (I32u) (27 + 4));
- }
- }
- }
-
- void GenExExe(ci, adrt)
- I32s ci;
- Ind adrt;
- { Pcells ce = cells + ci, ct;
- I32u dist;
- I32u who; /* 0 same cell; 1 daughter cell; 2 other cell; */
- /* 3 free memory; 4 daughter of other cell */
- Pgl tgl;
- I32s ti = ci;
-
- tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
- if(ce->d.flaw || ce->d.mut || !IsBit(tgl->bits,0))
- return;
- who = WhoIs(&ti, adrt);
- if(who < 4) tgl->bits |= (I32u) (ONE << (I32u) (2 + who));
- else tgl->bits |= (I32u) (ONE << (I32u) (2 + 2));
- if(!who) /* who == 0 == same cell */
- { dist = adrt - ce->mm.p;
- #ifdef ERROR
- if(tgl->gbits == NULL || dist < 0 || dist >= tgl->gen.size)
- { sprintf(mes[0],"decode GenExExe error 0");
- if (!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"core being saved");
- FEMessage(2);
- }
- while(hangup) ;
- WriteSoup(1);
- exit(0);
- }
- #endif
- tgl->gbits[dist][ce->c.tr] |= 1;
- }
- if(who == 2) /* is other cell */
- { ct = cells + ti;
- tgl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
- if(IsBit(tgl->bits,0))
- { tgl->bits |= (1 << (2 + 4));
- dist = adrt - ct->mm.p;
- #ifdef ERROR
- if(tgl->gbits == NULL || dist < 0 || dist >= tgl->gen.size)
- { sprintf(mes[0],"decode GenExExe error 1");
- if (!hangup)
- FEMessage(1);
- else
- { sprintf(mes[1],"core being saved");
- FEMessage(2);
- }
- while(hangup) ;
- WriteSoup(1);
- exit(0);
- }
- #endif
- tgl->gbits[dist][ce->c.tr] |= (1 << 1);
- }
- }
- }
-