Changeset 5207 for branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs
- Timestamp:
- 01/04/11 02:18:27 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs
r5184 r5207 18 18 using HeuristicLab.Selection; 19 19 using HeuristicLab.Parameters; 20 using HeuristicLab.Operators; 21 using System.Diagnostics; 22 using HeuristicLab.Encodings.RealVectorEncoding; 20 23 21 24 namespace HeuristicLab.MetaOptimization.Test { 22 25 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; 28 35 29 36 static void Main(string[] args) { 37 //TestTableBuilder(); 30 38 //TestShorten(); 31 39 32 40 //TestIntSampling(); 33 //TestDoubleSampling(); 41 //TestDoubleSampling(); return; 34 42 //TestTypeDiscovery(); 35 43 //TestOperators(); … … 39 47 //TestEnumeratorCollectionEnumerator(); 40 48 //TestCombinations4(); 49 //TestAlgorithmPerformanceIssue(); 41 50 42 51 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(); 43 86 44 87 MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem(); 45 88 metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions); 46 89 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) { 68 122 var random = new MersenneTwister(); 69 123 Console.WriteLine(algorithmVc.ParameterInfoString); … … 154 208 } 155 209 Console.WriteLine("You are about to create {0} algorithms.", count); 156 210 157 211 Experiment experiment = vc.GenerateExperiment(ga); 158 212 //foreach (var opt in experiment.Optimizers) { … … 170 224 private static void TestOperators() { 171 225 IRandom random = new MersenneTwister(); 172 ParameterConfigurationManipulator manip = new ParameterConfigurationManipulator();173 174 manip.IntValueManipulatorParameter.ActualValue = new UniformIntValueManipulator();175 manip.DoubleValueManipulatorParameter.ActualValue = new NormalDoubleValueManipulator();176 226 177 227 var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(100), new DoubleValue(0.1)); 178 228 using (var sw = new StreamWriter("out-DoubleValue.txt")) { 179 229 for (int i = 0; i < 10000; i++) { 180 var val = new DoubleValue( 50);230 var val = new DoubleValue(90); 181 231 NormalDoubleValueManipulator.ApplyStatic(random, val, doubleRange); 182 232 … … 244 294 metaLevelAlgorithm.Problem = metaOptimizationProblem; 245 295 metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine(); 246 296 247 297 metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last(); 248 298 249 299 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(); 250 313 251 314 return metaLevelAlgorithm; … … 265 328 } 266 329 267 private static IValueConfigurationSetupGAAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) {330 private static ParameterConfigurationTree SetupGAAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) { 268 331 baseLevelAlgorithm.Problem = new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem(); 269 332 baseLevelAlgorithm.MaximumGenerations.Value = baseAlgorithmMaxGenerations; 270 333 271 334 metaOptimizationProblem.Algorithm = baseLevelAlgorithm; 272 IValueConfigurationalgorithmVc = metaOptimizationProblem.ParameterConfigurationTree;335 ParameterConfigurationTree algorithmVc = metaOptimizationProblem.ParameterConfigurationTree; 273 336 274 337 metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() { … … 276 339 ProblemSize = new IntValue(500) 277 340 }); 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); 286 349 ConfigureElites(algorithmVc, 0, 10, 1); 287 //ConfigureSelectionOperator(algorithmVc, true);350 ConfigureSelectionOperator(algorithmVc, true); 288 351 return algorithmVc; 289 352 } … … 393 456 394 457 groupSizePc.ValueConfigurations.First().Optimize = true; 395 groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue( 2);458 groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue(0); 396 459 groupSizePc.ValueConfigurations.First().RangeConstraint.UpperBound = new IntValue(10); 397 460 groupSizePc.ValueConfigurations.First().RangeConstraint.StepSize = new IntValue(1); … … 441 504 442 505 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 } 447 520 sw.WriteLine(sb1.ToString()); 448 521 Console.WriteLine(sb1.ToString()); … … 463 536 464 537 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) { 466 540 sb.AppendLine(result.ToString()); 467 541 if (result.Name == "Population") { … … 469 543 var orderedRuns = rc.OrderBy(x => x.Results["RunsAverageQuality"]); 470 544 471 sb.AppendLine("Qual. PoSi MutRa Eli SelOp MutOp");545 TableBuilder tb = new TableBuilder("AvgQual", "AvgET", "PoSi", "MutRa", "Eli", "SelOp", "MutOp", "NrSelSubScopes"); 472 546 foreach (IRun run in orderedRuns) { 473 547 string selector; … … 478 552 } 479 553 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()); 487 563 } 564 sb.AppendLine(tb.ToString()); 488 565 } 489 566 } // foreach 490 Console.Clear();567 //Console.Clear(); 491 568 Console.WriteLine(sb.ToString()); 492 569 sw.WriteLine(sb.ToString()); 493 570 currentGeneration = ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value; 494 571 } // if 495 if (i % 30 == 0) GC.Collect();572 //if (i % 30 == 0) GC.Collect(); 496 573 i++; 497 574 } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped); … … 545 622 int lower = 10; 546 623 int upper = 20; 547 int stepsize = 7;624 int stepsize = 1; 548 625 for (int i = 0; i < 100; i++) { 549 626 int val; … … 631 708 int i = 0; 632 709 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()) { 635 712 ok = true; 636 713 } else { … … 640 717 641 718 if (ok) { 642 for (int k = i -1; k >= 0; k--) {719 for (int k = i - 1; k >= 0; k--) { 643 720 enumerators[k].Reset(); 644 721 enumerators[k].MoveNext();
Note: See TracChangeset
for help on using the changeset viewer.