Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/01/10 20:37:36 (13 years ago)
Author:
cneumuel
Message:

#1215 worked on metaoptimization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs

    r4997 r5009  
    55using HeuristicLab.Core;
    66using HeuristicLab.PluginInfrastructure;
     7using HeuristicLab.Parameters;
    78using HeuristicLab.Problems.MetaOptimization;
    89using HeuristicLab.Data;
    910using System;
     11using System.Threading;
     12using HeuristicLab.Random;
     13using HeuristicLab.Optimization;
    1014
    1115namespace HeuristicLab.MetaOptimization.Test {
    1216  class Program {
     17    private static int metaAlgorithmPopulationSize = 20;
     18    private static int metaAlgorithmMaxGenerations = 100;
     19
     20    private static int baseAlgorithmPopulationSize = 20;
     21    private static int baseAlgorithmMaxGenerations = 30;
     22
    1323    static void Main(string[] args) {
    1424      //TestIntSampling();
     
    1727      GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
    1828      MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
     29      GeneticAlgorithm metaLevelAlgorithm = GetMetaAlgorithm(metaOptimizationProblem);
     30
     31      IValueConfiguration algorithmVc = SetupAlgorithm(baseLevelAlgorithm, metaOptimizationProblem);
     32
     33      Console.WriteLine("Press enter to start");
     34      Console.ReadLine();
     35      TestConfiguration(algorithmVc, baseLevelAlgorithm);
     36
     37      Console.WriteLine("Press enter to start");
     38      Console.ReadLine();
     39      TestOptimization(metaLevelAlgorithm);
     40
     41      //TestMemoryLeak(metaLevelAlgorithm);
     42
     43      Console.ReadLine();
     44    }
     45
     46    private static void TestMemoryLeak(GeneticAlgorithm metaLevelAlgorithm) {
     47      IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).AlgorithmParameterConfiguration;
     48
     49      Console.WriteLine("Starting Memory Test...");
     50      Console.ReadLine();
     51
     52      for (int i = 0; i < 1000; i++) {
     53        var clone = algorithmVc.Clone();
     54      }
     55
     56      Console.WriteLine("Finished. Now GC...");
     57      Console.ReadLine();
     58
     59      GC.Collect();
     60
     61      Console.WriteLine("Finished!");
     62      Console.ReadLine();
     63    }
     64
     65    private static GeneticAlgorithm GetMetaAlgorithm(MetaOptimizationProblem metaOptimizationProblem) {
     66      GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm();
     67      metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
     68      metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
     69
     70      metaLevelAlgorithm.Problem = metaOptimizationProblem;
     71      metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
     72      return metaLevelAlgorithm;
     73    }
     74
     75    private static IValueConfiguration SetupAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) {
     76      baseLevelAlgorithm.Problem = new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() { ProblemSize = new IntValue(2000) };
     77      baseLevelAlgorithm.PopulationSize.Value = baseAlgorithmPopulationSize;
     78      baseLevelAlgorithm.MaximumGenerations.Value = baseAlgorithmMaxGenerations;
     79
    1980      metaOptimizationProblem.Algorithm = baseLevelAlgorithm;
    2081      IValueConfiguration algorithmVc = metaOptimizationProblem.AlgorithmParameterConfiguration;
    21 
    22       //ConfigurePopulationSize(algorithmVc);
     82      metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem());
     83
     84      ConfigurePopulationSize(algorithmVc);
    2385      ConfigureMutationRate(algorithmVc);
    24 
    25       GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm();
    26       metaLevelAlgorithm.PopulationSize.Value = 10;
    27 
     86      ConfigureMutationOperator(algorithmVc);
     87      return algorithmVc;
     88    }
     89
     90    private static void TestConfiguration(IValueConfiguration algorithmVc, GeneticAlgorithm baseLevelAlgorithm) {
     91      IRandom rand = new MersenneTwister();
    2892      // set random values
    2993      for (int i = 0; i < 10; i++) {
    3094        IValueConfiguration clonedVc = (IValueConfiguration)algorithmVc.Clone();
    31         clonedVc.Randomize();
    32         clonedVc.Parameterize();
     95        clonedVc.Randomize(rand);
     96        clonedVc.Parameterize((GeneticAlgorithm)clonedVc.ActualValue.Value);
    3397        GeneticAlgorithm newAlg = (GeneticAlgorithm)clonedVc.ActualValue.Value;
    34         //Console.WriteLine(string.Format("PopSize: original: {0}, randomized: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
     98        Console.WriteLine(string.Format("PopSize: original: {0}, randomized: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
    3599        Console.WriteLine(string.Format("MutRate: original: {0}, randomized: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
     100        Console.WriteLine(string.Format("MutOp: original: {0}, randomized: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
    36101      }
    37102
     
    39104      for (int i = 0; i < 10; i++) {
    40105        IValueConfiguration clonedVc = (IValueConfiguration)algorithmVc.Clone();
    41         clonedVc.Mutate();
    42         clonedVc.Parameterize();
     106        clonedVc.Mutate(rand);
     107        clonedVc.Parameterize((GeneticAlgorithm)clonedVc.ActualValue.Value);
    43108        GeneticAlgorithm newAlg = (GeneticAlgorithm)clonedVc.ActualValue.Value;
    44         //Console.WriteLine(string.Format("PopSize: original: {0}, mutated: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
     109        Console.WriteLine(string.Format("PopSize: original: {0}, mutated: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
    45110        Console.WriteLine(string.Format("MutRate: original: {0}, mutated: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
     111        Console.WriteLine(string.Format("MutOp: original: {0}, mutated: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
    46112      }
    47113
     
    49115      for (int i = 0; i < 10; i++) {
    50116        IValueConfiguration clonedVc1 = (IValueConfiguration)algorithmVc.Clone();
    51         clonedVc1.Randomize();
    52         clonedVc1.Parameterize();
     117        clonedVc1.Randomize(rand);
     118        clonedVc1.Parameterize(baseLevelAlgorithm);
    53119
    54120        IValueConfiguration clonedVc2 = (IValueConfiguration)algorithmVc.Clone();
     
    58124        var popSizeBefore = first.PopulationSize.Value;
    59125        var mutRateBefore = first.MutationProbability.Value;
    60         clonedVc1.Cross(clonedVc2);
    61         clonedVc1.Parameterize();
    62 
    63         //Console.WriteLine(string.Format("PopSize: first: {0}, second: {1}, crossed: {2}", popSizeBefore, second.PopulationSize, first.PopulationSize));
     126        var mutOpBefore = first.Mutator;
     127
     128        clonedVc1.Cross(clonedVc2, rand);
     129        clonedVc1.Parameterize((GeneticAlgorithm)clonedVc2.ActualValue.Value);
     130
     131        Console.WriteLine(string.Format("PopSize: first: {0}, second: {1}, crossed: {2}", popSizeBefore, second.PopulationSize, first.PopulationSize));
    64132        Console.WriteLine(string.Format("MutRate: first: {0}, second: {1}, crossed: {2}", mutRateBefore, second.MutationProbability, first.MutationProbability));
    65       }
    66 
    67       Debugger.Break();
     133        Console.WriteLine(string.Format("MutRate: first: {0}, second: {1}, crossed: {2}", mutOpBefore, second.Mutator, first.Mutator));
     134      }
     135    }
     136
     137    private static void ConfigureMutationOperator(IValueConfiguration algorithmVc) {
     138      var mutationOperator = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Mutator").SingleOrDefault();
     139      mutationOperator.Optimize = true;
     140
     141      // uncheck multiMutator to avoid Michalewicz issue
     142      var multiMutator = mutationOperator.ValueConfigurations.Where(x => x.ActualValue.Value.ItemName.StartsWith("Multi")).SingleOrDefault();
     143      if (multiMutator != null) {
     144        mutationOperator.ValueConfigurations.SetItemCheckedState(multiMutator, false);
     145      }
    68146    }
    69147
     
    86164      mutationRateVc.RangeConstraint.UpperBound = new PercentValue(1.0);
    87165      mutationRateVc.RangeConstraint.StepSize = new PercentValue(0.01);
     166    }
     167
     168    private static void TestOptimization(GeneticAlgorithm metaLevelAlgorithm) {
     169      metaLevelAlgorithm.Start();
     170      do {
     171        Thread.Sleep(1000);
     172        Console.Clear();
     173        try {
     174          foreach (var result in metaLevelAlgorithm.Results) {
     175            Console.WriteLine(result.ToString());
     176            if (result.Name == "Population") {
     177              RunCollection rc = (RunCollection)result.Value;
     178              var orderedRuns = rc.OrderBy(x => x.Results["BestQuality"]);
     179              foreach (IRun run in orderedRuns) {
     180                Console.WriteLine("Q: {0} PoSi: {1} MuRa: {2} MuOp: {3}",
     181                  ((DoubleValue)run.Results["BestQuality"]).Value.ToString("0.00").PadLeft(4, ' '),
     182                  ((IntValue)run.Parameters["PopulationSize"]).Value.ToString().PadLeft(3, ' '),
     183                  ((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.00").PadRight(3, ' '),
     184                  run.Parameters["Mutator"]);
     185              }
     186            }
     187          }
     188        }
     189        catch { }
     190      } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped);
     191
     192      Console.WriteLine("Finished");
    88193    }
    89194
Note: See TracChangeset for help on using the changeset viewer.