Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7887 was 7137, checked in by ascheibe, 13 years ago

#1215 renamed ServiceLocator to HiveServiceLocator

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