Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/12/10 11:34:13 (14 years ago)
Author:
cneumuel
Message:

#1215

  • enabled multiple problems
  • enabled n repetitions
  • improved results output
  • reduced memory footprint significantly
  • removed viewhost icons for less screen space waste
File:
1 edited

Legend:

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

    r5023 r5087  
    1717using System.Text;
    1818using HeuristicLab.Selection;
     19using HeuristicLab.Algorithms.EvolutionStrategy;
    1920
    2021namespace HeuristicLab.MetaOptimization.Test {
    2122  class Program {
    22     private static int metaAlgorithmPopulationSize = 40;
     23    private static int metaAlgorithmPopulationSize = 50;
    2324    private static int metaAlgorithmMaxGenerations = 30;
    24 
    25     private static int baseAlgorithmMaxGenerations = 100;
     25    private static int metaProblemRepetitions = 3;
     26
     27    private static int baseAlgorithmMaxGenerations = 250;
    2628
    2729    static void Main(string[] args) {
     30      //TestShorten();
     31
    2832      //TestIntSampling();
    2933      //TestDoubleSampling();
    3034
    3135      GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
     36
    3237      MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
    33       GeneticAlgorithm metaLevelAlgorithm = GetMetaAlgorithm(metaOptimizationProblem);
    34 
    35       IValueConfiguration algorithmVc = SetupAlgorithm(baseLevelAlgorithm, metaOptimizationProblem);
     38      ((MetaOptimizationEvaluator)metaOptimizationProblem.Evaluator).Repetitions.Value = metaProblemRepetitions;
     39      GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
     40      //EvolutionStrategy metaLevelAlgorithm = GetMetaES(metaOptimizationProblem);
     41
     42      IValueConfiguration algorithmVc = SetupGAAlgorithm(baseLevelAlgorithm, metaOptimizationProblem);
    3643
    3744      //Console.WriteLine("Press enter to start");
    3845      //Console.ReadLine();
    3946      //TestConfiguration(algorithmVc, baseLevelAlgorithm);
    40 
     47     
    4148      //Console.WriteLine("Press enter to start");
    4249      //Console.ReadLine();
     
    6976    }
    7077
    71     private static GeneticAlgorithm GetMetaAlgorithm(MetaOptimizationProblem metaOptimizationProblem) {
     78    private static GeneticAlgorithm GetMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
    7279      GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm();
    7380      metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
     
    8390    }
    8491
    85     private static IValueConfiguration SetupAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) {
    86       baseLevelAlgorithm.Problem = new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
    87         Evaluator = new GriewankEvaluator(),
    88         ProblemSize = new IntValue(1000)
    89       };
     92    private static EvolutionStrategy GetMetaES(MetaOptimizationProblem metaOptimizationProblem) {
     93      EvolutionStrategy metaLevelAlgorithm = new EvolutionStrategy();
     94      metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
     95      metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
     96
     97      metaLevelAlgorithm.Problem = metaOptimizationProblem;
     98      metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
     99
     100      metaLevelAlgorithm.Mutator = new ParameterConfigurationManipulator();
     101      //metaLevelAlgorithm.MutationProbability.Value = 0.15;
     102
     103      return metaLevelAlgorithm;
     104    }
     105
     106    private static IValueConfiguration SetupGAAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) {
     107      baseLevelAlgorithm.Problem = new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem();
    90108      baseLevelAlgorithm.MaximumGenerations.Value = baseAlgorithmMaxGenerations;
    91109
    92110      metaOptimizationProblem.Algorithm = baseLevelAlgorithm;
    93111      IValueConfiguration algorithmVc = metaOptimizationProblem.AlgorithmParameterConfiguration;
    94       metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem());
     112
     113      metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
     114        Evaluator = new GriewankEvaluator(),
     115        ProblemSize = new IntValue(500)
     116      });
     117      metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
     118        Evaluator = new GriewankEvaluator(),
     119        ProblemSize = new IntValue(1000)
     120      });
    95121
    96122      ConfigurePopulationSize(algorithmVc);
     
    189215          vc.Optimize = true;
    190216          ConfigureTournamentGroupSize(vc);
     217        } else if (vc.ActualValue.ValueDataType == typeof(RandomSelector)) {
     218          selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
    191219        } else {
    192           selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, false);
     220          selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
    193221        }
    194222      }
     
    235263    }
    236264
    237     private static void TestOptimization(GeneticAlgorithm metaLevelAlgorithm) {
     265    private static void TestOptimization(EngineAlgorithm metaLevelAlgorithm) {
    238266      ContentManager.Initialize(new PersistenceContentManager());
    239267      string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Results");
    240       if(!Directory.Exists(path))
     268      if (!Directory.Exists(path))
    241269        Directory.CreateDirectory(path);
    242       string id = DateTime.Now.ToString("MM.dd.yy - HH;mm;ss,ffff");
    243       string resultPath = Path.Combine(path, string.Format("Test - {0} - Result.hl", id));
    244       string outputPath = Path.Combine(path, string.Format("Test - {0} - Console.txt", id));
     270      string id = DateTime.Now.ToString("yyyy.MM.dd - HH;mm;ss,ffff");
     271      string resultPath = Path.Combine(path, string.Format("{0} - Result.hl", id));
     272      string outputPath = Path.Combine(path, string.Format("{0} - Console.txt", id));
     273
    245274
    246275      using (var sw = new StreamWriter(outputPath)) {
     276        sw.AutoFlush = true;
     277
     278        StringBuilder sb1 = new StringBuilder();
     279        sb1.AppendLine(string.Format("Meta.PopulationSize: {0}", metaAlgorithmPopulationSize));
     280        sb1.AppendLine(string.Format("Meta.MaxGenerations: {0}", metaAlgorithmMaxGenerations));
     281        sb1.AppendLine(string.Format("Meta.Repetitions   : {0}", metaProblemRepetitions));
     282        sb1.AppendLine(string.Format("Base.MaxGenerations: {0}", baseAlgorithmMaxGenerations));
     283        sw.WriteLine(sb1.ToString());
     284        Console.WriteLine(sb1.ToString());
     285
    247286        metaLevelAlgorithm.Start();
    248287        int i = 0;
     
    250289        do {
    251290          Thread.Sleep(500);
    252           try {
    253             if (metaLevelAlgorithm.Results.ContainsKey("Generations") && ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value != currentGeneration) {
    254               StringBuilder sb = new StringBuilder();
    255               sb.AppendLine(DateTime.Now.ToLongTimeString());
    256               sb.AppendLine("=================================");
    257 
    258               foreach (var result in metaLevelAlgorithm.Results) {
    259                 sb.AppendLine(result.ToString());
    260                 if (result.Name == "Population") {
    261                   RunCollection rc = (RunCollection)result.Value;
    262                   var orderedRuns = rc.OrderBy(x => x.Results["BestQuality"]);
    263                  
    264                   sb.AppendLine("Qality PoSi MutRa Eli GrSi MutOp");
    265                   foreach (IRun run in orderedRuns) {
    266                     sb.AppendLine(string.Format("{0} {1} {2} {3} {4} {5}",
    267                       ((DoubleValue)run.Results["BestQuality"]).Value.ToString("#0.00").PadLeft(7, ' '),
    268                       ((IntValue)run.Parameters["PopulationSize"]).Value.ToString().PadLeft(3, ' ').PadRight(3, ' '),
    269                       ((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.00").PadLeft(5, ' '),
    270                       ((IntValue)run.Parameters["Elites"]).Value.ToString().PadLeft(3, ' '),
    271                       ((TournamentSelector)run.Parameters["Selector"]).GroupSizeParameter.Value.ToString().PadLeft(4, ' '),
    272                       run.Parameters["Mutator"]));
     291          if (metaLevelAlgorithm.Results.ContainsKey("Generations") && ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value != currentGeneration) {
     292            while (metaLevelAlgorithm.Results.Count < 3) Thread.Sleep(100);
     293            StringBuilder sb = new StringBuilder();
     294            sb.AppendLine(DateTime.Now.ToLongTimeString());
     295            sb.AppendLine("=================================");
     296
     297            foreach (var result in metaLevelAlgorithm.Results) {
     298              sb.AppendLine(result.ToString());
     299              if (result.Name == "Population") {
     300                RunCollection rc = (RunCollection)result.Value;
     301                var orderedRuns = rc.OrderBy(x => x.Results["RunsAverageQuality"]);
     302
     303                sb.AppendLine("Qual.  PoSi MutRa Eli GrSi MutOp");
     304                foreach (IRun run in orderedRuns) {
     305                  string selector;
     306                  if (run.Parameters["Selector"] is TournamentSelector) {
     307                    selector = string.Format("{0} ({1})", run.Parameters["Selector"].ToString(), ((TournamentSelector)run.Parameters["Selector"]).GroupSizeParameter.Value.ToString());
     308                  } else {
     309                    selector = string.Format("{0}", run.Parameters["Selector"].ToString());
    273310                  }
     311
     312                  sb.AppendLine(string.Format("{0} {1} {2} {3} {4} {5}",
     313                    ((DoubleValue)run.Results["RunsAverageQuality"]).Value.ToString("#0.00").PadLeft(7, ' '),
     314                    ((IntValue)run.Parameters["PopulationSize"]).Value.ToString().PadLeft(3, ' ').PadRight(3, ' '),
     315                    ((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.00").PadLeft(5, ' '),
     316                    ((IntValue)run.Parameters["Elites"]).Value.ToString().PadLeft(3, ' '),
     317                    Shorten(selector, 20).PadRight(20, ' '),
     318                    run.Parameters["Mutator"].ToString()));
    274319                }
    275               } // foreach
    276               Console.Clear();
    277               Console.WriteLine(sb.ToString());
    278               sw.WriteLine(sb.ToString());
    279               sw.Flush();
    280               currentGeneration = ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value;
    281             } // if
    282             if (i % 30 == 0) GC.Collect();
    283             i++;
    284           }
    285           catch { }
     320              }
     321            } // foreach
     322            Console.Clear();
     323            Console.WriteLine(sb.ToString());
     324            sw.WriteLine(sb.ToString());
     325            currentGeneration = ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value;
     326          } // if
     327          if (i % 30 == 0) GC.Collect();
     328          i++;
    286329        } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped);
    287330      }
     
    290333      Console.WriteLine("Storing...");
    291334
    292       ContentManager.Save(metaLevelAlgorithm, resultPath, true);
     335      ContentManager.Save((IStorableContent)metaLevelAlgorithm, resultPath, true);
    293336      Console.WriteLine("Finished");
     337    }
     338
     339    private static void TestShorten() {
     340      int n = 8;
     341      Console.WriteLine(Shorten("1", n));
     342      Console.WriteLine(Shorten("12", n));
     343      Console.WriteLine(Shorten("123", n));
     344      Console.WriteLine(Shorten("1234", n));
     345      Console.WriteLine(Shorten("12345", n));
     346      Console.WriteLine(Shorten("123456", n));
     347      Console.WriteLine(Shorten("1234567", n));
     348      Console.WriteLine(Shorten("12345678", n));
     349      Console.WriteLine(Shorten("123456789", n));
     350      Console.WriteLine(Shorten("1234567890", n));
     351      Console.WriteLine(Shorten("12345678901", n));
     352    }
     353
     354    private static string Shorten(string s, int n) {
     355      string placeholder = "..";
     356      if (s.Length <= n) return s;
     357      int len = n / 2 - placeholder.Length / 2;
     358      string start = s.Substring(0, len);
     359      string end = s.Substring(s.Length - len, len);
     360      return start + placeholder + end;
    294361    }
    295362
Note: See TracChangeset for help on using the changeset viewer.