home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / xplatfrm / tierra / bookeep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-26  |  16.7 KB  |  497 lines

  1. /* bookeep.c  4-10-91  bookeeping functions for the Tierra simulator */
  2. /** Tierra Simulator V3.0: Copyright (c) 1991 Thomas S. Ray **/
  3.  
  4. #include "license.h"
  5.  
  6. #ifndef lint
  7. static char     sccsid[] = "@(#)bookeep.c    2.7 10/6/91";
  8. #endif
  9.  
  10. #include "tierra.h"
  11. #include "extern.h"
  12.  
  13. void DivideBookeep(ci, ni)
  14.     I32s  ci, ni;
  15. {
  16.     Pcells  nc = cells + ni;
  17.     Pcells  ce = cells + ci;
  18.     Pgl     tgl, tcgl;
  19.     float   max;
  20.     I8s     same = 0;
  21.  
  22.     LastDiv = InstExe;
  23.     if (!ce->d.fecundity && !ce->d.mut && !ce->d.flaw) {
  24.     ce->d.d1.flags = ce->d.flags; /* record metabolic data 1st replicat */
  25.         ce->d.d1.inst  = ce->d.inst + 1;
  26.         ce->d.d1.mov_daught = ce->d.mov_daught;
  27.     }
  28.     ce->d.fecundity++;
  29.     nc->d.gen.size = nc->mm.s;
  30.     if (GeneBnker) {
  31.     if (ce->mm.s == nc->mm.s &&    /* if cell breeds true */
  32.            IsSameGen(nc->mm.s, soup + nc->mm.p, soup + ce->mm.p)) {
  33.         if (ce->d.fecundity == 1)
  34.                 nc->d.d1.BreedTrue = ce->d.d1.BreedTrue = 1;
  35.             nc->d.parent = ce->d.parent;
  36.             nc->d.gen = ce->d.gen;
  37.             nc->d.gli = ce->d.gli;
  38.             same = 1;
  39.         }
  40.         else {  /* if daughter is a new genotype (same = 0) */
  41.         nc->d.parent = ce->d.gen;
  42.             CheckGenotype(ni,&nc->d.gli); /* this will assign a gen.label */
  43.         }
  44.         tgl = (sl + nc->d.gli.si)->g + nc->d.gli.gi;  /* new cell g_list */
  45.         tcgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi; /* mother cell g_list */
  46.         if (ce->d.fecundity == 1 && !ce->d.mut && !ce->d.flaw)
  47.             tcgl->d1 = ce->d.d1;
  48.         else if (ce->d.fecundity == 2 && !ce->d.mut && !ce->d.flaw) {
  49.         tcgl->d2.inst = ce->d.inst + 1 - ce->d.d1.inst;
  50.             tcgl->d2.flags = ce->d.flags - ce->d.d1.flags;
  51.             tcgl->d2.mov_daught = ce->d.mov_daught;
  52.             if (same) tcgl->d2.BreedTrue = same;
  53.         }
  54.         tgl->pop++;
  55. /* this might be a good place to keep track of multiple parental genotypes. */
  56.         if (reaped) {
  57.         max = (float) tgl->pop / (float) NumCells;
  58.             if (max > tgl->MaxPropPop) tgl->MaxPropPop = max;
  59.             max = (float) tgl->pop * nc->d.gen.size / (float) SoupSize;
  60.             if (max > tgl->MaxPropInst) tgl->MaxPropInst = max;
  61.         }
  62.         /* criteria for saving genotype to disk */
  63.         if (reaped && !IsBit(tgl->bits,0) && tgl->pop > 3 &&
  64.            (tgl->MaxPropPop > SavThrPop || tgl->MaxPropInst > SavThrMem * .5))
  65.     {   if (!(sl + nc->d.gli.si)->num_s) NumSiz++;
  66.             SetBit(&tgl->bits, 0, 1); SetBit(&tgl->bits, 1, 1);
  67.             extract(ni);
  68.             (sl + nc->d.gli.si)->num_s++;
  69.             num_gen++;
  70.         }
  71.     }
  72.     ce->d.mov_daught = ce->d.mut = 0;
  73.     OutDisk('b',nc);
  74. }
  75.  
  76. void OutDisk(bd, nc)
  77.     I8s   bd;
  78.     Pcells  nc;
  79. {
  80.     I32s  ttime;
  81.     I8s   path[80], label[4];
  82.  
  83.     if(DiskOut)
  84.     {
  85.     if (FirstOutDisk) {
  86.     FirstOutDisk = 0; BrkupCum = 0; BrkupCou = 1;
  87. #ifdef IBM3090
  88.         if (BrkupSiz) sprintf(path,"break.1.d");
  89.         else sprintf(path,"tierra.run.d");
  90.         oufr = fopen(path,"w");
  91. #else
  92.         if (BrkupSiz) sprintf(path,"%sbreak.1", OutPath);
  93.         else sprintf(path,"%stierra.run", OutPath);
  94.         oufr = fopen(path,"w");
  95. #endif
  96.         if (oufr == NULL)
  97.     {   sprintf(mes[0],"OutDisk 1: file %s not opened, exiting", path);
  98.             FEMessage(1);
  99.             while (hangup) ;
  100.             exit(0);
  101.         }
  102.         sprintf(label,nc->d.gen.label);
  103. #ifdef IBM3090
  104.         Ascii2Ebcdic(label);
  105. #endif
  106.         BrkupCum += fprintf(oufr,"%lx %c %ld", InstExe.i, bd,
  107.             nc->d.gen.size);
  108.         if (GeneBnker) BrkupCum += 1 + fprintf(oufr," %s\n", label);
  109.         else BrkupCum += 1 + fprintf(oufr,"\n");
  110.     }
  111.     else {
  112.     ttime = InstExe.i - lo.time;
  113.         if (ttime < 0) ttime += 1000000L;
  114.         BrkupCum += fprintf(oufr,"%lx", ttime);
  115.         if (lo.bd != bd) BrkupCum += fprintf(oufr," %c", bd);
  116.         if (lo.size != nc->d.gen.size)
  117.         BrkupCum += fprintf(oufr," %ld", nc->d.gen.size);
  118.         if (GeneBnker && strcmp(lo.label,nc->d.gen.label)) {
  119.             sprintf(label,nc->d.gen.label);
  120. #ifdef IBM3090
  121.             Ascii2Ebcdic(label);
  122. #endif
  123.             BrkupCum += fprintf(oufr," %s", label);
  124.         }
  125.         BrkupCum += 1 + fprintf(oufr,"\n");
  126.         if (BrkupSiz && BrkupCum > BrkupSiz * 1024L) {
  127.         fclose(oufr); BrkupCum = 0; BrkupCou++;
  128. #ifdef IBM3090
  129.             sprintf(path,"break.%ld.d", BrkupCou);
  130.             oufr = fopen(path,"w");
  131. #else
  132.             sprintf(path,"%sbreak.%ld", OutPath, BrkupCou);
  133.             oufr = fopen(path,"w");
  134. #endif
  135.             if (oufr == NULL)
  136.             {   sprintf(mes[0],"OutDisk 2: file %s not opened, exiting", path);
  137.                 if (!hangup)
  138.                     FEMessage(1);
  139.                 else
  140.                 {   sprintf(mes[1],"system being saved to disk");
  141.                     FEMessage(2);
  142.                 }
  143.                 while (hangup) ;
  144.                 WriteSoup(1);
  145.                 exit(0);
  146.             }
  147.         }
  148.     }
  149.     }
  150.     else
  151.     {   if(FirstOutDisk) FirstOutDisk = 0;
  152.         else
  153.         {   ttime = InstExe.i - lo.time;
  154.             if(ttime < 0) ttime += 1000000L;
  155.         }
  156.     }
  157.     lo.bd = bd; lo.size = nc->d.gen.size; lo.time = InstExe.i;
  158.     strcpy(lo.label,nc->d.gen.label);
  159.     TimePop += (double) ttime * (double) NumCells;
  160.     if (bd == 'b') TimeBirth++;
  161.     else TimeDeath++;
  162. }
  163.  
  164. void ReapBookeep(ci)
  165.     I32s  ci;
  166. {
  167.     Pgl  tgl;
  168.     Pcells  ce = cells + ci;
  169.  
  170.     OutDisk('d',ce);
  171.     if (GeneBnker) {
  172.     tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
  173.         tgl->pop--;
  174. /* don't do this any more? -tom
  175.         if (tgl->pop <= 0 && IsBit(tgl->bits,1) && !IsBit(tgl->bits,0))
  176.             DelGenFile(tgl);
  177. */
  178.     }
  179.     InitCell(ci);
  180.     NumCells--;
  181.     reaped = 1;
  182. }
  183.  
  184. void MutBookeep(i)
  185.     Ind  i;
  186. {
  187.     I32s  ci;
  188.     I8s   md;
  189.     Pcells  ce;
  190.     Pgl     tgl;
  191.  
  192.     if (!GeneBnker || IsFree(i)) return ;
  193.     WhichCell(i, &ci, &md);
  194.     if (md == 'm') {
  195.     ce = cells + ci;
  196.         ((sl + ce->d.gli.si)->g + ce->d.gli.gi)->pop--;
  197.         OutDisk('d',ce);
  198.         ce->d.parent = ce->d.gen;        /* assign new genotype */
  199.         CheckGenotype(ci,&ce->d.gli);
  200.         tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
  201.         tgl->pop++;
  202.         OutDisk('b',ce);
  203.         ce->d.d1.flags = ce->d.d1.mov_daught = 0L;
  204.         ce->d.fecundity = ce->d.flags = 0L;
  205.         ce->d.d1.inst = ce->d.inst = 0L;
  206.         ce->d.mut++;
  207.     }
  208. }
  209.  
  210. void plan()
  211. {   I32s  i, n = 0L, upper_cell = 0L, indiv_gen_time, pop_gen_time;
  212.     I32s  MaxPop = 0L, MaxMem = 0L, pop = 0L, mem = 0L;
  213.     struct genotype  MaxGenPop, MaxGenMem;
  214.     double  prob_of_hit;
  215.     Pcells  ce;
  216.     I8s     buf[80];
  217.  
  218.     AverageSize = 0L;
  219.     for (i = 2; i < CellsSize; i++)
  220.     {   ce = cells + i;
  221.         if (ce->ld)
  222.         {   n++;
  223.             AverageSize += ce->d.gen.size;
  224.             upper_cell = i;
  225.             if (GeneBnker && InstExe.m)
  226.             {   pop = ((sl + ce->d.gli.si)->g + ce->d.gli.gi)->pop; 
  227.                 mem = pop * ce->d.gen.size;
  228.                 if (pop > MaxPop)
  229.                 {   MaxPop = pop;
  230.                     MaxGenPop = ce->d.gen;
  231.                 }
  232.                 if (mem > MaxMem)
  233.                 {   MaxMem = mem;
  234.                     MaxGenMem = ce->d.gen;
  235.                 }
  236.             }
  237.         }
  238.     }
  239. #ifdef ERROR
  240.     if (n != NumCells)
  241.     {   sprintf(mes[0],"ERROR: NumCells = %ld  count of cells = %ld",
  242.             NumCells, n);
  243.         FEMessage(1);
  244.     }
  245. #endif
  246.     if (((double) CellsSize > 1.8 * (double) (upper_cell + 1)) && reaped &&
  247.         DistFreq < 0)
  248.     {   sprintf(mes[0],"lbookeep: realloc, NumCells = %ld  upper_cell = %ld",
  249.             NumCells, upper_cell);
  250.         sprintf(buf,"    old CellsSize = %ld  ", CellsSize);
  251.         CellsSize = (I32s) (1.2 * (double) (upper_cell + 1));
  252.         if (CellsSize < upper_cell) CellsSize = upper_cell + 10;
  253.         if (CellsSize < NumCells + 2) CellsSize = NumCells + 12;
  254.         cells = (Pcells) threalloc((I8s Fp) cells,
  255.                 (I32u) CellsSize * sizeof(struct cell));
  256.         sprintf(mes[1],"%s new CellsSize = %ld", buf, CellsSize);
  257.         FEMessage(2);
  258.     }
  259.     AverageSize /= n;
  260.     if (GenPerMovMut) RateMovMut = (I32s) 2L * GenPerMovMut * AverageSize;
  261.     indiv_gen_time = 10L * AverageSize;
  262.     if (InstExe.m) pop_gen_time = NumCells * indiv_gen_time;
  263.     else pop_gen_time = indiv_gen_time * (SoupSize / (4L * AverageSize));
  264.     prob_of_hit = (double) AverageSize / (double) SoupSize;
  265.     if(GenPerBkgMut) RateMut=(I32s)(pop_gen_time*2L*GenPerBkgMut*prob_of_hit);
  266.     if (GenPerFlaw) RateFlaw = (I32s) indiv_gen_time * GenPerFlaw * 2L;
  267.     DropDead = 1L + AverageSize / 80L;
  268.     Search_limit = SearchLimit * (I32s) AverageSize;
  269.     if (InstExe.m) {
  270.     TimePop /= 1000000.;
  271.         Generations += (double) (TimeBirth + TimeDeath) / (2. * TimePop);
  272.     }
  273.     FEPlan(MaxPop, MaxMem, &MaxGenPop, &MaxGenMem);
  274.     TimePop = 0.;  TimeBirth = TimeDeath = 0L;
  275. }
  276.  
  277. void GenExTemp(adrt, ci, tsize)
  278.     Ind   adrt; /* address of beginning of template */
  279.     I32s  ci, tsize;   /* ci = index of cell executing instruction */
  280. {
  281.     I32s  i, ti;
  282.     I32u  who; /* 0 same cell; 1 daughter cell; 2 other cell; */
  283.                /* 3 free memory; 4 daughter of other cell */
  284.     Ind   dist;
  285.     Pgl   tgl, ogl;
  286.     Pcells  ce = cells + ci, ct;
  287.  
  288.     tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
  289.     for (i = 0; i < tsize; i++)
  290.     {   ti = ci;
  291.         who = WhoIs(&ti, ad(ce->c.ip + 1 + i)); /* who has template pattern */
  292.         if (who < 4) tgl->bits |= (I32u) (ONE << (I32u) (12 + who));
  293.         else tgl->bits |= (I32u) (ONE << (I32u) (12 + 2));
  294.         if (!who) {
  295.         dist = ad(ce->c.ip + 1 + i) - ce->mm.p;
  296.             dist = ad(dist);
  297. #ifdef ERROR
  298.             if (tgl->genome == NULL || dist < 0 || dist >= tgl->gen.size)
  299.             {   sprintf(mes[0],"GenExTemp error 0");
  300.                 if(!hangup)
  301.                     FEMessage(1);
  302.                 else
  303.                 {   sprintf(mes[1],"core being saved");
  304.                     FEMessage(2);
  305.                 }
  306.                 while (hangup) ;
  307.                 WriteSoup(1);
  308.                 exit(0);
  309.             }
  310. #endif
  311.          /* *(tgl->genome + dist) |= (1 << 5); */
  312.             tgl->gbits[dist][ce->c.tr] |= 1;
  313.         }
  314.         if (who == 2) {
  315.         ct = cells + ti;
  316.             ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
  317.             if (IsBit(ogl->bits,0)) {
  318.         ogl->bits |= (I32u) (ONE << (I32u) (12 + 4));
  319.                 dist = ad(ce->c.ip + 1 + i) - ct->mm.p;
  320.                 dist = ad(dist);
  321. #ifdef ERROR
  322.                 if (ogl->genome == NULL || dist < 0 || dist >= ogl->gen.size)
  323.                 {   sprintf(mes[0],"GenExTemp error 1");
  324.                     if (!hangup)
  325.                         FEMessage(1);
  326.                     else
  327.                     {   sprintf(mes[1],"core being saved");
  328.                         FEMessage(2);
  329.                     }
  330.                     while (hangup) ;
  331.                     WriteSoup(1);
  332.                     exit(0);
  333.                 }
  334. #endif
  335.              /* *(ogl->genome + dist) |= (1 << 6); */
  336.                 ogl->gbits[dist][ce->c.tr] |= (1 << 1);
  337.             }
  338.         }
  339.         ti = ci;
  340.         who = WhoIs(&ti, ad(adrt + i)); /* who has complementary template */
  341.         if (who < 4) tgl->bits |= (I32u) (ONE << (I32u) (7 + who));
  342.         else tgl->bits |= (I32u) (ONE << (I32u) (7 + 2));
  343.         if (!who) {
  344.         dist = ad(adrt + i) - ce->mm.p;
  345.             dist = ad(dist);
  346. #ifdef ERROR
  347.             if (tgl->genome == NULL || dist < 0 || dist >= tgl->gen.size)
  348.             {   sprintf(mes[0],"GenExTemp error 2");
  349.                 if (!hangup)
  350.                     FEMessage(1);
  351.                 else
  352.                 {   sprintf(mes[1],"core being saved");
  353.                     FEMessage(2);
  354.                 }
  355.                 while (hangup) ;
  356.                 WriteSoup(1);
  357.                 exit(0);
  358.             }
  359. #endif
  360.          /* *(tgl->genome + dist) |= (1 << 5); */
  361.             tgl->gbits[dist][ce->c.tr] |= 1;
  362.         }
  363.         if (who == 2)
  364.         {   ct = cells + ti;
  365.             ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
  366.             if (IsBit(ogl->bits,0))
  367.             {   ogl->bits |= (I32u) (ONE << (I32u) (7 + 4));
  368.                 dist = ad(adrt + i) - ct->mm.p;
  369.                 dist = ad(dist);
  370. #ifdef ERROR
  371.                 if (ogl->genome == NULL || dist < 0 || dist >= ogl->gen.size)
  372.                 {   sprintf(mes[0],"GenExTemp error 3");
  373.                     if (!hangup)
  374.                         FEMessage(1);
  375.                     else
  376.                     {   sprintf(mes[1],"core being saved");
  377.                         FEMessage(2);
  378.                     }
  379.                     while (hangup) ;
  380.                     WriteSoup(1);
  381.                     exit(0);
  382.                 }
  383. #endif
  384.              /* *(ogl->genome + dist) |= (1 << 6); */
  385.                 ogl->gbits[dist][ce->c.tr] |= (1 << 1);
  386.             }
  387.         }
  388.     }
  389. }
  390.  
  391. void GenExMov(ci, to, from)
  392. I32s  ci, to, from;
  393. {   Pcells  ce = (cells + ci), ct;
  394.     Pgl     tgl, ogl;
  395.     I32s    ti;
  396.     I32u  who; /* 0 same cell; 1 daughter cell; 2 other cell; */
  397.                /* 3 free memory; 4 daughter of other cell */
  398.  
  399.     tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
  400.     if(ce->d.flaw || ce->d.mut || !IsBit(tgl->bits,0))
  401.         return;
  402.     if(ce->mm.p <= ce->c.ip && ce->c.ip < (ce->mm.p + ce->mm.s))
  403.         /* the mov instruction being executed is within your own genome */
  404.     {   ti = ci;
  405.         who = WhoIs(&ti, from); /* who is it moved from */
  406.         if(who < 4) tgl->bits |= (I32u) (ONE << (I32u) (17 + who));
  407.         else tgl->bits |= (I32u) (ONE << (I32u) (17 + 2));
  408.         if(who == 2)
  409.         {   ct = cells + ti;
  410.             ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
  411.             if(IsBit(ogl->bits,0))
  412.                 ogl->bits |= (I32u) (ONE << (I32u) (17 + 4));
  413.         }
  414.         ti = ci;
  415.         who = WhoIs(&ti, to); /* who is it moved to */
  416.         if(who < 4) tgl->bits |= (I32u) (ONE << (I32u) (22 + who));
  417.         else tgl->bits |= (I32u) (ONE << (I32u) (22 + 2));
  418.         if(who == 2)
  419.         {   ct = cells + ti;
  420.             ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
  421.             if(IsBit(ogl->bits,0))
  422.                 ogl->bits |= (I32u) (ONE << (I32u) (22 + 4));
  423.         }
  424.     }
  425.     else /* these are moved from while executing instructions that */
  426.     {   ti = ci;      /* are not your own */
  427.         who = WhoIs(&ti, from); /* who is it moved from */
  428.         if(who < 4) tgl->bits |= (I32u) (ONE << (I32u) (27 + who));
  429.         else tgl->bits |= (I32u) (ONE << (I32u) (27 + 2));
  430.         if(who == 2)
  431.         {   ct = cells + ti; /* index of cell from which inst is moved */
  432.             ogl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
  433.             if(IsBit(ogl->bits,0))
  434.                 ogl->bits |= (I32u) (ONE << (I32u) (27 + 4));
  435.         }
  436.     }
  437. }
  438.  
  439. void GenExExe(ci, adrt)
  440. I32s  ci;
  441. Ind   adrt;
  442. {   Pcells  ce = cells + ci, ct;
  443.     I32u    dist;
  444.     I32u    who; /* 0 same cell; 1 daughter cell; 2 other cell; */
  445.                  /* 3 free memory; 4 daughter of other cell */
  446.     Pgl     tgl;
  447.     I32s    ti = ci;
  448.  
  449.     tgl = (sl + ce->d.gli.si)->g + ce->d.gli.gi;
  450.     if(ce->d.flaw || ce->d.mut || !IsBit(tgl->bits,0))
  451.         return;
  452.     who = WhoIs(&ti, adrt);
  453.     if(who < 4) tgl->bits |= (I32u) (ONE << (I32u) (2 + who));
  454.     else tgl->bits |= (I32u) (ONE << (I32u) (2 + 2));
  455.     if(!who) /* who == 0 == same cell */
  456.     {   dist = adrt - ce->mm.p;
  457. #ifdef ERROR
  458.         if(tgl->gbits == NULL || dist < 0 || dist >= tgl->gen.size)
  459.         {   sprintf(mes[0],"decode GenExExe error 0");
  460.             if (!hangup)
  461.                 FEMessage(1);
  462.             else
  463.             {   sprintf(mes[1],"core being saved");
  464.                 FEMessage(2);
  465.             }
  466.             while(hangup) ;
  467.             WriteSoup(1);
  468.             exit(0);
  469.         }
  470. #endif
  471.         tgl->gbits[dist][ce->c.tr] |= 1;
  472.     }
  473.     if(who == 2) /* is other cell */
  474.     {   ct = cells + ti;
  475.         tgl = (sl + ct->d.gli.si)->g + ct->d.gli.gi;
  476.         if(IsBit(tgl->bits,0))
  477.         {   tgl->bits |= (1 << (2 + 4));
  478.             dist = adrt - ct->mm.p;
  479. #ifdef ERROR
  480.             if(tgl->gbits == NULL || dist < 0 || dist >= tgl->gen.size)
  481.             {   sprintf(mes[0],"decode GenExExe error 1");
  482.                 if (!hangup)
  483.                     FEMessage(1);
  484.                 else
  485.                 {   sprintf(mes[1],"core being saved");
  486.                     FEMessage(2);
  487.                 }
  488.                 while(hangup) ;
  489.                 WriteSoup(1);
  490.                 exit(0);
  491.             }
  492. #endif
  493.             tgl->gbits[dist][ce->c.tr] |= (1 << 1);
  494.         }
  495.     }
  496. }
  497.