Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/30/10 22:50:59 (13 years ago)
Author:
cneumuel
Message:

#1215

  • enhanced combinations generator (now with batchruns!)
  • fixed ActualNames for metaopt-alg
  • added penalty for invalid solution-candidates (algs which throw exceptions)
  • migrated to .NET 4.0
File:
1 edited

Legend:

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

    r5144 r5184  
    1717using HeuristicLab.Random;
    1818using HeuristicLab.Selection;
     19using HeuristicLab.Parameters;
    1920
    2021namespace HeuristicLab.MetaOptimization.Test {
    2122  class Program {
    22     private static int metaAlgorithmPopulationSize = 50;
    23     private static int metaAlgorithmMaxGenerations = 30;
    24     private static int metaProblemRepetitions = 6;
    25 
    26     private static int baseAlgorithmMaxGenerations = 250;
     23    private static int metaAlgorithmPopulationSize = 10;
     24    private static int metaAlgorithmMaxGenerations = 10;
     25    private static int metaProblemRepetitions = 1;
     26
     27    private static int baseAlgorithmMaxGenerations = 10;
    2728
    2829    static void Main(string[] args) {
     
    3637      //TestCombinations2();
    3738      //TestCombinations3();
    38       TestCombinations4();
     39      //TestEnumeratorCollectionEnumerator();
     40      //TestCombinations4();
    3941
    4042      GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
     
    6567    private static void TestToString(IValueConfiguration algorithmVc) {
    6668      var random = new MersenneTwister();
    67       Console.WriteLine(algorithmVc.ToParameterInfoString());
     69      Console.WriteLine(algorithmVc.ParameterInfoString);
    6870      algorithmVc.Randomize(random);
    69       Console.WriteLine(algorithmVc.ToParameterInfoString());
     71      Console.WriteLine(algorithmVc.ParameterInfoString);
    7072      algorithmVc.Randomize(random);
    71       Console.WriteLine(algorithmVc.ToParameterInfoString());
     73      Console.WriteLine(algorithmVc.ParameterInfoString);
    7274      algorithmVc.Randomize(random);
    7375    }
     
    114116    }
    115117
     118    private static void TestEnumeratorCollectionEnumerator() {
     119      IEnumerable<int> list1 = new int[] { 1, 2, 3, 4, 5 };
     120      IEnumerable<int> list2 = new int[] { 10, 20, 30 };
     121      IEnumerable<int> list3 = new int[] { 300, 400, 500 };
     122
     123      var enumerators = new List<IEnumerator>();
     124
     125      EnumeratorCollectionEnumerator<int> enu = new EnumeratorCollectionEnumerator<int>();
     126      enu.AddEnumerator(list1.GetEnumerator());
     127      enu.AddEnumerator(list2.GetEnumerator());
     128      enu.AddEnumerator(list3.GetEnumerator());
     129      enu.Reset();
     130      while (enu.MoveNext()) {
     131        Console.WriteLine(enu.Current);
     132      }
     133    }
     134
    116135    private static void TestCombinations4() {
    117136      GeneticAlgorithm ga = new GeneticAlgorithm();
    118137      ga.Problem = new SingleObjectiveTestFunctionProblem();
     138      ga.Engine = new SequentialEngine.SequentialEngine();
     139
    119140      ParameterConfigurationTree vc = new ParameterConfigurationTree(ga);
    120141
    121       //ConfigurePopulationSize(vc, 20, 100, 20);
     142      ConfigurePopulationSize(vc, 20, 100, 20);
    122143      //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       //}
     144      //ConfigureMutationOperator(vc);
     145      ConfigureSelectionOperator(vc, true);
    129146
    130147      int count = 0;
     
    134151        var current = (IValueConfiguration)enumerator.Current;
    135152        count++;
    136         Console.WriteLine(current.ToParameterInfoString());
     153        Console.WriteLine(current.ParameterInfoString);
    137154      }
    138155      Console.WriteLine("You are about to create {0} algorithms.", count);
    139 
     156     
    140157      Experiment experiment = vc.GenerateExperiment(ga);
    141       foreach (var opt in experiment.Optimizers) {
    142         Console.WriteLine(opt.Name);
     158      //foreach (var opt in experiment.Optimizers) {
     159      //  Console.WriteLine(opt.Name);
     160      //}
     161
     162      experiment.Prepare();
     163      experiment.Start();
     164
     165      while (experiment.ExecutionState != ExecutionState.Stopped) {
     166        Thread.Sleep(500);
    143167      }
    144168    }
     
    220244      metaLevelAlgorithm.Problem = metaOptimizationProblem;
    221245      metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
    222 
    223       metaLevelAlgorithm.Mutator = new ParameterConfigurationManipulator();
     246     
     247      metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last();
     248     
    224249      metaLevelAlgorithm.MutationProbability.Value = 0.15;
    225250
     
    235260      metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
    236261
    237       metaLevelAlgorithm.Mutator = new ParameterConfigurationManipulator();
    238       //metaLevelAlgorithm.MutationProbability.Value = 0.15;
     262      metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last();
    239263
    240264      return metaLevelAlgorithm;
     
    257281      });
    258282
    259       ConfigurePopulationSize(algorithmVc, 20, 100, 1);
    260       ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01);
    261       ConfigureMutationOperator(algorithmVc);
    262       ConfigureElites(algorithmVc);
    263       ConfigureSelectionOperator(algorithmVc, true);
     283      ConfigurePopulationSize(algorithmVc, 0, 10, 1);
     284      //ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01);
     285      //ConfigureMutationOperator(algorithmVc);
     286      ConfigureElites(algorithmVc, 0, 10, 1);
     287      //ConfigureSelectionOperator(algorithmVc, true);
    264288      return algorithmVc;
    265289    }
     
    369393
    370394      groupSizePc.ValueConfigurations.First().Optimize = true;
    371       groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue(0);
    372       groupSizePc.ValueConfigurations.First().RangeConstraint.UpperBound = new IntValue(100);
     395      groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue(2);
     396      groupSizePc.ValueConfigurations.First().RangeConstraint.UpperBound = new IntValue(10);
    373397      groupSizePc.ValueConfigurations.First().RangeConstraint.StepSize = new IntValue(1);
    374398    }
     
    394418    }
    395419
    396     private static void ConfigureElites(IValueConfiguration algorithmVc) {
     420    private static void ConfigureElites(IValueConfiguration algorithmVc, int from, int to, int stepSize) {
    397421      var elitesPc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Elites").SingleOrDefault();
    398422      elitesPc.Optimize = true;
    399423      var elitesVc = elitesPc.ValueConfigurations.First();
    400424      elitesVc.Optimize = true;
    401       elitesVc.RangeConstraint.LowerBound = new IntValue(0);
    402       elitesVc.RangeConstraint.UpperBound = new IntValue(20);
    403       elitesVc.RangeConstraint.StepSize = new IntValue(1);
     425      elitesVc.RangeConstraint.LowerBound = new IntValue(from);
     426      elitesVc.RangeConstraint.UpperBound = new IntValue(to);
     427      elitesVc.RangeConstraint.StepSize = new IntValue(stepSize);
    404428    }
    405429
     
    423447        sw.WriteLine(sb1.ToString());
    424448        Console.WriteLine(sb1.ToString());
     449        metaLevelAlgorithm.Stopped += new EventHandler(metaLevelAlgorithm_Stopped);
     450        metaLevelAlgorithm.Paused += new EventHandler(metaLevelAlgorithm_Paused);
     451        metaLevelAlgorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(metaLevelAlgorithm_ExceptionOccurred);
    425452
    426453        metaLevelAlgorithm.Start();
     
    478505    }
    479506
     507    private static void metaLevelAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
     508      Console.WriteLine("metaLevelAlgorithm_ExceptionOccurred");
     509    }
     510
     511    private static void metaLevelAlgorithm_Paused(object sender, EventArgs e) {
     512      Console.WriteLine("metaLevelAlgorithm_Paused");
     513    }
     514
     515    private static void metaLevelAlgorithm_Stopped(object sender, EventArgs e) {
     516      Console.WriteLine("metaLevelAlgorithm_Stopped");
     517    }
     518
    480519    private static void TestShorten() {
    481520      int n = 8;
     
    554593    }
    555594
    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 
    589595    public override string ToString() {
    590596      StringBuilder sb = new StringBuilder();
Note: See TracChangeset for help on using the changeset viewer.