1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using HeuristicLab.Algorithms.GeneticProgramming;
|
---|
5 | using HeuristicLab.Algorithms.GrammaticalOptimization;
|
---|
6 | using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
|
---|
7 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
|
---|
10 |
|
---|
11 |
|
---|
12 | [TestClass]
|
---|
13 | public class RunGpExperiments {
|
---|
14 | private readonly static int[] popSizes = new int[] { 100, 250, 500, 1000, 2500, 5000, 10000 };
|
---|
15 | private readonly static double[] mutationRates = new double[] { 0.15 };
|
---|
16 | private readonly static int randSeed = 31415;
|
---|
17 |
|
---|
18 | internal class GPConfiguration {
|
---|
19 | public ISymbolicExpressionTreeProblem Problem;
|
---|
20 | public int PopSize;
|
---|
21 | public int MaxSize;
|
---|
22 | public int RandSeed;
|
---|
23 | public double MutationRate;
|
---|
24 |
|
---|
25 | public override string ToString() {
|
---|
26 | return string.Format("{0} {1} {2} {3} {4}", RandSeed, Problem, PopSize, MaxSize, MutationRate);
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | #region permutation
|
---|
31 | [TestMethod]
|
---|
32 | public void RunStandardGPPermutationProblem() {
|
---|
33 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
34 | {
|
---|
35 | (randSeed) => (ISymbolicExpressionTreeProblem)new PermutationProblem(),
|
---|
36 | };
|
---|
37 |
|
---|
38 | var maxSizes = new int[] { 32 };
|
---|
39 | int nReps = 20;
|
---|
40 | int maxIterations = 50000;
|
---|
41 | foreach (var instanceFactory in instanceFactories) {
|
---|
42 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
43 | RunStandardGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 | [TestMethod]
|
---|
48 | public void RunOffspringSelectionGPPermutationProblem() {
|
---|
49 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
50 | {
|
---|
51 | (randSeed) => (ISymbolicExpressionTreeProblem)new PermutationProblem(),
|
---|
52 | };
|
---|
53 |
|
---|
54 | var maxSizes = new int[] { 32 };
|
---|
55 | int nReps = 20;
|
---|
56 | int maxIterations = 50000;
|
---|
57 | foreach (var instanceFactory in instanceFactories) {
|
---|
58 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
59 | RunOffspringSelectionGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | #region royalpair
|
---|
66 | [TestMethod]
|
---|
67 | public void RunStandardGPRoyalPairProblem() {
|
---|
68 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
69 | {
|
---|
70 | (randSeed) => (ISymbolicExpressionTreeProblem)new RoyalPairProblem(),
|
---|
71 | };
|
---|
72 |
|
---|
73 | var maxSizes = new int[] { 32, 64, 128, 256 };
|
---|
74 | int nReps = 20;
|
---|
75 | int maxIterations = 50000;
|
---|
76 | foreach (var instanceFactory in instanceFactories) {
|
---|
77 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
78 | RunStandardGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | [TestMethod]
|
---|
83 | public void RunOffspringSelectionGPRoyalPairProblem() {
|
---|
84 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
85 | {
|
---|
86 | (randSeed) => (ISymbolicExpressionTreeProblem)new RoyalPairProblem(),
|
---|
87 | };
|
---|
88 |
|
---|
89 | var maxSizes = new int[] { 32, 64, 128, 256 };
|
---|
90 | int nReps = 20;
|
---|
91 | int maxIterations = 50000;
|
---|
92 | foreach (var instanceFactory in instanceFactories) {
|
---|
93 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
94 | RunOffspringSelectionGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | #endregion
|
---|
99 |
|
---|
100 | #region royalsymbol
|
---|
101 | [TestMethod]
|
---|
102 | public void RunStandardGPRoyalSymbolProblem() {
|
---|
103 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
104 | {
|
---|
105 | (randSeed) => (ISymbolicExpressionTreeProblem)new RoyalSymbolProblem(),
|
---|
106 | };
|
---|
107 |
|
---|
108 | var maxSizes = new int[] { 32, 64, 128, 256 };
|
---|
109 | int nReps = 20;
|
---|
110 | int maxIterations = 50000;
|
---|
111 | foreach (var instanceFactory in instanceFactories) {
|
---|
112 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
113 | RunStandardGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|
117 | [TestMethod]
|
---|
118 | public void RunOffspringSelectionGPRoyalSymbolProblem() {
|
---|
119 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
120 | {
|
---|
121 | (randSeed) => (ISymbolicExpressionTreeProblem)new RoyalSymbolProblem(),
|
---|
122 | };
|
---|
123 |
|
---|
124 | var maxSizes = new int[] { 32, 64, 128, 256 };
|
---|
125 | int nReps = 20;
|
---|
126 | int maxIterations = 50000;
|
---|
127 | foreach (var instanceFactory in instanceFactories) {
|
---|
128 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
129 | RunOffspringSelectionGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | #region findphrases
|
---|
136 | [TestMethod]
|
---|
137 | public void RunStandardGPFindPhrasesProblem() {
|
---|
138 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
139 | {
|
---|
140 | (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 0, 1, 0, true),
|
---|
141 | (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 0, 1, 0, false),
|
---|
142 | (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 50, 1, 0.8, false),
|
---|
143 | };
|
---|
144 |
|
---|
145 | var maxSizes = new int[] { 15 * 3 }; // * 3 for non-terminals
|
---|
146 | int nReps = 20;
|
---|
147 | int maxIterations = 50000;
|
---|
148 | foreach (var instanceFactory in instanceFactories) {
|
---|
149 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
150 | RunStandardGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | [TestMethod]
|
---|
155 | public void RunOffspringSelectionGPFindPhrasesProblem() {
|
---|
156 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
157 | {
|
---|
158 | (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 0, 1, 0, true),
|
---|
159 | (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 0, 1, 0, false),
|
---|
160 | (randSeed) => (ISymbolicExpressionTreeProblem) new FindPhrasesProblem(new Random(randSeed), 20, 5, 3, 5, 50, 1, 0.8, false),
|
---|
161 | };
|
---|
162 |
|
---|
163 | var maxSizes = new int[] { 15 * 3 }; // * 3 for non-terminals
|
---|
164 | int nReps = 20;
|
---|
165 | int maxIterations = 50000;
|
---|
166 | foreach (var instanceFactory in instanceFactories) {
|
---|
167 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
168 | RunOffspringSelectionGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | #endregion
|
---|
173 |
|
---|
174 | #region artificial ant
|
---|
175 | [TestMethod]
|
---|
176 | public void RunStandardGPArtificialAntProblem() {
|
---|
177 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
178 | {
|
---|
179 | (randSeed) => (ISymbolicExpressionTreeProblem) new SantaFeAntProblem(),
|
---|
180 | };
|
---|
181 |
|
---|
182 | var maxSizes = new int[] { 30, 50, 100 }; // size of sequential representation is 17
|
---|
183 | int nReps = 20;
|
---|
184 | int maxIterations = 100000; // randomsearch finds the optimum almost always for 100000 evals
|
---|
185 | foreach (var instanceFactory in instanceFactories) {
|
---|
186 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
187 | RunStandardGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 | [TestMethod]
|
---|
192 | public void RunOffspringSelectionGPArtificialAntProblem() {
|
---|
193 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
194 | {
|
---|
195 | (randSeed) => (ISymbolicExpressionTreeProblem) new SantaFeAntProblem(),
|
---|
196 | };
|
---|
197 |
|
---|
198 | var maxSizes = new int[] { 30, 50, 100 }; // size of sequential representation is 17
|
---|
199 | int nReps = 20;
|
---|
200 | int maxIterations = 100000; // randomsearch finds the optimum almost always for 100000 evals
|
---|
201 | foreach (var instanceFactory in instanceFactories) {
|
---|
202 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
203 | RunOffspringSelectionGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | #endregion
|
---|
208 |
|
---|
209 | #region symb-reg-poly-10
|
---|
210 | [TestMethod]
|
---|
211 | public void RunStandardGPPoly10Problem() {
|
---|
212 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
213 | {
|
---|
214 | (randSeed) => (ISymbolicExpressionTreeProblem) new SymbolicRegressionPoly10Problem(),
|
---|
215 | };
|
---|
216 |
|
---|
217 | var maxSizes = new int[] { 30, 50, 100 }; // size of sequential representation is 23
|
---|
218 | int nReps = 20;
|
---|
219 | int maxIterations = 100000; // sequentialsearch should find the optimum within 100000 evals
|
---|
220 | foreach (var instanceFactory in instanceFactories) {
|
---|
221 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
222 | RunStandardGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
223 | }
|
---|
224 | }
|
---|
225 | }
|
---|
226 | [TestMethod]
|
---|
227 | public void RunOffspringSelectionGPPoly10Problem() {
|
---|
228 | var instanceFactories = new Func<int, ISymbolicExpressionTreeProblem>[]
|
---|
229 | {
|
---|
230 | (randSeed) => (ISymbolicExpressionTreeProblem) new SymbolicRegressionPoly10Problem(),
|
---|
231 | };
|
---|
232 |
|
---|
233 | var maxSizes = new int[] { 30, 50, 100 }; // size of sequential representation is 23
|
---|
234 | int nReps = 20;
|
---|
235 | int maxIterations = 100000; // sequentialsearch should find the optimum within 100000 evals
|
---|
236 | foreach (var instanceFactory in instanceFactories) {
|
---|
237 | foreach (var conf in GenerateConfigurations(instanceFactory, nReps, popSizes, maxSizes, mutationRates)) {
|
---|
238 | RunOffspringSelectionGpForProblem(conf.RandSeed, conf.Problem, maxIterations, conf.PopSize, conf.MaxSize, conf.MutationRate);
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 | #endregion
|
---|
243 |
|
---|
244 | #region helpers
|
---|
245 | private IEnumerable<GPConfiguration> GenerateConfigurations(Func<int, ISymbolicExpressionTreeProblem> problemFactory,
|
---|
246 | int nReps,
|
---|
247 | IEnumerable<int> popSizes,
|
---|
248 | IEnumerable<int> maxSizes,
|
---|
249 | IEnumerable<double> mutationRates) {
|
---|
250 | var seedRand = new Random(randSeed);
|
---|
251 | // the problem seed is the same for all configuratons
|
---|
252 | // this guarantees that we solve the _same_ problem each time
|
---|
253 | // with different solvers and multiple repetitions
|
---|
254 | var problemSeed = randSeed;
|
---|
255 | for (int i = 0; i < nReps; i++) {
|
---|
256 | // in each repetition use the same random seed for all solver configuratons
|
---|
257 | // do nReps with different seeds for each configuration
|
---|
258 | var solverSeed = seedRand.Next();
|
---|
259 | foreach (var popSize in popSizes) {
|
---|
260 | foreach (var mutRate in mutationRates) {
|
---|
261 | foreach (var maxSize in maxSizes) {
|
---|
262 | yield return new GPConfiguration {
|
---|
263 | MaxSize = maxSize,
|
---|
264 | MutationRate = mutRate,
|
---|
265 | PopSize = popSize,
|
---|
266 | Problem = problemFactory(problemSeed),
|
---|
267 | RandSeed = solverSeed
|
---|
268 | };
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | private static void RunStandardGpForProblem(
|
---|
276 | int randSeed,
|
---|
277 | ISymbolicExpressionTreeProblem problem,
|
---|
278 | int maxIters,
|
---|
279 | int popSize,
|
---|
280 | int maxSize,
|
---|
281 | double mutationRate
|
---|
282 | ) {
|
---|
283 | var gp = new StandardGP(problem, new Random(randSeed), false);
|
---|
284 | var problemName = problem.GetType().Name;
|
---|
285 | var bestKnownQuality = problem.BestKnownQuality(maxSize);
|
---|
286 | RunGP(gp, problemName, bestKnownQuality, maxIters, popSize, mutationRate, maxSize);
|
---|
287 | }
|
---|
288 |
|
---|
289 | private static void RunOffspringSelectionGpForProblem(
|
---|
290 | int randSeed,
|
---|
291 | ISymbolicExpressionTreeProblem problem,
|
---|
292 | int maxIters,
|
---|
293 | int popSize,
|
---|
294 | int maxSize,
|
---|
295 | double mutationRate
|
---|
296 | ) {
|
---|
297 | var gp = new OffspringSelectionGP(problem, new Random(randSeed), false);
|
---|
298 | var problemName = problem.GetType().Name;
|
---|
299 | var bestKnownQuality = problem.BestKnownQuality(maxSize);
|
---|
300 | RunGP(gp, problemName, bestKnownQuality, maxIters, popSize, mutationRate, maxSize);
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 |
|
---|
305 |
|
---|
306 | private static void RunGP(IGPSolver gp, string problemName, double bestKnownQuality, int maxIters, int popSize, double mutationRate, int maxSize) {
|
---|
307 | int iterations = 0;
|
---|
308 | var globalStatistics = new SentenceSetStatistics(bestKnownQuality);
|
---|
309 | var gpName = gp.GetType().Name;
|
---|
310 | gp.SolutionEvaluated += (sentence, quality) => {
|
---|
311 | iterations++;
|
---|
312 | globalStatistics.AddSentence(sentence, quality);
|
---|
313 |
|
---|
314 | if (iterations % 1000 == 0) {
|
---|
315 | Console.WriteLine("\"{0,25}\" {1} {2:N2} {3} \"{4,25}\" {5}", gpName, popSize, mutationRate, maxSize, problemName, globalStatistics);
|
---|
316 | }
|
---|
317 | };
|
---|
318 |
|
---|
319 | gp.PopulationSize = popSize;
|
---|
320 | gp.MutationRate = mutationRate;
|
---|
321 | gp.MaxSolutionSize = maxSize + 2;
|
---|
322 | gp.MaxSolutionDepth = maxSize + 2;
|
---|
323 |
|
---|
324 | gp.Run(maxIters);
|
---|
325 | }
|
---|
326 | #endregion
|
---|
327 | }
|
---|
328 | }
|
---|