Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

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