Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/17/84; site mck-csc.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!mck-csc!bmg
From: bmg@mck-csc.UUCP (Bernard M. Gunther)
Newsgroups: net.sources
Subject: flibs from Sci Amer
Message-ID: <155@mck-csc.UUCP>
Date: Tue, 29-Oct-85 11:55:43 EST
Article-I.D.: mck-csc.155
Posted: Tue Oct 29 11:55:43 1985
Date-Received: Wed, 30-Oct-85 07:40:46 EST
Distribution: net
Organization: McKinsey & Company, Cambridge Systems Center
Lines: 282
This is my implementation of flibs (see the Scientific American, This is my implementation of flibs (see the Scientific American,
November 1985, Computer Recreations column). It is fully parameterized
and will work on a PC also. If you are going to compile it on a
PC remember to declare malloc (char *malloc()).
I have another few versions including one for the PC only which includes
a graphic display of all the flibs which are currently alive and another
version which is being tested which has a few patterns and the idea is
to accept or reject them. If anyone out there is interested, I'd be
willing to post these or mail them to you.
This is just a single file. A standard invoking of the program would
be:
flibs -n10 -s4 -p6 100101
which says:
10 flibs
4 states per flib
pattern length of 6
pattern of 100101
Feel free to mail me questions or comments. Hope you like it.
Bernie Gunther
ihnp4!mit-eddie!mck-csc!bmg
#!/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:
# flibs.c
# This archive created: Tue Oct 29 11:45:39 1985
export PATH; PATH=/bin:$PATH
if test -f 'flibs.c'
then
echo shar: over-writing existing file "'flibs.c'"
fi
cat << \SHAR_EOF > 'flibs.c'
/* This is a flibs program as taken from Scientific American, Nov. 1985 */
#include
int nflibs = 10;
int nstates = 4;
int psize = 6;
int finish = 100;
int iternum = 100;
int buckets = 20;
int quick = 0;
int mcount = 1;
int npatterns = 1;
int pbreed = 30;
int *chrom; /* [nflibs][ nstates*4]; */
int *state; /* [nflibs]; */
int *score; /* [nflibs]; */
int *pattern; /* [psize]; */
int *hist; /* for histogram */
int max,min; /* min/max check values */
int top,bot; /* holder of position */
int cmax, cmin;
main(argc, argv)
int argc;
char *argv[];
{
int i,j;
int generation =0;
int sinput;
char c;
char *s;
sinput = 1;
while ( sinput < argc && *argv[sinput] == '-') {
c = *(argv[sinput]+1);
switch(c) {
case 'n':
nflibs = atoi( argv[sinput]+2);
break;
case 's':
nstates = atoi( argv[sinput]+2);
break;
case 'p':
psize = atoi( argv[sinput]+2);
break;
case 'f':
finish = atoi(argv[sinput]+2);
break;
case 'i':
iternum = atoi(argv[sinput]+2);
break;
case 'q':
quick = 1;
break;
case 'b':
buckets = atoi(argv[sinput]+2);
break;
case 'c':
mcount = atoi(argv[sinput]+2);
break;
case 'm':
pbreed = atoi(argv[sinput]+2);
break;
default:
break;
}
sinput++;
}
if (argc < npatterns+sinput) {
printf(
"Flibs [-n number of flibs] [-s number of states] [-p pattern size]\n");
printf(" [-f finish threshhold] [-i number of iterations per test] [-quick]\n");
printf(" [-b buckets] [-c how many must solve to finish] [-m % chance to mate ]\n");
printf(" pattern \n");
exit(1);
}
printf("starting \n");
init();
for (j=0; j 35)
printf("*");
else if (hist[i] > 9)
printf("%c", (char)('A'+hist[i]-10));
else printf("%d",hist[i]);
}
printf("\n");
}
printf("finished \n");
}
init()
{
int i,j;
extern long time();
chrom = (int *)malloc( nflibs*(nstates*4) *sizeof(i));
state = (int *)malloc(nflibs * sizeof(i)); /* [nflibs]; */
score = (int *)malloc(nflibs * sizeof(i)); /* [nflibs]; */
pattern = (int*)malloc(psize *sizeof(i)); /* [psize]; */
hist = (int*) malloc((buckets+1)*sizeof(i));
srand( (unsigned) time( (long *) 0));
for (i=0;i nstates) printf("error!! stat = %d i=%d j=%d l=%d \n",stat,i,j,l);
}
}
}
check()
{
int i,x;
min = iternum;
max = 0;
cmin = 0;
cmax = 0;
for( i =0; i max) {
max = score[i];
top = i;
cmax = 0;
}
if (score[i] < min) {
min = score[i];
bot = i;
cmin = 0;
}
}
for (i=0; ibuckets)?buckets:x]++;
}
if (cmax >1) {
i = getrand(cmax)+1;
for (x=0; x1) {
i = getrand(cmin)+1;
for (x=0; x