allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (07/19/89)
Posting-number: Volume 7, Issue 75 Submitted-by: loy@gtx.UUCP (Bob Loy) Archive-name: whpl/part02 # whpl02of13.shar #---cut here---cut here---cut here---cut here---cut here--- #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files. # This archive created: Sat Jan 14 04:04:03 MST 1989 export PATH; PATH=/bin:$PATH echo shar: extracting "'statgene.c'" '(17969 characters)' if test -f 'statgene.c' then echo shar: will not over-write existing file "'statgene.c'" else sed 's/^X//' << \SHAR_EOF > 'statgene.c' X X#ifndef lint X static char sccsid[] = " %M% %I% "; X static char dateid[] = " %E% "; X#endif X X/*@#=========================================================================* X@@# X@@@#@^ statgene.c X@# X@# Purpose: X@@# Read whale files to create and print a table of the relative counts X@@# of the 64 types of huntgenes and 64 types of feedgenes X@# X@#@^Examples: X@#@^ statgene whl1204 X@#@^ statgene whl1204 whl1205 whl1206 X@#@^ statgene `cat whfile` , where whfile is a file containing a list of X@#@^ whale files to process. X@#@^ X@# Compiling: X@# cc -O statgene.c -o statgene X@# X@# Function: X@# void main(); X@# X@# General Comments: X@# Output table shows the relative gene frequencies in the whales of the X@# input files. Gives a chance to look at which genes are originally X@# randomly created, and which are getting selected as time goes on. X@# Of course, since in most breeds of whales, the genes are chunked X@# together within chromosomes, this program usually can give only an X@# overview of tendencies, rather than an absolute measure of gene X@# success. X@# X@# Note: The output table is in six columns. A number column from 0-15, X@# Four gene count columns, and a totalizing column. The gene columns X@# are from left to right, genes 0-15, 16-31, 32-47, and 48-63. Since X@# each group of 16 genes in each geneset are identical except for X@# rotation, the "same" gene appears in the same place in each column. X@# The totalize column is the total across the four individual columns. X@# X@#---------------------------------------------------------------------------*/ X X/*---------------------------------------------------------------------------* X Top-level Declaration - includes, defines, externs & globals. X*----------------------------------------------------------------------------*/ X X#include <stdio.h> X#define MAXLN 99 X#define TRUE 1 X#define FALSE 0 X X /* GLOBALS */ X static unsigned short maxln = MAXLN - 2; X X/*---------------------------------------------------------------------------* X@ X@@ void main(); Only function in file. X@ X@*---------------------------------------------------------------------------*/ X Xmain(argc, argv) X int argc; X char *argv[]; X{ X FILE *fopen(), *infile; X static char buf[MAXLN]; X static short wh_tot = 0; X short whthisfile = 0; X short ig[16]; X int huntg[64]; X int feedg[64]; X char *test = buf; X int j = 0, k; X X printf("\n"); X j = 1; X while((infile = fopen(argv[j], "r")) != NULL) X { X test = fgets(buf, maxln, infile); X while(test != NULL) X { X if(buf[0] == '#') X { X whthisfile++; X wh_tot++; X test = fgets(buf, maxln, infile); X test = fgets(buf, maxln, infile); X test = fgets(buf, maxln, infile); X sscanf(buf, "%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd", X &ig[0],&ig[1],&ig[2],&ig[3],&ig[4],&ig[5], X &ig[6],&ig[7],&ig[8],&ig[9],&ig[10],&ig[11], X &ig[12],&ig[13],&ig[14],&ig[15]); X for(k = 0; k < 16; k++) X { X switch(ig[k]) X { X case 0: X huntg[0]++; X break; X case 1: X huntg[1]++; X break; X case 2: X huntg[2]++; X break; X case 3: X huntg[3]++; X break; X case 4: X huntg[4]++; X break; X case 5: X huntg[5]++; X break; X case 6: X huntg[6]++; X break; X case 7: X huntg[7]++; X break; X case 8: X huntg[8]++; X break; X case 9: X huntg[9]++; X break; X case 10: X huntg[10]++; X break; X case 11: X huntg[11]++; X break; X case 12: X huntg[12]++; X break; X case 13: X huntg[13]++; X break; X case 14: X huntg[14]++; X break; X case 15: X huntg[15]++; X break; X case 16: X huntg[16]++; X break; X case 17: X huntg[17]++; X break; X case 18: X huntg[18]++; X break; X case 19: X huntg[19]++; X break; X case 20: X huntg[20]++; X break; X case 21: X huntg[21]++; X break; X case 22: X huntg[22]++; X break; X case 23: X huntg[23]++; X break; X case 24: X huntg[24]++; X break; X case 25: X huntg[25]++; X break; X case 26: X huntg[26]++; X break; X case 27: X huntg[27]++; X break; X case 28: X huntg[28]++; X break; X case 29: X huntg[29]++; X break; X case 30: X huntg[30]++; X break; X case 31: X huntg[31]++; X break; X case 32: X huntg[32]++; X break; X case 33: X huntg[33]++; X break; X case 34: X huntg[34]++; X break; X case 35: X huntg[35]++; X break; X case 36: X huntg[36]++; X break; X case 37: X huntg[37]++; X break; X case 38: X huntg[38]++; X break; X case 39: X huntg[39]++; X break; X case 40: X huntg[40]++; X break; X case 41: X huntg[41]++; X break; X case 42: X huntg[42]++; X break; X case 43: X huntg[43]++; X break; X case 44: X huntg[44]++; X break; X case 45: X huntg[45]++; X break; X case 46: X huntg[46]++; X break; X case 47: X huntg[47]++; X break; X case 48: X huntg[48]++; X break; X case 49: X huntg[49]++; X break; X case 50: X huntg[50]++; X break; X case 51: X huntg[51]++; X break; X case 52: X huntg[52]++; X break; X case 53: X huntg[53]++; X break; X case 54: X huntg[54]++; X break; X case 55: X huntg[55]++; X break; X case 56: X huntg[56]++; X break; X case 57: X huntg[57]++; X break; X case 58: X huntg[58]++; X break; X case 59: X huntg[59]++; X break; X case 60: X huntg[60]++; X break; X case 61: X huntg[61]++; X break; X case 62: X huntg[62]++; X break; X case 63: X huntg[63]++; X break; X } X } X test = fgets(buf, maxln, infile); X sscanf(buf, "%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd%hd", X &ig[0],&ig[1],&ig[2],&ig[3],&ig[4],&ig[5], X &ig[6],&ig[7],&ig[8],&ig[9],&ig[10],&ig[11], X &ig[12],&ig[13],&ig[14],&ig[15]); X for(k = 0; k < 16; k++) X { X switch(ig[k]) X { X case 0: X feedg[0]++; X break; X case 1: X feedg[1]++; X break; X case 2: X feedg[2]++; X break; X case 3: X feedg[3]++; X break; X case 4: X feedg[4]++; X break; X case 5: X feedg[5]++; X break; X case 6: X feedg[6]++; X break; X case 7: X feedg[7]++; X break; X case 8: X feedg[8]++; X break; X case 9: X feedg[9]++; X break; X case 10: X feedg[10]++; X break; X case 11: X feedg[11]++; X break; X case 12: X feedg[12]++; X break; X case 13: X feedg[13]++; X break; X case 14: X feedg[14]++; X break; X case 15: X feedg[15]++; X break; X case 16: X feedg[16]++; X break; X case 17: X feedg[17]++; X break; X case 18: X feedg[18]++; X break; X case 19: X feedg[19]++; X break; X case 20: X feedg[20]++; X break; X case 21: X feedg[21]++; X break; X case 22: X feedg[22]++; X break; X case 23: X feedg[23]++; X break; X case 24: X feedg[24]++; X break; X case 25: X feedg[25]++; X break; X case 26: X feedg[26]++; X break; X case 27: X feedg[27]++; X break; X case 28: X feedg[28]++; X break; X case 29: X feedg[29]++; X break; X case 30: X feedg[30]++; X break; X case 31: X feedg[31]++; X break; X case 32: X feedg[32]++; X break; X case 33: X feedg[33]++; X break; X case 34: X feedg[34]++; X break; X case 35: X feedg[35]++; X break; X case 36: X feedg[36]++; X break; X case 37: X feedg[37]++; X break; X case 38: X feedg[38]++; X break; X case 39: X feedg[39]++; X break; X case 40: X feedg[40]++; X break; X case 41: X feedg[41]++; X break; X case 42: X feedg[42]++; X break; X case 43: X feedg[43]++; X break; X case 44: X feedg[44]++; X break; X case 45: X feedg[45]++; X break; X case 46: X feedg[46]++; X break; X case 47: X feedg[47]++; X break; X case 48: X feedg[48]++; X break; X case 49: X feedg[49]++; X break; X case 50: X feedg[50]++; X break; X case 51: X feedg[51]++; X break; X case 52: X feedg[52]++; X break; X case 53: X feedg[53]++; X break; X case 54: X feedg[54]++; X break; X case 55: X feedg[55]++; X break; X case 56: X feedg[56]++; X break; X case 57: X feedg[57]++; X break; X case 58: X feedg[58]++; X break; X case 59: X feedg[59]++; X break; X case 60: X feedg[60]++; X break; X case 61: X feedg[61]++; X break; X case 62: X feedg[62]++; X break; X case 63: X feedg[63]++; X break; X } X } X } X test = fgets(buf, maxln, infile); X } X printf("This file: whales = %h4d\n", whthisfile); X whthisfile = 0; X fclose(infile); X j++; X } X printf("All files: wh_tot =%h5d\n", wh_tot); X printf("\n"); X printf("Huntgenes: 0-15 16-31 32-47 48-63 Total\n"); X printf("\n"); X for(j = 0; j < 16; j++) X { X printf("%5d: %6d %6d %6d %6d %8d\n", X j, huntg[j], huntg[j+16], huntg[j+32], huntg[j+48], X huntg[j] + huntg[j+16] + huntg[j+32] + huntg[j+48]); X } X printf("\n"); X printf("Feedgenes: 0-15 16-31 32-47 48-63 Total\n"); X printf("\n"); X for(k = 0; k < 16; k++) X { X printf("%5d: %6d %6d %6d %6d %8d\n", X k, feedg[k], feedg[k+16], feedg[k+32], feedg[k+48], X feedg[k] + feedg[k+16] + feedg[k+32] + feedg[k+48]); X } X printf("\n"); X printf("All files: whale total =%h5d\n", wh_tot); X printf("\n"); X} SHAR_EOF if test 17969 -ne "`wc -c < 'statgene.c'`" then echo shar: error transmitting "'statgene.c'" '(should have been 17969 characters)' fi fi # end of overwriting check echo shar: extracting "'whpl.h'" '(11048 characters)' if test -f 'whpl.h' then echo shar: will not over-write existing file "'whpl.h'" else sed 's/^X//' << \SHAR_EOF > 'whpl.h' X X/*@--------------------------------------------------------------------------* X@* X@* Copyright (c) 1987, 1988 Bob Loy X@* X@* Please don't try to make money from whpl.c or associated files whpl.h, X@* whx.c, genes.c, whio.c, or rando.c. Also, don't be changing my X@* name in this notice. X@* Permission is otherwise granted to the user to freely delete or modify X@* or add to any of the code in the above-named source files. X@* X@* In other words, Experiment! X@* X@*---------------------------------------------------------------------------*/ X#ifndef lint X static char hdsccsid[] = " whpl.h 1.12 "; X static char hddateid[] = " 88/12/16 "; X#endif X/*@#==========================================================================* X@@# X@@@# whpl.h X@#@^ X@# Purpose: X@@# Include file for whx.c, whpl.c, genes.c & whio.c X@# X@# Compiling: X@# cc -O whx.o whpl.o genes.o whio.o rando.o -o whx -lm X@# or: X@# cc -O -DSUN whx.o whpl.o genes.o whio.o rando.o -o whx -lm -lpixrect X@# (for display on Sun workstations like the 3/50) X@# X@# Included in: X@# whx.c, whpl.c, genes.c, whio.c X@# X@# X@# General Comments: X@# X@# "Whales & Plankton" (whpl) is a program which models a small ecology X@# and is a study of genetic algorithms. X@# X@# The inspiration for it comes from two of A. K. Dewdney's Scientific X@# American Computer Recreation columns: "Sharks and Fish Wage an Ecological X@# War on the Toroidal Planet Wa-Tor" of December, 1984, and from November, X@# 1985, "Exploring the Field of Genetic Algorithms in a Primordial Computer X@# Sea Full of Flibs" [Finite LIving BlobS]. X@# X@# For this program, the finny inhabitants of the planet Wa-Tor have been X@# replaced: The fish by very dumb plankton (they just float there and X@# multiply), and by whales who, if they are not 'smart' enough at finding X@# plankton, get replaced by decendents who are. The whales originally have X@# randomly loaded gene sets which guide their movements over the two dimen- X@# sional array looking for, and eating, the plankton. Periodically, the X@# whales mate and produce offspring by mixing different genes from different X@# parents. I include the Darwinian pressure of only allowing the most suc- X@# cessful whales to procreate. X@# X@# The whpl system of programs is self-documenting; the character '@' is X@# used as a key: X@# A search for a string of three @'s will find the lines with the source X@# file names. X@# A search for a string of two @'s will also get function names and a X@# short description of each function. X@# And a search for the single "@" will also find all the other comment X@# lines at the tops of files and at the tops of individual functions. X@# So a utility like the Unix "grep", for example, can be used to pull X@# out the "overview" comments from one or more of the whpl-associated files. X@# There are also some special-purpose keys which are a subset of the X@# above examples: X@# Search for "@#" to find only the top-of-file comments. X@# Search for "@^" to find comments which apply to input and output. X@# X@# See comments at top of whx.c for how to compile & run the program; see X@# comments at top of whio.c for command line examples and comments about X@# file I/O; see comments in genes.c for explanations of breeds and genes; X@# and see individual functions for more specific comments. X@# X@#---------------------------------------------------------------------------*/ X X/*---------------------------------------------------------------------------* X Top-level Declaration - includes, defines & structures. X*----------------------------------------------------------------------------*/ X X#include <stdio.h> X#include <math.h> X X#ifdef SUN X#include <pixrect/pixrect_hs.h> X#endif X X/* Constants */ X X#define FILVER 1 /* File Version re: fileout() */ X#define DATRUN 2088120000 /* Date-run */ X#define GENNUM 1000000000 /* Generation number */ X#define MUTATE 1000000000 /* Future ID field */ X#define AREA 250000 /* Size of location array */ X#define VBIAS 500 /* Vertical Bias of location array */ X#define HBIAS 1 /* Horizontal Bias of location array */ X#define STARTX 0 /* X-coord of top left pixel of display */ X#define STARTY 0 /* Y-coord of top left pixel of display */ X#define NUMWH 25 /* Number of whales to start with */ X#define MONTHS 30 /* Number of months for one cycle */ X#define SAFETY 25 /* Maximum number of cycles per run */ X#define MVGRP 2500 /* Moves per whale per month */ X#define PLUS 3 /* Whale energy increase when feeds */ X#define MINUS 1 /* Whale energy decrease when doesn't */ X#define MAXGMV 4 /* Size of mvwhle[]; moves / gene firing */ X#define MALIM 4 /* Max males females may attract to mate */ X#define INBREED1 1 /* Inbreeding level; 0 or 1 ] The */ X#define INBREED2 1 /* Inbreeding level; 0 or 1 ] lower, */ X#define TOCLONE 0 /* Cloning level; 0, 1 or 2 ] the less X likely to happen */ X#define DIELIM 0 /* Chances to die (0=dynamic'ly changed) */ X#define INGENTYP "m4m4zz" /* Gene string to init whale hunt, feed, */ X /* count, and extra genes */ X#define HGPERCHR 1 /* Is set >=largest whale.hgperchr in run */ X#define FGPERCHR 1 /* Is set >=largest whale.fgperchr in run */ X#define COUNTVAL 4 /* Presently used in place of countgene */ X#define NORMAL 0 /* If 1, number of all whales' moves are */ X /* approx equal, else indiv whale sets */ X#define BRKGENE 0 /* 0 or 1; 1 = exit gene as soon as */ X /* hunt/feed mode changes */ X#define BRKCHRMO 1 /* ...Same for chromosomes; not used in */ X /* v1.10: defacto setting = 1 */ X#define MAXWH 254 /* Maximum number of whales program can */ X /* deal with at any one time */ X#define INTERLIM 150 /* Print interim files when whales reach */ X#define FNMTRUNC 4 /* Remove 1st chars of DATRUN for fnames */ X#define TRUE 1 /* Boolian */ X#define FALSE 0 /* Boolian */ X#define Uint unsigned int /* Shortens some of the typing */ X#define Ushort unsigned short /* Shortens some of the typing */ X#define Uchar unsigned char /* Shortens some of the typing */ X#define Ulong unsigned long /* Shortens some of the typing */ X#define MAXSHORT 32767 /* Sun short of 16 bits */ X#define MAXLONG 2147483647 /* Sun long of 32 bits */ X#define MAXINT MAXLONG /* Sun int of 32 bits */ X#define MAXUSHORT 65534 /* Unsigned short of 16 bits */ X#define MAXULONG 4294967294 /* Unsigned long of 32 bits */ X#define MAXUINT MAXULONG /* Unsigned int of 32 bits */ X X/* Macros */ X X#define MAX(x,y) ((x) > (y) ? (x) : (y)) X#define MIN(x,y) ((x) < (y) ? (x) : (y)) X#define AVE(x,y) (((x) + (y)) / 2) X X/* Structure definition for record used in sorting whales by energy: */ X X typedef struct X { X int ener; /* Energy of whale */ X short whal; /* Whale number in order in file */ X } energarr; X X/* Structure definitions for records of whales: @*/ X/*@*/ X typedef struct /* Whale ID record @*/ X { /* @*/ X Uint datrun; /* = DATRUN + (command line opt) run @*/ X Uint gennum; /* = GENNUM + year * 10000 + file sequence @*/ X Uint mutate; /* = Unused in whpl.c version 1.10 @*/ X char gns[7]; /* = hunttype, hgperchr, feedtype, fgperchr, @*/ X /* countgene, & extragene fields, below @*/ X } whalid; /* @*/ X/*@*/ X typedef struct /* Success rec, all fields based on all whales @*/ X { /* read in or created in a particular run @*/ X short diff; /* Difficulty; ancesavs of whales / totl @*/ X short totl; /* Total number of whales @*/ X short rank; /* Percential rank at end of run @*/ X } success; /* @*/ X/*@*/ X typedef struct /* Main whale record @*/ X { /* @*/ X int sortfield; /* Unused, available for user @*/ X int energy; /* Truly a life or death matter! @*/ X int enrec[20]; /* Ending energy records through consec runs @*/ X short endex; /* Next open enrec[] record @*/ X short die; /* Num times whale has been below die level @*/ X int locatn; /* Location of whale in Wa-Tor array @*/ X int age; /* Number of mating cycles survived @*/ X char sex; /* Occasionally (seriously, Male or Female) @*/ X int offratio; /* Ratio of offspring to age @*/ X int rating; /* A cumulative rating of whale's robustness @*/ X int lastrat; /* Rating from most recent run @*/ X success comp[20]; /* See success struct, above @*/ X int ancesave; /* Average depth of ancestry @*/ X short ancesmax; /* Maximum depth of ancestry @*/ X short ancesmin; /* Minimum depth of ancestry @*/ X whalid father; /* ID of father @*/ X whalid mother; /* ID of mother @*/ X whalid self; /* ID of self @*/ X whalid offspr[50]; /* ID's of children @*/ X short childex; /* Next open offspr[] record @*/ X short allchldn; /* All children ever spawned @*/ X char hunttype; /* Hunt genes type @*/ X Uchar hgperchr; /* Hunt genes per chromosome @*/ X char feedtype; /* Feed genes type @*/ X Uchar fgperchr; /* Feed genes per chromosome @*/ X short huntgene[16]; /* Array of hunt genes @*/ X short huntdex; /* Index into huntgene[] @*/ X short feedgene[16]; /* Array of feed genes @*/ X short feeddex; /* Index into feedgene[] @*/ X Uchar countgene; /* Count gene value (not used ver 1.10) @*/ X Uchar extragene; /* User definable gene (not used ver 1.10) @*/ X int lastfed; /* Number of moves since last ate plankton @*/ X char huntfeed; /* Switch: hunting or feeding? @*/ X } whale_type; /* @*/ X/*@*/ SHAR_EOF if test 11048 -ne "`wc -c < 'whpl.h'`" then echo shar: error transmitting "'whpl.h'" '(should have been 11048 characters)' fi fi # end of overwriting check # End of shell archive exit 0 --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Bob Loy | Life is the detour you take on the ...!sun!sunburn!gtx!loy | way to where you're really going. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~