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

  1. /* tsetup.c  19-8-91  Artificial Life simulator  setup routines */
  2. /** Tierra Simulator V3.0: Copyright (c) 1991 Thomas S. Ray **/
  3.  
  4. #include "license.h"
  5.  
  6. #ifndef lint
  7. static char     sccsid[] = "@(#)tsetup.c    2.15 10/6/91";
  8. #endif
  9.  
  10. #include "tierra.h"
  11. #include "extern.h"
  12.  
  13. I8s GetAVar(data)
  14.     I8s  data[85];
  15. {
  16.     if(!strncmp(data,"alive",5))
  17.     {   sscanf(data,"alive = %ld", &alive); return 1; }
  18.     if(!strncmp(data,"BrkupSiz",8))
  19.     {   sscanf(data,"BrkupSiz = %ld", &BrkupSiz); return 1; }
  20.     if(!strncmp(data,"CellsSize",9))
  21.     {   sscanf(data,"CellsSize = %ld", &CellsSize); return 1; }
  22.     if(!strncmp(data,"debug",5))
  23.     {   sscanf(data,"debug = %ld", &debug); return 1; }
  24.     if(!strncmp(data,"DiskOut",7))
  25.     {   sscanf(data,"DiskOut = %ld", &DiskOut); return 1; }
  26.     if(!strncmp(data,"DistFreq",8))
  27.     {   sscanf(data,"DistFreq = %f", &DistFreq); return 1; }
  28.     if(!strncmp(data,"DistProp",8))
  29.     {   sscanf(data,"DistProp = %f", &DistProp); return 1; }
  30.     if(!strncmp(data,"DivSameSiz",10))
  31.     {   sscanf(data,"DivSameSiz = %ld", &DivSameSiz); return 1; }
  32.     if(!strncmp(data,"DivSameGen",10))
  33.     {   sscanf(data,"DivSameGen = %ld", &DivSameGen); return 1; }
  34.     if(!strncmp(data,"DropDead",8))
  35.     {   sscanf(data,"DropDead = %ld", &DropDead); return 1; }
  36.     if(!strncmp(data,"GeneBnker",9))
  37.     {   sscanf(data,"GeneBnker = %ld", &GeneBnker); return 1; }
  38.     if(!strncmp(data,"GenebankPath",12))
  39.     {   sscanf(data,"GenebankPath = %s", GenebankPath); return 1; }
  40.     if(!strncmp(data,"GenPerBkgMut",12))
  41.     {   sscanf(data,"GenPerBkgMut = %f", &GenPerBkgMut); return 1; }
  42.     if(!strncmp(data,"GenPerFlaw",10))
  43.     {   sscanf(data,"GenPerFlaw = %f", &GenPerFlaw); return 1; }
  44.     if(!strncmp(data,"GenPerMovMut",12))
  45.     {   sscanf(data,"GenPerMovMut = %f", &GenPerMovMut); return 1; }
  46.     if(!strncmp(data,"hangup",6))
  47.     {   sscanf(data,"hangup = %ld", &hangup); return 1; }
  48.     if(!strncmp(data,"MaxFreeBlocks",13))
  49.     {   sscanf(data,"MaxFreeBlocks = %ld", &MaxFreeBlocks); return 1; }
  50.     if(!strncmp(data,"MaxMalMult",10))
  51.     {   sscanf(data,"MaxMalMult = %f", &MaxMalMult); return 1; }
  52.     if(!strncmp(data,"MinCellSize",11))
  53.     {   sscanf(data,"MinCellSize = %ld", &MinCellSize); return 1; }
  54.     if(!strncmp(data,"MinTemplSize ",12))
  55.     {   sscanf(data,"MinTemplSize = %ld", &MinTemplSize); return 1; }
  56.     if(!strncmp(data,"MovPropThrDiv",13))
  57.     {   sscanf(data,"MovPropThrDiv = %f", &MovPropThrDiv); return 1; }
  58.     if(!strncmp(data,"new_soup",8))
  59.     {   sscanf(data,"new_soup = %ld", &new_soup); return 1; }
  60.     if(!strncmp(data,"NumCells",8))
  61.     {   sscanf(data,"NumCells = %ld", &NumCells); return 1; }
  62.     if(!strncmp(data,"OutPath",7))
  63.     {   sscanf(data,"OutPath = %s", OutPath); return 1; }
  64.     if(!strncmp(data,"PhotonPow",9))
  65.     {   sscanf(data,"PhotonPow = %f", &PhotonPow); return 1; }
  66.     if(!strncmp(data,"PhotonWidth",11))
  67.     {   sscanf(data,"PhotonWidth = %ld", &PhotonWidth); return 1; }
  68.     if(!strncmp(data,"PhotonWord",10))
  69.     {   sscanf(data,"PhotonWord = %s", PhotonWord); return 1; }
  70.     if(!strncmp(data,"RamBankSiz",10))
  71.     {   sscanf(data,"RamBankSiz = %ld", &RamBankSiz); return 1; }
  72.     if(!strncmp(data,"SaveFreq",8))
  73.     {   sscanf(data,"SaveFreq = %ld", &SaveFreq); return 1; }
  74.     if(!strncmp(data,"SavThrMem",9))
  75.     {   sscanf(data,"SavThrMem = %f", &SavThrMem); return 1; }
  76.     if(!strncmp(data,"SavThrPop",9))
  77.     {   sscanf(data,"SavThrPop = %f", &SavThrPop); return 1; }
  78.     if(!strncmp(data,"SearchLimit",11))
  79.     {   sscanf(data,"SearchLimit = %f", &SearchLimit); return 1; }
  80.     if(!strncmp(data,"seed",4))
  81.     {   sscanf(data,"seed = %ld", &seed); return 1; }
  82.     if(!strncmp(data,"SizDepSlice",11))
  83.     {   sscanf(data,"SizDepSlice = %ld", &SizDepSlice); return 1; }
  84.     if(!strncmp(data,"SlicePow",8))
  85.     {   sscanf(data,"SlicePow = %lf", &SlicePow); return 1; }
  86.     if(!strncmp(data,"SliceSize",9))
  87.     {   sscanf(data,"SliceSize = %ld", &SliceSize); return 1; }
  88.     if(!strncmp(data,"SliceStyle",10))
  89.     {   sscanf(data,"SliceStyle = %ld", &SliceStyle); return 1; }
  90.     if(!strncmp(data,"SlicFixFrac",11))
  91.     {   sscanf(data,"SlicFixFrac = %f", &SlicFixFrac); return 1; }
  92.     if(!strncmp(data,"SlicRanFrac",11))
  93.     {   sscanf(data,"SlicRanFrac = %f", &SlicRanFrac); return 1; }
  94.     if(!strncmp(data,"SoupSize",8))
  95.     {   sscanf(data,"SoupSize = %ld", &SoupSize); return 1; }
  96.     if(!strncmp(data,"WatchExe",8))
  97.     {   sscanf(data,"WatchExe = %ld", &WatchExe); return 1; }
  98.     if(!strncmp(data,"WatchMov",8))
  99.     {   sscanf(data,"WatchMov = %ld", &WatchMov); return 1; }
  100.     if(!strncmp(data,"WatchTem",8))
  101.     {   sscanf(data,"WatchTem = %ld", &WatchTem); return 1; }
  102.     return 0;
  103. }
  104.  
  105. void GetSoup()
  106. {   FILE  *inf;
  107.     I8s   data[85];
  108.     I32s  i;
  109.  
  110.     sprintf(mes[0],"Using instruction set (INST) = %d",INST);
  111.     FEMessage(1);
  112.  
  113. #ifdef __TURBOC__
  114.     timezone = (I32s) 5L * 60L * 60L;
  115. #endif
  116.     inf = fopen(soup_fn,"r");
  117.     if(inf == NULL)
  118.     {   sprintf(mes[0],"GetSoup: file %s not opened, exiting", soup_fn);
  119.         if (!hangup)
  120.             FEMessage(1);
  121.         else
  122.         {   sprintf(mes[1],"system being saved to disk");
  123.             FEMessage(2);
  124.         }
  125.         while(hangup) ;
  126.         exit(0);
  127.     }
  128.     fgets(data,84,inf); sscanf(data,"tierra core:"); /* file header line */
  129.     fgets(data,84,inf);                              /* blank line */
  130.     fgets(data,84,inf);
  131.     while(strlen(data) > 3)
  132.     {   if(!GetAVar(data))
  133.         {   sprintf(mes[0],"bad soup_in line: %s", data);
  134.             FEMessage(1);
  135.         }
  136.         fgets(data,84,inf);
  137.     }
  138.     if(new_soup)
  139.     {   GenInList = (I8s **) thcalloc((I32u) NumCells, (I32u) sizeof(I8s  *));
  140.         GenInBuf = (I8s  *) thcalloc((I32u) NumCells * 13,(I32u) sizeof(I8s));
  141.         for(i = 0; i < NumCells; i++)
  142.         {   fgets(data,84,inf);
  143.             GenInList[i] = GenInBuf + (i * 13);
  144.             sscanf(data,"%s", GenInList[i]);
  145.         }
  146.     }
  147.     FEStartup(); /* still a dummy function */
  148.  
  149.         /* allocate arrays */
  150.     if(NumCells > CellsSize - 2) CellsSize = NumCells + 22L;
  151.     sprintf(mes[0],"sizeof(Instruction)   = %ld", (I32s) sizeof(Instruction));
  152.     sprintf(mes[1],"sizeof(struct cell)   = %ld", (I32s) sizeof(struct cell));
  153.     sprintf(mes[2],"sizeof(struct mem_fr) = %ld",(I32s)sizeof(struct mem_fr));
  154.     FEMessage(3);
  155. #ifdef __TURBOC__
  156.     sprintf(mes[0],"coreleft = %lu", (I32u) coreleft());
  157.     FEMessage(1);
  158. #endif
  159.     soup = (HpInst) thcalloc((I32u ) SoupSize, (I32u) sizeof(Instruction));
  160.     sprintf(mes[0],"    %ld bytes allocated for soup",
  161.         SoupSize * sizeof(Instruction));
  162.     cells = (Pcells) thcalloc((I32u ) CellsSize, (I32u) sizeof(struct cell));
  163.     sprintf(mes[1],"    %ld bytes allocated for cells",
  164.         CellsSize * sizeof(struct cell));
  165.     FreeMem = (struct mem_fr Hp)
  166.         thcalloc((I32u ) MaxFreeBlocks, (I32u) sizeof(struct mem_fr));
  167.     sprintf(mes[2],"    %ld bytes allocated for mem_fr",
  168.         MaxFreeBlocks * sizeof(struct mem_fr));
  169.     FEMessage(3);
  170. #ifdef __TURBOC__
  171.     sprintf(mes[0],"coreleft = %lu  tsetup (soup, cells, FreeMem)",
  172.         (I32u) coreleft());
  173.     FEMessage(1);
  174. #endif
  175.     if(!soup || !cells || !FreeMem)
  176.     {   sprintf(mes[0],"Tierra setup malloc error, exiting");
  177.         if (!hangup)
  178.             FEMessage(1);
  179.         else
  180.         {   sprintf(mes[1],"system being saved to disk");
  181.             FEMessage(2);
  182.         }
  183.         while(hangup) ;
  184.         exit(0);
  185.     }
  186.     else
  187.     {   sprintf(mes[0],"tsetup: arrays allocated without error");
  188.         FEMessage(1);
  189.     }
  190. #ifdef unix
  191.     SLASH = 47;
  192. #endif
  193. #if __TURBOC__ || OS2_MC
  194.     SLASH = 92;
  195. #endif
  196.     TotFlaw = TotMovMut = TotMut = extr = isolate = fragment = 0;
  197.     Disturb.m = Disturb.i = DistNext.m = DistNext.i = 0L;
  198.     if(DivSameGen) DivSameSiz = 1;
  199.     ONE = 1;
  200.     if(GeneBnker) GetGenFileList();
  201.     else WatchExe = WatchMov = WatchTem = 0;
  202.     if(new_soup) GetNewSoup();
  203.     else GetOldSoup(inf);
  204.     if(GeneBnker) StupGenLists();
  205.     sprintf(mes[0],"tsetup: soup gotten");
  206.     FEMessage(1);
  207.     if(SliceStyle == 1)
  208.     {   PhotonSize = (I32s) strlen(PhotonWord);
  209.         PhotonTranslate(PhotonInst,PhotonWord);
  210.         slicer = SlicerPhoton;
  211.     }
  212.     else if(SliceStyle == 0) slicer = SlicerQueue;
  213.     else if(SliceStyle == 2) slicer = RanSlicerQueue;
  214.     if(new_soup)
  215.     {   thfree(GenInList); thfree(GenInBuf); }
  216.     fclose(inf);
  217. }
  218.  
  219. void StupGenLists()
  220. {   I32s  i, j, onum, newgen;
  221.     Pcells  ce;
  222.     I8s     gfile[80];
  223.     FILE    *afp;
  224.     head_t  head;
  225.     indx_t  *indx;
  226.     Pgl     tgl, tglt;
  227.     Psl     tsl;
  228.     struct gl_index  glia, glib;
  229.  
  230.     for(i = 2; i < CellsSize; i++)
  231.     {   ce = cells + i;
  232.         if(ce->ld)
  233.         {   newgen = 0;
  234.             IsNewSize(i,&ce->d.gli);
  235.             tsl = sl + ce->d.gli.si;
  236.             ce->d.gli.gi = Lbl2Int(ce->d.gen.label);
  237.             if(ce->d.gli.gi >= tsl->a_num)
  238.             {   onum = tsl->a_num;
  239.                 tsl->a_num = ce->d.gli.gi + 4;
  240.                 tsl->g = (Pgl) threalloc((I8s Hp) tsl->g,
  241.                     sizeof(struct g_list) * tsl->a_num);
  242.                 for(j = onum; j < tsl->a_num; j++)
  243.                     InitGList(tsl->g + j, ce->d.gli.si, j, tsl->size);
  244.             }
  245.             tgl = tsl->g + ce->d.gli.gi;
  246.             if(strcmp(ce->d.gen.label,tgl->gen.label) || tgl->genome == NULL)
  247.                 /* if new genotype */
  248.             {   /* read genotype from disk and put in rambank at tgl */
  249.                 if(strcmp(ce->d.gen.label,tgl->gen.label))
  250.                 {   tsl->num++; newgen = 1; }
  251.                 if (IsBit(tgl->bits,0))
  252. #ifdef IBM3090
  253.                     sprintf(gfile,"%04ld.gen.d", tsl->size);
  254.                 else
  255.                     sprintf(gfile,"%04ld.tmp.d", tsl->size);
  256. #else
  257.                     sprintf(gfile,"%s%04ld.gen", GenebankPath, tsl->size);
  258.                 else
  259.                     sprintf(gfile,"%s%04ld.tmp", GenebankPath, tsl->size);
  260. #endif
  261.                 if (!(afp = fopen(gfile,"rb")))
  262.                 {   fprintf(stderr,"file %s not opened\n", gfile);
  263.                     exit(9);
  264.                 }
  265.                 head = read_head(afp);
  266.                 if(strncmp(head.magic,"tie",3) || head.magic[3] - '0' != INST)
  267.                 {   fprintf(stderr,"IsInGenBank: bad magic number");
  268.                     exit(10);
  269.                 }
  270.                 indx = read_indx(afp,&head);
  271.                 if ((j = find_gen(indx,ce->d.gen.label,head.n)) == head.n)
  272.                     fprintf(stderr,"%s not in archive\n", ce->d.gen.label);
  273.                 else
  274.                 {   glia = tgl->a; glib = tgl->b;
  275.                     tglt = get_gen(afp, &head, &indx[j], j);
  276.                     *tgl = *tglt;
  277.                     thfree(tglt);
  278.                     tgl->a = glia; tgl->b = glib;
  279.                 }
  280.                 if(newgen)
  281.                     AddTopGenQueue(&ce->d.gli);
  282.                 thfree(indx);
  283.                 fclose(afp);
  284.             }
  285.             tgl->pop++;
  286.         }
  287.     }
  288.     for(i = 0; i < siz_sl; i++)
  289.     {   tsl = sl + i;
  290.         for(j = 0; j < tsl->a_num; j++)
  291.         {   tgl = tsl->g + j;
  292.             if(!strcmp(tgl->gen.label,"---"))
  293.                 Int2Lbl(j,tgl->gen.label);
  294.         }
  295.     }
  296. }
  297.  
  298. void GetNewSoup()
  299. {   I32s    i, k, ci, cs, spaces = 0;
  300.     Ind     l;
  301.     HpInst  si;
  302.     Pcells  ce;
  303.  
  304.     sprintf(mes[0],"beginning of GetNewSoup");
  305.     FEMessage(1);
  306.     if(!seed) seed = (I32s) time(NULL);
  307.     tsrand(seed);
  308.     sprintf(mes[0],"seed = %ld", seed);
  309.     FEMessage(1);
  310.     reaped = InstExe.i = InstExe.m = ExtractCount = CountMovMut =
  311.         CountMutRate = CountFlaw = RateMovMut = RateMut = RateFlaw = 0L;
  312.     FreeBlocks = FirstOutDisk = 1L;
  313.     Generations = 0.;
  314.     FreeMemCurrent = SoupSize;
  315.     /* initialize soup array */
  316.     for(l = 0; l < SoupSize; l++)
  317.     {   for (k = 0; k < PLOIDY; k++)
  318.         {   soup[l][k].inst = 0;
  319.             soup[l][k].read = 0;
  320.             soup[l][k].write = 0;
  321.             soup[l][k].exec = 0;
  322.         }
  323.     }
  324.     sprintf(mes[0],"init of soup complete");
  325.     FEMessage(1);
  326.         /* initialize FreeMem array */
  327.     SoupBot = 0; SoupTop = 1;
  328.     FreeMem->p = 0; FreeMem->s = 0;
  329.     FreeMem->n = 2; FreeMem->o = (I8s ) 1;
  330.     (FreeMem + 1)->p = SoupSize; (FreeMem + 1)->s = (I32s) 0;
  331.     (FreeMem + 1)->n = 1; (FreeMem + 1)->o = (I8s ) 1;
  332.     (FreeMem + 2)->p = 0; (FreeMem + 2)->s = SoupSize;
  333.     (FreeMem + 2)->n = 1; (FreeMem + 2)->o = (I8s ) 1;
  334.     for(i = 3; i < MaxFreeBlocks; i++)
  335.     {   (FreeMem + i)->p = 0;
  336.         (FreeMem + i)->n = 0;
  337.         (FreeMem + i)->s = 0;
  338.         (FreeMem + i)->o = (I8s ) 0;
  339.     }
  340.         /* initialize cells array */
  341.     for(i = 0; i < CellsSize; i++) InitCell(i);
  342.     cells->ld = (cells + 1)->ld = 1;
  343.     si = soup; ce = cells + 2; ci = 2;
  344.     ThisSlice = BottomReap = TopReap = 2;
  345.  
  346.     /* read in the cell genotypes */
  347.     for (i = 0; i < NumCells; i++)
  348.     {   if (!strncmp("space", GenInList[i], 5))
  349.         {   sscanf(GenInList[i], "%*s%ld", &cs);
  350.             sprintf(mes[0],"skipping %ld instructions", cs);
  351.             FEMessage(1);
  352.             spaces++;
  353.         }
  354.         else
  355.         {   sscanf(GenInList[i], "%4ld", &cs);
  356.             sprintf(mes[0],
  357.                "GetNewSoup: about to read %ld instructions of cell %ld",cs,i);
  358.             FEMessage(1);
  359.             ReadACreature(GenInList[i], ce, ci, si);
  360.             ce++; ci++;
  361.         }
  362.            si += cs;
  363.     }
  364.     NumCells -= spaces;
  365.     plan();
  366. }
  367.  
  368. void ReadACreature(crit, ce, ci, si)
  369.     I8s     *crit;
  370.     I32s    ci;
  371.     Pcells  ce;
  372.     HpInst  si;
  373. {
  374.     I32s    cs, j, k;
  375.     Pgl  g;
  376.     char cpath[256], gen[4];
  377.     FILE *fp;
  378.     head_t head;
  379.     indx_t *indx;
  380.     short n;
  381.  
  382.     sscanf(crit, "%4ld%3s", &cs, gen);
  383.     sprintf(cpath, "%s%04ld.gen", GenebankPath, cs);
  384.     if (!(fp = open_ar(cpath, cs, INST, 0)))
  385.     {   perror("ReadACreature");
  386.         exit(7);
  387.     }
  388.     head = read_head(fp);
  389.     indx = read_indx(fp, &head);
  390.     n = find_gen(indx, gen, head.n);
  391.     g = get_gen(fp, &head, &indx[n], n);
  392.     fclose(fp);
  393.     thfree(indx);
  394.     ce->c.ip = ce->mm.p = MemAlloc(&cs);
  395.     ce->d.gen.size = ce->mm.s = cs;
  396.     ce->d.gen = g->gen;
  397.     ce->d.parent = g->parent;
  398.     ce->ld = 1;
  399.     if (ci == 2)
  400.     {   ce->q.p_reap = 0;
  401.         ce->q.n_reap = 1;
  402.         cells->q.n_reap = 2;
  403.         (cells + 1)->q.p_reap = 2;
  404.     }
  405.     else {
  406.     EntBotSlicer(ci);
  407.     EntBotReaper(ci);
  408.     }
  409.     OutDisk('b',ce);
  410.     for (j = 0; j < cs; j++)
  411.     {   for (k = 0; k < PLOIDY; k++)
  412.         {   si[0][k] = g->genome[j][k];
  413.         }
  414.         si++;
  415.     }
  416.     if(g->comments) thfree(g->comments);
  417.     if(g->genome) thfree(g->genome);
  418.     thfree(g);
  419. }
  420.  
  421. void GetOldSoup(inf)
  422.     FILE  *inf;
  423. {
  424.     I8s   data[85];
  425.     FILE  *inc;
  426.  
  427.     fgets(data,84,inf); sscanf(data,"AverageSize = %ld", &AverageSize);
  428.     fgets(data,84,inf); sscanf(data,"BottomReap = %ld", &BottomReap);
  429.     fgets(data,84,inf); sscanf(data,"BrkupCou = %ld", &BrkupCou);
  430.     fgets(data,84,inf); sscanf(data,"BrkupCum = %ld", &BrkupCum);
  431.     fgets(data,84,inf); sscanf(data,"comsoc = %ld", &comsoc);
  432.     fgets(data,84,inf); sscanf(data,"CountFlaw = %ld", &CountFlaw);
  433.     fgets(data,84,inf); sscanf(data,"CountMovMut = %ld", &CountMovMut);
  434.     fgets(data,84,inf); sscanf(data,"CountMutRate = %ld", &CountMutRate);
  435.     fgets(data,84,inf); sscanf(data,"debug_switch = %ld", &debug_switch);
  436.     fgets(data,84,inf); sscanf(data,"DistNext.m = %ld\n", &DistNext.m);
  437.     fgets(data,84,inf); sscanf(data,"DistNext.i = %ld\n", &DistNext.i);
  438.     fgets(data,84,inf); sscanf(data,"Disturb.m = %ld\n", &Disturb.m);
  439.     fgets(data,84,inf); sscanf(data,"Disturb.i = %ld\n", &Disturb.i);
  440.     fgets(data,84,inf); sscanf(data,"extr = %ld", &extr);
  441.     fgets(data,84,inf); sscanf(data,"ExtractCount = %ld", &ExtractCount);
  442.     fgets(data,84,inf); sscanf(data,"FirstOutDisk = %ld", &FirstOutDisk);
  443.     fgets(data,84,inf); sscanf(data,"fragment = %ld", &fragment);
  444.     fgets(data,84,inf); sscanf(data,"FreeBlocks = %ld", &FreeBlocks);
  445.     fgets(data,84,inf); sscanf(data,"FreeMemCurrent = %ld",&FreeMemCurrent);
  446.     fgets(data,84,inf); sscanf(data,"Generations = %lf",&Generations);
  447.     fgets(data,84,inf); sscanf(data,"InstExe.i = %ld", &InstExe.i);
  448.     fgets(data,84,inf); sscanf(data,"InstExe.m = %ld", &InstExe.m);
  449.     fgets(data,84,inf); sscanf(data,"isolate = %ld", &isolate);
  450.     fgets(data,84,inf); sscanf(data,"LastDiv.i = %ld", &LastDiv.i);
  451.     fgets(data,84,inf); sscanf(data,"LastDiv.m = %ld", &LastDiv.m);
  452.     fgets(data,84,inf); sscanf(data,"RandIx1 = %ld", &RandIx1);
  453.     fgets(data,84,inf); sscanf(data,"RandIx2 = %ld", &RandIx2);
  454.     fgets(data,84,inf); sscanf(data,"RandIx3 = %ld", &RandIx3);
  455.     fgets(data,84,inf); sscanf(data,"RateFlaw = %ld", &RateFlaw);
  456.     fgets(data,84,inf); sscanf(data,"RateMovMut = %ld", &RateMovMut);
  457.     fgets(data,84,inf); sscanf(data,"RateMut = %ld", &RateMut);
  458.     fgets(data,84,inf); sscanf(data,"reaped = %ld", &reaped);
  459.     fgets(data,84,inf); sscanf(data,"runflag = %ld", &runflag);
  460.     fgets(data,84,inf); sscanf(data,"Search_limit = %ld", &Search_limit);
  461.     fgets(data,84,inf); sscanf(data,"SoupBot = %ld", &SoupBot);
  462.     fgets(data,84,inf); sscanf(data,"SoupTop = %ld", &SoupTop);
  463.     fgets(data,84,inf); sscanf(data,"ThisSlice = %ld", &ThisSlice);
  464.     fgets(data,84,inf); sscanf(data,"TimeBirth = %ld", &TimeBirth);
  465.     fgets(data,84,inf); sscanf(data,"TimeDeath = %ld", &TimeDeath);
  466.     fgets(data,84,inf); sscanf(data,"TimePop = %lf", &TimePop);
  467.     fgets(data,84,inf); sscanf(data,"TopReap = %ld", &TopReap);
  468.     fgets(data,84,inf); sscanf(data,"TotFlaw = %ld", &TotFlaw);
  469.     fgets(data,84,inf); sscanf(data,"TotMovMut = %ld", &TotMovMut);
  470.     fgets(data,84,inf); sscanf(data,"TotMut = %ld", &TotMut);
  471. #ifdef IBM3090
  472.     strcpy(data,"core_out.io.d");
  473. #else
  474.     strcpy(data,"core_out");
  475. #endif
  476.     inc = fopen(data,"rb");
  477.     if (inc == NULL)
  478.     {   sprintf(mes[0],"GetOldSoup 1: file %s not opened, exiting", data);
  479.         if (!hangup)
  480.             FEMessage(1);
  481.         else
  482.         {   sprintf(mes[1],"system being saved to disk");
  483.             FEMessage(2);
  484.         }
  485.         while(hangup) ;
  486.         exit(0);
  487.     }
  488.     if (DiskOut) fread(&lo, sizeof(struct LastOut), 1, inc);
  489.     fread(&is, sizeof(struct inst), 1, inc);
  490.     fread(TrandArray, sizeof(double), 98, inc);
  491.     tfread((I8s Hp)FreeMem,(I32s)sizeof(struct mem_fr),MaxFreeBlocks,inc);
  492.     tfread((I8s Hp) soup, (I32s) sizeof(Instruction), SoupSize, inc);
  493.     tfread((I8s Hp) cells, (I32s) sizeof(struct cell), CellsSize, inc);
  494.     fclose(inc);
  495.     if (DiskOut) {
  496. #ifdef IBM3090
  497.         if(BrkupSiz) sprintf(data,"break.%ld.d", BrkupCou);
  498.         else sprintf(data,"tierra.run");
  499. #else
  500.         if(BrkupSiz) sprintf(data,"%sbreak.%ld", OutPath, BrkupCou);
  501.         else sprintf(data,"%stierra.run", OutPath);
  502. #endif
  503.         oufr = fopen(data,"a");
  504.         if(oufr == NULL)
  505.         {   sprintf(mes[0],"GetOldSoup 2: file %s not opened, exiting");
  506.             if (!hangup)
  507.                 FEMessage(1);
  508.             else
  509.             {   sprintf(mes[1],"system being saved to disk");
  510.                 FEMessage(2);
  511.             }
  512.             while(hangup) ;
  513.             exit(0);
  514.         }
  515.     }
  516. }
  517.  
  518. void WriteSoup(close_disk)
  519.     I8s  close_disk;
  520. {
  521.     FILE    *ouf;
  522.     I32s    i, j, TNumGen = 0, TNumSiz = 0;
  523.     long int  tp;
  524.     I8s     comd[120], path[99], tpath[99];
  525.     Psl     tsl;
  526.     Pgl     tgl;
  527.     struct SizList  *tgi;
  528.  
  529.     FILE *fp, *tf;
  530.     head_t head, thead;
  531.     indx_t *indx, *tindx;
  532.     
  533.     if (DiskOut && close_disk) fclose(oufr);
  534.     if (GeneBnker) {
  535.     tgi = (struct SizList  *) thcalloc(NumSizl, sizeof(struct SizList));
  536.         for (i=0; i < NumSizl; i++) { /* make list of sizes, record their */
  537.             tsl = sl + i;              /* position in the size list. */
  538.             (tgi + i)->size = tsl->size;
  539.             (tgi + i)->pos = i; /* records the position in the size list */
  540.             if (tsl->num_s) (tgi + i)->dir = 1;
  541.             for (j = 0; j < tsl->a_num; j++) {
  542.         tgl = tsl->g + j;
  543.                 if (tgl->pop || IsBit(tgl->bits,0)) (tgi + i)->ext++;
  544.             }
  545.             if ((tgi + i)->ext) TNumSiz++;
  546.             TNumGen += (tgi + i)->ext;
  547.         }                            /* then sort the list by size */
  548.         qsort((void *) tgi, NumSizl, sizeof(struct SizList), slcmp);
  549.  
  550.         for (i = 0; i < NumSizl; i++) {
  551.         tsl = sl + (tgi + i)->pos;  /* position in sl of the ith size */
  552.             if ((tgi + i)->ext) {
  553.         sprintf(path,"%s%04ld.gen", GenebankPath, tsl->size);
  554.         sprintf(tpath,"%s%04ld.tmp", GenebankPath, tsl->size);
  555.         fp = open_ar(path, tsl->size, INST, !(tgi + i)->dir);
  556.         tf = open_ar(tpath, tsl->size, INST, 1);
  557.         head = read_head(fp);
  558.         thead = read_head(tf);
  559.         indx = read_indx(fp, &head);
  560.         tindx = read_indx(tf, &thead);
  561.         for (j=0; j<tsl->a_num; j++) {
  562.             /* for each genotype of this size */
  563.             tgl = tsl->g + j;
  564.             if (IsBit(tgl->bits,0) || tgl->pop) {
  565.             /* if this genotype has been saved to disk */
  566.             /* or has a residual population */
  567.             SetBit(&tgl->bits, 1, 1);
  568.             if (IsBit(tgl->bits,0))
  569.                 add_gen(fp, &head, &indx, tgl);
  570.             else add_gen(tf, &thead, &tindx, tgl);
  571.             }
  572.         }
  573.         fclose(fp);
  574.         fclose(tf);
  575.         if (!head.n) unlink(path);
  576.         if (!thead.n) unlink(tpath);
  577.         thfree(indx);
  578.         thfree(tindx);
  579.             }
  580.         }
  581.         thfree(tgi);
  582.     }
  583.     new_soup = 0;
  584.     if(alive <= InstExe.m) alive = InstExe.m + 5;
  585. #ifdef IBM3090
  586.         sprintf(comd,"soup_out.io.d");
  587. #else
  588.         sprintf(comd,"soup_out");
  589. #endif
  590.     ouf = fopen(comd,"w");
  591.     if(ouf == NULL)
  592.     {   sprintf(mes[0],"WriteSoup 2: file %s not opened, exiting", comd);
  593.         if (!hangup)
  594.             FEMessage(1);
  595.         else
  596.         {   sprintf(mes[1],"system being saved to disk");
  597.             FEMessage(2);
  598.         }
  599.         while(hangup) ;
  600.         exit(0);
  601.     }
  602.     fprintf(ouf,"tierra core:  %s\n", ctime(&tp));
  603.     fprintf(ouf,"alive = %ld\n", alive);
  604.     fprintf(ouf,"BrkupSiz = %ld\n", BrkupSiz);
  605.     fprintf(ouf,"CellsSize = %ld\n", CellsSize);
  606.     fprintf(ouf,"debug = %ld\n", debug);
  607.     fprintf(ouf,"DiskOut = %ld\n", DiskOut);
  608.     fprintf(ouf,"DistFreq = %f\n", DistFreq);
  609.     fprintf(ouf,"DistProp = %f\n", DistProp);
  610.     fprintf(ouf,"DivSameSiz = %ld\n", DivSameSiz);
  611.     fprintf(ouf,"DivSameGen = %ld\n", DivSameGen);
  612.     fprintf(ouf,"DropDead = %ld\n", DropDead);
  613.     fprintf(ouf,"GeneBnker = %ld\n", GeneBnker);
  614.     fprintf(ouf,"GenebankPath = %s\n", GenebankPath);
  615.     fprintf(ouf,"GenPerBkgMut = %f\n", GenPerBkgMut);
  616.     fprintf(ouf,"GenPerFlaw = %f\n", GenPerFlaw);
  617.     fprintf(ouf,"GenPerMovMut = %f\n", GenPerMovMut);
  618.     fprintf(ouf,"hangup = %ld\n", hangup);
  619.     fprintf(ouf,"MaxFreeBlocks = %ld\n", MaxFreeBlocks);
  620.     fprintf(ouf,"MaxMalMult = %g\n", MaxMalMult);
  621.     fprintf(ouf,"MinCellSize = %ld\n", MinCellSize);
  622.     fprintf(ouf,"MinTemplSize = %ld\n", MinTemplSize);
  623.     fprintf(ouf,"MovPropThrDiv = %g\n", MovPropThrDiv);
  624.     fprintf(ouf,"new_soup = %ld\n", new_soup);
  625.     fprintf(ouf,"NumCells = %ld\n", NumCells);
  626.     fprintf(ouf,"OutPath = %s\n", OutPath);
  627.     fprintf(ouf,"PhotonPow = %g\n", PhotonPow);
  628.     fprintf(ouf,"PhotonWidth = %ld\n", PhotonWidth);
  629.     fprintf(ouf,"PhotonWord = %s\n", PhotonWord);
  630.     fprintf(ouf,"RamBankSiz = %ld\n", RamBankSiz);
  631.     fprintf(ouf,"SaveFreq = %ld\n", SaveFreq);
  632.     fprintf(ouf,"SavThrMem = %g\n", SavThrMem);
  633.     fprintf(ouf,"SavThrPop = %g\n", SavThrPop);
  634.     fprintf(ouf,"SearchLimit = %g\n", SearchLimit);
  635.     fprintf(ouf,"seed = %ld\n", seed);
  636.     fprintf(ouf,"SizDepSlice = %ld\n", SizDepSlice);
  637.     fprintf(ouf,"SlicePow = %g\n", SlicePow);
  638.     fprintf(ouf,"SliceSize = %ld\n", SliceSize);
  639.     fprintf(ouf,"SliceStyle = %ld\n", SliceStyle);
  640.     fprintf(ouf,"SlicFixFrac = %g\n", SlicFixFrac);
  641.     fprintf(ouf,"SlicRanFrac = %g\n", SlicRanFrac);
  642.     fprintf(ouf,"SoupSize = %ld\n", SoupSize);
  643.     fprintf(ouf,"WatchExe = %ld\n", WatchExe);
  644.     fprintf(ouf,"WatchMov = %ld\n", WatchMov);
  645.     fprintf(ouf,"WatchTem = %ld\n", WatchTem);
  646.     fprintf(ouf,"\n");
  647.     /* end soup_in variables */
  648.     fprintf(ouf,"AverageSize = %ld\n", AverageSize);
  649.     fprintf(ouf,"BottomReap = %ld\n", BottomReap);
  650.     fprintf(ouf,"BrkupCou = %ld\n", BrkupCou);
  651.     fprintf(ouf,"BrkupCum = %ld\n", BrkupCum);
  652.     fprintf(ouf,"comsoc = %ld\n", comsoc);
  653.     fprintf(ouf,"CountFlaw = %ld\n", CountFlaw);
  654.     fprintf(ouf,"CountMovMut = %ld\n", CountMovMut);
  655.     fprintf(ouf,"CountMutRate = %ld\n", CountMutRate);
  656.     fprintf(ouf,"debug_switch = %ld\n", debug_switch);
  657.     fprintf(ouf,"DistNext.m = %ld\n", DistNext.m);
  658.     fprintf(ouf,"DistNext.i = %ld\n", DistNext.i);
  659.     fprintf(ouf,"Disturb.m = %ld\n", Disturb.m);
  660.     fprintf(ouf,"Disturb.i = %ld\n", Disturb.i);
  661.     fprintf(ouf,"extr = %ld\n", extr);
  662.     fprintf(ouf,"ExtractCount = %ld\n", ExtractCount);
  663.     fprintf(ouf,"FirstOutDisk = %ld\n", FirstOutDisk);
  664.     fprintf(ouf,"fragment = %ld\n", fragment);
  665.     fprintf(ouf,"FreeBlocks = %ld\n", FreeBlocks);
  666.     fprintf(ouf,"FreeMemCurrent = %ld\n", FreeMemCurrent);
  667.     fprintf(ouf,"Generations = %lf\n", Generations);
  668.     fprintf(ouf,"InstExe.i = %ld\n", InstExe.i);
  669.     fprintf(ouf,"InstExe.m = %ld\n", InstExe.m);
  670.     fprintf(ouf,"isolate = %ld\n", isolate);
  671.     fprintf(ouf,"LastDiv.i = %ld\n", LastDiv.i);
  672.     fprintf(ouf,"LastDiv.m = %ld\n", LastDiv.m);
  673.     fprintf(ouf,"RandIx1 = %ld\n", RandIx1);
  674.     fprintf(ouf,"RandIx2 = %ld\n", RandIx2);
  675.     fprintf(ouf,"RandIx3 = %ld\n", RandIx3);
  676.     fprintf(ouf,"RateFlaw = %ld\n", RateFlaw);
  677.     fprintf(ouf,"RateMovMut = %ld\n", RateMovMut);
  678.     fprintf(ouf,"RateMut = %ld\n", RateMut);
  679.     fprintf(ouf,"reaped = %ld\n", reaped);
  680.     fprintf(ouf,"runflag = %ld\n", runflag);
  681.     fprintf(ouf,"Search_limit = %ld\n", Search_limit);
  682.     fprintf(ouf,"SoupBot = %ld\n", SoupBot);
  683.     fprintf(ouf,"SoupTop = %ld\n", SoupTop);
  684.     fprintf(ouf,"ThisSlice = %ld\n", ThisSlice);
  685.     fprintf(ouf,"TimeBirth = %ld\n", TimeBirth);
  686.     fprintf(ouf,"TimeDeath = %ld\n", TimeDeath);
  687.     fprintf(ouf,"TimePop = %lf\n", TimePop);
  688.     fprintf(ouf,"TopReap = %ld\n", TopReap);
  689.     fprintf(ouf,"TotFlaw = %ld\n", TotFlaw);
  690.     fprintf(ouf,"TotMovMut = %ld\n", TotMovMut);
  691.     fprintf(ouf,"TotMut = %ld\n", TotMut);
  692.     fclose(ouf);
  693. #ifdef IBM3090
  694.     strcpy(comd,"core_out.io.d");
  695. #else
  696.     strcpy(comd,"core_out");
  697. #endif
  698.     ouf = fopen(comd,"wb");
  699.     if (ouf == NULL)
  700.     {   sprintf(mes[0],"WriteSoup 3: file %s not opened, exiting", comd);
  701.         if (!hangup)
  702.             FEMessage(1);
  703.         else
  704.         {   sprintf(mes[1],"system being saved to disk");
  705.             FEMessage(2);
  706.         }
  707.         while(hangup) ;
  708.         exit(0);
  709.     }
  710.     if (DiskOut) fwrite(&lo, sizeof(struct LastOut), 1, ouf);
  711.     fwrite(&is, sizeof(struct inst), 1, ouf);
  712.     fwrite(TrandArray, sizeof(double), 98, ouf);
  713.     tfwrite((I8s Hp) FreeMem, (I32s) sizeof(struct mem_fr),
  714.         MaxFreeBlocks, ouf);
  715.     tfwrite((I8s Hp) soup, (I32s) sizeof(Instruction), SoupSize, ouf);
  716.     tfwrite((I8s Hp) cells, (I32s) sizeof(struct cell), CellsSize, ouf);
  717.     fclose(ouf);
  718. }
  719.  
  720. I16s glcmp(gl1, gl2)
  721.     const void  *gl1, *gl2;
  722. {   Pgl  g1 = gl1, g2 = gl2;
  723.  
  724.     return strcmp(g1->gen.label, g2->gen.label);
  725. }
  726.  
  727. I16s slcmp(sl1, sl2)
  728.     const void  *sl1, *sl2;
  729. {   struct SizList  *s1 = sl1, *s2 = sl2;
  730.  
  731.     if (s1->size == s2->size) return 0;
  732.     if (s1->size <  s2->size) return -1;
  733.     if (s1->size >  s2->size) return 1;
  734.     return 0;
  735. }
  736.  
  737. void InitCell(ci)
  738.     I32s  ci;
  739. {
  740.     I16s  i;
  741.     Pcells  ce = cells + ci;
  742.  
  743.     ce->d.gen.size = 0L;
  744.     ce->d.gen.label[0] = ce->d.gen.label[1] = ce->d.gen.label[2] = 45;
  745.     ce->d.gen.label[3] = 0; /* "---" */
  746.     ce->d.fecundity = ce->d.flags = ce->d.mov_daught = ce->d.inst =
  747.     ce->d.mut = ce->d.flaw = 0L;
  748.     ce->d.d1.inst = ce->d.d1.flags = ce->d.d1.mov_daught = 0L;
  749.     ce->d.d1.BreedTrue = (I8s) 0;
  750.     ce->d.parent.size = 0L;
  751.     ce->d.parent.label[0] = ce->d.parent.label[1] = ce->d.parent.label[2] =45;
  752.     ce->d.parent.label[3] = 0; /* "---" */
  753.     ce->d.gli.si = ce->d.gli.gi = 0L;
  754.     ce->d.ib = (I16s) 0; /* instruction bank */
  755.     ce->d.ni = (I32s) -1; /* index to daughter cell */
  756.     ce->d.is = (I8s) 0; /* cpu is active */
  757.     ce->d.dm = (I8s) 0; /* daughter or mother */
  758.     ce->d.ploidy = (I8s) 1; /* how many tracks */
  759.     ce->q.n_time=ce->q.p_time=ce->q.n_reap=ce->q.p_reap = ci;
  760.     ce->mm.s = ce->md.s = 0L;
  761.     ce->mm.p = ce->md.p = (Ind) 0;
  762.     for (i = 0; i < NUM_REGS; i++) ce->c.re[i] = 0L;
  763.     ce->c.sp = (I16s) STACK_SIZE - 1;
  764.     for (i = 0; i < STACK_SIZE; i++) ce->c.st[i] = 0L;
  765.     ce->c.ip = (Reg) 0;
  766.     ce->c.fl = ce->c.tr = (I8s) 0;
  767.     ce->ld = (I8s) 0;
  768. }
  769.  
  770.