- Timestamp:
- 12/21/10 01:13:49 (14 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs
r5111 r5144 1 using System.Collections.Generic; 2 using System.Diagnostics; 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.IO; 3 5 using System.Linq; 6 using System.Text; 7 using System.Threading; 8 using HeuristicLab.Algorithms.EvolutionStrategy; 4 9 using HeuristicLab.Algorithms.GeneticAlgorithm; 10 using HeuristicLab.Common; 5 11 using HeuristicLab.Core; 12 using HeuristicLab.Data; 13 using HeuristicLab.Optimization; 6 14 using HeuristicLab.PluginInfrastructure; 7 using HeuristicLab.Parameters;8 15 using HeuristicLab.Problems.MetaOptimization; 9 using HeuristicLab.Data; 10 using System; 11 using System.Threading; 16 using HeuristicLab.Problems.TestFunctions; 12 17 using HeuristicLab.Random; 13 using HeuristicLab.Optimization;14 using HeuristicLab.Common;15 using System.IO;16 using HeuristicLab.Problems.TestFunctions;17 using System.Text;18 18 using HeuristicLab.Selection; 19 using HeuristicLab.Algorithms.EvolutionStrategy;20 using HeuristicLab.PluginInfrastructure.Manager;21 19 22 20 namespace HeuristicLab.MetaOptimization.Test { … … 35 33 //TestTypeDiscovery(); 36 34 //TestOperators(); 37 35 //TestCombinations(); 36 //TestCombinations2(); 37 //TestCombinations3(); 38 TestCombinations4(); 39 38 40 GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm(); 39 41 … … 45 47 IValueConfiguration algorithmVc = SetupGAAlgorithm(baseLevelAlgorithm, metaOptimizationProblem); 46 48 49 //TestToString(algorithmVc); 50 51 47 52 //Console.WriteLine("Press enter to start"); 48 53 //Console.ReadLine(); 49 54 //TestConfiguration(algorithmVc, baseLevelAlgorithm); 50 55 51 56 //Console.WriteLine("Press enter to start"); 52 57 //Console.ReadLine(); … … 58 63 } 59 64 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 60 146 private static void TestOperators() { 61 147 IRandom random = new MersenneTwister(); … … 64 150 manip.IntValueManipulatorParameter.ActualValue = new UniformIntValueManipulator(); 65 151 manip.DoubleValueManipulatorParameter.ActualValue = new NormalDoubleValueManipulator(); 66 152 67 153 var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(100), new DoubleValue(0.1)); 68 154 using (var sw = new StreamWriter("out-DoubleValue.txt")) { … … 70 156 var val = new DoubleValue(50); 71 157 NormalDoubleValueManipulator.ApplyStatic(random, val, doubleRange); 72 158 73 159 sw.WriteLine(val); 74 160 } … … 98 184 private static void TestTypeDiscovery() { 99 185 PluginLoader.pluginAssemblies.Any(); 100 186 101 187 var items = ApplicationManager.Manager.GetInstances(typeof(DoubleArray)).ToArray(); 102 188 … … 107 193 108 194 private static void TestMemoryLeak(GeneticAlgorithm metaLevelAlgorithm) { 109 IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem). AlgorithmParameterConfiguration;195 IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).ParameterConfigurationTree; 110 196 111 197 Console.WriteLine("Starting Memory Test..."); … … 160 246 161 247 metaOptimizationProblem.Algorithm = baseLevelAlgorithm; 162 IValueConfiguration algorithmVc = metaOptimizationProblem. AlgorithmParameterConfiguration;248 IValueConfiguration algorithmVc = metaOptimizationProblem.ParameterConfigurationTree; 163 249 164 250 metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() { … … 171 257 }); 172 258 173 ConfigurePopulationSize(algorithmVc );174 ConfigureMutationRate(algorithmVc );259 ConfigurePopulationSize(algorithmVc, 20, 100, 1); 260 ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01); 175 261 ConfigureMutationOperator(algorithmVc); 176 262 ConfigureElites(algorithmVc); 177 ConfigureSelectionOperator(algorithmVc );263 ConfigureSelectionOperator(algorithmVc, true); 178 264 return algorithmVc; 179 265 } … … 203 289 GeneticAlgorithm newAlg = (GeneticAlgorithm)baseLevelAlgorithm.Clone(); 204 290 //clonedVc.Mutate(rand); 205 291 206 292 //.Apply(rand, clonedVc); todo 207 293 clonedVc.Parameterize(newAlg); … … 259 345 } 260 346 261 private static void ConfigureSelectionOperator(IValueConfiguration algorithmVc ) {347 private static void ConfigureSelectionOperator(IValueConfiguration algorithmVc, bool configureTournamenSize) { 262 348 var selectionOperatorPc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Selector").SingleOrDefault(); 263 349 selectionOperatorPc.Optimize = true; … … 266 352 if (vc.ActualValue.ValueDataType == typeof(TournamentSelector)) { 267 353 selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true); 268 vc.Optimize = true; 269 ConfigureTournamentGroupSize(vc); 354 if (configureTournamenSize) { 355 vc.Optimize = true; 356 ConfigureTournamentGroupSize(vc); 357 } 270 358 } else if (vc.ActualValue.ValueDataType == typeof(RandomSelector)) { 271 359 selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true); … … 286 374 } 287 375 288 private static void ConfigurePopulationSize(IValueConfiguration algorithmVc ) {376 private static void ConfigurePopulationSize(IValueConfiguration algorithmVc, int lower, int upper, int stepsize) { 289 377 var populationSizePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "PopulationSize").SingleOrDefault(); 290 378 populationSizePc.Optimize = true; 291 379 var populationSizeVc = populationSizePc.ValueConfigurations.First(); 292 380 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) { 299 387 var mutationRatePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "MutationProbability").SingleOrDefault(); 300 388 mutationRatePc.Optimize = true; 301 389 var mutationRateVc = mutationRatePc.ValueConfigurations.First(); 302 390 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); 306 394 } 307 395 … … 446 534 } 447 535 } 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 } 448 661 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/app.config
r4997 r5144 13 13 </assemblyBinding> 14 14 </runtime> 15 < /configuration>15 <startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
Note: See TracChangeset
for help on using the changeset viewer.