Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/21/10 01:13:49 (13 years ago)
Author:
cneumuel
Message:

#1215

  • added possibility to create all parameter combinations from a ParameterConfigurationTree and generate an experiment from them
File:
1 edited

Legend:

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

    r5111 r5144  
    1 using System.Collections.Generic;
    2 using System.Diagnostics;
     1using System;
     2using System.Collections;
     3using System.Collections.Generic;
     4using System.IO;
    35using System.Linq;
     6using System.Text;
     7using System.Threading;
     8using HeuristicLab.Algorithms.EvolutionStrategy;
    49using HeuristicLab.Algorithms.GeneticAlgorithm;
     10using HeuristicLab.Common;
    511using HeuristicLab.Core;
     12using HeuristicLab.Data;
     13using HeuristicLab.Optimization;
    614using HeuristicLab.PluginInfrastructure;
    7 using HeuristicLab.Parameters;
    815using HeuristicLab.Problems.MetaOptimization;
    9 using HeuristicLab.Data;
    10 using System;
    11 using System.Threading;
     16using HeuristicLab.Problems.TestFunctions;
    1217using HeuristicLab.Random;
    13 using HeuristicLab.Optimization;
    14 using HeuristicLab.Common;
    15 using System.IO;
    16 using HeuristicLab.Problems.TestFunctions;
    17 using System.Text;
    1818using HeuristicLab.Selection;
    19 using HeuristicLab.Algorithms.EvolutionStrategy;
    20 using HeuristicLab.PluginInfrastructure.Manager;
    2119
    2220namespace HeuristicLab.MetaOptimization.Test {
     
    3533      //TestTypeDiscovery();
    3634      //TestOperators();
    37      
     35      //TestCombinations();
     36      //TestCombinations2();
     37      //TestCombinations3();
     38      TestCombinations4();
     39
    3840      GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
    3941
     
    4547      IValueConfiguration algorithmVc = SetupGAAlgorithm(baseLevelAlgorithm, metaOptimizationProblem);
    4648
     49      //TestToString(algorithmVc);
     50
     51
    4752      //Console.WriteLine("Press enter to start");
    4853      //Console.ReadLine();
    4954      //TestConfiguration(algorithmVc, baseLevelAlgorithm);
    50      
     55
    5156      //Console.WriteLine("Press enter to start");
    5257      //Console.ReadLine();
     
    5863    }
    5964
     65    private static void TestToString(IValueConfiguration algorithmVc) {
     66      var random = new MersenneTwister();
     67      Console.WriteLine(algorithmVc.ToParameterInfoString());
     68      algorithmVc.Randomize(random);
     69      Console.WriteLine(algorithmVc.ToParameterInfoString());
     70      algorithmVc.Randomize(random);
     71      Console.WriteLine(algorithmVc.ToParameterInfoString());
     72      algorithmVc.Randomize(random);
     73    }
     74
     75    private static void TestCombinations() {
     76      Console.WriteLine("IntRange 3-18:3");
     77      IntValueRange intRange = new IntValueRange(new IntValue(3), new IntValue(18), new IntValue(3));
     78      foreach (var val in intRange.GetCombinations()) {
     79        Console.WriteLine(val);
     80      }
     81
     82      Console.WriteLine("DoubleRange 1.0-2.5:0.5");
     83      var dblRange = new DoubleValueRange(new DoubleValue(0.7), new DoubleValue(2.8), new DoubleValue(0.5));
     84      foreach (var val in dblRange.GetCombinations()) {
     85        Console.WriteLine(val);
     86      }
     87
     88      Console.WriteLine("PercentRange 33%-66%:33%");
     89      var pctRange = new PercentValueRange(new PercentValue(0.32), new PercentValue(0.98), new PercentValue(0.33));
     90      foreach (var val in pctRange.GetCombinations()) {
     91        Console.WriteLine(val);
     92      }
     93    }
     94
     95    private static void TestCombinations3() {
     96      Node root = new Node("root");
     97      root.ChildNodes.Add(new Node("root.n1"));
     98      root.ChildNodes.Add(new Node("root.n2"));
     99      Node n3 = new Node("root.n3");
     100      n3.ChildNodes.Add(new Node("root.n3.n1"));
     101      n3.ChildNodes.Add(new Node("root.n3.n2"));
     102      root.ChildNodes.Add(n3);
     103
     104      Console.WriteLine(root.ToString());
     105      Console.WriteLine("--");
     106      int cnt = 0;
     107      var enumerator = new NodeEnumerator(root);
     108      enumerator.Reset();
     109      while (enumerator.MoveNext()) {
     110        Console.WriteLine(enumerator.Current.ToString());
     111        cnt++;
     112      }
     113      Console.WriteLine("count: " + cnt);
     114    }
     115
     116    private static void TestCombinations4() {
     117      GeneticAlgorithm ga = new GeneticAlgorithm();
     118      ga.Problem = new SingleObjectiveTestFunctionProblem();
     119      ParameterConfigurationTree vc = new ParameterConfigurationTree(ga);
     120
     121      //ConfigurePopulationSize(vc, 20, 100, 20);
     122      //ConfigureMutationRate(vc, 0.10, 0.60, 0.10);
     123      ConfigureMutationOperator(vc);
     124      ConfigureSelectionOperator(vc, false);
     125
     126      //foreach (var combination in vc.IterateCombinations()) {
     127      //  Console.WriteLine(combination.ToParameterInfoString());
     128      //}
     129
     130      int count = 0;
     131      IEnumerator enumerator = new ParameterCombinationsEnumerator(vc);
     132      enumerator.Reset();
     133      while (enumerator.MoveNext()) {
     134        var current = (IValueConfiguration)enumerator.Current;
     135        count++;
     136        Console.WriteLine(current.ToParameterInfoString());
     137      }
     138      Console.WriteLine("You are about to create {0} algorithms.", count);
     139
     140      Experiment experiment = vc.GenerateExperiment(ga);
     141      foreach (var opt in experiment.Optimizers) {
     142        Console.WriteLine(opt.Name);
     143      }
     144    }
     145
    60146    private static void TestOperators() {
    61147      IRandom random = new MersenneTwister();
     
    64150      manip.IntValueManipulatorParameter.ActualValue = new UniformIntValueManipulator();
    65151      manip.DoubleValueManipulatorParameter.ActualValue = new NormalDoubleValueManipulator();
    66      
     152
    67153      var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(100), new DoubleValue(0.1));
    68154      using (var sw = new StreamWriter("out-DoubleValue.txt")) {
     
    70156          var val = new DoubleValue(50);
    71157          NormalDoubleValueManipulator.ApplyStatic(random, val, doubleRange);
    72          
     158
    73159          sw.WriteLine(val);
    74160        }
     
    98184    private static void TestTypeDiscovery() {
    99185      PluginLoader.pluginAssemblies.Any();
    100      
     186
    101187      var items = ApplicationManager.Manager.GetInstances(typeof(DoubleArray)).ToArray();
    102188
     
    107193
    108194    private static void TestMemoryLeak(GeneticAlgorithm metaLevelAlgorithm) {
    109       IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).AlgorithmParameterConfiguration;
     195      IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).ParameterConfigurationTree;
    110196
    111197      Console.WriteLine("Starting Memory Test...");
     
    160246
    161247      metaOptimizationProblem.Algorithm = baseLevelAlgorithm;
    162       IValueConfiguration algorithmVc = metaOptimizationProblem.AlgorithmParameterConfiguration;
     248      IValueConfiguration algorithmVc = metaOptimizationProblem.ParameterConfigurationTree;
    163249
    164250      metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
     
    171257      });
    172258
    173       ConfigurePopulationSize(algorithmVc);
    174       ConfigureMutationRate(algorithmVc);
     259      ConfigurePopulationSize(algorithmVc, 20, 100, 1);
     260      ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01);
    175261      ConfigureMutationOperator(algorithmVc);
    176262      ConfigureElites(algorithmVc);
    177       ConfigureSelectionOperator(algorithmVc);
     263      ConfigureSelectionOperator(algorithmVc, true);
    178264      return algorithmVc;
    179265    }
     
    203289        GeneticAlgorithm newAlg = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
    204290        //clonedVc.Mutate(rand);
    205        
     291
    206292        //.Apply(rand, clonedVc); todo
    207293        clonedVc.Parameterize(newAlg);
     
    259345    }
    260346
    261     private static void ConfigureSelectionOperator(IValueConfiguration algorithmVc) {
     347    private static void ConfigureSelectionOperator(IValueConfiguration algorithmVc, bool configureTournamenSize) {
    262348      var selectionOperatorPc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Selector").SingleOrDefault();
    263349      selectionOperatorPc.Optimize = true;
     
    266352        if (vc.ActualValue.ValueDataType == typeof(TournamentSelector)) {
    267353          selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
    268           vc.Optimize = true;
    269           ConfigureTournamentGroupSize(vc);
     354          if (configureTournamenSize) {
     355            vc.Optimize = true;
     356            ConfigureTournamentGroupSize(vc);
     357          }
    270358        } else if (vc.ActualValue.ValueDataType == typeof(RandomSelector)) {
    271359          selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
     
    286374    }
    287375
    288     private static void ConfigurePopulationSize(IValueConfiguration algorithmVc) {
     376    private static void ConfigurePopulationSize(IValueConfiguration algorithmVc, int lower, int upper, int stepsize) {
    289377      var populationSizePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "PopulationSize").SingleOrDefault();
    290378      populationSizePc.Optimize = true;
    291379      var populationSizeVc = populationSizePc.ValueConfigurations.First();
    292380      populationSizeVc.Optimize = true;
    293       populationSizeVc.RangeConstraint.LowerBound = new IntValue(20);
    294       populationSizeVc.RangeConstraint.UpperBound = new IntValue(100);
    295       populationSizeVc.RangeConstraint.StepSize = new IntValue(1);
    296     }
    297 
    298     private static void ConfigureMutationRate(IValueConfiguration algorithmVc) {
     381      populationSizeVc.RangeConstraint.LowerBound = new IntValue(lower);
     382      populationSizeVc.RangeConstraint.UpperBound = new IntValue(upper);
     383      populationSizeVc.RangeConstraint.StepSize = new IntValue(stepsize);
     384    }
     385
     386    private static void ConfigureMutationRate(IValueConfiguration algorithmVc, double lower, double upper, double stepsize) {
    299387      var mutationRatePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "MutationProbability").SingleOrDefault();
    300388      mutationRatePc.Optimize = true;
    301389      var mutationRateVc = mutationRatePc.ValueConfigurations.First();
    302390      mutationRateVc.Optimize = true;
    303       mutationRateVc.RangeConstraint.LowerBound = new PercentValue(0.0);
    304       mutationRateVc.RangeConstraint.UpperBound = new PercentValue(1.0);
    305       mutationRateVc.RangeConstraint.StepSize = new PercentValue(0.01);
     391      mutationRateVc.RangeConstraint.LowerBound = new PercentValue(lower);
     392      mutationRateVc.RangeConstraint.UpperBound = new PercentValue(upper);
     393      mutationRateVc.RangeConstraint.StepSize = new PercentValue(stepsize);
    306394    }
    307395
     
    446534    }
    447535  }
     536
     537  public class Node {
     538    public string Name { get; set; }
     539    public int ActualValue { get; set; }
     540    public int[] PossibleValues { get; set; }
     541    public List<Node> ChildNodes { get; set; }
     542
     543    public Node(string name) {
     544      this.Name = name;
     545      PossibleValues = new int[] { 1, 2, 3 };
     546      ChildNodes = new List<Node>();
     547    }
     548
     549    public void Init() {
     550      this.ActualValue = PossibleValues.First();
     551      foreach (var child in ChildNodes) {
     552        child.Init();
     553      }
     554    }
     555
     556    public IEnumerable<Node> IterateCombinations() {
     557      foreach (int val in PossibleValues) {
     558        this.ActualValue = val;
     559        if (ChildNodes.Count > 0) {
     560          List<IEnumerable<Node>> lists = new List<IEnumerable<Node>>();
     561          List<IEnumerator<Node>> enumerators = new List<IEnumerator<Node>>();
     562
     563          foreach (Node child in ChildNodes) {
     564            IEnumerable<Node> combinations = child.IterateCombinations();
     565            IEnumerator<Node> enumerator = combinations.GetEnumerator();
     566            enumerator.MoveNext(); // initialize
     567            lists.Add(combinations);
     568            enumerators.Add(enumerator);
     569          }
     570
     571
     572          bool abort = false;
     573          while (!abort) {
     574            abort = true;
     575            foreach (var enumerator in enumerators) {
     576              if (enumerator.MoveNext()) {
     577                abort = false;
     578                yield return this;
     579              }
     580            }
     581          }
     582
     583        } else {
     584          yield return this;
     585        }
     586      }
     587    }
     588
     589    public override string ToString() {
     590      StringBuilder sb = new StringBuilder();
     591      sb.Append(string.Format("{0}:{1}", this.Name, this.ActualValue));
     592      if (this.ChildNodes.Count() > 0) {
     593        sb.Append(" (");
     594        var lst = new List<string>();
     595        foreach (Node child in ChildNodes) {
     596          lst.Add(child.ToString());
     597        }
     598        sb.Append(string.Join(", ", lst.ToArray()));
     599        sb.Append(")");
     600      }
     601
     602      return sb.ToString();
     603    }
     604  }
     605
     606  public class NodeEnumerator : IEnumerator<Node> {
     607    private Node node;
     608    private List<IEnumerator> enumerators;
     609
     610    public NodeEnumerator(Node node) {
     611      this.node = node;
     612      this.enumerators = new List<IEnumerator>();
     613    }
     614
     615    public Node Current {
     616      get { return node; }
     617    }
     618    object IEnumerator.Current {
     619      get { return Current; }
     620    }
     621
     622    public void Dispose() { }
     623
     624    public bool MoveNext() {
     625      int i = 0;
     626      bool ok = false;
     627      while(!ok && i < enumerators.Count) {
     628        if(enumerators[i].MoveNext()) {
     629          ok = true;
     630        } else {
     631          i++;
     632        }
     633      }
     634
     635      if (ok) {
     636        for (int k = i-1; k >= 0; k--) {
     637          enumerators[k].Reset();
     638          enumerators[k].MoveNext();
     639        }
     640      } else {
     641        return false;
     642      }
     643
     644      node.ActualValue = (int)enumerators[0].Current;
     645      return true;
     646    }
     647
     648    public void Reset() {
     649      enumerators.Clear();
     650      enumerators.Add(node.PossibleValues.GetEnumerator());
     651      enumerators[0].Reset();
     652
     653      foreach (var child in node.ChildNodes) {
     654        var enumerator = new NodeEnumerator(child);
     655        enumerator.Reset();
     656        enumerator.MoveNext();
     657        enumerators.Add(enumerator);
     658      }
     659    }
     660  }
    448661}
Note: See TracChangeset for help on using the changeset viewer.