Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5361 was 5361, checked in by cneumuel, 13 years ago

#1215

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