Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6185 was 6090, checked in by cneumuel, 14 years ago

#1215

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