Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

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