comparison trunk/ga_code.d @ 0:4b2e8e8a633e

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