home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / TIERRA / BOOKEEP.C < prev    next >
C/C++ Source or Header  |  1992-09-09  |  12KB  |  423 lines

  1. /* bookeep.c   9-9-92  bookeeping functions for the Tierra Simulator */
  2. /* Tierra Simulator V4.0: Copyright (c) 1991, 1992 Tom Ray & Virtual Life */
  3.  
  4. #ifndef lint
  5. static char sccsid[] = "@(#)bookeep.c    1.5 7/21/92";
  6. #endif /* lint */
  7.  
  8. #include "license.h"
  9. #include "tierra.h"
  10. #include "extern.h"
  11.  
  12.  
  13. #ifdef MEM_CHK
  14. #include <memcheck.h>
  15. #endif /* MEM_CHK */
  16.  
  17. void DivideBookeep(ce, nc)
  18.     Pcells   ce, nc; /* ce = mother cell, nc = daughter cell */
  19. {   I8s      same = 0;
  20.     Event    SizGen;
  21.     GlIndex  GiHash;
  22.  
  23.     LastDiv = InstExe;
  24.     if (!ce->d.fecundity && !ce->d.mut && !ce->d.flaw)
  25.     {   ce->d.d1.flags = ce->d.flags;    /* record metabolic data 1st repl */
  26.         ce->d.d1.inst = ce->d.inst + 1;
  27.         ce->d.d1.mov_daught = ce->d.mov_daught;
  28.     }
  29.     ce->d.fecundity++;
  30.     nc->d.gen.size = nc->mm.s;
  31.     if (GeneBnker)
  32.     {   nc->d.genome = soup + nc->mm.p;
  33.         if (ce->mm.s == nc->mm.s &&    /* if cell breeds true */
  34.             IsSameGen(nc->mm.s, soup + nc->mm.p, soup + ce->mm.p))
  35.         {   if (ce->d.fecundity == 1)
  36.                 nc->d.d1.BreedTrue = ce->d.d1.BreedTrue = 1;
  37.             nc->d.parent = ce->d.parent;
  38.             nc->d.gen = ce->d.gen;
  39.             nc->d.gi = ce->d.gi;
  40.             nc->d.hash = ce->d.hash;
  41.             same = 1;
  42.         }
  43.         else     /* if daughter is a new genotype (same = 0) */
  44.         {   nc->d.parent = ce->d.gen; /* this will assign a gen.label */
  45.             GiHash = CheckGenotype(nc->d, 17); /* by checking .gen files */
  46.             nc->d.hash = GiHash.si;
  47.             nc->d.gi = GiHash.gi;
  48.             strcpy(nc->d.gen.label, Int2Lbl(GiHash.gi));
  49.         }
  50.  
  51.         SizGen = DivGenBook(ce, nc, InstExe, reaped, 1, same, 0);
  52.         NumGenotypes += SizGen.i;
  53.         NumSizes += SizGen.m;
  54.  
  55.     }
  56.     ce->d.mov_daught = ce->d.mut = 0;
  57.     OutDisk((I32s)'b', nc);
  58. #if FRONTEND != STDIO
  59.     FEStats(); 
  60. #endif /* FRONTEND != STDIO */
  61. }
  62.  
  63. void ReapBookeep(ce)
  64.     Pcells  ce;
  65. {   Event   SizGen;
  66.  
  67.     OutDisk((I32s)'d', ce);
  68.     if (GeneBnker)
  69.     {   SizGen = ReapGenBook(ce);
  70.         NumGenotypes += SizGen.i;
  71.         NumSizes += SizGen.m;
  72.     }
  73. #ifdef MICRO
  74.     if (ce == MicroSlice) MicroSlice = NULL;
  75. #endif /*MICRO */
  76.     InitCell(ce->q.this.a,ce->q.this.i,ce);
  77.     NumCells--;
  78.     reaped = 1;
  79. }
  80.  
  81. void MutBookeep(i)
  82.     I32s i;
  83. {
  84.     I8s    md;
  85.     Pcells ce;
  86.     Event   SizGen;
  87.     GlIndex  GiHash;
  88.  
  89.     if (!GeneBnker || IsFree(i)) return;
  90.     WhichCell(i, &ce, &md);
  91.     if (md == 'd')
  92.         return;
  93.     SizGen = ReapGenBook(ce);
  94.     NumGenotypes += SizGen.i;
  95.  
  96.     OutDisk((I32s)'d', ce);
  97.     ce->d.parent = ce->d.gen;    /* assign new genotype */
  98.     ce->d.gi = -1;
  99.     strcpy(ce->d.gen.label, "---");
  100.  
  101.     GiHash = CheckGenotype(ce->d, 17); /* by checking .gen files */
  102.     ce->d.hash = GiHash.si;
  103.     ce->d.gi = GiHash.gi;
  104.     strcpy(ce->d.gen.label, Int2Lbl(GiHash.gi));
  105.  
  106.     SizGen = DivGenBook(NULL, ce, InstExe, reaped, 0, 0, 0);
  107. /*  SizGen = DivGenBook(NULL, ce, 1, reaped, InstExe, 0, 0); */
  108.     NumGenotypes += SizGen.i;
  109.  
  110.     OutDisk((I32s)'b', ce);
  111.     ce->d.d1.flags = ce->d.d1.mov_daught = 0L;
  112.     ce->d.fecundity = ce->d.flags = 0L;
  113.     ce->d.d1.inst = ce->d.inst = 0L;
  114.     ce->d.mut++;
  115. }
  116.  
  117. void OutDisk(bd, nc)
  118.     I32s bd;
  119.     Pcells nc;
  120. {   I32s ttime;
  121.     I8s label[4];
  122.  
  123.     if (DiskOut)
  124.     {   if (FirstOutDisk)
  125.         {   FirstOutDisk = 0;
  126.             BrkupCum = 0;
  127.             BrkupCou = 1;
  128. #ifdef IBM3090
  129.             if (BrkupSiz)
  130.                 sprintf(Buff, "break.1.d");
  131.             else sprintf(Buff, "tierra.run.d");
  132.             oufr = fopen(Buff, "w");
  133. #else /* IBM3090 */
  134.             if (BrkupSiz)
  135.                 sprintf(Buff, "%sbreak.1", OutPath);
  136.             else sprintf(Buff, "%stierra.run", OutPath);
  137.             oufr = fopen(Buff, "w");
  138. #endif /* IBM3090 */
  139.             if (oufr == NULL)
  140.             {   FEError(-108,EXIT,NOWRITE, 
  141.                    "Tierra OutDisk() 1: file %s not opened, exiting\n", Buff);
  142.             }
  143.             sprintf(label, nc->d.gen.label);
  144. #ifdef IBM3090
  145.             Ascii2Ebcdic(label);
  146. #endif /* IBM3090 */
  147.             BrkupCum += fprintf(oufr, "%lx %c %ld", InstExe.i, (I8s) bd,
  148.                 nc->d.gen.size);
  149.             if (GeneBnker)
  150.                 BrkupCum += 1 + fprintf(oufr, " %s\n", label);
  151.             else BrkupCum += 1 + fprintf(oufr, "\n");
  152.         }
  153.         else
  154.         {   ttime = InstExe.i - lo.time;
  155.             if (ttime < 0)
  156.                 ttime += 1000000L;
  157.             BrkupCum += fprintf(oufr, "%lx", ttime);
  158.             if (lo.bd != bd)
  159.                 BrkupCum += fprintf(oufr, " %c", bd);
  160.             if (lo.size != nc->d.gen.size)
  161.                 BrkupCum += fprintf(oufr, " %ld", nc->d.gen.size);
  162.             if (GeneBnker && strcmp(lo.label, nc->d.gen.label))
  163.             {   sprintf(label, nc->d.gen.label);
  164. #ifdef IBM3090
  165.                 Ascii2Ebcdic(label);
  166. #endif /* IBM3090 */
  167.                 BrkupCum += fprintf(oufr, " %s", label);
  168.             }
  169.             BrkupCum += 1 + fprintf(oufr, "\n");
  170.             if (BrkupSiz && BrkupCum > BrkupSiz * 1024L)
  171.             {   fclose(oufr);
  172.                 BrkupCum = 0;
  173.                 BrkupCou++;
  174. #ifdef IBM3090
  175.                 sprintf(Buff, "break.%ld.d", BrkupCou);
  176.                 oufr = fopen(Buff, "w");
  177. #else /* IBM3090 */
  178.                 sprintf(Buff, "%sbreak.%ld", OutPath, BrkupCou);
  179.                 oufr = fopen(Buff, "w");
  180. #endif /* IBM3090 */
  181.                 if (oufr == NULL)
  182.                 {   FEError(-109,EXIT,WRITE,
  183.                    "Tierra OutDisk() 2: file %s not opened, exiting\n", Buff);
  184.                 }
  185.             }
  186.         }
  187.     }
  188.     else
  189.     {   if (FirstOutDisk) FirstOutDisk = 0;
  190.         else
  191.         {   ttime = InstExe.i - lo.time;
  192.             if (ttime < 0) ttime += 1000000L;
  193.         }
  194.     }
  195.     lo.bd = bd;
  196.     lo.size = nc->d.gen.size;
  197.     lo.time = InstExe.i;
  198.     strcpy(lo.label, nc->d.gen.label);
  199.     TimePop += (double) ttime *(double) NumCells;
  200.     if ((I8s) bd == 'b')
  201.         TimeBirth++;
  202.     else TimeDeath++;
  203. }
  204.  
  205. void plan()
  206. {   I32s i, j, n = 0, indiv_gen_time, pop_gen_time;
  207.     I32s MaxPop = 0, MaxMem = 0, pop = 0, mem = 0, ar, ci;
  208.     Genotype MaxGenPop, MaxGenMem;
  209.     double prob_of_hit;
  210.     Pcells ce;
  211.     I8s  *chk;
  212.     Pcells Fp  tcells;
  213. #ifdef MEM_PROF
  214.     I32s  SizSoup, SizCells, SizFreeMem, SizSl, SizSli = 0;
  215.     I32s  SizGl = 0, SizGli = 0, SizGen = 0;
  216. #endif /* MEM_PROF */
  217.  
  218. #ifndef CM5
  219.     if (GeneBnker && reaped)
  220.     {
  221.         GarbageCollectGB();
  222. #ifdef ERROR
  223.         VerifyGB();
  224. #endif /* ERROR */
  225.     }
  226. #endif /* CM5 */
  227.  
  228.     /* begin calculate averages */
  229.     AverageSize = 0;
  230.     chk = tcalloc(NumCelAr, sizeof(I8s));
  231.     for (ar = 0; ar < NumCelAr; ar++) for (ci = 0; ci < CelArSiz; ci++)
  232.     {   if (ar == 0 && ci < 2)
  233.             continue;
  234.         ce = &cells[ar][ci];
  235.         if (ce->ld)
  236.         {   n++; chk[ar] = 1;
  237.             AverageSize += ce->d.gen.size;
  238. #ifndef CM5
  239.             if (GeneBnker && InstExe.m)
  240.             {   pop = sl[ce->d.gen.size]->g[ce->d.gi]->pop;
  241.                 mem = pop * ce->d.gen.size;
  242.                 if (pop > MaxPop)
  243.                 {   MaxPop = pop;
  244.                     MaxGenPop = ce->d.gen;
  245.                 }
  246.                 if (mem > MaxMem)
  247.                 {   MaxMem = mem;
  248.                     MaxGenMem = ce->d.gen;
  249.                 }
  250.             }
  251. #endif /* CM5 */
  252.         }
  253.     }
  254. #ifdef CM5
  255.     if (GeneBnker && InstExe.m)
  256.         FindMaxGen();
  257. #endif /* CM5 */
  258. /* end calculate averages */
  259.  
  260.     /* begin garbage collect for cells array */
  261.     if (reaped)
  262.         for(ar = NumCelAr - 1; ar > 0; ar--)
  263.         {   if (chk[ar])
  264.                 break;
  265.             if (cells[ar])
  266.             {   tfree(cells[ar]);
  267.                 cells[ar] = NULL;
  268.             }
  269.             NumCelAr--;
  270.             tcells = (Pcells Fp) trecalloc((Pcells Fp) cells,
  271.                 (I32u) NumCelAr * sizeof(Pcells Fp),
  272.                 (I32u) (NumCelAr + 1) * sizeof(Pcells Fp));
  273.             if (tcells)
  274.                 cells = tcells;
  275.             else if (cells)
  276.             {   tfree(cells);
  277.                 cells = NULL;
  278.              FEError(-129,EXIT,WRITE,"Tierra plan() cells trecalloc error\n");
  279.             }
  280.             CellsSize = NumCelAr * CelArSiz;
  281.         } /* end garbage collect for cells array */
  282.  
  283.     if (chk)
  284.     {   tfree(chk);
  285.         chk = NULL;
  286.     }
  287.  
  288. #ifdef MEM_PROF /* calculate memory profile */
  289.  
  290.     TotMemUse = SizSoup = SoupSize * sizeof(Instruction);
  291.     TotMemUse += SizCells = CellsSize * sizeof(struct cell);
  292.     TotMemUse += SizFreeMem = MaxFreeBlocks * sizeof(MemFr);
  293.     if(GeneBnker)
  294.     {   TotMemUse += SizSl = siz_sl * sizeof(SList Fp);
  295.         for (i = 0; i < siz_sl; i++)
  296.         {   if (sl[i])
  297.             {   TotMemUse += sizeof(SList);
  298.                 SizSli += sizeof(SList);
  299.                 TotMemUse += sl[i]->a_num * sizeof(GList Fp);
  300.                 SizGl += sl[i]->a_num * sizeof(GList Fp);
  301.                 for (j = 0; j < sl[i]->a_num; j++)
  302.                 {   if ((I32s) sl[i]->g[j] > 4)
  303.                     {   TotMemUse += sizeof(GList);
  304.                         SizGli += sizeof(GList);
  305.                         if (sl[i]->g[j]->genome)
  306.                         {   TotMemUse += i * sizeof(Instruction);
  307.                             SizGen += i * sizeof(Instruction);
  308.                         }
  309.                         if (sl[i]->g[j]->gbits)
  310.                         {   TotMemUse += i * sizeof(GenBits);
  311.                             SizGen += i * sizeof(GenBits);
  312.                         }
  313.                     }
  314.                 }
  315.             }
  316.         }
  317.     }
  318.  
  319. #endif /* MEM_PROF */
  320.  
  321.     /* begin calculate averages */
  322.     if (n != NumCells)
  323.     {   FEError(-130,EXIT,NOWRITE,
  324.          "Tierra plan() NumCells = %ld  count of cells = %ld\n", NumCells, n);
  325.     }
  326.     AverageSize /= n;
  327.     if (GenPerMovMut)
  328.         RateMovMut = (I32s) 2L *GenPerMovMut * AverageSize;
  329.     indiv_gen_time = 10L * AverageSize;
  330.     if (InstExe.m)
  331.         pop_gen_time = NumCells * indiv_gen_time;
  332.     else pop_gen_time = indiv_gen_time * (SoupSize / (4L * AverageSize));
  333.     prob_of_hit = (double) AverageSize / (double) SoupSize;
  334.     if (GenPerBkgMut)
  335.         RateMut = (I32s) (pop_gen_time * 2L * GenPerBkgMut * prob_of_hit);
  336.     if (GenPerFlaw)
  337.         RateFlaw = (I32s) indiv_gen_time *GenPerFlaw * 2L;
  338.     if (DropDead) DropDead = 1L + AverageSize / 80L;  /* DAN */
  339.     Search_limit = (I32s) (SearchLimit * AverageSize);
  340.     Put_limit = (I32s) (PutLimit * AverageSize);
  341.     MalLimit = MalTol * AverageSize;
  342.     if (MalLimit >= SoupSize)
  343.         MalLimit = SoupSize - 1;
  344.     if (InstExe.m)
  345.     {   TimePop /= 1000000.;
  346.         Generations += (double) (TimeBirth + TimeDeath) / (2. * TimePop);
  347.     }
  348.     /* end calculate averages */
  349.  
  350.     FEPlan(MaxPop, MaxMem, &MaxGenPop, &MaxGenMem);
  351.  
  352. #ifdef MEM_PROF
  353.  
  354.     FEMemProf(SizSoup, SizCells, SizFreeMem, SizSl, SizSli,
  355.         SizGl, SizGli, SizGen);
  356.  
  357. #endif /* MEM_PROF */
  358.  
  359.     TimePop = 0.;
  360.     TimeBirth = TimeDeath = 0L;
  361. }
  362.  
  363. I16s Lbl2Int(s)
  364.     I8s *s;
  365. {
  366.     if (s[0] == '-')
  367.     return -1;
  368.     return (s[2] - 'a') + (26 * (s[1] - 'a')) + (676 * (s[0] - 'a'));
  369. }
  370.  
  371. I8s *Int2Lbl(i)
  372.     I32s i;
  373. {
  374.     static I8s s[4];
  375.  
  376.     if (i < 0) {
  377.     strcpy(s, "---");
  378.     return s;
  379.     }
  380.     s[0] = 'a' + (I16s) i / 676;
  381.     i %= 676;
  382.     s[1] = 'a' + (I16s) i / 26;
  383.     i %= 26;
  384.     s[2] = 'a' + (I16s) i;
  385.     s[3] = 0;
  386.     return s;
  387. }
  388.  
  389. I32u WhoIs(ce, a)
  390.     Pcells  Fp ce;
  391.     I32s a;
  392. {
  393.     I8s md;
  394.  
  395.     if (a >= (*ce)->mm.p && a < (*ce)->mm.p + (*ce)->mm.s)
  396.         return 0;          /* same cell */
  397.     if (a >= (*ce)->md.p && a < (*ce)->md.p + (*ce)->md.s)
  398.         return 1;          /* daughter cell */
  399.     if (IsFree(a))
  400.         return 3;          /* is free memory */
  401.     WhichCell(a, ce, &md);
  402.     if (md == 'm')
  403.         return 2;          /* is other cell */
  404.     return 4;              /* is the daughter of another cell */
  405. }
  406.  
  407. I8s IsSameGen(size, g1, g2)/* compare two genomes */
  408.     I32s size;
  409.     FpInst g1, g2;
  410. {
  411.     I32s i, j;
  412.  
  413.     for (i = 0; i < size; i++) 
  414. #if PLOIDY == 1
  415.         if ((g1 + i)->inst != (g2 + i)->inst)
  416. #else /* PLOIDY > 1 */
  417.     for (j = 0; j < PLOIDY; j++)
  418.         if ((g1 + i)[j]->inst != (g2 + i)[j]->inst)
  419. #endif /* PLOIDY > 1 */
  420.             return 0;
  421.     return 1;
  422. }
  423.