-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn.c
755 lines (605 loc) · 16.9 KB
/
learn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
* learn.c: Rog-O-Matic XIV (CMU) Sat Jul 5 23:55:06 1986 - mlm
* Copyright (C) 1985 by A. Appel, G. Jacobson, L. Hamey, and M. Mauldin
*
* Genetic learning component.
*
* EDITLOG
* LastEditDate = Sat Jul 5 23:55:06 1986 - Michael Mauldin
* LastFileName = /usre3/mlm/src/rog/ver14/learn.c
*
* HISTORY
* 5-Jul-86 Michael Mauldin (mlm) at Carnegie-Mellon University
* Created.
*/
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include "types.h"
# define TRIALS(g) ((g)->score.count)
# define NONE (-1)
# define MAXM 100
# define ALLELE 100
# define ZEROSTAT {0, 0, 0, 0, 0}
typedef struct
{ int id, creation, father, mother, dna[MAXKNOB];
statistic score, level;
} genotype;
extern int knob[];
extern double mean(), stdev(), sqrt();
extern FILE *wopen();
static int inittime=0, trialno=0, lastid=0;
static int crosses=0, shifts=0, mutations=0;
static statistic g_score = ZEROSTAT;
static statistic g_level = ZEROSTAT;
static genotype *genes[MAXM];
static int length = 0;
static int mindiff = 10, pmutate = 4, pshift = 2, mintrials = 1;
static double step = 0.33; /* standard deviations from the mean */
static FILE *glog=NULL;
static int compgene();
static parsegene();
static int compgene();
static summgene();
static birth();
static printdna();
static cross();
static mutate();
static shift();
static randompool();
static selectgene();
static unique();
static untested();
static youngest();
static makeunique();
static triangle();
static badgene();
static writegene();
static initgene();
/*
* Start a new gene pool
*/
initpool (k, m)
{ char *ctime();
inittime = time (0);
if (glog) fprintf (glog, "Gene pool initalized, k %d, m %d, %s",
k, m, ctime (&inittime));
randompool (m);
}
/*
* Summarize the current gene pool
*/
analyzepool (full)
int full;
{ register int g;
char *ctime();
qsort (genes, length, sizeof (*genes), compgene);
printf ("Gene pool size %d, started %s", length, ctime (&inittime));
printf ("Trials %d, births %d (crosses %d, mutations %d, shifts %d)\n",
trialno, lastid, crosses, mutations, shifts);
printf ("Mean score %1.0lf+%1.0lf, Mean level %3.1lf+%3.1lf\n\n",
mean (&g_score), stdev (&g_score),
mean (&g_level), stdev (&g_level));
/* Give average of each gene */
if (full == 2)
{ statistic gs;
register int k;
extern char *knob_name[];
for (k=0; k<MAXKNOB; k++)
{ clearstat (&gs);
for (g=0; g<length; g++)
{ addstat (&gs, genes[g]->dna[k]); }
printf ("%s%5.2lf+%1.2lf\n", knob_name[k], mean (&gs), stdev (&gs));
}
}
/* List detail of gene pool */
else
{
for (g=0; g<length; g++)
{ printf ("Living: "); summgene (stdout, genes[g]);
if (full)
{ if (genes[g]->mother)
printf (" Parents: %3d,%-3d", genes[g]->father, genes[g]->mother);
else
printf (" Parent: %3d, ", genes[g]->father);
printf (" best %4.0lf/%-2.0lf",
genes[g]->score.high, genes[g]->level.high);
printf (" DNA "); printdna (stdout, genes[g]); printf ("\n\n");
}
}
}
}
/*
* setknobs: Read gene pool, pick genotype, and set knobs accordingly.
*/
setknobs (newid, knb, best, avg)
int *newid, *knb, *best, *avg;
{ register int i, g;
++trialno;
g = pickgenotype (); /* Pick one genotype */
*newid = genes[g]->id;
for (i=0; i<MAXKNOB; i++) /* Set the knobs for that genotype */
knb[i] = genes[g]->dna[i];
*best = genes[g]->score.high;
*avg = (int) mean (&(genes[g]->score));
}
/*
* evalknobs: Add a data point to the gene pool
*/
evalknobs (gid, score, level)
int gid, score, level;
{ register int g;
/* Find out which gene has the correct id */
for (g=0; g<length; g++)
if (gid == genes[g]->id) break;
/* If he got deleted by someone else, blow it off */
if (g >= length) return;
/* Add information about performance */
addstat (&(genes[g]->score), score);
addstat (&g_score, score);
addstat (&(genes[g]->level), level);
addstat (&g_level, level);
if (glog)
{ fprintf (glog, "Trial %4d, Id %3d -> %4d/%-2d ",
trialno, genes[g]->id, score, level);
fprintf (glog, "age %2d, %4.0lf+%-4.0lf %4.1lf+%3.1lf\n",
TRIALS(genes[g]),
mean (&(genes[g]->score)), stdev (&(genes[g]->score)),
mean (&(genes[g]->level)), stdev (&(genes[g]->level)));
}
}
/*
* openlog: Open the gene log file
*/
FILE *openlog (genelog)
register char *genelog;
{ glog = wopen (genelog, "a");
return (glog);
}
/*
* closelog: Close the log file
*/
closelog ()
{ if (glog) fclose (glog);
}
/*
* pickgenotype: Run one trial, record performance, and do some learning
*/
pickgenotype ()
{ register int youth, father, mother, new;
/* Find genotype with fewer trials than needed to measure its performance */
youth = untested ();
if (youth >= 0) return (youth);
/*
* Have a good measure of all genotypes, pick a father, a mother, and
* a loser and create a new genotype using genetic operators.
*/
father = selectgene (NONE, NONE);
mother = selectgene (father, NONE);
new = badgene (father, mother);
/* If no losers yet, return the youngest */
if (new < 0) return (youngest ());
/* Shift a single genotype with probability pshift */
if (randint (100) < pshift)
{ if (glog)
{ fprintf (glog, "Select: "); summgene (glog, genes[father]);
fprintf (glog, "Death: "); summgene (glog, genes[new]);
}
shift (father, new);
}
/* Mutate a single genotype with probability pmutate */
else if (randint (100-pshift) < pmutate)
{ if (glog)
{ fprintf (glog, "Select: "); summgene (glog, genes[father]);
fprintf (glog, "Death: "); summgene (glog, genes[new]);
}
mutate (father, new);
}
/* Cross two genotypes with probability 1-pshift-pmutate */
else
{ if (glog)
{ fprintf (glog, "Select: "); summgene (glog, genes[father]);
fprintf (glog, "Select: "); summgene (glog, genes[mother]);
fprintf (glog, "Death: "); summgene (glog, genes[new]);
}
cross (father, mother, new);
}
/* Log the birth */
if (glog) birth (glog, genes[new]);
return (new); /* Evaluate the new genotype */
}
/*
* readgenes: Open the genepool for reading, and fill the current gene pool.
* Returns true if the file was was, and 0 if there was no fail. Exits
* if the file exists and cannot be read.
*/
readgenes (genepool)
register char *genepool;
{ char buf[BUFSIZ];
register char *b;
register int g=0;
FILE *gfil;
if ((gfil = fopen (genepool, "r")) == NULL)
{ if (fexists (genepool))
quit (1, "Cannot open file '%s'\n", genepool);
else
return (0);
}
/* Read the header line */
b = buf;
fgets (b, BUFSIZ, gfil);
sscanf (b, "%d %d %d %d %d %d",
&inittime, &trialno, &lastid, &crosses, &shifts, &mutations);
SKIPTO ('|', b);
parsestat (b, &g_score);
SKIPTO ('|', b);
parsestat (b, &g_level);
/* Now read in each genotype */
while (fgets (buf, BUFSIZ, gfil) && length < MAXM-1)
{ if (g >= length)
{ genes[g] = (genotype *) malloc (sizeof (**genes));
length++;
}
initgene (genes[g]);
parsegene (buf, genes[g++]);
}
fclose (gfil);
return (1);
}
/*
* parsegene: Given a string representing a genotype and a genotype
* structure, fill the structure according to the string.
*/
static parsegene (buf, gene)
register char *buf;
register genotype *gene;
{ register int i;
/* Get genotype specific info */
sscanf (buf, "%d %d %d %d", &gene->id, &gene->creation,
&gene->father, &gene->mother);
/* Read each DNA gene */
SKIPTO ('|', buf);
SKIPCHAR (' ', buf);
for (i=0; ISDIGIT (*buf); i++)
{ if (i < MAXKNOB) gene->dna[i] = atoi (buf);
SKIPDIG (buf);
SKIPCHAR (' ', buf);
}
/* Read the score and level performance stats */
SKIPTO ('|', buf);
parsestat (buf, &(gene->score));
SKIPTO ('|', buf);
parsestat (buf, &(gene->level));
}
/*
* writegenes: Write the gene pool 'genes' out to file 'genepool'
*/
writegenes (genepool)
register char *genepool;
{ register FILE *gfil;
register int g;
/* Open the gene file */
if ((gfil = wopen (genepool, "w")) == NULL)
quit (1, "Cannot open file '%s'\n", genepool);
/* Write the header line */
fprintf (gfil, "%d %d %d %d %d %d",
inittime, trialno, lastid, crosses, shifts, mutations);
fprintf (gfil, "|");
writestat (gfil, &g_score);
fprintf (gfil, "|");
writestat (gfil, &g_level);
fprintf (gfil, "|\n");
/* Loop through each genotype */
for (g=0; g<length; g++)
writegene (gfil, genes[g]);
fclose (gfil);
}
/*
* Write out one line representing the gene.
*/
static writegene (gfil, g)
register FILE *gfil;
register genotype *g;
{ register int i;
/* Print genotype specific info */
fprintf (gfil, "%3d %4d %3d %3d|", g->id, g->creation,
g->father, g->mother);
/* Write out dna */
for (i=0; i<MAXKNOB; i++)
{ fprintf (gfil, "%2d", g->dna[i]);
if (i < MAXKNOB-1) fprintf (gfil, " ");
}
fprintf (gfil, "|");
/* Write out statistics */
writestat (gfil, &(g->score));
fprintf (gfil, "|");
writestat (gfil, &(g->level));
fprintf (gfil, "|\n");
}
/*
* initgene: Allocate a new genotype structure, set everything to 0.
*/
static initgene (gene)
register genotype *gene;
{ register int i;
/* Clear genoptye specific info */
gene->id = gene->creation = gene->father = gene->mother = 0;
/* Clear the dna */
for (i = 0; i < MAXKNOB; i++) gene->dna[i] = 0;
/* Clear the statictics */
clearstat (&(gene->score));
clearstat (&(gene->level));
}
/*
* compgene: Compare two genotypes in terms of score.
*/
static int compgene (a, b)
genotype **a, **b;
{ register int result;
result = (int) mean (&((*b)->score)) -
(int) mean (&((*a)->score));
if (result) return (result);
else return ((*a)->id - (*b)->id);
}
/*
* summgene: Summarize a single genotype
*/
static summgene (f, gene)
register FILE *f;
register genotype *gene;
{ fprintf (f, "%3d age %2d, created %4d, ",
gene->id, TRIALS(gene), gene->creation);
fprintf (f, "score %5.0lf+%-4.0lf level %4.1lf+%-3.1lf\n",
mean (&(gene->score)), stdev (&(gene->score)),
mean (&(gene->level)), stdev (&(gene->level)));
}
/*
* Birth: Summarize Record the birth of a genotype.
*/
static birth (f, gene)
register FILE *f;
register genotype *gene;
{
if (!glog) return;
fprintf (f, "Birth: %d ", gene->id);
if (gene->mother)
fprintf (f, "(%d,%d)", gene->father, gene->mother);
else
fprintf (f, "(%d)", gene->father);
fprintf (f, " created %d, DNA ", gene->creation);
printdna (f, gene);
fprintf (f, "\n");
}
/*
* printdna: Print the genotype of a gene
*/
static printdna (f, gene)
FILE *f;
register genotype *gene;
{ register int i;
fprintf (f, "(");
for (i=0; i < MAXKNOB; i++)
{ fprintf (f, "%02d", gene->dna[i]);
if (i < MAXKNOB-1) fprintf (f, " ");
}
fprintf (f, ")");
}
/*
* cross: Cross two genotypes producing a new genotype
*/
static cross (father, mother, new)
register int father, mother, new;
{ register int cpoint, i;
/* Set the new genotypes info */
genes[new]->id = ++lastid;
genes[new]->creation = trialno;
genes[new]->father = genes[father]->id;
genes[new]->mother = genes[mother]->id;
clearstat (&(genes[new]->score));
clearstat (&(genes[new]->level));
/* Pick a crossover point and dominant parent */
cpoint = randint (MAXKNOB-1) + 1;
/* Fifty/fifty chance we swap father and mother */
if (randint (100) < 50)
{ father ^= mother; mother ^= father; father ^= mother; }
/* Copy the dna over */
for (i=0; i<MAXKNOB; i++)
genes[new]->dna[i] = (i<cpoint) ?
genes[father]->dna[i] : genes[mother]->dna[i];
makeunique (new);
/* Log the crossover */
if (glog)
{ fprintf (glog, "Crossing %d and %d produces %d\n",
genes[father]->id, genes[mother]->id, genes[new]->id);
}
crosses++;
}
/*
* mutate: mutate a genes producing a new gene
*/
static mutate (father, new)
register int father, new;
{ register int i;
/* Set the new genotypes info */
genes[new]->id = ++lastid;
genes[new]->creation = trialno;
genes[new]->father = genes[father]->id;
genes[new]->mother = 0;
clearstat (&(genes[new]->score));
clearstat (&(genes[new]->level));
/* Copy the dna over */
for (i=0; i<MAXKNOB; i++)
genes[new]->dna[i] = genes[father]->dna[i];
/* Randomly change genes until the new genotype is unique */
do
{ i=randint (MAXKNOB);
genes[new]->dna[i] = (genes[new]->dna[i] +
triangle (20) + ALLELE) % ALLELE;
} while (!unique (new));
/* Log the mutation */
if (glog)
{ fprintf (glog, "Mutating %d produces %d\n",
genes[father]->id, genes[new]->id);
}
mutations++;
}
/*
* shift: shift a gene producing a new gene
*/
static shift (father, new)
register int father, new;
{ register int i, offset;
/* Set the new genotypes info */
genes[new]->id = ++lastid;
genes[new]->creation = trialno;
genes[new]->father = genes[father]->id;
genes[new]->mother = 0;
clearstat (&(genes[new]->score));
clearstat (&(genes[new]->level));
/* Pick an offset, triangularly distributed around 0, until unique */
offset = triangle (20);
for (i=0; i<MAXKNOB; i++)
genes[new]->dna[i] = (genes[father]->dna[i] +
offset + ALLELE) % ALLELE;
makeunique (new);
/* Now log the shift */
if (glog)
{ fprintf (glog, "Shifting %d by %d produces %d\n",
genes[father]->id, offset, genes[new]->id);
}
shifts++;
}
/*
* randompool: Initialize the pool to a random starting point
*/
static randompool (m)
register int m;
{ register int i, g;
for (g=0; g<m; g++)
{ if (g >= length)
{ genes[g] = (genotype *) malloc (sizeof (**genes));
length++;
}
initgene (genes[g]);
genes[g]->id = ++lastid;
for (i=0; i<MAXKNOB; i++) genes[g]->dna[i] = randint (ALLELE);
birth (glog, genes[g]);
}
length = m;
}
/*
* selectgene: Select a random gene, weighted by mean score.
*/
static selectgene (e1, e2)
register int e1, e2;
{ register int total=0;
register int g;
/* Find the total worth */
for (g=0; g<length; g++)
{ if (g==e1 || g==e2) continue;
/* total += (int) mean (&(genes[g]->score)); */
total += genes[g]->score.high;
}
/* Pick a random number and find the corresponding gene */
if (total > 0)
{ for (g=0, total=randint (total); g<length; g++)
{ if (g==e1 || g==e2) continue;
/* total -= (int) mean (&(genes[g]->score)); */
total -= genes[g]->score.high;
if (total < 0) return (g);
}
}
/* Total worth zero, pick any gene at random */
while ((g = randint (length))==e1 || g==e2) ;
return (g);
}
/*
* unique: Return false if gene is an exact copy of another gene.
*/
static unique (new)
register int new;
{ register int g, i, delta, sumsquares;
for (g=0; g<length; g++)
{ if (g != new)
{ sumsquares = 0;
for (i=0; i<MAXKNOB; i++)
{ delta = genes[g]->dna[i] - genes[new]->dna[i];
sumsquares += delta * delta;
}
if (sumsquares < mindiff) return (0);
}
}
return (1);
}
/*
* untested: Return the index of the youngest genotype with too few
* trials to have an accurate measure. The number of trials is
* greater for older genotypes.
*/
static untested ()
{ register int g, y= -1, trials=1e9, newtrials, count=length;
for (g = randint (length); count-- > 0; g = (g+1) % length)
{ if (TRIALS (genes[g]) >= trials) continue;
newtrials = trialno - genes[g]->creation; /* Turns since creation */
if (TRIALS (genes[g]) < newtrials / (4 * length) + mintrials)
{ y = g; trials = TRIALS (genes[g]); }
}
return (y);
}
/*
* youngest: Return the index of the youngest genotype
*/
static youngest ()
{ register int g, y=0, trials=1e9, newtrials, count=length;
for (g = randint (length); count-- > 0; g = (g+1) % length)
{ newtrials = TRIALS (genes[g]);
if (newtrials < trials) { y=g; trials=newtrials; }
}
return (y);
}
/*
* makeunique: Mutate a genotype until it is unique
*/
static makeunique (new)
register int new;
{ register int i;
while (!unique (new))
{ i=randint (MAXKNOB);
genes[new]->dna[i] = (genes[new]->dna[i] +
triangle (20) + ALLELE) % ALLELE;
}
}
/*
* triangle: Return a non-zero triangularly distributed number from -n to n.
*/
static triangle (n)
register int n;
{ register int val;
do
{ val = randint (n) - randint (n);
} while (val==0);
return (val);
}
/*
* badgene: Find the worst performer so far (with the lowest id).
* only consider genotypes dominated by other genotypes.
*/
static badgene (e1, e2)
register int e1, e2;
{ register int g, worst, trials;
double worstval, bestval, avg, dev, value;
worst = -1; worstval = 1.0e9;
bestval = -1.0e9;
for (g=0; g<length; g++)
{ if ((trials = TRIALS (genes[g])) < mintrials) continue;
avg = mean (&(genes[g]->score));
dev = stdev (&(genes[g]->score)) / sqrt ((double) trials);
value = avg - step * dev;
if (value > bestval) { bestval=value; }
if (g==e1 || g==e2) continue;
value = avg + step * dev;
if (value < worstval) { worst=g; worstval=value; }
}
if (worstval < bestval) return (worst);
else return (-1);
}