Changeset 5184 for branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs
- Timestamp:
- 12/30/10 22:50:59 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs
r5144 r5184 17 17 using HeuristicLab.Random; 18 18 using HeuristicLab.Selection; 19 using HeuristicLab.Parameters; 19 20 20 21 namespace HeuristicLab.MetaOptimization.Test { 21 22 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; 27 28 28 29 static void Main(string[] args) { … … 36 37 //TestCombinations2(); 37 38 //TestCombinations3(); 38 TestCombinations4(); 39 //TestEnumeratorCollectionEnumerator(); 40 //TestCombinations4(); 39 41 40 42 GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm(); … … 65 67 private static void TestToString(IValueConfiguration algorithmVc) { 66 68 var random = new MersenneTwister(); 67 Console.WriteLine(algorithmVc. ToParameterInfoString());69 Console.WriteLine(algorithmVc.ParameterInfoString); 68 70 algorithmVc.Randomize(random); 69 Console.WriteLine(algorithmVc. ToParameterInfoString());71 Console.WriteLine(algorithmVc.ParameterInfoString); 70 72 algorithmVc.Randomize(random); 71 Console.WriteLine(algorithmVc. ToParameterInfoString());73 Console.WriteLine(algorithmVc.ParameterInfoString); 72 74 algorithmVc.Randomize(random); 73 75 } … … 114 116 } 115 117 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 116 135 private static void TestCombinations4() { 117 136 GeneticAlgorithm ga = new GeneticAlgorithm(); 118 137 ga.Problem = new SingleObjectiveTestFunctionProblem(); 138 ga.Engine = new SequentialEngine.SequentialEngine(); 139 119 140 ParameterConfigurationTree vc = new ParameterConfigurationTree(ga); 120 141 121 //ConfigurePopulationSize(vc, 20, 100, 20);142 ConfigurePopulationSize(vc, 20, 100, 20); 122 143 //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); 129 146 130 147 int count = 0; … … 134 151 var current = (IValueConfiguration)enumerator.Current; 135 152 count++; 136 Console.WriteLine(current. ToParameterInfoString());153 Console.WriteLine(current.ParameterInfoString); 137 154 } 138 155 Console.WriteLine("You are about to create {0} algorithms.", count); 139 156 140 157 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); 143 167 } 144 168 } … … 220 244 metaLevelAlgorithm.Problem = metaOptimizationProblem; 221 245 metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine(); 222 223 metaLevelAlgorithm.Mutator = new ParameterConfigurationManipulator(); 246 247 metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last(); 248 224 249 metaLevelAlgorithm.MutationProbability.Value = 0.15; 225 250 … … 235 260 metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine(); 236 261 237 metaLevelAlgorithm.Mutator = new ParameterConfigurationManipulator(); 238 //metaLevelAlgorithm.MutationProbability.Value = 0.15; 262 metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last(); 239 263 240 264 return metaLevelAlgorithm; … … 257 281 }); 258 282 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); 264 288 return algorithmVc; 265 289 } … … 369 393 370 394 groupSizePc.ValueConfigurations.First().Optimize = true; 371 groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue( 0);372 groupSizePc.ValueConfigurations.First().RangeConstraint.UpperBound = new IntValue(10 0);395 groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue(2); 396 groupSizePc.ValueConfigurations.First().RangeConstraint.UpperBound = new IntValue(10); 373 397 groupSizePc.ValueConfigurations.First().RangeConstraint.StepSize = new IntValue(1); 374 398 } … … 394 418 } 395 419 396 private static void ConfigureElites(IValueConfiguration algorithmVc ) {420 private static void ConfigureElites(IValueConfiguration algorithmVc, int from, int to, int stepSize) { 397 421 var elitesPc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Elites").SingleOrDefault(); 398 422 elitesPc.Optimize = true; 399 423 var elitesVc = elitesPc.ValueConfigurations.First(); 400 424 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); 404 428 } 405 429 … … 423 447 sw.WriteLine(sb1.ToString()); 424 448 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); 425 452 426 453 metaLevelAlgorithm.Start(); … … 478 505 } 479 506 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 480 519 private static void TestShorten() { 481 520 int n = 8; … … 554 593 } 555 594 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(); // initialize567 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 595 public override string ToString() { 590 596 StringBuilder sb = new StringBuilder();
Note: See TracChangeset
for help on using the changeset viewer.