annotate trunk/ga_code.d @ 2:9655c8362b25

Added the Perceptron class and the perceptron_test testing program.
author revcompgeek
date Sat, 05 Apr 2008 23:41:30 -0600
parents 4b2e8e8a633e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
1 /++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
2 + Genetic Algorithm Code Test +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
3 + Author: Matt P. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
4 + Version: 0.5 +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
5 + +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
6 + Runs a genetic algorithm test using a simple code +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
7 + guessing algorithm. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
8 + +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
9 + Usage: ga_code [options] +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
10 + Options: +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
11 + -h +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
12 + Prints this information. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
13 + -v +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
14 + Turns on verbose output. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
15 + -m float +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
16 + Specifies the mutation rate for each generation. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
17 + Default: 0.05 +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
18 + +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
19 + -d float +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
20 + Specifies the survival rate for each generation. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
21 + Default: 0.5 +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
22 + +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
23 + -p integer +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
24 + Specifies the population for each generation. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
25 + Default: 1000 +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
26 + +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
27 + -l integer +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
28 + Specifies how long the code to be guessed is. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
29 + Default: 20 +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
30 + +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
31 + -n integer +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
32 + Specifies how many possibilities there are for +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
33 + each character of the code string. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
34 + Default: 20 +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
35 + +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
36 + -r integer +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
37 + Specifies the number of times to repeat the test. +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
38 + Default: 20 +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
39 + +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
40 + -c char +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
41 + Specifies the crossover type +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
42 + s / 1 - One point crossover +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
43 + t / 2 - Two point crossover +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
44 + u / 3 - Uniform crossover +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
45 + Default: s +
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
46 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
47
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
48
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
49 module ga_code;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
50
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
51 import aid.ga;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
52 import tango.math.Random;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
53 import tango.math.Math;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
54 import tango.io.Stdout;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
55 //import tango.text.String;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
56 import Float = tango.text.convert.Float;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
57 import Integer = tango.text.convert.Integer;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
58
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
59 import bcd.sys.times;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
60
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
61 char[] target;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
62
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
63 int numChars = 20;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
64 int stringLen = 20;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
65
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
66 double average(double[] array){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
67 double avg = 0;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
68 foreach (double v; array)
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
69 avg += v;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
70 avg /= array.length;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
71 return avg;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
72 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
73
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
74 double stdDev(double[] array){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
75 double avg = average(array);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
76 double stddev = 0;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
77 foreach (double v; array){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
78 stddev += (v - avg) * (v - avg);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
79 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
80 stddev = sqrt(stddev/array.length);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
81 return stddev;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
82 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
83
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
84 class Foo {
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
85 public double calculateFitness(char[] gene){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
86 double fitness=0;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
87 for (uint i = 0; i < stringLen; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
88 if (gene[i] == target[i])
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
89 fitness += 1;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
90 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
91 return fitness;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
92 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
93
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
94 public char[] getRandomGenotype(){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
95 char[] t;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
96 t.length = stringLen;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
97 for (int i = 0; i < stringLen; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
98 t[i] = Random.shared.next(numChars) + 'a';
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
99 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
100 return t;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
101 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
102
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
103 public char getRandomChar(uint index){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
104 return cast(char)(Random.shared.next(numChars)+'a');
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
105 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
106 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
107
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
108 void usage(){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
109 auto s =
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
110 "Usage: ga_test [options] \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
111 "Options: \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
112 "-h \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
113 " Prints this information. \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
114 "-v \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
115 " Turns on verbose output. \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
116 "-m float \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
117 " Specifies the mutation rate for each generation. \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
118 " Default: 0.05 \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
119 " \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
120 "-d float \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
121 " Specifies the survival rate for each generation. \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
122 " Default: 0.5 \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
123 " \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
124 "-p integer \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
125 " Specifies the population for each generation. \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
126 " Default: 1000 \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
127 " \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
128 "-l integer \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
129 " Specifies how long the code to be guessed is. \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
130 " Default: 20 \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
131 " \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
132 "-n integer \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
133 " Specifies how many possibilities there are for \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
134 " each character of the code string. \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
135 " Default: 20 \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
136 " \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
137 "-r integer \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
138 " Specifies the number of times to repeat the test.\n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
139 " Default: 20 \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
140 " \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
141 "-c char \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
142 " Specifies the crossover type \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
143 " s / 1 - One point crossover \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
144 " t / 2 - Two point crossover \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
145 " u / 3 - Uniform crossover \n"
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
146 " Default: s ";
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
147 Stdout(s).newline;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
148 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
149
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
150 void main(char[][] args){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
151 Foo f = new Foo();
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
152 GeneticAlgorithm ga = new GeneticAlgorithm();
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
153 ga.calculateFitness = &f.calculateFitness;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
154 ga.getRandomGenotype = &f.getRandomGenotype;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
155 ga.getRandomChar = &f.getRandomChar;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
156 ga.fitnessThreshold = stringLen;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
157 int rep = 20;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
158 bool dotime=true;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
159
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
160
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
161 for (auto i = 1; i < args.length; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
162 char[] arg = args[i];
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
163 switch (arg){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
164 case "-h":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
165 usage();
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
166 return;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
167 case "-m":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
168 ga.mutationRate = Float.parse(args[++i]);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
169 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
170 case "-d":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
171 ga.survivalRate = Float.parse(args[++i]);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
172 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
173 case "-p":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
174 ga.startPopulation = Integer.parse(args[++i]);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
175 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
176 case "-l":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
177 stringLen = Integer.parse(args[++i]);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
178 ga.fitnessThreshold = stringLen;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
179 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
180 case "-n":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
181 numChars = Integer.parse(args[++i]);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
182 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
183 case "-r":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
184 rep = Integer.parse(args[++i]);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
185 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
186 case "-v":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
187 ga.verbose = true;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
188 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
189 case "-c":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
190 auto t = args[++i];
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
191 switch (t){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
192 case "s","1":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
193 ga.crossoverType = 1;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
194 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
195 case "t","2":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
196 ga.crossoverType = 2;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
197 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
198 default:
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
199 ga.crossoverType = 3;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
200 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
201 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
202 case "-b":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
203 ga.bailout = Integer.parse(args[++i]);
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
204 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
205 case "-t":
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
206 dotime = false;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
207 break;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
208 default:
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
209 Stdout.format("Unknown parameter: {}",arg).newline;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
210 usage();
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
211 return;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
212 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
213 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
214 target = ga.getRandomGenotype();
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
215
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
216 double[] time;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
217 double[] gens;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
218 long start,end;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
219
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
220 for(auto i = 0; i < rep; i++){
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
221 if(dotime) start = iutime();
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
222 uint generations = ga.run();
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
223 if(dotime) end = iutime() - start;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
224
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
225 if(dotime) time ~= end;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
226 gens ~= generations;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
227 }
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
228
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
229 if(dotime) Stdout.format("{0}, {1:.5}, {2}, {3:.5}", average(gens),stdDev(gens),average(time),stdDev(time)).newline;
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
230 else Stdout.format("{0}, {1:.5}", average(gens),stdDev(gens)).newline; //", {2}, {3:.5}" ,average(time),stdDev(time)
9655c8362b25 Added the Perceptron class and the perceptron_test testing program.
revcompgeek
parents: 0
diff changeset
231 }