Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/04/11 02:18:27 (13 years ago)
Author:
cneumuel
Message:

#1215

  • lots of memory-consumption improvements
  • validValues -> validTypes (this saves memory!)
  • changed manipulators; modifications are less significant, out-of-bound-values are resampled instead of set to lower or upper bound
  • changed the way a base-level algorithm gets executed -> introduced AlgorithmExecutor
File:
1 edited

Legend:

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

    r5184 r5207  
    1818using HeuristicLab.Selection;
    1919using HeuristicLab.Parameters;
     20using HeuristicLab.Operators;
     21using System.Diagnostics;
     22using HeuristicLab.Encodings.RealVectorEncoding;
    2023
    2124namespace HeuristicLab.MetaOptimization.Test {
    2225  class Program {
    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;
     26    private static int metaAlgorithmPopulationSize = 50;
     27    private static int metaAlgorithmMaxGenerations = 30;
     28    private static int metaProblemRepetitions = 5;
     29    private static int baseAlgorithmMaxGenerations = 1000;
     30
     31    //private static int metaAlgorithmPopulationSize = 10;
     32    //private static int metaAlgorithmMaxGenerations = 20;
     33    //private static int metaProblemRepetitions = 3;
     34    //private static int baseAlgorithmMaxGenerations = 10;
    2835
    2936    static void Main(string[] args) {
     37      //TestTableBuilder();
    3038      //TestShorten();
    3139
    3240      //TestIntSampling();
    33       //TestDoubleSampling();
     41      //TestDoubleSampling(); return;
    3442      //TestTypeDiscovery();
    3543      //TestOperators();
     
    3947      //TestEnumeratorCollectionEnumerator();
    4048      //TestCombinations4();
     49      //TestAlgorithmPerformanceIssue();
    4150
    4251      GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
     52
     53      MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
     54      metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
     55      //GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
     56      GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
     57      //EvolutionStrategy metaLevelAlgorithm = GetMetaES(metaOptimizationProblem);
     58
     59      IValueConfiguration algorithmVc = SetupGAAlgorithm(baseLevelAlgorithm, metaOptimizationProblem);
     60
     61      //TestToString(algorithmVc);
     62
     63
     64      //Console.WriteLine("Press enter to start");
     65      //Console.ReadLine();
     66      //TestConfiguration(algorithmVc, baseLevelAlgorithm);
     67
     68      //Console.WriteLine("Press enter to start");
     69      //Console.ReadLine();
     70      TestOptimization(metaLevelAlgorithm);
     71
     72      //TestMemoryLeak(metaLevelAlgorithm);
     73
     74      Console.ReadLine();
     75    }
     76
     77    private static void TestAlgorithmPerformanceIssue() {
     78      ContentManager.Initialize(new PersistenceContentManager());
     79      Queue<TimeSpan> latestExecutionTimes = new Queue<TimeSpan>();
     80      int size = 10;
     81
     82      GeneticAlgorithm ga = new GeneticAlgorithm();
     83      ga.PopulationSize.Value = 3;
     84      ga.MaximumGenerations.Value = 1;
     85      ga.Engine = new SequentialEngine.SequentialEngine();
    4386
    4487      MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
    4588      metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
    4689      GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
    47       //EvolutionStrategy metaLevelAlgorithm = GetMetaES(metaOptimizationProblem);
    48 
    49       IValueConfiguration algorithmVc = SetupGAAlgorithm(baseLevelAlgorithm, metaOptimizationProblem);
    50 
    51       //TestToString(algorithmVc);
    52 
    53 
    54       //Console.WriteLine("Press enter to start");
    55       //Console.ReadLine();
    56       //TestConfiguration(algorithmVc, baseLevelAlgorithm);
    57 
    58       //Console.WriteLine("Press enter to start");
    59       //Console.ReadLine();
    60       TestOptimization(metaLevelAlgorithm);
    61 
    62       //TestMemoryLeak(metaLevelAlgorithm);
    63 
    64       Console.ReadLine();
    65     }
    66 
    67     private static void TestToString(IValueConfiguration algorithmVc) {
     90      ParameterConfigurationTree algorithmVc = SetupGAAlgorithm(ga, metaOptimizationProblem);
     91      Stopwatch sw = new Stopwatch();
     92
     93      for (int i = 0; i < 1000; i++) {
     94        sw.Start();
     95        GeneticAlgorithm clonedGa = (GeneticAlgorithm)ga.Clone();
     96        clonedGa.Name = "CLONED GA";
     97        algorithmVc.Parameterize(clonedGa);
     98        clonedGa.Prepare(true);
     99        var executor = new AlgorithmExecutor(clonedGa);
     100        executor.StartSync();
     101        sw.Stop();
     102        latestExecutionTimes.Enqueue(sw.Elapsed);
     103        Console.WriteLine("{0}: {1} ({2})", i, sw.Elapsed, latestExecutionTimes.Count > size ? TimeSpan.FromMilliseconds(latestExecutionTimes.Average(t => t.TotalMilliseconds)).ToString() : "-");
     104        if (latestExecutionTimes.Count > size) {
     105          latestExecutionTimes.Dequeue();
     106        }
     107        sw.Reset();
     108      }
     109    }
     110
     111    private static void TestTableBuilder() {
     112      TableBuilder tb = new TableBuilder("column_1", "col2", "col3");
     113      tb.AppendRow("1", "humpi", "0.23124");
     114      tb.AppendRow("2", "sf", "0.23124");
     115      tb.AppendRow("5", "humpi dampti", "0.224");
     116      tb.AppendRow("10", "egon asdf", "0.4");
     117      tb.AppendRow("15", "MichaelizcMultiVfds", "0.23124564");
     118      Console.WriteLine(tb.ToString());
     119    }
     120
     121    private static void TestToInfoString(IValueConfiguration algorithmVc) {
    68122      var random = new MersenneTwister();
    69123      Console.WriteLine(algorithmVc.ParameterInfoString);
     
    154208      }
    155209      Console.WriteLine("You are about to create {0} algorithms.", count);
    156      
     210
    157211      Experiment experiment = vc.GenerateExperiment(ga);
    158212      //foreach (var opt in experiment.Optimizers) {
     
    170224    private static void TestOperators() {
    171225      IRandom random = new MersenneTwister();
    172       ParameterConfigurationManipulator manip = new ParameterConfigurationManipulator();
    173 
    174       manip.IntValueManipulatorParameter.ActualValue = new UniformIntValueManipulator();
    175       manip.DoubleValueManipulatorParameter.ActualValue = new NormalDoubleValueManipulator();
    176226
    177227      var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(100), new DoubleValue(0.1));
    178228      using (var sw = new StreamWriter("out-DoubleValue.txt")) {
    179229        for (int i = 0; i < 10000; i++) {
    180           var val = new DoubleValue(50);
     230          var val = new DoubleValue(90);
    181231          NormalDoubleValueManipulator.ApplyStatic(random, val, doubleRange);
    182232
     
    244294      metaLevelAlgorithm.Problem = metaOptimizationProblem;
    245295      metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
    246      
     296
    247297      metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last();
    248      
     298
    249299      metaLevelAlgorithm.MutationProbability.Value = 0.15;
     300
     301      return metaLevelAlgorithm;
     302    }
     303
     304    private static GeneticAlgorithm GetParallelMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
     305      GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
     306
     307      UniformSubScopesProcessor uniformSubScopesProcessor =
     308        (HeuristicLab.Operators.UniformSubScopesProcessor)
     309        ((HeuristicLab.Operators.AlgorithmOperator)metaLevelAlgorithm.OperatorGraph.Operators.ElementAt(2)).OperatorGraph.Operators.ElementAt(8);
     310      uniformSubScopesProcessor.Parallel.Value = true;
     311
     312      metaLevelAlgorithm.Engine = new ParallelEngine.ParallelEngine();
    250313
    251314      return metaLevelAlgorithm;
     
    265328    }
    266329
    267     private static IValueConfiguration SetupGAAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) {
     330    private static ParameterConfigurationTree SetupGAAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) {
    268331      baseLevelAlgorithm.Problem = new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem();
    269332      baseLevelAlgorithm.MaximumGenerations.Value = baseAlgorithmMaxGenerations;
    270333
    271334      metaOptimizationProblem.Algorithm = baseLevelAlgorithm;
    272       IValueConfiguration algorithmVc = metaOptimizationProblem.ParameterConfigurationTree;
     335      ParameterConfigurationTree algorithmVc = metaOptimizationProblem.ParameterConfigurationTree;
    273336
    274337      metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
     
    276339        ProblemSize = new IntValue(500)
    277340      });
    278       metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
    279         Evaluator = new GriewankEvaluator(),
    280         ProblemSize = new IntValue(1000)
    281       });
    282 
    283       ConfigurePopulationSize(algorithmVc, 0, 10, 1);
    284       //ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01);
    285       //ConfigureMutationOperator(algorithmVc);
     341      //metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
     342      //  Evaluator = new GriewankEvaluator(),
     343      //  ProblemSize = new IntValue(1000)
     344      //});
     345
     346      ConfigurePopulationSize(algorithmVc, 0, 100, 1);
     347      ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01);
     348      ConfigureMutationOperator(algorithmVc);
    286349      ConfigureElites(algorithmVc, 0, 10, 1);
    287       //ConfigureSelectionOperator(algorithmVc, true);
     350      ConfigureSelectionOperator(algorithmVc, true);
    288351      return algorithmVc;
    289352    }
     
    393456
    394457      groupSizePc.ValueConfigurations.First().Optimize = true;
    395       groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue(2);
     458      groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue(0);
    396459      groupSizePc.ValueConfigurations.First().RangeConstraint.UpperBound = new IntValue(10);
    397460      groupSizePc.ValueConfigurations.First().RangeConstraint.StepSize = new IntValue(1);
     
    441504
    442505        StringBuilder sb1 = new StringBuilder();
    443         sb1.AppendLine(string.Format("Meta.PopulationSize: {0}", metaAlgorithmPopulationSize));
    444         sb1.AppendLine(string.Format("Meta.MaxGenerations: {0}", metaAlgorithmMaxGenerations));
    445         sb1.AppendLine(string.Format("Meta.Repetitions   : {0}", metaProblemRepetitions));
    446         sb1.AppendLine(string.Format("Base.MaxGenerations: {0}", baseAlgorithmMaxGenerations));
     506        sb1.AppendFormat("Meta.PopulationSize: {0}\n", metaAlgorithmPopulationSize);
     507        sb1.AppendFormat("Meta.MaxGenerations: {0}\n", metaAlgorithmMaxGenerations);
     508        sb1.AppendFormat("Meta.Repetitions   : {0}\n", metaProblemRepetitions);
     509        sb1.AppendFormat("Meta.MutProb       : {0}\n", ((GeneticAlgorithm)metaLevelAlgorithm).MutationProbability.Value);
     510        sb1.AppendFormat("Base.MaxGenerations: {0}\n", baseAlgorithmMaxGenerations);
     511        sb1.AppendLine("Problems:");
     512        foreach (var prob in ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).Problems) {
     513          sb1.Append(prob.Name);
     514          var sotf = prob as SingleObjectiveTestFunctionProblem;
     515          if (sotf != null) {
     516            sb1.AppendFormat(" {0}", sotf.ProblemSize.Value);
     517          }
     518          sb1.AppendLine();
     519        }
    447520        sw.WriteLine(sb1.ToString());
    448521        Console.WriteLine(sb1.ToString());
     
    463536
    464537            sb.AppendLine(metaLevelAlgorithm.ExecutionState.ToString());
    465             foreach (var result in metaLevelAlgorithm.Results) {
     538            var rsClone = (ResultCollection)metaLevelAlgorithm.Results.Clone();
     539            foreach (var result in rsClone) {
    466540              sb.AppendLine(result.ToString());
    467541              if (result.Name == "Population") {
     
    469543                var orderedRuns = rc.OrderBy(x => x.Results["RunsAverageQuality"]);
    470544
    471                 sb.AppendLine("Qual.  PoSi MutRa Eli SelOp MutOp");
     545                TableBuilder tb = new TableBuilder("AvgQual", "AvgET", "PoSi", "MutRa", "Eli", "SelOp", "MutOp", "NrSelSubScopes");
    472546                foreach (IRun run in orderedRuns) {
    473547                  string selector;
     
    478552                  }
    479553
    480                   sb.AppendLine(string.Format("{0} {1} {2} {3} {4} {5}",
    481                     ((DoubleValue)run.Results["RunsAverageQuality"]).Value.ToString("#0.00").PadLeft(7, ' '),
    482                     ((IntValue)run.Parameters["PopulationSize"]).Value.ToString().PadLeft(3, ' ').PadRight(3, ' '),
    483                     ((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.00").PadLeft(5, ' '),
    484                     ((IntValue)run.Parameters["Elites"]).Value.ToString().PadLeft(3, ' '),
    485                     Shorten(selector, 20).PadRight(20, ' '),
    486                     run.Parameters.ContainsKey("Mutator") ? run.Parameters["Mutator"].ToString() : "null"));
     554                  tb.AppendRow(
     555                    ((DoubleValue)run.Results["RunsAverageQuality"]).Value.ToString("#0.00"),
     556                    run.Results["RunsAverageExecutionTime"].ToString(),
     557                    ((IntValue)run.Parameters["PopulationSize"]).Value.ToString(),
     558                    ((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.00"),
     559                    ((IntValue)run.Parameters["Elites"]).Value.ToString(),
     560                    Shorten(selector, 20),
     561                    Shorten(run.Parameters.ContainsKey("Mutator") ? run.Parameters["Mutator"].ToString() : "null", 40),
     562                    ((ISelector)run.Parameters["Selector"]).NumberOfSelectedSubScopesParameter.Value.ToString());
    487563                }
     564                sb.AppendLine(tb.ToString());
    488565              }
    489566            } // foreach
    490             Console.Clear();
     567            //Console.Clear();
    491568            Console.WriteLine(sb.ToString());
    492569            sw.WriteLine(sb.ToString());
    493570            currentGeneration = ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value;
    494571          } // if
    495           if (i % 30 == 0) GC.Collect();
     572          //if (i % 30 == 0) GC.Collect();
    496573          i++;
    497574        } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped);
     
    545622      int lower = 10;
    546623      int upper = 20;
    547       int stepsize = 7;
     624      int stepsize = 1;
    548625      for (int i = 0; i < 100; i++) {
    549626        int val;
     
    631708      int i = 0;
    632709      bool ok = false;
    633       while(!ok && i < enumerators.Count) {
    634         if(enumerators[i].MoveNext()) {
     710      while (!ok && i < enumerators.Count) {
     711        if (enumerators[i].MoveNext()) {
    635712          ok = true;
    636713        } else {
     
    640717
    641718      if (ok) {
    642         for (int k = i-1; k >= 0; k--) {
     719        for (int k = i - 1; k >= 0; k--) {
    643720          enumerators[k].Reset();
    644721          enumerators[k].MoveNext();
Note: See TracChangeset for help on using the changeset viewer.