Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6090 was 6090, checked in by cneumuel, 14 years ago

#1215

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