allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (07/19/89)
Posting-number: Volume 7, Issue 83 Submitted-by: loy@gtx.UUCP (Bob Loy) Archive-name: whpl/part10 # whpl10of13.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:05:04 MST 1989 export PATH; PATH=/bin:$PATH echo shar: extracting "'dealwh.c'" '(13358 characters)' if test -f 'dealwh.c' then echo shar: will not over-write existing file "'dealwh.c'" else sed 's/^X//' << \SHAR_EOF > 'dealwh.c' X X#ifndef lint X static char sccsid[] = " %M% %I% "; X static char dateid[] = " %E% "; X#endif X X/*@#=========================================================================* X@@# X@@@#@^ dealwh.c X@# X@# Purpose: X@@# Deal the whales from an input file to mutiple output files. X@# X@#@^Options: X@#@^ -i input file name (required option) X@#@^ -n number of output files up to 16 (required option) X@#@^ X@# Examples: X@# dealwh -i whl -n 8 X@# Opens input file "whl" and deals whales out as evenly as possible X@# to the eight files "whxa" "whxb" "whxc" "whxd" "whxe" "whxg" X@# "whxh" and "whxi" X@# X@# Compiling: X@# cc -O dealwh.c -o dealwh X@# X@# Functions: X@# void open_outfiles(); X@# void close_outfiles(); X@# void write_outheads(); X@# void write_outfile(); X@# void pick_outfile(); X@# void cmd_line(); X@# void main(); X@# X@# General Comments: X@# Header is copied intact from the input file to the output files, so X@# the Number & Live fields will be incorrect in the output files. X@# Output filenames "whxf" and "whxm" are not created to avoid conflict X@# with filenames which may be created by utility sortcount.c. Output X@# filename "whxq" is not created because it is hard to type. The X@# 'highest' filename which may be created (n = 16) is "whxs". X@# There is no checking to see if the output files were opened properly. 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 /* Functions From libraries: */ X X /* GLOBALS */ X static unsigned short maxln = MAXLN - 2; X static char whinstr[MAXLN]; X static char *test = whinstr; X static char infname[40]; X static int numf = 0; X static short infileflg = FALSE; X static FILE *infile; X static FILE *out01; X static FILE *out02; X static FILE *out03; X static FILE *out04; X static FILE *out05; X static FILE *out06; X static FILE *out07; X static FILE *out08; X static FILE *out09; X static FILE *out10; X static FILE *out11; X static FILE *out12; X static FILE *out13; X static FILE *out14; X static FILE *out15; X static FILE *out16; X X X/*---------------------------------------------------------------------------* X@ X@@ void open_outfiles(); Opens output files based on -n cmnd line value. X@ caller: main() X@ calls : fopen() X@ proc : case statements fall through on purpose. X@ X@*---------------------------------------------------------------------------*/ X Xvoid Xopen_outfiles() X{ X switch(numf) X { X case 16: X out16 = fopen("whxs", "w"); X fprintf(stderr, " Output file: \"whxs\" opened.\n"); X case 15: X out15 = fopen("whxr", "w"); X fprintf(stderr, " Output file: \"whxr\" opened.\n"); X case 14: X out14 = fopen("whxp", "w"); X fprintf(stderr, " Output file: \"whxp\" opened.\n"); X case 13: X out13 = fopen("whxo", "w"); X fprintf(stderr, " Output file: \"whxo\" opened.\n"); X case 12: X out12 = fopen("whxn", "w"); X fprintf(stderr, " Output file: \"whxn\" opened.\n"); X case 11: X out11 = fopen("whxl", "w"); X fprintf(stderr, " Output file: \"whxl\" opened.\n"); X case 10: X out10 = fopen("whxk", "w"); X fprintf(stderr, " Output file: \"whxk\" opened.\n"); X case 9: X out09 = fopen("whxj", "w"); X fprintf(stderr, " Output file: \"whxj\" opened.\n"); X case 8: X out08 = fopen("whxi", "w"); X fprintf(stderr, " Output file: \"whxi\" opened.\n"); X case 7: X out07 = fopen("whxh", "w"); X fprintf(stderr, " Output file: \"whxh\" opened.\n"); X case 6: X out06 = fopen("whxg", "w"); X fprintf(stderr, " Output file: \"whxg\" opened.\n"); X case 5: X out05 = fopen("whxe", "w"); X fprintf(stderr, " Output file: \"whxe\" opened.\n"); X case 4: X out04 = fopen("whxd", "w"); X fprintf(stderr, " Output file: \"whxd\" opened.\n"); X case 3: X out03 = fopen("whxc", "w"); X fprintf(stderr, " Output file: \"whxc\" opened.\n"); X case 2: X out02 = fopen("whxb", "w"); X fprintf(stderr, " Output file: \"whxb\" opened.\n"); X case 1: X out01 = fopen("whxa", "w"); X fprintf(stderr, " Output file: \"whxa\" opened.\n"); X break; X default: X fprintf(stderr, "\nopen_outfiles(): switch error\n"); X } X} X X/*---------------------------------------------------------------------------* X@ X@@ void close_outfiles(); Closes output files based on -n cmnd line value. X@ caller: main() X@ calls : fclose() X@ proc : case statements fall through on purpose. X@ X@*---------------------------------------------------------------------------*/ X Xvoid Xclose_outfiles() X{ X switch(numf) X { X case 16: X fclose(out16); X case 15: X fclose(out15); X case 14: X fclose(out14); X case 13: X fclose(out13); X case 12: X fclose(out12); X case 11: X fclose(out11); X case 10: X fclose(out10); X case 9: X fclose(out09); X case 8: X fclose(out08); X case 7: X fclose(out07); X case 6: X fclose(out06); X case 5: X fclose(out05); X case 4: X fclose(out04); X case 3: X fclose(out03); X case 2: X fclose(out02); X case 1: X fclose(out01); X break; X default: X fprintf(stderr, "\nclose_outfiles(): switch error\n"); X } X} X X/*---------------------------------------------------------------------------* X@ X@@ void write_outheads(); Writes header information to output files. X@ caller: main() X@ calls : fclose() X@ proc : case statements fall through on purpose. X@ X@*---------------------------------------------------------------------------*/ X Xvoid Xwrite_outheads() X{ X switch(numf) X { X case 16: X fputs(whinstr, out16); X case 15: X fputs(whinstr, out15); X case 14: X fputs(whinstr, out14); X case 13: X fputs(whinstr, out13); X case 12: X fputs(whinstr, out12); X case 11: X fputs(whinstr, out11); X case 10: X fputs(whinstr, out10); X case 9: X fputs(whinstr, out09); X case 8: X fputs(whinstr, out08); X case 7: X fputs(whinstr, out07); X case 6: X fputs(whinstr, out06); X case 5: X fputs(whinstr, out05); X case 4: X fputs(whinstr, out04); X case 3: X fputs(whinstr, out03); X case 2: X fputs(whinstr, out02); X case 1: X fputs(whinstr, out01); X break; X default: X fprintf(stderr, "\nwrite_outheads(): switch error\n"); X } X} X X/*---------------------------------------------------------------------------* X@ X@@ void write_outfile(); Writes out present whale to an output file, and X@@ finds next whale in input file. X@ caller: pick_outfile() X@ calls : fgets(), fputs() X@ X@*---------------------------------------------------------------------------*/ X Xvoid Xwrite_outfile(filep) X FILE *filep; X{ X do{ X fputs(whinstr, filep); X test = fgets(whinstr, maxln, infile); X if(whinstr[0] == '#') X break; X } while(test != NULL); X} X X/*---------------------------------------------------------------------------* X@ X@@ void pick_outfile(which); Picks which output file gets next whale. X@ caller: main() X@ calls : write_outfile() X@ X@*---------------------------------------------------------------------------*/ X Xvoid Xpick_outfile(parm) X int parm; X{ X switch(parm) X { X case 16: X write_outfile(out16); X break; X case 15: X write_outfile(out15); X break; X case 14: X write_outfile(out14); X break; X case 13: X write_outfile(out13); X break; X case 12: X write_outfile(out12); X break; X case 11: X write_outfile(out11); X break; X case 10: X write_outfile(out10); X break; X case 9: X write_outfile(out09); X break; X case 8: X write_outfile(out08); X break; X case 7: X write_outfile(out07); X break; X case 6: X write_outfile(out06); X break; X case 5: X write_outfile(out05); X break; X case 4: X write_outfile(out04); X break; X case 3: X write_outfile(out03); X break; X case 2: X write_outfile(out02); X break; X case 1: X write_outfile(out01); X break; X default: X fprintf(stderr, "\nwrite_outheads(): switch error\n"); X } X} X X/*---------------------------------------------------------------------------* X@ X@@ void cmd_line(argc, argv); Decipher command line arguments. X@ output: Sets flags & loads values into variables X@ caller: main() X@ calls : getopt(), may call exit() X@ X@*---------------------------------------------------------------------------*/ X Xvoid Xcmd_line(argc, argv) X int argc; X char *argv[]; X{ X extern int optind; X extern char *optarg; X int cc; X int run; X X /* check command line arguments */ X while ((cc = getopt(argc, argv, "i:n:")) != EOF) X switch(cc) X { X case 'i': X case 'I': X strncpy(infname, optarg, 39); X infileflg = TRUE; X break; X case 'n': X case 'N': X numf = atoi(optarg); X break; X default: X fprintf(stderr, "\n"); X fprintf(stderr, "usage: "); X fprintf(stderr, X "symmet -i<input file name>\n"); X fprintf(stderr, X " -n<number ofoutput file names>\n"); X fprintf(stderr, "\n"); X exit(2); X break; X } X} X X/*---------------------------------------------------------------------------* X@ X@@ void main(argc, argv); Calling function for dealwh.c X@ input : Command line arguments. X@ calls : cmd_line(), whaleread(); malloc(), fopen(), exit() X@ X@*---------------------------------------------------------------------------*/ X Xvoid Xmain(argc, argv) X int argc; X char *argv[]; X{ X int j; X short foundin = 0; X X fprintf(stdout, "\n"); X cmd_line(argc, argv); /* Handle command line arguments */ X if (!numf) X { X fprintf(stderr, "\n Need to use -n option to specify\n"); X fprintf(stderr, " number of output files to deal to.\n"); X fprintf(stderr, " Program exiting.\n\n"); X exit(1); X } X open_outfiles(); X if (infileflg) /* Read one input whale file */ X { X if ((infile = fopen(infname, "r")) != NULL) X { X while ((test = fgets(whinstr, maxln, infile)) != NULL) X { X if (whinstr[0] == '#') X { X test = fgets(whinstr, maxln, infile); X if (whinstr[0] == 'w' && whinstr[1] == 'h') X { X test = NULL; X break; X } X else X { X fprintf(stderr, "\n Bad Input Whale File\n"); X fprintf(stderr, " Program exiting.\n\n"); X close_outfiles(); X exit(1); X } X } X write_outheads(numf); X if (whinstr[2] == 'I' && whinstr[3] == 'N') X { X test = fgets(whinstr, maxln, infile); X if (whinstr[4] == 'N' && whinstr[5] == 'u') X { X write_outheads(numf); X foundin++; X break; X } X } X } X if (test == NULL) X rewind(infile); X } X else X { X fprintf(stderr, "\n WARNING: did not open input file %s\n\n", X infname); X fprintf(stderr, " Program exiting.\n\n"); X close_outfiles(); X exit(1); X } X } X else X { X fprintf(stderr, "\n No INPUT FILE specified on command line\n"); X fprintf(stdout, " Program exiting.\n\n"); X close_outfiles(); X exit(1); X } X if (!foundin) X { X fprintf(stderr, "\n Warning: not a standard Whale File\n"); X } X /* Read in whales from file if proper flags set */ X while ((test = fgets(whinstr, maxln, infile)) != NULL) X { X if (whinstr[0] == '#') X break; X write_outheads(numf); X } X j = 0; X do{ X j++; X if (j > numf) X j = 1; X pick_outfile(j); X } while(test != NULL); X close_outfiles(); X fclose(infile); X fprintf(stdout, "\nDONE\n\n"); X} SHAR_EOF if test 13358 -ne "`wc -c < 'dealwh.c'`" then echo shar: error transmitting "'dealwh.c'" '(should have been 13358 characters)' fi fi # end of overwriting check echo shar: extracting "'genes.c'" '(13891 characters)' if test -f 'genes.c' then echo shar: will not over-write existing file "'genes.c'" else sed 's/^X//' << \SHAR_EOF > 'genes.c' 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 sccsid[] = " genes.c 1.10 "; X static char dateid[] = " 88/12/13 "; X#endif X/*@#==========================================================================* X@@#{ X@@@# genes.c X@# X@# Purpose: X@@# Contains genesets to be accessed by whx.c X@# X@# Compiling: X@# cc -O genes.c -c X@# X@# Dependency: X@# whpl.h, whx.c, whpl.c, rando.c X@# X@# Subordinate to: X@# whx.c X@# X@# Function: X@# void genesets(); X@# presently contains sets 'a', 'c', 'i', 'm' & 'o' X@# (whpl.c, v1.10) X@# X@# General Comments: X@# Also see comments in genesets(), below, and in movewhale() in whpl.c. X@# The genes of the whales in whpl are the most important feature of the X@# program. They provide for the individiation of the whales, making X@# each one move over the Wa-Tor array in a unique way. Each whale has X@# a set of 16 hunt genes and a set of 16 feed genes, e.g., X@# 63 40 35 36 50 49 49 37 41 52 37 18 23 19 42 34 <- hunt gene set X@# 0 47 3 44 30 25 39 31 62 33 5 43 24 63 44 58 <- feed gene set X@# which are used to index the switch statements in genesets(), below. X@# These genes are either randomly assigned (for newly created whales), X@# or inherited from parent whales in various mixtures. X@# More on genes later, but first perhaps it's a good idea to look at some X@# other data in the whale structure which which might be refered to X@# as defining a whale's "breed." This data shows up as part of the X@# whale's unique ID in whale files (e.g., "a1m@zz"), and also as X@# structure (record) fields. Here's a sample mapping: X@# X@# Character Allowable Field in Decoded X@# from characters whale values in X@# whale ID in string structure struc-field X@# ---------- ------------ ---------- ---------------- X@# a P-ASCII * .hunttype no change X@# 1 0,1,2,4,8,@ .hgperchr 0,1,2,4,8,16 ** X@# m P-ASCII * .feedtype no change X@# @ 0,1,2,4,8,@ .fgperchr 0,1,2,4,8,16 ** X@# z 'a' - 'z' .countgene ] a thru y = 1 *** X@# z 'a' - 'z' .extragene ] thru 25, z = 0 X@# X@# * Printable ASCII X@# ** Actually stored as unsigned char, but char '0' is always X@# subtracted from value when used in program. X@# *** Function geneval() in whpl.c does the correct decoding X@# X@# These breed sets are assigned to newly created whales from the constant X@# INGENTYP defined in whpl.h or from the "-g" command line option. X@# They are also inheritable. Countgene and extragene are not used in X@# v1.10, so the value 'z' should be used in their assignment for now. X@# The original plan for countgene was to determine, on an individual X@# whale basis, how many moves of finding no food to wait before X@# returning to hunt mode. Presently the constant COUNTVAL determines X@# this for all whales in a given run. Extragene is just that, an extra X@# gene for custom programming (perhaps as a complement to countgene?). X@# The other four characters are actually two pairs, and affect either the X@# 16 hunt genes or the 16 feed genes, respectively. For this example, X@# the whales' breed would be hunt type 'a', with 1 gene per chromosome, X@# and feed type 'm', with 16 genes per chromosome. Any ASCII value X@# can be used for the type field, but the user will have to make his X@# own gene sets for other than the types a,c,i,m,o already supplied X@# in v1.10. There is sort of a naming convention in place for types X@# named with lower-case letters (see below). X@# A very important consideration in how the genes control whale movement X@# is that the genes can be 'chunked' into chromosomes. For whale X@# movement, the chromosome is randomly selected, and all the genes X@# which make up that chromo are stepped through one at a time. So a X@# given chromosome for a given whale always produces the same series X@# of moves. Of course, the genes themselves are 'chunks' of location X@# changes on the contiguous 2-D "Wa-Tor" torroid (the locarr[] array). X@# Some genes map to just one change, while others map into as many as X@# four (or more: see the comments in function genesets() below). X@# The .hgperchr & .fgperchr fields must divide 16 evenly so that the X@# chromosomes will fit into the 16 genes of the hunt or feed sets. X@# Setting the genes per chromo field(s) to 1 is the way to get genes X@# to fire independently. Setting it to 0 has a special effect: It X@# causes that whale's moves to be totally random within it's gene X@# type. This is provided as a control; whales with a 0 in either X@# of the per chromo fields neither breed nor die. X@# For whpl.c, v1.10: There are a total of 13 different types of genes X@# (times 4 rotations) in this file, but no one geneset has all of X@# them. Set 'm' has the most, with 11. Set 'a' is the simplest, X@# consisting of just single moves for all gene firings. So for whales X@# assigned the example breed above, the hunt moves would be very X@# simple, while the feed moves may be quite exotic. X@# Note on a naming convention for whale gene sets (highest case statement X@# level): Any type char value is legitimate to use here, but for now, X@# I have started with lower-case letters: X@# 'a' marks the beginning of sets in which mvwhle[0] = 1. X@# 'c' marks the beginning of sets in which mvwhle[0] <= 2. X@# 'g' marks the beginning of sets in which mvwhle[0] <= 3. X@# 'm' marks the beginning of sets in which mvwhle[0] <= 4. X@# Notes on the order of genes within the 64 case statements of each set: X@# First, each quartile of 16 genes is a duplicate of the others, X@# the only differences being the four possible rotations. For conven- X@# tion, we'll call: X@# cases 0 - 15 = "west" X@# cases 16 - 31 = "north" X@# cases 33 - 47 = "east" X@# cases 48 - 63 = "south" X@# Any gene case with a final location change exactly north-west X@# (e.g., cases 6 thru 9 of geneset 'i'), is included in the west X@# rotation, exactly north-east is in the north rotation, and so on X@# around the circle. X@# Second, The lowest number gene within each rotation set of 16 X@# changes the ultimate location of the whale the most in its partic- X@# ular direction, and the highest the least, based on this formula: X@# For example, in the west direction: X@# total units west / number of steps to get there (mvwhle[0]) X@# ^^^^ignoring any drift north or south. X@# Ties (e.g., 1/2 & 2/4) are ordered somewhat sloppily. X@# No claim is made that this is a formula which will map in any way X@# to the natural selection of either huntgenes or feedgenes, it is X@# just a handy way to order them. X@#} X@#---------------------------------------------------------------------------*/ X X/*---------------------------------------------------------------------------* X Top-level Declaration - includes & externs X*----------------------------------------------------------------------------*/ X X#include "whpl.h" X X /* EXTERNS FROM */ X /* Functions From rando.c: */ X extern long randout(); X X /* Variables From whpl.c: */ X extern int debug; X extern Uint vbias; X extern whale_type *whale; X X X /* EXTERNS TO */ X /* Variables To whx.c: */ X short randflg = FALSE; /* Flag set TRUE for random moves */ X long aiches = 0; /* A debug variable tracking hunt moves */ X long effs = 0; /* A debug variable tracking feed moves */ X int mvwhle[MAXGMV + 1]; /* Array which holds location changes for X one move for one whale */ X X /* Functions To whx.c: */ X void genesets(); X X X/*---------------------------------------------------------------------------* X@ X@@ void genesets(which whale) - Movement controlling switch statements; X@@ called from movewhale() in whpl.c; see also comments there. X@ input : See the externs to whx.c above. X@ output: Fills part of mvwhle[], depending on which gene gets fired. X@ The passed whale's section of moves[] in movewhale() is X@ eventually assigned the locations in mvwhle[]. X@ caller: movewhale() X@ calls : randout(), may call fprintf(). X@ proc : Consists of two levels of switch statements, the top level being X@ the gene set, and the lower level being the individual genes X@ within that set. Both switches normally get their values X@ loaded from structure members of the passed whale pointer. X@ Location mvwhle[0] is set first thing when a particular case X@ statement for gene firing is reached. It contains the number X@ of whale location changes which will be set for that gene. X@ Up to four actual location changes can be stored for each gene X@ firing. This may be increased for new genesets if mvwhle[] is X@ increased in size by increasing the constant MAXGMV in whpl.h. X@ MAXGMV may also be safely decreased (for speed) if whales in X@ a given run only have genetypes which won't use all four X@ locations. X This function has been optimized for running under the Sun Micro X Systems' C compiler. Perhaps separate functions for each X type of gene would be more modular than the switches within X switches below, but these genes are accessed so continually X in the running of whpl that the overhead of such function X calls has a noticeable effect on the run time. X@ X@*---------------------------------------------------------------------------*/ X Xvoid Xgenesets(wich) X int wich; X{ X char type; X short chomp; X register int n; X X if (whale[wich].huntfeed == 'H') X { X type = whale[wich].hunttype; X chomp = whale[wich].huntgene[whale[wich].huntdex]; X aiches++; X } X else if (whale[wich].huntfeed == 'F') X { X type = whale[wich].feedtype; X chomp = whale[wich].feedgene[whale[wich].feeddex]; X effs++; X } X else X { X type = 'a'; X chomp = randout() & 0x3F; X if (debug >= 2) X fprintf(stderr, "genesets(): whale[wich].huntfeed corrupted\n"); X } X if (randflg) X chomp = randout() & 0x3F; X n = whale[wich].locatn; X switch(type) X { X case 'a' : X case 'A' : X switch(chomp) X { X case 0 : X case 1 : X case 2 : X case 3 : X case 4 : X case 5 : X case 6 : X case 7 : X case 8 : X case 9 : X case 10: X case 11: X case 12: X case 13: X case 14: X case 15: X mvwhle[0] = 1; X mvwhle[1] = n - HBIAS; X break; X case 16: X case 17: X case 18: X case 19: X case 20: X case 21: X case 22: X case 23: X case 24: X case 25: X case 26: X case 27: X case 28: X case 29: X case 30: X case 31: X mvwhle[0] = 1; X mvwhle[1] = n - vbias; X break; X case 32: X case 33: X case 34: X case 35: X case 36: X case 37: X case 38: X case 39: X case 40: X case 41: X case 42: X case 43: X case 44: X case 45: X case 46: X case 47: X mvwhle[0] = 1; X mvwhle[1] = n + HBIAS; X break; X case 48: X case 49: X case 50: X case 51: X case 52: X case 53: X case 54: X case 55: X case 56: X case 57: X case 58: X case 59: X case 60: X case 61: X case 62: X case 63: X mvwhle[0] = 1; X mvwhle[1] = n + vbias; X break; X default: X if (debug >= 2) X fprintf(stderr, X "\n genesets(): type: a case: default\n"); X break; X } X break; SHAR_EOF if test 13891 -ne "`wc -c < 'genes.c'`" then echo shar: error transmitting "'genes.c'" '(should have been 13891 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. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~