- Timestamp:
- 12/12/10 11:34:13 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs
r5023 r5087 17 17 using System.Text; 18 18 using HeuristicLab.Selection; 19 using HeuristicLab.Algorithms.EvolutionStrategy; 19 20 20 21 namespace HeuristicLab.MetaOptimization.Test { 21 22 class Program { 22 private static int metaAlgorithmPopulationSize = 40;23 private static int metaAlgorithmPopulationSize = 50; 23 24 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; 26 28 27 29 static void Main(string[] args) { 30 //TestShorten(); 31 28 32 //TestIntSampling(); 29 33 //TestDoubleSampling(); 30 34 31 35 GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm(); 36 32 37 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); 36 43 37 44 //Console.WriteLine("Press enter to start"); 38 45 //Console.ReadLine(); 39 46 //TestConfiguration(algorithmVc, baseLevelAlgorithm); 40 47 41 48 //Console.WriteLine("Press enter to start"); 42 49 //Console.ReadLine(); … … 69 76 } 70 77 71 private static GeneticAlgorithm GetMeta Algorithm(MetaOptimizationProblem metaOptimizationProblem) {78 private static GeneticAlgorithm GetMetaGA(MetaOptimizationProblem metaOptimizationProblem) { 72 79 GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm(); 73 80 metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize; … … 83 90 } 84 91 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(); 90 108 baseLevelAlgorithm.MaximumGenerations.Value = baseAlgorithmMaxGenerations; 91 109 92 110 metaOptimizationProblem.Algorithm = baseLevelAlgorithm; 93 111 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 }); 95 121 96 122 ConfigurePopulationSize(algorithmVc); … … 189 215 vc.Optimize = true; 190 216 ConfigureTournamentGroupSize(vc); 217 } else if (vc.ActualValue.ValueDataType == typeof(RandomSelector)) { 218 selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true); 191 219 } else { 192 selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, false);220 selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true); 193 221 } 194 222 } … … 235 263 } 236 264 237 private static void TestOptimization( GeneticAlgorithm metaLevelAlgorithm) {265 private static void TestOptimization(EngineAlgorithm metaLevelAlgorithm) { 238 266 ContentManager.Initialize(new PersistenceContentManager()); 239 267 string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Results"); 240 if (!Directory.Exists(path))268 if (!Directory.Exists(path)) 241 269 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 245 274 246 275 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 247 286 metaLevelAlgorithm.Start(); 248 287 int i = 0; … … 250 289 do { 251 290 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()); 273 310 } 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())); 274 319 } 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++; 286 329 } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped); 287 330 } … … 290 333 Console.WriteLine("Storing..."); 291 334 292 ContentManager.Save( metaLevelAlgorithm, resultPath, true);335 ContentManager.Save((IStorableContent)metaLevelAlgorithm, resultPath, true); 293 336 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; 294 361 } 295 362
Note: See TracChangeset
for help on using the changeset viewer.