Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs @ 6017

Last change on this file since 6017 was 6017, checked in by cneumuel, 12 years ago

#1215

  • fixed import of existing algorithm
  • moved operators in subfolders
  • extended tests for SymbolicExpressionGrammar
File size: 53.0 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.IO;
6using System.Linq;
7using System.Reflection;
8using System.Text;
9using System.Threading;
10using System.Threading.Tasks;
11using HeuristicLab.Algorithms.EvolutionStrategy;
12using HeuristicLab.Algorithms.GeneticAlgorithm;
13using HeuristicLab.Common;
14using HeuristicLab.Core;
15using HeuristicLab.Data;
16using HeuristicLab.Encodings.RealVectorEncoding;
17//using HeuristicLab.Hive.ExperimentManager;
18using HeuristicLab.Optimization;
19using HeuristicLab.Parameters;
20using HeuristicLab.PluginInfrastructure;
21using HeuristicLab.PluginInfrastructure.Manager;
22using HeuristicLab.Problems.DataAnalysis.Symbolic;
23using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
24using HeuristicLab.Problems.MetaOptimization;
25using HeuristicLab.Problems.TestFunctions;
26using HeuristicLab.Random;
27using HeuristicLab.Selection;
28
29namespace HeuristicLab.MetaOptimization.Test {
30  class Program {
31    static void Main(string[] args) {
32      PluginManager pm = new PluginManager(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
33      pm.DiscoverAndCheckPlugins();
34      pm.Run(pm.Applications.Where(x => x.Name == "TestApp").SingleOrDefault());
35    }
36  }
37
38  [Plugin("TestPlugin", "1.0.0.0")]
39  [PluginFile("HeuristicLab.MetaOptimization.Test.exe", PluginFileType.Assembly)]
40  public class TestPlugin : PluginBase { }
41
42  [Application("TestApp")]
43  public class TestApp : ApplicationBase {
44    //private static int metaAlgorithmPopulationSize = 50;
45    //private static int metaAlgorithmMaxGenerations = 30;
46    //private static int metaProblemRepetitions = 5;
47    //private static int baseAlgorithmMaxGenerations = 1000;
48
49    private static int metaAlgorithmPopulationSize = 20;
50    private static int metaAlgorithmMaxGenerations = 20;
51    private static int metaProblemRepetitions = 3;
52    private static int baseAlgorithmMaxGenerations = 100;
53    private static double mutationProbability = 0.10;
54
55    public override void Run() {
56      ContentManager.Initialize(new PersistenceContentManager());
57
58      //TestTableBuilder();
59      //TestShorten();
60
61      //TestSimilarities(); return;
62      //TestIntSampling();
63      //TestDoubleSampling(); return;
64      //TestTypeDiscovery();
65      //TestOperators();
66      //TestCombinations();
67      //TestCombinations2();
68      //TestCombinations3();
69      //TestEnumeratorCollectionEnumerator();
70      //TestCombinations4(); return;
71      //TestAlgorithmPerformanceIssue(); return;
72      //TestWaitAny();
73      //TestExecutionTimeUpdateInvervalPerformance();
74      //TestMemoryConsumption();
75      //TestNormalCrossover();
76      //TestItemDictionary();
77
78      //TestSymbolicDataAnalysisGrammar(); return;
79
80      MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
81      metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
82      //GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
83      GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
84      //GeneticAlgorithm metaLevelAlgorithm = GetHiveParallelMetaGA(metaOptimizationProblem);
85
86      //EvolutionStrategy metaLevelAlgorithm = GetMetaES(metaOptimizationProblem);
87
88      var algorithmVc = SetupGAAlgorithm(typeof(GeneticAlgorithm), metaOptimizationProblem);
89
90      //TestToString(algorithmVc);
91
92
93      //Console.WriteLine("Press enter to start");
94      //Console.ReadLine();
95      //TestConfiguration(algorithmVc, typeof(GeneticAlgorithm), metaOptimizationProblem.Problems.First());
96
97      //Console.WriteLine("Press enter to start");
98      //Console.ReadLine();
99      TestOptimization(metaLevelAlgorithm);
100
101      //TestMemoryLeak(metaLevelAlgorithm);
102
103      Console.ReadLine();
104    }
105
106    private void TestSymbolicDataAnalysisGrammar() {
107      var random = new MersenneTwister();
108
109      var grammar1 = new TypeCoherentExpressionGrammar();
110      var grammar2 = new TypeCoherentExpressionGrammar();
111     
112      Console.WriteLine("========== Grammar1: ==========");
113      PrintGrammar(grammar1);
114      //Console.WriteLine("========== Grammar2: ==========");
115      //PrintGrammar(grammar2);
116
117      var vc1 = new SymbolicExpressionGrammarValueConfiguration(grammar1);
118      ConfigureSymbolicExpressionGrammarVc(vc1);
119      var vc2 = new SymbolicExpressionGrammarValueConfiguration(grammar2);
120      ConfigureSymbolicExpressionGrammarVc(vc2);
121
122      vc1.Mutate(random, new MutateDelegate(ParameterConfigurationManipulator.Mutate), new UniformIntValueManipulator(), new UniformDoubleValueManipulator());
123      vc1.Parameterize(grammar1);
124
125      Console.WriteLine("========== Grammar1 (mutated): ==========");
126      PrintGrammar(grammar1);
127
128      vc1.Cross(random, vc2, new CrossDelegate(ParameterConfigurationCrossover.Cross), new DiscreteIntValueCrossover(), new AverageDoubleValueCrossover());
129      vc1.Parameterize(grammar1);
130
131      Console.WriteLine("========== Grammar1 (crossed): ==========");
132      PrintGrammar(grammar1);
133
134      //RealVector v1 = GetInitialFrequenciesAsRealVector(grammar1);
135      //RealVector v2 = GetInitialFrequenciesAsRealVector(grammar2);
136
137      //for (int i = 0; i < 10; i++) {
138      //  RealVector v3 = DiscreteCrossover.Apply(random, new ItemArray<RealVector>(new List<RealVector> { v1, v2 }));
139
140      //  var grammar3 = new TypeCoherentExpressionGrammar();
141      //  SetInitialFrequenciesFromRealVector(grammar3, v3);
142
143      //  Console.WriteLine("\n========== Crossed: ==========");
144      //  PrintGrammar(grammar3);
145      //}
146
147    }
148
149    private static void PrintGrammar(TypeCoherentExpressionGrammar grammar) {
150      foreach (var symbol in grammar.Symbols) {
151        Console.WriteLine("{0} ({1})", symbol.ToString(), symbol.InitialFrequency);
152      }
153    }
154
155    private static RealVector GetInitialFrequenciesAsRealVector(TypeCoherentExpressionGrammar grammar) {
156      var vector = new RealVector(grammar.Symbols.Count());
157      for (int i = 0; i < grammar.Symbols.Count(); i++) {
158        vector[i] = grammar.Symbols.ElementAt(i).InitialFrequency;
159      }
160      return vector;
161    }
162
163    private static void SetInitialFrequenciesFromRealVector(TypeCoherentExpressionGrammar grammar, RealVector vector) {
164      for (int i = 0; i < grammar.Symbols.Count(); i++) {
165        grammar.Symbols.ElementAt(i).InitialFrequency = vector[i];
166      }
167    }
168
169    private static void TestSimilarities() {
170      Console.WriteLine("\nDoubleRange:");
171      var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(10), new DoubleValue(1));
172      var a = new DoubleValue(5.0);
173
174      for (double d = 0; d < 10; d += 0.1) {
175        var similarity = doubleRange.CalculateSimilarity(a, new DoubleValue(d));
176        Console.WriteLine("{0}: {1}", d, similarity);
177      }
178
179      Console.WriteLine("\nPecentRange:");
180      var percentRange = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(1));
181      var b = new PercentValue(0.05);
182
183      for (double d = 0; d < 1; d += 0.01) {
184        var similarity = percentRange.CalculateSimilarity(b, new PercentValue(d));
185        Console.WriteLine("{0}: {1}", d, similarity);
186      }
187
188      Console.WriteLine("\nIntRange:");
189      var intRange = new IntValueRange(new IntValue(50), new IntValue(100), new IntValue(1));
190      var c = new IntValue(90);
191
192      for (int i = 0; i < 100; i++) {
193        var similarity = intRange.CalculateSimilarity(c, new IntValue(i));
194        Console.WriteLine("{0}: {1}", i, similarity);
195      }
196
197      Console.WriteLine("\nValueConfigurations:");
198      var vc1 = SetupGAAlgorithm(typeof(GeneticAlgorithm), new MetaOptimizationProblem());
199      vc1.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "Elites").Optimize = true;
200      vc1.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "PopulationSize").Optimize = true;
201      vc1.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").Optimize = true;
202      vc1.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "Selector").Optimize = true;
203
204      var vc2 = (ParameterConfigurationTree)vc1.Clone();
205      Console.WriteLine("Assert(1): {0}", vc1.CalculateSimilarity(vc2));
206
207      ((IntValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "PopulationSize").ValueConfigurations[0].ActualValue.Value).Value = 75;
208      Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
209
210      ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.15;
211      Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
212
213      ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.25;
214      Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
215      ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.35;
216      Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
217      ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.45;
218      Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
219      ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.55;
220      Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
221
222      vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "Selector").ActualValueConfigurationIndex = 3;
223      Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
224
225      var random = new Random.MersenneTwister(0);
226      for (int i = 0; i < 10; i++) {
227        vc2.Randomize(random);
228        Console.WriteLine("Randomized: {0}", vc1.CalculateSimilarity(vc2));
229      }
230    }
231
232    private static void TestItemDictionary() {
233      var dict = new ItemDictionary<StringValue, RunCollection>();
234      dict.Add(new StringValue("a"), new RunCollection());
235      dict.Add(new StringValue("b"), new RunCollection());
236      dict.Add(new StringValue("c"), new RunCollection());
237
238      Console.WriteLine(dict.ContainsKey(new StringValue("a")));
239      Console.WriteLine(dict.Count(x => x.Key.Value == "a"));
240
241    }
242
243    private static void TestNormalCrossover() {
244      var random = new MersenneTwister();
245      double d1 = 0.5;
246      double d2 = 0.6;
247      var doubleRange = new DoubleValueRange(new DoubleValue(0.0), new DoubleValue(1.0), new DoubleValue(0.01));
248
249      using (var sw = new StreamWriter("normalCrossover-DoubleValue.txt")) {
250        for (int i = 0; i < 10000; i++) {
251          sw.WriteLine(NormalDoubleValueCrossover.ApplyStatic(random, new DoubleValue(d1), new DoubleValue(d2), doubleRange));
252        }
253      }
254
255      int i1 = 180;
256      int i2 = 160;
257      var intRange = new IntValueRange(new IntValue(100), new IntValue(200), new IntValue(1));
258
259      using (var sw = new StreamWriter("normalCrossover-IntValue.txt")) {
260        for (int i = 0; i < 10000; i++) {
261          sw.WriteLine(NormalIntValueCrossover.ApplyStatic(random, new IntValue(i1), new IntValue(i2), intRange));
262        }
263      }
264    }
265
266    private static void TestMemoryConsumption() {
267      Queue<TimeSpan> latestExecutionTimes = new Queue<TimeSpan>();
268      GeneticAlgorithm ga = new GeneticAlgorithm();
269      ga.PopulationSize.Value = 3;
270      ga.MaximumGenerations.Value = 1;
271      ga.Engine = new SequentialEngine.SequentialEngine();
272      throw new NotImplementedException("TODO: set ga properties correctly");
273
274      MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
275      metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
276      GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
277      ParameterConfigurationTree algorithmVc = SetupGAAlgorithm(typeof(GeneticAlgorithm), metaOptimizationProblem);
278      Stopwatch sw = new Stopwatch();
279
280      var algs = new List<IAlgorithm>();
281      for (int i = 0; i < 10000; i++) {
282        sw.Start();
283        GeneticAlgorithm clonedGa = (GeneticAlgorithm)ga.Clone();
284        clonedGa.Name = "CLONED GA";
285        algorithmVc.Parameterize(clonedGa);
286        algs.Add(clonedGa);
287        sw.Reset();
288        ContentManager.Save((IStorableContent)metaLevelAlgorithm, "alg_" + i + ".hl", true);
289        Console.WriteLine("Cloned alg #{0}", i);
290      }
291    }
292
293    private static void TestExecutionTimeUpdateInvervalPerformance() {
294      TableBuilder tb = new TableBuilder("Tasks", "Interval", "TotalExecutionTime", "AvgExecutionTime", "TimeElapsed", "TotalTimeElapsed", "Speedup", "ExecutionTimeChangedCount", "RealExecutionTimeUpdate(ms)");
295      int tasks = 4;
296      int repetitions = 3;
297
298      // warmup
299      RepeatExecuteParallel(3, 1, 1, tb);
300      tb.AppendRow("--", "--", "--", "--", "--", "--", "--", "--", "--");
301      RepeatExecuteParallel(repetitions, tasks, 1, tb);
302      RepeatExecuteParallel(repetitions, tasks, 2.5, tb);
303      RepeatExecuteParallel(repetitions, tasks, 5, tb);
304      RepeatExecuteParallel(repetitions, tasks, 10, tb);
305      RepeatExecuteParallel(repetitions, tasks, 25, tb);
306      RepeatExecuteParallel(repetitions, tasks, 50, tb);
307      RepeatExecuteParallel(repetitions, tasks, 100, tb);
308      RepeatExecuteParallel(repetitions, tasks, 250, tb);
309      RepeatExecuteParallel(repetitions, tasks, 500, tb);
310      RepeatExecuteParallel(repetitions, tasks, 1000, tb);
311      RepeatExecuteParallel(repetitions, tasks, 2500, tb);
312      RepeatExecuteParallel(repetitions, tasks, 5000, tb);
313
314      using (var sw = new StreamWriter("TestExecutionTimeUpdateInvervalPerformance.txt")) {
315        sw.Write(tb.ToString());
316      }
317    }
318
319    private static GeneticAlgorithm CreateGA() {
320      GeneticAlgorithm ga = new GeneticAlgorithm();
321      ga.Problem = new SingleObjectiveTestFunctionProblem() { ProblemSize = new IntValue(250) };
322      ga.Engine = new SequentialEngine.SequentialEngine();
323      ga.SetSeedRandomly.Value = false;
324      ga.Seed.Value = 0;
325      return ga;
326    }
327
328    private static void RepeatExecuteParallel(int repetitions, int tasks, double executionTimeUpdateIntervalMs, TableBuilder tb) {
329      for (int i = 0; i < repetitions; i++) {
330        ExecuteParallel(tasks, executionTimeUpdateIntervalMs, tb);
331        Console.Clear();
332        Console.WriteLine(tb.ToString());
333      }
334    }
335
336    private static void ExecuteParallel(int taskCount, double executionTimeUpdateIntervalMs, TableBuilder tb) {
337      Task<TimeSpan>[] tasks = new Task<TimeSpan>[taskCount];
338      EngineAlgorithm[] algs = new EngineAlgorithm[taskCount];
339      for (int i = 0; i < taskCount; i++) {
340        GeneticAlgorithm alg = CreateGA();
341        //((Engine)alg.Engine).ExecutionTimeUpdateInterval = TimeSpan.FromMilliseconds(executionTimeUpdateIntervalMs);
342        algs[i] = alg;
343      }
344      Console.WriteLine("Creating algs finished.");
345
346      for (int i = 0; i < taskCount; i++) {
347        tasks[i] = new Task<TimeSpan>((alg) => {
348          Console.WriteLine("Task {0} started.", Task.CurrentId);
349
350          Stopwatch swx = new Stopwatch();
351          swx.Start();
352          ((EngineAlgorithm)alg).ExecutionTimeChanged += new EventHandler(Program_ExecutionTimeChanged);
353          ((EngineAlgorithm)alg).StartSync();
354          ((EngineAlgorithm)alg).ExecutionTimeChanged -= new EventHandler(Program_ExecutionTimeChanged);
355          swx.Stop();
356          Console.WriteLine("Task {0} finished.", Task.CurrentId);
357          return swx.Elapsed;
358        }, algs[i]);
359      }
360      Console.WriteLine("Creating tasks finished.");
361      counter = 0;
362      Stopwatch sw = new Stopwatch();
363      sw.Start();
364      foreach (var task in tasks) task.Start();
365      Task.WaitAll(tasks);
366      sw.Stop();
367
368      if (!algs.All(alg => alg.ExecutionState == ExecutionState.Stopped))
369        throw new Exception("Not all algs stopped properly");
370
371      if (!algs.All(alg => ((DoubleValue)alg.Results["BestQuality"].Value).Value == ((DoubleValue)algs.First().Results["BestQuality"].Value).Value))
372        throw new Exception("Not all algs have the same resutls");
373
374      if (tb != null) {
375        double totalExecutionTimeMilliseconds = algs.Select(x => x.ExecutionTime.TotalMilliseconds).Sum();
376        double totalMilliseconds = tasks.Select(t => t.Result.TotalMilliseconds).Sum();
377        tb.AppendRow(
378          taskCount.ToString(),
379          executionTimeUpdateIntervalMs.ToString(),
380          TimeSpan.FromMilliseconds(totalExecutionTimeMilliseconds).ToString(),
381          TimeSpan.FromMilliseconds(totalExecutionTimeMilliseconds / taskCount).ToString(),
382          sw.Elapsed.ToString(),
383          TimeSpan.FromMilliseconds(totalMilliseconds).ToString(),
384          (totalMilliseconds / sw.ElapsedMilliseconds).ToString("0.00"),
385          counter.ToString(),
386          (totalExecutionTimeMilliseconds / counter).ToString("0.00"));
387      }
388      tasks = null;
389      algs = null;
390      GC.Collect();
391      Console.WriteLine("Test finished.");
392    }
393
394    private static int counter = 0;
395    static void Program_ExecutionTimeChanged(object sender, EventArgs e) {
396      System.Threading.Interlocked.Increment(ref counter);
397    }
398
399    private static void TestWaitAny() {
400      System.Random rand = new System.Random();
401      var tasks = new List<Task<int>>();
402      for (int i = 0; i < 10; i++) {
403        tasks.Add(Task.Factory.StartNew<int>((x) => {
404          int sleep = ((int)x - 10) * -1000;
405          Console.WriteLine("sleeping: {0} ms", sleep);
406          Thread.Sleep(0); // make context switch
407          Thread.Sleep(sleep);
408          return (int)x * (int)x;
409        }, i));
410      }
411
412      // --> WaitAll processes tasks lazy but in order.
413      Task.WaitAll();
414      foreach (var task in tasks) {
415        Console.WriteLine(task.Result);
416      }
417
418      // -> WaitAny processes any finished task first. but the finished task needs to be removed from list in order to process all tasks
419      //for (int i = 0; i < 10; i++) {
420      //  var tasksArray = tasks.ToArray();
421      //  var task = tasksArray[Task.WaitAny(tasksArray)];
422      //  Console.WriteLine(task.Result);
423      //  tasks.Remove(task);
424      //}
425
426      Console.WriteLine("Finished TestWaitAny");
427    }
428
429    private static void TestAlgorithmPerformanceIssue() {
430      Queue<TimeSpan> latestExecutionTimes = new Queue<TimeSpan>();
431      int size = 10;
432      var random = new Random.MersenneTwister(0);
433
434      GeneticAlgorithm ga = new GeneticAlgorithm();
435      ga.PopulationSize.Value = 5;
436      ga.MaximumGenerations.Value = 5;
437      ga.Engine = new SequentialEngine.SequentialEngine();
438      ga.Problem = new SingleObjectiveTestFunctionProblem();
439
440      //MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
441      ////metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
442      //GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
443      //ParameterConfigurationTree algorithmVc = SetupGAAlgorithm(typeof(GeneticAlgorithm), metaOptimizationProblem);
444      //algorithmVc.Randomize(random);
445      Stopwatch sw = new Stopwatch();
446
447      var algs = new Queue<IAlgorithm>(); // keep them in memory
448      // -> BINGO! -> .NET cannot hold more than 16 algorithms with their ThreadLocal<T> objects efficiently,
449      // so if they are kept in memory, runtime at the 17. execution drops significantly
450      // because creating ThreadLocal<T> takes all the runtime.
451      // when the algs are not stored in a list however this effect does not occur.
452
453
454      for (int i = 0; i < 1000; i++) {
455        GeneticAlgorithm clonedGa = (GeneticAlgorithm)ga.Clone();
456        clonedGa.Name = "CLONED GA";
457        //algorithmVc.Randomize(random);
458        //algorithmVc.Parameterize(clonedGa);
459        clonedGa.Prepare(true);
460        sw.Start();
461        algs.Enqueue(clonedGa);
462
463        //if (algs.Count > 24)
464        //  algs.Dequeue();
465        clonedGa.StartSync();
466        sw.Stop();
467        latestExecutionTimes.Enqueue(sw.Elapsed);
468        Console.WriteLine("{0}: {1} ({2})", i, sw.Elapsed, latestExecutionTimes.Count > size ? TimeSpan.FromMilliseconds(latestExecutionTimes.Average(t => t.TotalMilliseconds)).ToString() : "-");
469        if (latestExecutionTimes.Count > size) {
470          latestExecutionTimes.Dequeue();
471        }
472        sw.Reset();
473      }
474    }
475
476    private static void TestTableBuilder() {
477      TableBuilder tb = new TableBuilder("column_1", "col2", "col3");
478      tb.AppendRow("1", "humpi", "0.23124");
479      tb.AppendRow("2", "sf", "0.23124");
480      tb.AppendRow("5", "humpi dampti", "0.224");
481      tb.AppendRow("10", "egon asdf", "0.4");
482      tb.AppendRow("15", "MichaelizcMultiVfds", "0.23124564");
483      Console.WriteLine(tb.ToString());
484    }
485
486    private static void TestToInfoString(IValueConfiguration algorithmVc) {
487      var random = new MersenneTwister();
488      Console.WriteLine(algorithmVc.ParameterInfoString);
489      algorithmVc.Randomize(random);
490      Console.WriteLine(algorithmVc.ParameterInfoString);
491      algorithmVc.Randomize(random);
492      Console.WriteLine(algorithmVc.ParameterInfoString);
493      algorithmVc.Randomize(random);
494    }
495
496    private static void TestCombinations() {
497      Console.WriteLine("IntRange 3-18:3");
498      IntValueRange intRange = new IntValueRange(new IntValue(3), new IntValue(18), new IntValue(3));
499      foreach (var val in intRange.GetCombinations()) {
500        Console.WriteLine(val);
501      }
502
503      Console.WriteLine("DoubleRange 1.0-2.5:0.5");
504      var dblRange = new DoubleValueRange(new DoubleValue(0.7), new DoubleValue(2.8), new DoubleValue(0.5));
505      foreach (var val in dblRange.GetCombinations()) {
506        Console.WriteLine(val);
507      }
508
509      Console.WriteLine("PercentRange 33%-66%:33%");
510      var pctRange = new PercentValueRange(new PercentValue(0.32), new PercentValue(0.98), new PercentValue(0.33));
511      foreach (var val in pctRange.GetCombinations()) {
512        Console.WriteLine(val);
513      }
514    }
515
516    private static void TestCombinations3() {
517      Node root = new Node("root");
518      root.ChildNodes.Add(new Node("root.n1"));
519      root.ChildNodes.Add(new Node("root.n2"));
520      Node n3 = new Node("root.n3");
521      n3.ChildNodes.Add(new Node("root.n3.n1"));
522      n3.ChildNodes.Add(new Node("root.n3.n2"));
523      root.ChildNodes.Add(n3);
524
525      Console.WriteLine(root.ToString());
526      Console.WriteLine("--");
527      int cnt = 0;
528      var enumerator = new NodeEnumerator(root);
529      enumerator.Reset();
530      while (enumerator.MoveNext()) {
531        Console.WriteLine(enumerator.Current.ToString());
532        cnt++;
533      }
534      Console.WriteLine("count: " + cnt);
535    }
536
537    private static void TestEnumeratorCollectionEnumerator() {
538      IEnumerable<int> list1 = new int[] { 1, 2, 3, 4, 5 };
539      IEnumerable<int> list2 = new int[] { 10, 20, 30 };
540      IEnumerable<int> list3 = new int[] { 300, 400, 500 };
541
542      var enumerators = new List<IEnumerator>();
543
544      EnumeratorCollectionEnumerator<int> enu = new EnumeratorCollectionEnumerator<int>();
545      enu.AddEnumerator(list1.GetEnumerator());
546      enu.AddEnumerator(list2.GetEnumerator());
547      enu.AddEnumerator(list3.GetEnumerator());
548      enu.Reset();
549      while (enu.MoveNext()) {
550        Console.WriteLine(enu.Current);
551      }
552    }
553
554    private static void TestCombinations4() {
555      GeneticAlgorithm ga = new GeneticAlgorithm();
556      ga.Problem = new SingleObjectiveTestFunctionProblem();
557      ga.Engine = new SequentialEngine.SequentialEngine();
558
559      ParameterConfigurationTree vc = new ParameterConfigurationTree(ga, new SingleObjectiveTestFunctionProblem());
560
561      ConfigurePopulationSize(vc, 20, 100, 20);
562      //ConfigureMutationRate(vc, 0.10, 0.60, 0.10);
563      ConfigureMutationOperator(vc);
564      //ConfigureSelectionOperator(vc, true);
565
566      int count = 0;
567      IEnumerator enumerator = new ParameterCombinationsEnumerator(vc);
568      enumerator.Reset();
569      while (enumerator.MoveNext()) {
570        var current = (IValueConfiguration)enumerator.Current;
571        count++;
572        Console.WriteLine(current.ParameterInfoString);
573      }
574      Console.WriteLine("You are about to create {0} algorithms.", count);
575
576      Experiment experiment = vc.GenerateExperiment(ga);
577      //foreach (var opt in experiment.Optimizers) {
578      //  Console.WriteLine(opt.Name);
579      //}
580
581      experiment.Prepare();
582      experiment.Start();
583
584      while (experiment.ExecutionState != ExecutionState.Stopped) {
585        Thread.Sleep(500);
586      }
587    }
588
589    private static void TestOperators() {
590      IRandom random = new MersenneTwister();
591
592      var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(100), new DoubleValue(0.1));
593      using (var sw = new StreamWriter("out-DoubleValue.txt")) {
594        for (int i = 0; i < 10000; i++) {
595          var val = new DoubleValue(90);
596          NormalDoubleValueManipulator.ApplyStatic(random, val, doubleRange);
597
598          sw.WriteLine(val);
599        }
600      }
601
602      var percentRange = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.001));
603      using (var sw = new StreamWriter("out-PercentValue.txt")) {
604        for (int i = 0; i < 10000; i++) {
605          var val = new PercentValue(0.5);
606          NormalDoubleValueManipulator.ApplyStatic(random, val, percentRange.AsDoubleValueRange());
607          sw.WriteLine(val);
608        }
609      }
610
611      var intRange = new IntValueRange(new IntValue(0), new IntValue(100), new IntValue(1));
612      using (var sw = new StreamWriter("out-IntValue.txt")) {
613        for (int i = 0; i < 10000; i++) {
614          var val = new IntValue(50);
615          UniformIntValueManipulator.ApplyStatic(random, val, intRange);
616          sw.WriteLine(val);
617        }
618      }
619
620      Console.ReadLine();
621    }
622
623    private static void TestTypeDiscovery() {
624      var items = ApplicationManager.Manager.GetInstances(typeof(DoubleArray)).ToArray();
625
626      foreach (var item in items) {
627        Console.WriteLine(item.ToString());
628      }
629    }
630
631    private static void TestMemoryLeak(GeneticAlgorithm metaLevelAlgorithm) {
632      IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).ParameterConfigurationTree;
633
634      Console.WriteLine("Starting Memory Test...");
635      Console.ReadLine();
636
637      var clones = new List<object>();
638      for (int i = 0; i < 1000; i++) {
639        var clone = algorithmVc.Clone();
640        clones.Add(clone);
641      }
642
643      Console.WriteLine("Finished. Now GC...");
644      Console.ReadLine();
645
646      GC.Collect();
647
648      Console.WriteLine("Finished!");
649      Console.ReadLine();
650    }
651
652    private static GeneticAlgorithm GetMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
653      GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm();
654      metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
655      metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
656
657      metaLevelAlgorithm.Problem = metaOptimizationProblem;
658      metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
659
660      metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Where(x => x.GetType() == typeof(ParameterConfigurationOnePositionsManipulator)).Single();
661      //metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Where(x => x.GetType() == typeof(ParameterConfigurationAllPositionsManipulator)).Single();
662
663      metaLevelAlgorithm.MutationProbability.Value = mutationProbability;
664      //metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(LinearRankSelector)).Single();
665      //metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(TournamentSelector)).Single();
666      //metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(GenderSpecificSelector)).Single();
667      //metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(BestSelector)).Single();
668      metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(ProportionalSelector)).Single();
669
670      return metaLevelAlgorithm;
671    }
672
673    private static GeneticAlgorithm GetParallelMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
674      GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
675      metaLevelAlgorithm.Engine = new ParallelEngine.ParallelEngine();
676      return metaLevelAlgorithm;
677    }
678
679    //private static GeneticAlgorithm GetHiveParallelMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
680    //  GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
681    //  metaLevelAlgorithm.Engine = new HiveEngine.HiveEngine();
682    //  ServiceLocator.Instance.ClientFacadePool.UserName = "cneumuel";
683    //  ServiceLocator.Instance.ClientFacadePool.Password = "cneumuel";
684    //  ServiceLocator.Instance.StreamedClientFacadePool.UserName = "cneumuel";
685    //  ServiceLocator.Instance.StreamedClientFacadePool.Password = "cneumuel";
686    //  return metaLevelAlgorithm;
687    //}
688
689    private static EvolutionStrategy GetMetaES(MetaOptimizationProblem metaOptimizationProblem) {
690      EvolutionStrategy metaLevelAlgorithm = new EvolutionStrategy();
691      metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
692      metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
693
694      metaLevelAlgorithm.Problem = metaOptimizationProblem;
695      metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
696
697      metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last();
698
699      return metaLevelAlgorithm;
700    }
701
702    private static ParameterConfigurationTree SetupGAAlgorithm(Type baseLevelAlgorithmType, MetaOptimizationProblem metaOptimizationProblem) {
703      metaOptimizationProblem.AlgorithmType.Value = baseLevelAlgorithmType;
704      //metaOptimizationProblem.Problems.Clear();
705
706      //metaOptimizationProblem.ProblemType.Value = typeof(SingleObjectiveTestFunctionProblem);
707      //metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
708      //  Evaluator = new GriewankEvaluator(),
709      //  ProblemSize = new IntValue(2)
710      //});
711      //metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
712      //  Evaluator = new GriewankEvaluator(),
713      //  ProblemSize = new IntValue(20)
714      //});
715      //metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
716      //  Evaluator = new GriewankEvaluator(),
717      //  ProblemSize = new IntValue(500)
718      //});
719
720      metaOptimizationProblem.ProblemType.Value = typeof(SymbolicRegressionSingleObjectiveProblem);
721     
722      ParameterConfigurationTree algorithmVc = metaOptimizationProblem.ParameterConfigurationTree;
723      ((IntValue)algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MaximumGenerations").ActualValue.Value).Value = baseAlgorithmMaxGenerations;
724
725      //ConfigurePopulationSize(algorithmVc, 15, 20, 1);
726      //ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01);
727      //ConfigureMutationOperator(algorithmVc);
728      //ConfigureElites(algorithmVc, 0, 8, 1);
729      //ConfigureSelectionOperator(algorithmVc, true);
730
731      ConfigureSymbolicExpressionGrammar(algorithmVc);
732
733      return algorithmVc;
734    }
735
736    private static void ConfigureSymbolicExpressionGrammar(ParameterConfigurationTree vc) {
737      var pc = vc.ProblemConfiguration.ParameterConfigurations.Single(x => x.Name == "SymbolicExpressionTreeGrammar");
738      pc.Optimize = true;
739
740      SymbolicExpressionGrammarValueConfiguration symbolicExpressionGrammarVc = null;
741      foreach (var valconf in pc.ValueConfigurations) {
742        if (valconf.ActualValue.Value.ItemName != "TypeCoherentExpressionGrammar") {
743          pc.ValueConfigurations.SetItemCheckedState(valconf, false);
744        } else {
745          symbolicExpressionGrammarVc = valconf as SymbolicExpressionGrammarValueConfiguration;
746        }
747      }
748
749      ConfigureSymbolicExpressionGrammarVc(symbolicExpressionGrammarVc);
750    }
751
752    private static void ConfigureSymbolicExpressionGrammarVc(SymbolicExpressionGrammarValueConfiguration symbolicExpressionGrammarVc) {
753      symbolicExpressionGrammarVc.Optimize = true;
754      foreach (var pc in symbolicExpressionGrammarVc.ParameterConfigurations) {
755        if (pc.Name != "Constant"
756          && pc.Name != "Variable"
757          && pc.Name != "ProgramRootSymbol"
758          && pc.Name != "StartSymbol") {
759          pc.Optimize = true;
760          var initialFrequencyVc = ((ParameterizedValueConfiguration)pc.ValueConfigurations.First()).ParameterConfigurations.Single(x => x.Name == "InitialFrequency");
761          initialFrequencyVc.Optimize = true;
762        }
763      }
764      //var additionPc = symbolicExpressionGrammarVc.ParameterConfigurations.Single(x => x.Name == "Addition");
765      //additionPc.Optimize = true;
766    }
767
768    private static void TestConfiguration(ParameterConfigurationTree algorithmVc, Type baseLevelAlgorithmType, IProblem problem) {
769      IRandom rand = new FastRandom(0);
770      var baseLevelAlgorithm = (GeneticAlgorithm)MetaOptimizationUtil.CreateParameterizedAlgorithmInstance(algorithmVc, baseLevelAlgorithmType, problem);
771
772      // set random values
773      for (int i = 0; i < 10; i++) {
774        var clonedVc = (ParameterConfigurationTree)algorithmVc.Clone();
775        GeneticAlgorithm newAlg = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
776        clonedVc.Randomize(rand);
777        clonedVc.Parameterize(newAlg);
778        Console.WriteLine(string.Format("PopSize: original: {0}, randomized: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
779        Console.WriteLine(string.Format("MutRate: original: {0}, randomized: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
780        Console.WriteLine(string.Format("MutOp: original: {0}, randomized: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
781        Console.WriteLine(string.Format("SelOp: original: {0}, randomized: {1}", baseLevelAlgorithm.Selector, newAlg.Selector));
782        //Console.WriteLine(string.Format("GrSi: original: {0}, randomized: {1}", "?", ((TournamentSelector)newAlg.Selector).GroupSizeParameter.Value));
783        Console.WriteLine("---");
784      }
785
786      Console.WriteLine("=======================");
787      algorithmVc.Randomize(rand);
788      algorithmVc.Parameterize(baseLevelAlgorithm);
789      // mutate
790      for (int i = 0; i < 10; i++) {
791        var clonedVc = (ParameterConfigurationTree)algorithmVc.Clone();
792        GeneticAlgorithm newAlg = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
793        ParameterConfigurationManipulator.Apply(rand, clonedVc, new UniformIntValueManipulator(), new NormalDoubleValueManipulator());
794        clonedVc.Parameterize(newAlg);
795
796        Console.WriteLine(string.Format("PopSize: original: {0}, mutated: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
797        Console.WriteLine(string.Format("MutRate: original: {0}, mutated: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
798        Console.WriteLine(string.Format("MutOp: original: {0}, mutated: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
799        Console.WriteLine(string.Format("SelOp: original: {0}, mutated: {1}", baseLevelAlgorithm.Selector, newAlg.Selector));
800        //Console.WriteLine(string.Format("GrSi: original: {0}, mutated: {1}", ((TournamentSelector)baseLevelAlgorithm.Selector).GroupSizeParameter.Value, ((TournamentSelector)newAlg.Selector).GroupSizeParameter.Value));
801        Console.WriteLine("---");
802      }
803
804      Console.WriteLine("=======================");
805      // cross
806      for (int i = 0; i < 10; i++) {
807        var clonedVc1 = (ParameterConfigurationTree)algorithmVc.Clone();
808        var clonedVc2 = (ParameterConfigurationTree)algorithmVc.Clone();
809
810        GeneticAlgorithm first = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
811        GeneticAlgorithm second = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
812
813        clonedVc1.Randomize(rand);
814        clonedVc1.Parameterize(first);
815
816        clonedVc2.Randomize(rand);
817        clonedVc2.Parameterize(second);
818
819        var popSizeBefore = first.PopulationSize.Value;
820        var mutRateBefore = first.MutationProbability.Value;
821        var mutOpBefore = first.Mutator;
822        var selOpBefore = first.Selector;
823        //var groupSizeBefore = ((TournamentSelector)first.Selector).GroupSizeParameter.Value.Value;
824
825        //clonedVc1.Cross(clonedVc2, rand); todo
826
827        ParameterConfigurationCrossover.Apply(rand, clonedVc1, clonedVc2, new DiscreteIntValueCrossover(), new AverageDoubleValueCrossover());
828        clonedVc1.Parameterize(first);
829
830        Console.WriteLine(string.Format("PopSize: first: {0}, second: {1}, crossed: {2}", popSizeBefore, second.PopulationSize, first.PopulationSize));
831        Console.WriteLine(string.Format("MutRate: first: {0}, second: {1}, crossed: {2}", mutRateBefore, second.MutationProbability, first.MutationProbability));
832        Console.WriteLine(string.Format("MutOp: first: {0}, second: {1}, crossed: {2}", mutOpBefore, second.Mutator, first.Mutator));
833        Console.WriteLine(string.Format("SelOp: first: {0}, second: {1}, crossed: {2}", selOpBefore, second.Selector, first.Selector));
834        //Console.WriteLine(string.Format("GrSi: first: {0}, second: {1}, crossed: {2}", groupSizeBefore, ((TournamentSelector)second.Selector).GroupSizeParameter.Value, ((TournamentSelector)first.Selector).GroupSizeParameter.Value));
835        Console.WriteLine("---");
836      }
837      Console.WriteLine("=======================");
838    }
839
840    private static void ConfigureMutationOperator(ParameterConfigurationTree algorithmVc) {
841      var mutationOperator = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "Mutator").SingleOrDefault();
842      mutationOperator.Optimize = true;
843
844      // uncheck multiMutator to avoid Michalewicz issue
845      //var multiMutator = mutationOperator.ValueConfigurations.Where(x => x.ActualValue.Value != null && x.ActualValue.Value.ItemName.StartsWith("Multi")).SingleOrDefault();
846      //if (multiMutator != null) {
847      //  mutationOperator.ValueConfigurations.SetItemCheckedState(multiMutator, false);
848      //}
849
850      // add another normal - don't do this with 'new', because ActualNames will not be set correctly. It should be copied from an existing one
851      // mutationOperator.ValueConfigurations.Add(new ParameterizedValueConfiguration(new NormalAllPositionsManipulator(), typeof(NormalAllPositionsManipulator)), true);
852    }
853
854    private static void ConfigureSelectionOperator(ParameterConfigurationTree algorithmVc, bool configureTournamenSize) {
855      var selectionOperatorPc = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "Selector").SingleOrDefault();
856      selectionOperatorPc.Optimize = true;
857
858      foreach (var vc in selectionOperatorPc.ValueConfigurations) {
859        if (vc.ActualValue.ValueDataType == typeof(TournamentSelector)) {
860          selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
861          if (configureTournamenSize) {
862            vc.Optimize = true;
863            ConfigureTournamentGroupSize((ParameterizedValueConfiguration)vc);
864          }
865        } else if (vc.ActualValue.ValueDataType == typeof(RandomSelector)) {
866          selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
867        } else {
868          selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
869        }
870      }
871    }
872
873    private static void ConfigureTournamentGroupSize(ParameterizedValueConfiguration tournamentVc) {
874      var groupSizePc = tournamentVc.ParameterConfigurations.Where(x => x.ParameterName == "GroupSize").SingleOrDefault();
875      groupSizePc.Optimize = true;
876      var groupSizeVc = (RangeValueConfiguration)groupSizePc.ValueConfigurations.First();
877      groupSizeVc.Optimize = true;
878      groupSizeVc.RangeConstraint.LowerBound = new IntValue(0);
879      groupSizeVc.RangeConstraint.UpperBound = new IntValue(10);
880      groupSizeVc.RangeConstraint.StepSize = new IntValue(1);
881    }
882
883    private static void ConfigurePopulationSize(ParameterConfigurationTree algorithmVc, int lower, int upper, int stepsize) {
884      var populationSizePc = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "PopulationSize").SingleOrDefault();
885      populationSizePc.Optimize = true;
886      var populationSizeVc = (RangeValueConfiguration)populationSizePc.ValueConfigurations.First();
887      populationSizeVc.Optimize = true;
888      populationSizeVc.RangeConstraint.LowerBound = new IntValue(lower);
889      populationSizeVc.RangeConstraint.UpperBound = new IntValue(upper);
890      populationSizeVc.RangeConstraint.StepSize = new IntValue(stepsize);
891    }
892
893    private static void ConfigureMutationRate(ParameterConfigurationTree algorithmVc, double lower, double upper, double stepsize) {
894      var mutationRatePc = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "MutationProbability").SingleOrDefault();
895      mutationRatePc.Optimize = true;
896      var mutationRateVc = (RangeValueConfiguration)mutationRatePc.ValueConfigurations.First();
897      mutationRateVc.Optimize = true;
898      mutationRateVc.RangeConstraint.LowerBound = new PercentValue(lower);
899      mutationRateVc.RangeConstraint.UpperBound = new PercentValue(upper);
900      mutationRateVc.RangeConstraint.StepSize = new PercentValue(stepsize);
901    }
902
903    private static void ConfigureElites(ParameterConfigurationTree algorithmVc, int from, int to, int stepSize) {
904      var elitesPc = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "Elites").SingleOrDefault();
905      elitesPc.Optimize = true;
906      var elitesVc = (RangeValueConfiguration)elitesPc.ValueConfigurations.First();
907      elitesVc.Optimize = true;
908      elitesVc.RangeConstraint.LowerBound = new IntValue(from);
909      elitesVc.RangeConstraint.UpperBound = new IntValue(to);
910      elitesVc.RangeConstraint.StepSize = new IntValue(stepSize);
911    }
912
913    private static void TestOptimization(EngineAlgorithm metaLevelAlgorithm) {
914      string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Results");
915      if (!Directory.Exists(path))
916        Directory.CreateDirectory(path);
917      string id = DateTime.Now.ToString("yyyy.MM.dd - HH;mm;ss,ffff");
918      string resultPath = Path.Combine(path, string.Format("{0} - Result.hl", id));
919      string outputPath = Path.Combine(path, string.Format("{0} - Console.txt", id));
920
921      ContentManager.Save((IStorableContent)metaLevelAlgorithm, resultPath+"-before.hl", true);
922
923      using (var sw = new StreamWriter(outputPath)) {
924        sw.AutoFlush = true;
925
926        StringBuilder sb1 = new StringBuilder();
927        sb1.AppendFormat("Meta.PopulationSize: {0}\n", metaAlgorithmPopulationSize);
928        sb1.AppendFormat("Meta.MaxGenerations: {0}\n", metaAlgorithmMaxGenerations);
929        sb1.AppendFormat("Meta.Repetitions   : {0}\n", metaProblemRepetitions);
930        sb1.AppendFormat("Meta.MutProb       : {0}\n", ((GeneticAlgorithm)metaLevelAlgorithm).MutationProbability.Value);
931        sb1.AppendFormat("Base.MaxGenerations: {0}\n", baseAlgorithmMaxGenerations);
932        sb1.AppendLine("Problems:");
933        foreach (var prob in ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).Problems) {
934          sb1.Append(prob.Name);
935          var sotf = prob as SingleObjectiveTestFunctionProblem;
936          if (sotf != null) {
937            sb1.AppendFormat(" {0}", sotf.ProblemSize.Value);
938          }
939          sb1.AppendLine();
940        }
941        sw.WriteLine(sb1.ToString());
942        Console.WriteLine(sb1.ToString());
943        metaLevelAlgorithm.Stopped += new EventHandler(metaLevelAlgorithm_Stopped);
944        metaLevelAlgorithm.Paused += new EventHandler(metaLevelAlgorithm_Paused);
945        metaLevelAlgorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(metaLevelAlgorithm_ExceptionOccurred);
946
947        metaLevelAlgorithm.Start();
948        int i = 0;
949        int currentGeneration = -1;
950        do {
951          Thread.Sleep(1000);
952          if (metaLevelAlgorithm.Results.ContainsKey("Generations") && ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value != currentGeneration) {
953            while (metaLevelAlgorithm.Results.Count < 6) Thread.Sleep(1000);
954            StringBuilder sb = new StringBuilder();
955            sb.AppendLine(DateTime.Now.ToLongTimeString());
956            sb.AppendLine("=================================");
957
958            sb.AppendLine(metaLevelAlgorithm.ExecutionState.ToString());
959            ResultCollection rsClone = null;
960            while (rsClone == null) {
961              try {
962                rsClone = (ResultCollection)metaLevelAlgorithm.Results.Clone();
963              }
964              catch { }
965            }
966            foreach (var result in rsClone) {
967              sb.AppendLine(result.ToString());
968              if (result.Name == "Population") {
969                RunCollection rc = (RunCollection)result.Value;
970                var orderedRuns = rc.OrderBy(x => x.Results["AverageQualityNormalized"]);
971
972                TableBuilder tb = new TableBuilder("QNorm", "Qualities"/*, "PoSi"*/ /*,"MutRa"*/ /*,"Eli", "SelOp",*/ /*"MutOp"*//*, "NrSelSubScopes"*/);
973                foreach (IRun run in orderedRuns) {
974                  //string selector;
975                  //if (run.Parameters["Selector"] is TournamentSelector) {
976                  //  selector = string.Format("{0} ({1})", run.Parameters["Selector"].ToString(), ((TournamentSelector)run.Parameters["Selector"]).GroupSizeParameter.Value.ToString());
977                  //} else {
978                  //  selector = string.Format("{0}", run.Parameters["Selector"].ToString());
979                  //}
980
981                  tb.AppendRow(
982                    ((DoubleValue)run.Results["AverageQualityNormalized"]).Value.ToString("#0.0000")
983                    ,((DoubleArray)run.Results["RunsAverageQualities"]).ToString()
984                    //,((IntValue)run.Parameters["PopulationSize"]).Value.ToString()
985                    //,((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.0000")
986                    //,((IntValue)run.Parameters["Elites"]).Value.ToString()
987                    //,Shorten(selector, 20)
988                    //,Shorten(run.Parameters.ContainsKey("Mutator") ? run.Parameters["Mutator"].ToString() : "null", 40)
989                    //,((ISelector)run.Parameters["Selector"]).NumberOfSelectedSubScopesParameter.Value.ToString()
990                    );
991                }
992                sb.AppendLine(tb.ToString());
993              }
994            } // foreach
995            //Console.Clear();
996            Console.WriteLine(sb.ToString());
997            sw.WriteLine(sb.ToString());
998            currentGeneration = ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value;
999          } // if
1000          //if (i % 30 == 0) GC.Collect();
1001          i++;
1002        } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped);
1003      }
1004
1005      Console.WriteLine();
1006      Console.WriteLine("Storing...");
1007
1008      ContentManager.Save((IStorableContent)metaLevelAlgorithm, resultPath, true);
1009      Console.WriteLine("Finished");
1010    }
1011
1012    private static void metaLevelAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
1013      Console.WriteLine("metaLevelAlgorithm_ExceptionOccurred");
1014      Console.WriteLine(e.Value.ToString());
1015      if (e.Value.InnerException != null) {
1016        Console.WriteLine(e.Value.InnerException.ToString());
1017      }
1018    }
1019
1020    private static void metaLevelAlgorithm_Paused(object sender, EventArgs e) {
1021      Console.WriteLine("metaLevelAlgorithm_Paused");
1022    }
1023
1024    private static void metaLevelAlgorithm_Stopped(object sender, EventArgs e) {
1025      Console.WriteLine("metaLevelAlgorithm_Stopped");
1026    }
1027
1028    private static void TestShorten() {
1029      int n = 8;
1030      Console.WriteLine(Shorten("1", n));
1031      Console.WriteLine(Shorten("12", n));
1032      Console.WriteLine(Shorten("123", n));
1033      Console.WriteLine(Shorten("1234", n));
1034      Console.WriteLine(Shorten("12345", n));
1035      Console.WriteLine(Shorten("123456", n));
1036      Console.WriteLine(Shorten("1234567", n));
1037      Console.WriteLine(Shorten("12345678", n));
1038      Console.WriteLine(Shorten("123456789", n));
1039      Console.WriteLine(Shorten("1234567890", n));
1040      Console.WriteLine(Shorten("12345678901", n));
1041    }
1042
1043    private static string Shorten(string s, int n) {
1044      string placeholder = "..";
1045      if (s.Length <= n) return s;
1046      int len = n / 2 - placeholder.Length / 2;
1047      string start = s.Substring(0, len);
1048      string end = s.Substring(s.Length - len, len);
1049      return start + placeholder + end;
1050    }
1051
1052    private static void TestIntSampling() {
1053      System.Random rand = new System.Random();
1054      int lower = 10;
1055      int upper = 20;
1056      int stepsize = 1;
1057      for (int i = 0; i < 100; i++) {
1058        int val;
1059        do {
1060          val = rand.Next(lower / stepsize, upper / stepsize + 1) * stepsize;
1061        } while (val < lower || val > upper);
1062        Console.WriteLine(val);
1063      }
1064    }
1065
1066    private static void TestDoubleSampling() {
1067      System.Random rand = new System.Random();
1068      double lower = 2;
1069      double upper = 3;
1070      double stepsize = 0.6;
1071      for (int i = 0; i < 100; i++) {
1072        double val;
1073        do {
1074          val = Math.Round((rand.NextDouble() * (upper - lower) + lower) / stepsize, 0) * stepsize;
1075        } while (val < lower || val > upper);
1076        Console.WriteLine(val);
1077      }
1078    }
1079
1080    private static IEnumerable<IItem> GetValidValues(IValueParameter valueParameter) {
1081      return ApplicationManager.Manager.GetInstances(valueParameter.DataType).Select(x => (IItem)x).OrderBy(x => x.ItemName);
1082    }
1083  }
1084
1085  public class Node {
1086    public string Name { get; set; }
1087    public int ActualValue { get; set; }
1088    public int[] PossibleValues { get; set; }
1089    public List<Node> ChildNodes { get; set; }
1090
1091    public Node(string name) {
1092      this.Name = name;
1093      PossibleValues = new int[] { 1, 2, 3 };
1094      ChildNodes = new List<Node>();
1095    }
1096
1097    public void Init() {
1098      this.ActualValue = PossibleValues.First();
1099      foreach (var child in ChildNodes) {
1100        child.Init();
1101      }
1102    }
1103
1104    public override string ToString() {
1105      StringBuilder sb = new StringBuilder();
1106      sb.Append(string.Format("{0}:{1}", this.Name, this.ActualValue));
1107      if (this.ChildNodes.Count() > 0) {
1108        sb.Append(" (");
1109        var lst = new List<string>();
1110        foreach (Node child in ChildNodes) {
1111          lst.Add(child.ToString());
1112        }
1113        sb.Append(string.Join(", ", lst.ToArray()));
1114        sb.Append(")");
1115      }
1116
1117      return sb.ToString();
1118    }
1119  }
1120
1121  public class NodeEnumerator : IEnumerator<Node> {
1122    private Node node;
1123    private List<IEnumerator> enumerators;
1124
1125    public NodeEnumerator(Node node) {
1126      this.node = node;
1127      this.enumerators = new List<IEnumerator>();
1128    }
1129
1130    public Node Current {
1131      get { return node; }
1132    }
1133    object IEnumerator.Current {
1134      get { return Current; }
1135    }
1136
1137    public void Dispose() { }
1138
1139    public bool MoveNext() {
1140      int i = 0;
1141      bool ok = false;
1142      while (!ok && i < enumerators.Count) {
1143        if (enumerators[i].MoveNext()) {
1144          ok = true;
1145        } else {
1146          i++;
1147        }
1148      }
1149
1150      if (ok) {
1151        for (int k = i - 1; k >= 0; k--) {
1152          enumerators[k].Reset();
1153          enumerators[k].MoveNext();
1154        }
1155      } else {
1156        return false;
1157      }
1158
1159      node.ActualValue = (int)enumerators[0].Current;
1160      return true;
1161    }
1162
1163    public void Reset() {
1164      enumerators.Clear();
1165      enumerators.Add(node.PossibleValues.GetEnumerator());
1166      enumerators[0].Reset();
1167
1168      foreach (var child in node.ChildNodes) {
1169        var enumerator = new NodeEnumerator(child);
1170        enumerator.Reset();
1171        enumerator.MoveNext();
1172        enumerators.Add(enumerator);
1173      }
1174    }
1175  }
1176}
Note: See TracBrowser for help on using the repository browser.