Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

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