Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

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