[5144] | 1 | using System;
|
---|
| 2 | using System.Collections;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using System.IO;
|
---|
[4997] | 5 | using System.Linq;
|
---|
[5144] | 6 | using System.Text;
|
---|
| 7 | using System.Threading;
|
---|
| 8 | using HeuristicLab.Algorithms.EvolutionStrategy;
|
---|
[4997] | 9 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
[5144] | 10 | using HeuristicLab.Common;
|
---|
[4997] | 11 | using HeuristicLab.Core;
|
---|
[5144] | 12 | using HeuristicLab.Data;
|
---|
| 13 | using HeuristicLab.Optimization;
|
---|
[4997] | 14 | using HeuristicLab.PluginInfrastructure;
|
---|
| 15 | using HeuristicLab.Problems.MetaOptimization;
|
---|
[5144] | 16 | using HeuristicLab.Problems.TestFunctions;
|
---|
[5009] | 17 | using HeuristicLab.Random;
|
---|
[5023] | 18 | using HeuristicLab.Selection;
|
---|
[5184] | 19 | using HeuristicLab.Parameters;
|
---|
[5207] | 20 | using HeuristicLab.Operators;
|
---|
| 21 | using System.Diagnostics;
|
---|
| 22 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[5212] | 23 | using HeuristicLab.Hive.ExperimentManager;
|
---|
[5231] | 24 | using System.Threading.Tasks;
|
---|
[4997] | 25 |
|
---|
| 26 | namespace HeuristicLab.MetaOptimization.Test {
|
---|
| 27 | class Program {
|
---|
[5212] | 28 | //private static int metaAlgorithmPopulationSize = 50;
|
---|
| 29 | //private static int metaAlgorithmMaxGenerations = 30;
|
---|
| 30 | //private static int metaProblemRepetitions = 5;
|
---|
| 31 | //private static int baseAlgorithmMaxGenerations = 1000;
|
---|
[5009] | 32 |
|
---|
[5303] | 33 | private static int metaAlgorithmPopulationSize = 10;
|
---|
| 34 | private static int metaAlgorithmMaxGenerations = 10;
|
---|
| 35 | private static int metaProblemRepetitions = 3;
|
---|
| 36 | private static int baseAlgorithmMaxGenerations = 20;
|
---|
| 37 | private static double mutationProbability = 0.00;
|
---|
[5009] | 38 |
|
---|
[4997] | 39 | static void Main(string[] args) {
|
---|
[5267] | 40 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
| 41 |
|
---|
[5207] | 42 | //TestTableBuilder();
|
---|
[5087] | 43 | //TestShorten();
|
---|
| 44 |
|
---|
[4997] | 45 | //TestIntSampling();
|
---|
[5207] | 46 | //TestDoubleSampling(); return;
|
---|
[5110] | 47 | //TestTypeDiscovery();
|
---|
[5111] | 48 | //TestOperators();
|
---|
[5144] | 49 | //TestCombinations();
|
---|
| 50 | //TestCombinations2();
|
---|
| 51 | //TestCombinations3();
|
---|
[5184] | 52 | //TestEnumeratorCollectionEnumerator();
|
---|
| 53 | //TestCombinations4();
|
---|
[5207] | 54 | //TestAlgorithmPerformanceIssue();
|
---|
[5262] | 55 | //TestWaitAny();
|
---|
[5267] | 56 | //TestExecutionTimeUpdateInvervalPerformance();
|
---|
[5277] | 57 | //TestMemoryConsumption();
|
---|
[5293] | 58 | //TestNormalCrossover();
|
---|
[5303] | 59 | //TestItemDictionary();
|
---|
[5144] | 60 |
|
---|
[4997] | 61 | GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
|
---|
[5087] | 62 |
|
---|
[4997] | 63 | MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
|
---|
[5110] | 64 | metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
|
---|
[5303] | 65 | GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
|
---|
| 66 | //GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
|
---|
[5212] | 67 | //GeneticAlgorithm metaLevelAlgorithm = GetHiveParallelMetaGA(metaOptimizationProblem);
|
---|
| 68 |
|
---|
[5087] | 69 | //EvolutionStrategy metaLevelAlgorithm = GetMetaES(metaOptimizationProblem);
|
---|
[5009] | 70 |
|
---|
[5087] | 71 | IValueConfiguration algorithmVc = SetupGAAlgorithm(baseLevelAlgorithm, metaOptimizationProblem);
|
---|
[5009] | 72 |
|
---|
[5144] | 73 | //TestToString(algorithmVc);
|
---|
| 74 |
|
---|
| 75 |
|
---|
[5023] | 76 | //Console.WriteLine("Press enter to start");
|
---|
| 77 | //Console.ReadLine();
|
---|
| 78 | //TestConfiguration(algorithmVc, baseLevelAlgorithm);
|
---|
[5144] | 79 |
|
---|
[5023] | 80 | //Console.WriteLine("Press enter to start");
|
---|
| 81 | //Console.ReadLine();
|
---|
[5009] | 82 | TestOptimization(metaLevelAlgorithm);
|
---|
| 83 |
|
---|
| 84 | //TestMemoryLeak(metaLevelAlgorithm);
|
---|
| 85 |
|
---|
| 86 | Console.ReadLine();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[5303] | 89 | private static void TestItemDictionary() {
|
---|
| 90 | var dict = new ItemDictionary<StringValue, RunCollection>();
|
---|
| 91 | dict.Add(new StringValue("a"), new RunCollection());
|
---|
| 92 | dict.Add(new StringValue("b"), new RunCollection());
|
---|
| 93 | dict.Add(new StringValue("c"), new RunCollection());
|
---|
| 94 |
|
---|
| 95 | Console.WriteLine(dict.ContainsKey(new StringValue("a")));
|
---|
| 96 | Console.WriteLine(dict.Count(x => x.Key.Value == "a"));
|
---|
| 97 |
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[5293] | 100 | private static void TestNormalCrossover() {
|
---|
| 101 | var random = new MersenneTwister();
|
---|
| 102 | double d1 = 0.5;
|
---|
| 103 | double d2 = 0.6;
|
---|
| 104 | var doubleRange = new DoubleValueRange(new DoubleValue(0.0), new DoubleValue(1.0), new DoubleValue(0.01));
|
---|
| 105 |
|
---|
| 106 | using (var sw = new StreamWriter("normalCrossover-DoubleValue.txt")) {
|
---|
| 107 | for (int i = 0; i < 10000; i++) {
|
---|
| 108 | sw.WriteLine(NormalDoubleValueCrossover.ApplyStatic(random, new DoubleValue(d1), new DoubleValue(d2), doubleRange));
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | int i1 = 180;
|
---|
| 113 | int i2 = 160;
|
---|
| 114 | var intRange = new IntValueRange(new IntValue(100), new IntValue(200), new IntValue(1));
|
---|
| 115 |
|
---|
| 116 | using (var sw = new StreamWriter("normalCrossover-IntValue.txt")) {
|
---|
| 117 | for (int i = 0; i < 10000; i++) {
|
---|
| 118 | sw.WriteLine(NormalIntValueCrossover.ApplyStatic(random, new IntValue(i1), new IntValue(i2), intRange));
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[5267] | 123 | private static void TestMemoryConsumption() {
|
---|
| 124 | Queue<TimeSpan> latestExecutionTimes = new Queue<TimeSpan>();
|
---|
| 125 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 126 | ga.PopulationSize.Value = 3;
|
---|
| 127 | ga.MaximumGenerations.Value = 1;
|
---|
| 128 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 129 |
|
---|
| 130 | MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
|
---|
| 131 | metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
|
---|
| 132 | GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
|
---|
| 133 | ParameterConfigurationTree algorithmVc = SetupGAAlgorithm(ga, metaOptimizationProblem);
|
---|
| 134 | Stopwatch sw = new Stopwatch();
|
---|
| 135 |
|
---|
| 136 | var algs = new List<IAlgorithm>();
|
---|
| 137 | for (int i = 0; i < 10000; i++) {
|
---|
| 138 | sw.Start();
|
---|
| 139 | GeneticAlgorithm clonedGa = (GeneticAlgorithm)ga.Clone();
|
---|
| 140 | clonedGa.Name = "CLONED GA";
|
---|
| 141 | algorithmVc.Parameterize(clonedGa);
|
---|
| 142 | algs.Add(clonedGa);
|
---|
| 143 | sw.Reset();
|
---|
[5277] | 144 | ContentManager.Save((IStorableContent)metaLevelAlgorithm, "alg_" + i + ".hl", true);
|
---|
[5267] | 145 | Console.WriteLine("Cloned alg #{0}", i);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[5262] | 149 | private static void TestExecutionTimeUpdateInvervalPerformance() {
|
---|
| 150 | TableBuilder tb = new TableBuilder("Tasks", "Interval", "TotalExecutionTime", "AvgExecutionTime", "TimeElapsed", "TotalTimeElapsed", "Speedup", "ExecutionTimeChangedCount", "RealExecutionTimeUpdate(ms)");
|
---|
[5267] | 151 | int tasks = 4;
|
---|
[5262] | 152 | int repetitions = 3;
|
---|
| 153 |
|
---|
| 154 | // warmup
|
---|
| 155 | RepeatExecuteParallel(3, 1, 1, tb);
|
---|
| 156 | tb.AppendRow("--", "--", "--", "--", "--", "--", "--", "--", "--");
|
---|
[5277] | 157 | RepeatExecuteParallel(repetitions, tasks, 1, tb);
|
---|
| 158 | RepeatExecuteParallel(repetitions, tasks, 2.5, tb);
|
---|
[5262] | 159 | RepeatExecuteParallel(repetitions, tasks, 5, tb);
|
---|
| 160 | RepeatExecuteParallel(repetitions, tasks, 10, tb);
|
---|
| 161 | RepeatExecuteParallel(repetitions, tasks, 25, tb);
|
---|
| 162 | RepeatExecuteParallel(repetitions, tasks, 50, tb);
|
---|
[5277] | 163 | RepeatExecuteParallel(repetitions, tasks, 100, tb);
|
---|
| 164 | RepeatExecuteParallel(repetitions, tasks, 250, tb);
|
---|
| 165 | RepeatExecuteParallel(repetitions, tasks, 500, tb);
|
---|
[5262] | 166 | RepeatExecuteParallel(repetitions, tasks, 1000, tb);
|
---|
[5277] | 167 | RepeatExecuteParallel(repetitions, tasks, 2500, tb);
|
---|
[5262] | 168 | RepeatExecuteParallel(repetitions, tasks, 5000, tb);
|
---|
| 169 |
|
---|
| 170 | using (var sw = new StreamWriter("TestExecutionTimeUpdateInvervalPerformance.txt")) {
|
---|
| 171 | sw.Write(tb.ToString());
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | private static GeneticAlgorithm CreateGA() {
|
---|
| 176 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 177 | ga.Problem = new SingleObjectiveTestFunctionProblem() { ProblemSize = new IntValue(250) };
|
---|
| 178 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 179 | ga.SetSeedRandomly.Value = false;
|
---|
| 180 | ga.Seed.Value = 0;
|
---|
| 181 | return ga;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | private static void RepeatExecuteParallel(int repetitions, int tasks, double executionTimeUpdateIntervalMs, TableBuilder tb) {
|
---|
| 185 | for (int i = 0; i < repetitions; i++) {
|
---|
[5277] | 186 | ExecuteParallel(tasks, executionTimeUpdateIntervalMs, tb);
|
---|
| 187 | Console.Clear();
|
---|
[5262] | 188 | Console.WriteLine(tb.ToString());
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | private static void ExecuteParallel(int taskCount, double executionTimeUpdateIntervalMs, TableBuilder tb) {
|
---|
| 193 | Task<TimeSpan>[] tasks = new Task<TimeSpan>[taskCount];
|
---|
| 194 | EngineAlgorithm[] algs = new EngineAlgorithm[taskCount];
|
---|
| 195 | for (int i = 0; i < taskCount; i++) {
|
---|
| 196 | GeneticAlgorithm alg = CreateGA();
|
---|
[5267] | 197 | //((Engine)alg.Engine).ExecutionTimeUpdateInterval = TimeSpan.FromMilliseconds(executionTimeUpdateIntervalMs);
|
---|
[5262] | 198 | algs[i] = alg;
|
---|
| 199 | }
|
---|
| 200 | Console.WriteLine("Creating algs finished.");
|
---|
| 201 |
|
---|
| 202 | for (int i = 0; i < taskCount; i++) {
|
---|
| 203 | tasks[i] = new Task<TimeSpan>((alg) => {
|
---|
| 204 | Console.WriteLine("Task {0} started.", Task.CurrentId);
|
---|
[5277] | 205 |
|
---|
[5262] | 206 | Stopwatch swx = new Stopwatch();
|
---|
| 207 | swx.Start();
|
---|
| 208 | ((EngineAlgorithm)alg).ExecutionTimeChanged += new EventHandler(Program_ExecutionTimeChanged);
|
---|
| 209 | var executor = new AlgorithmExecutor((EngineAlgorithm)alg);
|
---|
| 210 | executor.StartSync();
|
---|
| 211 | ((EngineAlgorithm)alg).ExecutionTimeChanged -= new EventHandler(Program_ExecutionTimeChanged);
|
---|
| 212 | swx.Stop();
|
---|
| 213 | Console.WriteLine("Task {0} finished.", Task.CurrentId);
|
---|
| 214 | return swx.Elapsed;
|
---|
| 215 | }, algs[i]);
|
---|
| 216 | }
|
---|
| 217 | Console.WriteLine("Creating tasks finished.");
|
---|
| 218 | counter = 0;
|
---|
| 219 | Stopwatch sw = new Stopwatch();
|
---|
| 220 | sw.Start();
|
---|
| 221 | foreach (var task in tasks) task.Start();
|
---|
| 222 | Task.WaitAll(tasks);
|
---|
| 223 | sw.Stop();
|
---|
| 224 |
|
---|
| 225 | if (!algs.All(alg => alg.ExecutionState == ExecutionState.Stopped))
|
---|
| 226 | throw new Exception("Not all algs stopped properly");
|
---|
| 227 |
|
---|
| 228 | if (!algs.All(alg => ((DoubleValue)alg.Results["BestQuality"].Value).Value == ((DoubleValue)algs.First().Results["BestQuality"].Value).Value))
|
---|
| 229 | throw new Exception("Not all algs have the same resutls");
|
---|
| 230 |
|
---|
| 231 | if (tb != null) {
|
---|
| 232 | double totalExecutionTimeMilliseconds = algs.Select(x => x.ExecutionTime.TotalMilliseconds).Sum();
|
---|
| 233 | double totalMilliseconds = tasks.Select(t => t.Result.TotalMilliseconds).Sum();
|
---|
| 234 | tb.AppendRow(
|
---|
[5277] | 235 | taskCount.ToString(),
|
---|
| 236 | executionTimeUpdateIntervalMs.ToString(),
|
---|
[5262] | 237 | TimeSpan.FromMilliseconds(totalExecutionTimeMilliseconds).ToString(),
|
---|
[5277] | 238 | TimeSpan.FromMilliseconds(totalExecutionTimeMilliseconds / taskCount).ToString(),
|
---|
[5262] | 239 | sw.Elapsed.ToString(),
|
---|
| 240 | TimeSpan.FromMilliseconds(totalMilliseconds).ToString(),
|
---|
| 241 | (totalMilliseconds / sw.ElapsedMilliseconds).ToString("0.00"),
|
---|
| 242 | counter.ToString(),
|
---|
[5277] | 243 | (totalExecutionTimeMilliseconds / counter).ToString("0.00"));
|
---|
[5262] | 244 | }
|
---|
| 245 | tasks = null;
|
---|
| 246 | algs = null;
|
---|
| 247 | GC.Collect();
|
---|
| 248 | Console.WriteLine("Test finished.");
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | private static int counter = 0;
|
---|
| 252 | static void Program_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 253 | System.Threading.Interlocked.Increment(ref counter);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[5231] | 256 | private static void TestWaitAny() {
|
---|
| 257 | System.Random rand = new System.Random();
|
---|
| 258 | var tasks = new List<Task<int>>();
|
---|
| 259 | for (int i = 0; i < 10; i++) {
|
---|
| 260 | tasks.Add(Task.Factory.StartNew<int>((x) => {
|
---|
| 261 | int sleep = ((int)x - 10) * -1000;
|
---|
| 262 | Console.WriteLine("sleeping: {0} ms", sleep);
|
---|
| 263 | Thread.Sleep(0); // make context switch
|
---|
| 264 | Thread.Sleep(sleep);
|
---|
| 265 | return (int)x * (int)x;
|
---|
| 266 | }, i));
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | // --> WaitAll processes tasks lazy but in order.
|
---|
| 270 | Task.WaitAll();
|
---|
| 271 | foreach (var task in tasks) {
|
---|
| 272 | Console.WriteLine(task.Result);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | // -> WaitAny processes any finished task first. but the finished task needs to be removed from list in order to process all tasks
|
---|
| 276 | //for (int i = 0; i < 10; i++) {
|
---|
| 277 | // var tasksArray = tasks.ToArray();
|
---|
| 278 | // var task = tasksArray[Task.WaitAny(tasksArray)];
|
---|
| 279 | // Console.WriteLine(task.Result);
|
---|
| 280 | // tasks.Remove(task);
|
---|
| 281 | //}
|
---|
| 282 |
|
---|
| 283 | Console.WriteLine("Finished TestWaitAny");
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[5207] | 286 | private static void TestAlgorithmPerformanceIssue() {
|
---|
| 287 | Queue<TimeSpan> latestExecutionTimes = new Queue<TimeSpan>();
|
---|
| 288 | int size = 10;
|
---|
| 289 |
|
---|
| 290 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 291 | ga.PopulationSize.Value = 3;
|
---|
| 292 | ga.MaximumGenerations.Value = 1;
|
---|
| 293 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 294 |
|
---|
| 295 | MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
|
---|
| 296 | metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
|
---|
| 297 | GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
|
---|
| 298 | ParameterConfigurationTree algorithmVc = SetupGAAlgorithm(ga, metaOptimizationProblem);
|
---|
| 299 | Stopwatch sw = new Stopwatch();
|
---|
| 300 |
|
---|
| 301 | for (int i = 0; i < 1000; i++) {
|
---|
| 302 | sw.Start();
|
---|
| 303 | GeneticAlgorithm clonedGa = (GeneticAlgorithm)ga.Clone();
|
---|
| 304 | clonedGa.Name = "CLONED GA";
|
---|
| 305 | algorithmVc.Parameterize(clonedGa);
|
---|
| 306 | clonedGa.Prepare(true);
|
---|
| 307 | var executor = new AlgorithmExecutor(clonedGa);
|
---|
| 308 | executor.StartSync();
|
---|
| 309 | sw.Stop();
|
---|
| 310 | latestExecutionTimes.Enqueue(sw.Elapsed);
|
---|
| 311 | Console.WriteLine("{0}: {1} ({2})", i, sw.Elapsed, latestExecutionTimes.Count > size ? TimeSpan.FromMilliseconds(latestExecutionTimes.Average(t => t.TotalMilliseconds)).ToString() : "-");
|
---|
| 312 | if (latestExecutionTimes.Count > size) {
|
---|
| 313 | latestExecutionTimes.Dequeue();
|
---|
| 314 | }
|
---|
| 315 | sw.Reset();
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | private static void TestTableBuilder() {
|
---|
| 320 | TableBuilder tb = new TableBuilder("column_1", "col2", "col3");
|
---|
| 321 | tb.AppendRow("1", "humpi", "0.23124");
|
---|
| 322 | tb.AppendRow("2", "sf", "0.23124");
|
---|
| 323 | tb.AppendRow("5", "humpi dampti", "0.224");
|
---|
| 324 | tb.AppendRow("10", "egon asdf", "0.4");
|
---|
| 325 | tb.AppendRow("15", "MichaelizcMultiVfds", "0.23124564");
|
---|
| 326 | Console.WriteLine(tb.ToString());
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | private static void TestToInfoString(IValueConfiguration algorithmVc) {
|
---|
[5144] | 330 | var random = new MersenneTwister();
|
---|
[5184] | 331 | Console.WriteLine(algorithmVc.ParameterInfoString);
|
---|
[5144] | 332 | algorithmVc.Randomize(random);
|
---|
[5184] | 333 | Console.WriteLine(algorithmVc.ParameterInfoString);
|
---|
[5144] | 334 | algorithmVc.Randomize(random);
|
---|
[5184] | 335 | Console.WriteLine(algorithmVc.ParameterInfoString);
|
---|
[5144] | 336 | algorithmVc.Randomize(random);
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | private static void TestCombinations() {
|
---|
| 340 | Console.WriteLine("IntRange 3-18:3");
|
---|
| 341 | IntValueRange intRange = new IntValueRange(new IntValue(3), new IntValue(18), new IntValue(3));
|
---|
| 342 | foreach (var val in intRange.GetCombinations()) {
|
---|
| 343 | Console.WriteLine(val);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | Console.WriteLine("DoubleRange 1.0-2.5:0.5");
|
---|
| 347 | var dblRange = new DoubleValueRange(new DoubleValue(0.7), new DoubleValue(2.8), new DoubleValue(0.5));
|
---|
| 348 | foreach (var val in dblRange.GetCombinations()) {
|
---|
| 349 | Console.WriteLine(val);
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | Console.WriteLine("PercentRange 33%-66%:33%");
|
---|
| 353 | var pctRange = new PercentValueRange(new PercentValue(0.32), new PercentValue(0.98), new PercentValue(0.33));
|
---|
| 354 | foreach (var val in pctRange.GetCombinations()) {
|
---|
| 355 | Console.WriteLine(val);
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | private static void TestCombinations3() {
|
---|
| 360 | Node root = new Node("root");
|
---|
| 361 | root.ChildNodes.Add(new Node("root.n1"));
|
---|
| 362 | root.ChildNodes.Add(new Node("root.n2"));
|
---|
| 363 | Node n3 = new Node("root.n3");
|
---|
| 364 | n3.ChildNodes.Add(new Node("root.n3.n1"));
|
---|
| 365 | n3.ChildNodes.Add(new Node("root.n3.n2"));
|
---|
| 366 | root.ChildNodes.Add(n3);
|
---|
| 367 |
|
---|
| 368 | Console.WriteLine(root.ToString());
|
---|
| 369 | Console.WriteLine("--");
|
---|
| 370 | int cnt = 0;
|
---|
| 371 | var enumerator = new NodeEnumerator(root);
|
---|
| 372 | enumerator.Reset();
|
---|
| 373 | while (enumerator.MoveNext()) {
|
---|
| 374 | Console.WriteLine(enumerator.Current.ToString());
|
---|
| 375 | cnt++;
|
---|
| 376 | }
|
---|
| 377 | Console.WriteLine("count: " + cnt);
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[5184] | 380 | private static void TestEnumeratorCollectionEnumerator() {
|
---|
| 381 | IEnumerable<int> list1 = new int[] { 1, 2, 3, 4, 5 };
|
---|
| 382 | IEnumerable<int> list2 = new int[] { 10, 20, 30 };
|
---|
| 383 | IEnumerable<int> list3 = new int[] { 300, 400, 500 };
|
---|
| 384 |
|
---|
| 385 | var enumerators = new List<IEnumerator>();
|
---|
| 386 |
|
---|
| 387 | EnumeratorCollectionEnumerator<int> enu = new EnumeratorCollectionEnumerator<int>();
|
---|
| 388 | enu.AddEnumerator(list1.GetEnumerator());
|
---|
| 389 | enu.AddEnumerator(list2.GetEnumerator());
|
---|
| 390 | enu.AddEnumerator(list3.GetEnumerator());
|
---|
| 391 | enu.Reset();
|
---|
| 392 | while (enu.MoveNext()) {
|
---|
| 393 | Console.WriteLine(enu.Current);
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[5144] | 397 | private static void TestCombinations4() {
|
---|
| 398 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 399 | ga.Problem = new SingleObjectiveTestFunctionProblem();
|
---|
[5184] | 400 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 401 |
|
---|
[5144] | 402 | ParameterConfigurationTree vc = new ParameterConfigurationTree(ga);
|
---|
| 403 |
|
---|
[5184] | 404 | ConfigurePopulationSize(vc, 20, 100, 20);
|
---|
[5144] | 405 | //ConfigureMutationRate(vc, 0.10, 0.60, 0.10);
|
---|
[5184] | 406 | //ConfigureMutationOperator(vc);
|
---|
| 407 | ConfigureSelectionOperator(vc, true);
|
---|
[5144] | 408 |
|
---|
| 409 | int count = 0;
|
---|
| 410 | IEnumerator enumerator = new ParameterCombinationsEnumerator(vc);
|
---|
| 411 | enumerator.Reset();
|
---|
| 412 | while (enumerator.MoveNext()) {
|
---|
| 413 | var current = (IValueConfiguration)enumerator.Current;
|
---|
| 414 | count++;
|
---|
[5184] | 415 | Console.WriteLine(current.ParameterInfoString);
|
---|
[5144] | 416 | }
|
---|
| 417 | Console.WriteLine("You are about to create {0} algorithms.", count);
|
---|
[5207] | 418 |
|
---|
[5184] | 419 | Experiment experiment = vc.GenerateExperiment(ga);
|
---|
| 420 | //foreach (var opt in experiment.Optimizers) {
|
---|
| 421 | // Console.WriteLine(opt.Name);
|
---|
| 422 | //}
|
---|
[5144] | 423 |
|
---|
[5184] | 424 | experiment.Prepare();
|
---|
| 425 | experiment.Start();
|
---|
| 426 |
|
---|
| 427 | while (experiment.ExecutionState != ExecutionState.Stopped) {
|
---|
| 428 | Thread.Sleep(500);
|
---|
[5144] | 429 | }
|
---|
| 430 | }
|
---|
| 431 |
|
---|
[5111] | 432 | private static void TestOperators() {
|
---|
| 433 | IRandom random = new MersenneTwister();
|
---|
| 434 |
|
---|
| 435 | var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(100), new DoubleValue(0.1));
|
---|
| 436 | using (var sw = new StreamWriter("out-DoubleValue.txt")) {
|
---|
| 437 | for (int i = 0; i < 10000; i++) {
|
---|
[5207] | 438 | var val = new DoubleValue(90);
|
---|
[5111] | 439 | NormalDoubleValueManipulator.ApplyStatic(random, val, doubleRange);
|
---|
[5144] | 440 |
|
---|
[5111] | 441 | sw.WriteLine(val);
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | var percentRange = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.001));
|
---|
| 446 | using (var sw = new StreamWriter("out-PercentValue.txt")) {
|
---|
| 447 | for (int i = 0; i < 10000; i++) {
|
---|
| 448 | var val = new PercentValue(0.5);
|
---|
| 449 | NormalDoubleValueManipulator.ApplyStatic(random, val, percentRange.AsDoubleValueRange());
|
---|
| 450 | sw.WriteLine(val);
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | var intRange = new IntValueRange(new IntValue(0), new IntValue(100), new IntValue(1));
|
---|
| 455 | using (var sw = new StreamWriter("out-IntValue.txt")) {
|
---|
| 456 | for (int i = 0; i < 10000; i++) {
|
---|
| 457 | var val = new IntValue(50);
|
---|
| 458 | UniformIntValueManipulator.ApplyStatic(random, val, intRange);
|
---|
| 459 | sw.WriteLine(val);
|
---|
| 460 | }
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | Console.ReadLine();
|
---|
| 464 | }
|
---|
| 465 |
|
---|
[5110] | 466 | private static void TestTypeDiscovery() {
|
---|
| 467 | PluginLoader.pluginAssemblies.Any();
|
---|
[5144] | 468 |
|
---|
[5110] | 469 | var items = ApplicationManager.Manager.GetInstances(typeof(DoubleArray)).ToArray();
|
---|
| 470 |
|
---|
| 471 | foreach (var item in items) {
|
---|
| 472 | Console.WriteLine(item.ToString());
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 |
|
---|
[5009] | 476 | private static void TestMemoryLeak(GeneticAlgorithm metaLevelAlgorithm) {
|
---|
[5144] | 477 | IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).ParameterConfigurationTree;
|
---|
[5009] | 478 |
|
---|
| 479 | Console.WriteLine("Starting Memory Test...");
|
---|
| 480 | Console.ReadLine();
|
---|
| 481 |
|
---|
[5023] | 482 | var clones = new List<object>();
|
---|
[5009] | 483 | for (int i = 0; i < 1000; i++) {
|
---|
| 484 | var clone = algorithmVc.Clone();
|
---|
[5023] | 485 | clones.Add(clone);
|
---|
[5009] | 486 | }
|
---|
| 487 |
|
---|
| 488 | Console.WriteLine("Finished. Now GC...");
|
---|
| 489 | Console.ReadLine();
|
---|
| 490 |
|
---|
| 491 | GC.Collect();
|
---|
| 492 |
|
---|
| 493 | Console.WriteLine("Finished!");
|
---|
| 494 | Console.ReadLine();
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[5087] | 497 | private static GeneticAlgorithm GetMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
[5009] | 498 | GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm();
|
---|
| 499 | metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
|
---|
| 500 | metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
|
---|
| 501 |
|
---|
| 502 | metaLevelAlgorithm.Problem = metaOptimizationProblem;
|
---|
| 503 | metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
|
---|
[5207] | 504 |
|
---|
[5303] | 505 | metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Where(x => x.GetType() == typeof(ParameterConfigurationOnePositionsManipulator)).Single();
|
---|
| 506 | //metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Where(x => x.GetType() == typeof(ParameterConfigurationAllPositionsManipulator)).Single();
|
---|
[5207] | 507 |
|
---|
[5281] | 508 | metaLevelAlgorithm.MutationProbability.Value = mutationProbability;
|
---|
[5023] | 509 |
|
---|
[5009] | 510 | return metaLevelAlgorithm;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[5207] | 513 | private static GeneticAlgorithm GetParallelMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
| 514 | GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
|
---|
| 515 | metaLevelAlgorithm.Engine = new ParallelEngine.ParallelEngine();
|
---|
[5212] | 516 | return metaLevelAlgorithm;
|
---|
| 517 | }
|
---|
[5207] | 518 |
|
---|
[5212] | 519 | private static GeneticAlgorithm GetHiveParallelMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
| 520 | GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
|
---|
| 521 | metaLevelAlgorithm.Engine = new HiveEngine.HiveEngine();
|
---|
| 522 | ServiceLocator.Instance.ClientFacadePool.UserName = "cneumuel";
|
---|
| 523 | ServiceLocator.Instance.ClientFacadePool.Password = "cneumuel";
|
---|
| 524 | ServiceLocator.Instance.StreamedClientFacadePool.UserName = "cneumuel";
|
---|
| 525 | ServiceLocator.Instance.StreamedClientFacadePool.Password = "cneumuel";
|
---|
[5207] | 526 | return metaLevelAlgorithm;
|
---|
| 527 | }
|
---|
| 528 |
|
---|
[5087] | 529 | private static EvolutionStrategy GetMetaES(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
| 530 | EvolutionStrategy metaLevelAlgorithm = new EvolutionStrategy();
|
---|
| 531 | metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
|
---|
| 532 | metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
|
---|
| 533 |
|
---|
| 534 | metaLevelAlgorithm.Problem = metaOptimizationProblem;
|
---|
| 535 | metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 536 |
|
---|
[5184] | 537 | metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last();
|
---|
[5087] | 538 |
|
---|
| 539 | return metaLevelAlgorithm;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
[5207] | 542 | private static ParameterConfigurationTree SetupGAAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) {
|
---|
[5087] | 543 | baseLevelAlgorithm.Problem = new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem();
|
---|
[5009] | 544 | baseLevelAlgorithm.MaximumGenerations.Value = baseAlgorithmMaxGenerations;
|
---|
| 545 |
|
---|
[4997] | 546 | metaOptimizationProblem.Algorithm = baseLevelAlgorithm;
|
---|
[5207] | 547 | ParameterConfigurationTree algorithmVc = metaOptimizationProblem.ParameterConfigurationTree;
|
---|
[4997] | 548 |
|
---|
[5087] | 549 | metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
|
---|
| 550 | Evaluator = new GriewankEvaluator(),
|
---|
[5303] | 551 | ProblemSize = new IntValue(2)
|
---|
[5281] | 552 | });
|
---|
[5303] | 553 | metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
|
---|
| 554 | Evaluator = new GriewankEvaluator(),
|
---|
| 555 | ProblemSize = new IntValue(20)
|
---|
| 556 | });
|
---|
| 557 | metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
|
---|
| 558 | Evaluator = new GriewankEvaluator(),
|
---|
| 559 | ProblemSize = new IntValue(500)
|
---|
| 560 | });
|
---|
[5087] | 561 |
|
---|
[5212] | 562 | ConfigurePopulationSize(algorithmVc, 12, 100, 1);
|
---|
[5303] | 563 | ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01);
|
---|
[5267] | 564 | ConfigureMutationOperator(algorithmVc);
|
---|
[5293] | 565 | ConfigureElites(algorithmVc, 0, 10, 1);
|
---|
[5277] | 566 | //ConfigureSelectionOperator(algorithmVc, true);
|
---|
[5009] | 567 | return algorithmVc;
|
---|
| 568 | }
|
---|
[4997] | 569 |
|
---|
[5009] | 570 | private static void TestConfiguration(IValueConfiguration algorithmVc, GeneticAlgorithm baseLevelAlgorithm) {
|
---|
[5023] | 571 | IRandom rand = new FastRandom(0);
|
---|
[4997] | 572 | // set random values
|
---|
| 573 | for (int i = 0; i < 10; i++) {
|
---|
| 574 | IValueConfiguration clonedVc = (IValueConfiguration)algorithmVc.Clone();
|
---|
[5111] | 575 | GeneticAlgorithm newAlg = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
|
---|
[5009] | 576 | clonedVc.Randomize(rand);
|
---|
[5111] | 577 | clonedVc.Parameterize(newAlg);
|
---|
[5009] | 578 | Console.WriteLine(string.Format("PopSize: original: {0}, randomized: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
|
---|
[4997] | 579 | Console.WriteLine(string.Format("MutRate: original: {0}, randomized: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
|
---|
[5009] | 580 | Console.WriteLine(string.Format("MutOp: original: {0}, randomized: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
|
---|
[5023] | 581 | Console.WriteLine(string.Format("SelOp: original: {0}, randomized: {1}", baseLevelAlgorithm.Selector, newAlg.Selector));
|
---|
[5111] | 582 | //Console.WriteLine(string.Format("GrSi: original: {0}, randomized: {1}", "?", ((TournamentSelector)newAlg.Selector).GroupSizeParameter.Value));
|
---|
[5023] | 583 | Console.WriteLine("---");
|
---|
[4997] | 584 | }
|
---|
| 585 |
|
---|
[5023] | 586 | Console.WriteLine("=======================");
|
---|
| 587 | algorithmVc.Randomize(rand);
|
---|
| 588 | algorithmVc.Parameterize(baseLevelAlgorithm);
|
---|
[4997] | 589 | // mutate
|
---|
| 590 | for (int i = 0; i < 10; i++) {
|
---|
| 591 | IValueConfiguration clonedVc = (IValueConfiguration)algorithmVc.Clone();
|
---|
[5111] | 592 | GeneticAlgorithm newAlg = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
|
---|
[5277] | 593 | ParameterConfigurationManipulator.Apply(rand, clonedVc, new UniformIntValueManipulator(), new NormalDoubleValueManipulator());
|
---|
| 594 | clonedVc.Parameterize(newAlg);
|
---|
[5144] | 595 |
|
---|
[5009] | 596 | Console.WriteLine(string.Format("PopSize: original: {0}, mutated: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
|
---|
[4997] | 597 | Console.WriteLine(string.Format("MutRate: original: {0}, mutated: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
|
---|
[5009] | 598 | Console.WriteLine(string.Format("MutOp: original: {0}, mutated: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
|
---|
[5023] | 599 | Console.WriteLine(string.Format("SelOp: original: {0}, mutated: {1}", baseLevelAlgorithm.Selector, newAlg.Selector));
|
---|
[5111] | 600 | //Console.WriteLine(string.Format("GrSi: original: {0}, mutated: {1}", ((TournamentSelector)baseLevelAlgorithm.Selector).GroupSizeParameter.Value, ((TournamentSelector)newAlg.Selector).GroupSizeParameter.Value));
|
---|
[5023] | 601 | Console.WriteLine("---");
|
---|
[4997] | 602 | }
|
---|
| 603 |
|
---|
[5023] | 604 | Console.WriteLine("=======================");
|
---|
[4997] | 605 | // cross
|
---|
| 606 | for (int i = 0; i < 10; i++) {
|
---|
| 607 | IValueConfiguration clonedVc1 = (IValueConfiguration)algorithmVc.Clone();
|
---|
[5023] | 608 | IValueConfiguration clonedVc2 = (IValueConfiguration)algorithmVc.Clone();
|
---|
| 609 |
|
---|
[5111] | 610 | GeneticAlgorithm first = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
|
---|
| 611 | GeneticAlgorithm second = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
|
---|
[5023] | 612 |
|
---|
[5009] | 613 | clonedVc1.Randomize(rand);
|
---|
[5023] | 614 | clonedVc1.Parameterize(first);
|
---|
[4997] | 615 |
|
---|
[5023] | 616 | clonedVc2.Randomize(rand);
|
---|
| 617 | clonedVc2.Parameterize(second);
|
---|
[4997] | 618 |
|
---|
| 619 | var popSizeBefore = first.PopulationSize.Value;
|
---|
| 620 | var mutRateBefore = first.MutationProbability.Value;
|
---|
[5009] | 621 | var mutOpBefore = first.Mutator;
|
---|
[5023] | 622 | var selOpBefore = first.Selector;
|
---|
[5111] | 623 | //var groupSizeBefore = ((TournamentSelector)first.Selector).GroupSizeParameter.Value.Value;
|
---|
[4997] | 624 |
|
---|
[5111] | 625 | //clonedVc1.Cross(clonedVc2, rand); todo
|
---|
[5277] | 626 |
|
---|
| 627 | ParameterConfigurationCrossover.Apply(rand, clonedVc1, clonedVc2, new DiscreteIntValueCrossover(), new AverageDoubleValueCrossover());
|
---|
[5023] | 628 | clonedVc1.Parameterize(first);
|
---|
[5009] | 629 |
|
---|
| 630 | Console.WriteLine(string.Format("PopSize: first: {0}, second: {1}, crossed: {2}", popSizeBefore, second.PopulationSize, first.PopulationSize));
|
---|
[4997] | 631 | Console.WriteLine(string.Format("MutRate: first: {0}, second: {1}, crossed: {2}", mutRateBefore, second.MutationProbability, first.MutationProbability));
|
---|
[5023] | 632 | Console.WriteLine(string.Format("MutOp: first: {0}, second: {1}, crossed: {2}", mutOpBefore, second.Mutator, first.Mutator));
|
---|
| 633 | Console.WriteLine(string.Format("SelOp: first: {0}, second: {1}, crossed: {2}", selOpBefore, second.Selector, first.Selector));
|
---|
[5111] | 634 | //Console.WriteLine(string.Format("GrSi: first: {0}, second: {1}, crossed: {2}", groupSizeBefore, ((TournamentSelector)second.Selector).GroupSizeParameter.Value, ((TournamentSelector)first.Selector).GroupSizeParameter.Value));
|
---|
[5023] | 635 | Console.WriteLine("---");
|
---|
[4997] | 636 | }
|
---|
[5023] | 637 | Console.WriteLine("=======================");
|
---|
[5009] | 638 | }
|
---|
[4997] | 639 |
|
---|
[5009] | 640 | private static void ConfigureMutationOperator(IValueConfiguration algorithmVc) {
|
---|
| 641 | var mutationOperator = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Mutator").SingleOrDefault();
|
---|
| 642 | mutationOperator.Optimize = true;
|
---|
| 643 |
|
---|
| 644 | // uncheck multiMutator to avoid Michalewicz issue
|
---|
[5110] | 645 | var multiMutator = mutationOperator.ValueConfigurations.Where(x => x.ActualValue.Value != null && x.ActualValue.Value.ItemName.StartsWith("Multi")).SingleOrDefault();
|
---|
[5009] | 646 | if (multiMutator != null) {
|
---|
| 647 | mutationOperator.ValueConfigurations.SetItemCheckedState(multiMutator, false);
|
---|
| 648 | }
|
---|
[4997] | 649 | }
|
---|
| 650 |
|
---|
[5144] | 651 | private static void ConfigureSelectionOperator(IValueConfiguration algorithmVc, bool configureTournamenSize) {
|
---|
[5023] | 652 | var selectionOperatorPc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Selector").SingleOrDefault();
|
---|
| 653 | selectionOperatorPc.Optimize = true;
|
---|
| 654 |
|
---|
| 655 | foreach (var vc in selectionOperatorPc.ValueConfigurations) {
|
---|
| 656 | if (vc.ActualValue.ValueDataType == typeof(TournamentSelector)) {
|
---|
| 657 | selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
|
---|
[5144] | 658 | if (configureTournamenSize) {
|
---|
| 659 | vc.Optimize = true;
|
---|
| 660 | ConfigureTournamentGroupSize(vc);
|
---|
| 661 | }
|
---|
[5087] | 662 | } else if (vc.ActualValue.ValueDataType == typeof(RandomSelector)) {
|
---|
| 663 | selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
|
---|
[5023] | 664 | } else {
|
---|
[5087] | 665 | selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
|
---|
[5023] | 666 | }
|
---|
| 667 | }
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | private static void ConfigureTournamentGroupSize(IValueConfiguration tournamentVc) {
|
---|
| 671 | var groupSizePc = tournamentVc.ParameterConfigurations.Where(x => x.ParameterName == "GroupSize").SingleOrDefault();
|
---|
| 672 | groupSizePc.Optimize = true;
|
---|
| 673 |
|
---|
| 674 | groupSizePc.ValueConfigurations.First().Optimize = true;
|
---|
[5207] | 675 | groupSizePc.ValueConfigurations.First().RangeConstraint.LowerBound = new IntValue(0);
|
---|
[5184] | 676 | groupSizePc.ValueConfigurations.First().RangeConstraint.UpperBound = new IntValue(10);
|
---|
[5023] | 677 | groupSizePc.ValueConfigurations.First().RangeConstraint.StepSize = new IntValue(1);
|
---|
| 678 | }
|
---|
| 679 |
|
---|
[5144] | 680 | private static void ConfigurePopulationSize(IValueConfiguration algorithmVc, int lower, int upper, int stepsize) {
|
---|
[4997] | 681 | var populationSizePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "PopulationSize").SingleOrDefault();
|
---|
| 682 | populationSizePc.Optimize = true;
|
---|
| 683 | var populationSizeVc = populationSizePc.ValueConfigurations.First();
|
---|
| 684 | populationSizeVc.Optimize = true;
|
---|
[5144] | 685 | populationSizeVc.RangeConstraint.LowerBound = new IntValue(lower);
|
---|
| 686 | populationSizeVc.RangeConstraint.UpperBound = new IntValue(upper);
|
---|
| 687 | populationSizeVc.RangeConstraint.StepSize = new IntValue(stepsize);
|
---|
[4997] | 688 | }
|
---|
| 689 |
|
---|
[5144] | 690 | private static void ConfigureMutationRate(IValueConfiguration algorithmVc, double lower, double upper, double stepsize) {
|
---|
[4997] | 691 | var mutationRatePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "MutationProbability").SingleOrDefault();
|
---|
| 692 | mutationRatePc.Optimize = true;
|
---|
| 693 | var mutationRateVc = mutationRatePc.ValueConfigurations.First();
|
---|
| 694 | mutationRateVc.Optimize = true;
|
---|
[5144] | 695 | mutationRateVc.RangeConstraint.LowerBound = new PercentValue(lower);
|
---|
| 696 | mutationRateVc.RangeConstraint.UpperBound = new PercentValue(upper);
|
---|
| 697 | mutationRateVc.RangeConstraint.StepSize = new PercentValue(stepsize);
|
---|
[4997] | 698 | }
|
---|
| 699 |
|
---|
[5184] | 700 | private static void ConfigureElites(IValueConfiguration algorithmVc, int from, int to, int stepSize) {
|
---|
[5023] | 701 | var elitesPc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Elites").SingleOrDefault();
|
---|
| 702 | elitesPc.Optimize = true;
|
---|
| 703 | var elitesVc = elitesPc.ValueConfigurations.First();
|
---|
| 704 | elitesVc.Optimize = true;
|
---|
[5184] | 705 | elitesVc.RangeConstraint.LowerBound = new IntValue(from);
|
---|
| 706 | elitesVc.RangeConstraint.UpperBound = new IntValue(to);
|
---|
| 707 | elitesVc.RangeConstraint.StepSize = new IntValue(stepSize);
|
---|
[5023] | 708 | }
|
---|
| 709 |
|
---|
[5087] | 710 | private static void TestOptimization(EngineAlgorithm metaLevelAlgorithm) {
|
---|
[5023] | 711 | string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Results");
|
---|
[5087] | 712 | if (!Directory.Exists(path))
|
---|
[5023] | 713 | Directory.CreateDirectory(path);
|
---|
[5087] | 714 | string id = DateTime.Now.ToString("yyyy.MM.dd - HH;mm;ss,ffff");
|
---|
| 715 | string resultPath = Path.Combine(path, string.Format("{0} - Result.hl", id));
|
---|
| 716 | string outputPath = Path.Combine(path, string.Format("{0} - Console.txt", id));
|
---|
[5023] | 717 |
|
---|
| 718 | using (var sw = new StreamWriter(outputPath)) {
|
---|
[5087] | 719 | sw.AutoFlush = true;
|
---|
| 720 |
|
---|
| 721 | StringBuilder sb1 = new StringBuilder();
|
---|
[5207] | 722 | sb1.AppendFormat("Meta.PopulationSize: {0}\n", metaAlgorithmPopulationSize);
|
---|
| 723 | sb1.AppendFormat("Meta.MaxGenerations: {0}\n", metaAlgorithmMaxGenerations);
|
---|
| 724 | sb1.AppendFormat("Meta.Repetitions : {0}\n", metaProblemRepetitions);
|
---|
| 725 | sb1.AppendFormat("Meta.MutProb : {0}\n", ((GeneticAlgorithm)metaLevelAlgorithm).MutationProbability.Value);
|
---|
| 726 | sb1.AppendFormat("Base.MaxGenerations: {0}\n", baseAlgorithmMaxGenerations);
|
---|
| 727 | sb1.AppendLine("Problems:");
|
---|
| 728 | foreach (var prob in ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).Problems) {
|
---|
| 729 | sb1.Append(prob.Name);
|
---|
| 730 | var sotf = prob as SingleObjectiveTestFunctionProblem;
|
---|
| 731 | if (sotf != null) {
|
---|
| 732 | sb1.AppendFormat(" {0}", sotf.ProblemSize.Value);
|
---|
| 733 | }
|
---|
| 734 | sb1.AppendLine();
|
---|
| 735 | }
|
---|
[5087] | 736 | sw.WriteLine(sb1.ToString());
|
---|
| 737 | Console.WriteLine(sb1.ToString());
|
---|
[5184] | 738 | metaLevelAlgorithm.Stopped += new EventHandler(metaLevelAlgorithm_Stopped);
|
---|
| 739 | metaLevelAlgorithm.Paused += new EventHandler(metaLevelAlgorithm_Paused);
|
---|
| 740 | metaLevelAlgorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(metaLevelAlgorithm_ExceptionOccurred);
|
---|
[5087] | 741 |
|
---|
[5023] | 742 | metaLevelAlgorithm.Start();
|
---|
| 743 | int i = 0;
|
---|
| 744 | int currentGeneration = -1;
|
---|
| 745 | do {
|
---|
[5277] | 746 | Thread.Sleep(1000);
|
---|
[5087] | 747 | if (metaLevelAlgorithm.Results.ContainsKey("Generations") && ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value != currentGeneration) {
|
---|
[5281] | 748 | while (metaLevelAlgorithm.Results.Count < 6) Thread.Sleep(1000);
|
---|
[5087] | 749 | StringBuilder sb = new StringBuilder();
|
---|
| 750 | sb.AppendLine(DateTime.Now.ToLongTimeString());
|
---|
| 751 | sb.AppendLine("=================================");
|
---|
[5023] | 752 |
|
---|
[5111] | 753 | sb.AppendLine(metaLevelAlgorithm.ExecutionState.ToString());
|
---|
[5281] | 754 | ResultCollection rsClone = null;
|
---|
| 755 | while (rsClone == null) {
|
---|
| 756 | try {
|
---|
| 757 | rsClone = (ResultCollection)metaLevelAlgorithm.Results.Clone();
|
---|
| 758 | }
|
---|
| 759 | catch { }
|
---|
| 760 | }
|
---|
[5207] | 761 | foreach (var result in rsClone) {
|
---|
[5087] | 762 | sb.AppendLine(result.ToString());
|
---|
| 763 | if (result.Name == "Population") {
|
---|
| 764 | RunCollection rc = (RunCollection)result.Value;
|
---|
[5212] | 765 | var orderedRuns = rc.OrderBy(x => x.Results["AverageQualityNormalized"]);
|
---|
[5087] | 766 |
|
---|
[5281] | 767 | TableBuilder tb = new TableBuilder("QNorm", "Qualities", "PoSi", "MutRa", "Eli", "SelOp", "MutOp", "NrSelSubScopes");
|
---|
[5087] | 768 | foreach (IRun run in orderedRuns) {
|
---|
| 769 | string selector;
|
---|
| 770 | if (run.Parameters["Selector"] is TournamentSelector) {
|
---|
| 771 | selector = string.Format("{0} ({1})", run.Parameters["Selector"].ToString(), ((TournamentSelector)run.Parameters["Selector"]).GroupSizeParameter.Value.ToString());
|
---|
| 772 | } else {
|
---|
| 773 | selector = string.Format("{0}", run.Parameters["Selector"].ToString());
|
---|
[5023] | 774 | }
|
---|
[5087] | 775 |
|
---|
[5207] | 776 | tb.AppendRow(
|
---|
[5293] | 777 | ((DoubleValue)run.Results["AverageQualityNormalized"]).Value.ToString("#0.0000"),
|
---|
[5281] | 778 | ((DoubleArray)run.Results["RunsAverageQualities"]).ToString(),
|
---|
[5207] | 779 | ((IntValue)run.Parameters["PopulationSize"]).Value.ToString(),
|
---|
[5293] | 780 | ((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.0000"),
|
---|
[5207] | 781 | ((IntValue)run.Parameters["Elites"]).Value.ToString(),
|
---|
| 782 | Shorten(selector, 20),
|
---|
| 783 | Shorten(run.Parameters.ContainsKey("Mutator") ? run.Parameters["Mutator"].ToString() : "null", 40),
|
---|
| 784 | ((ISelector)run.Parameters["Selector"]).NumberOfSelectedSubScopesParameter.Value.ToString());
|
---|
[5023] | 785 | }
|
---|
[5207] | 786 | sb.AppendLine(tb.ToString());
|
---|
[5087] | 787 | }
|
---|
| 788 | } // foreach
|
---|
[5207] | 789 | //Console.Clear();
|
---|
[5087] | 790 | Console.WriteLine(sb.ToString());
|
---|
| 791 | sw.WriteLine(sb.ToString());
|
---|
| 792 | currentGeneration = ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value;
|
---|
| 793 | } // if
|
---|
[5207] | 794 | //if (i % 30 == 0) GC.Collect();
|
---|
[5087] | 795 | i++;
|
---|
[5023] | 796 | } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped);
|
---|
| 797 | }
|
---|
[5009] | 798 |
|
---|
[5023] | 799 | Console.WriteLine();
|
---|
| 800 | Console.WriteLine("Storing...");
|
---|
| 801 |
|
---|
[5087] | 802 | ContentManager.Save((IStorableContent)metaLevelAlgorithm, resultPath, true);
|
---|
[5009] | 803 | Console.WriteLine("Finished");
|
---|
| 804 | }
|
---|
| 805 |
|
---|
[5184] | 806 | private static void metaLevelAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 807 | Console.WriteLine("metaLevelAlgorithm_ExceptionOccurred");
|
---|
[5212] | 808 | Console.WriteLine(e.Value.ToString());
|
---|
| 809 | if (e.Value.InnerException != null) {
|
---|
| 810 | Console.WriteLine(e.Value.InnerException.ToString());
|
---|
| 811 | }
|
---|
[5184] | 812 | }
|
---|
| 813 |
|
---|
| 814 | private static void metaLevelAlgorithm_Paused(object sender, EventArgs e) {
|
---|
| 815 | Console.WriteLine("metaLevelAlgorithm_Paused");
|
---|
| 816 | }
|
---|
| 817 |
|
---|
| 818 | private static void metaLevelAlgorithm_Stopped(object sender, EventArgs e) {
|
---|
| 819 | Console.WriteLine("metaLevelAlgorithm_Stopped");
|
---|
| 820 | }
|
---|
| 821 |
|
---|
[5087] | 822 | private static void TestShorten() {
|
---|
| 823 | int n = 8;
|
---|
| 824 | Console.WriteLine(Shorten("1", n));
|
---|
| 825 | Console.WriteLine(Shorten("12", n));
|
---|
| 826 | Console.WriteLine(Shorten("123", n));
|
---|
| 827 | Console.WriteLine(Shorten("1234", n));
|
---|
| 828 | Console.WriteLine(Shorten("12345", n));
|
---|
| 829 | Console.WriteLine(Shorten("123456", n));
|
---|
| 830 | Console.WriteLine(Shorten("1234567", n));
|
---|
| 831 | Console.WriteLine(Shorten("12345678", n));
|
---|
| 832 | Console.WriteLine(Shorten("123456789", n));
|
---|
| 833 | Console.WriteLine(Shorten("1234567890", n));
|
---|
| 834 | Console.WriteLine(Shorten("12345678901", n));
|
---|
| 835 | }
|
---|
| 836 |
|
---|
| 837 | private static string Shorten(string s, int n) {
|
---|
| 838 | string placeholder = "..";
|
---|
| 839 | if (s.Length <= n) return s;
|
---|
| 840 | int len = n / 2 - placeholder.Length / 2;
|
---|
| 841 | string start = s.Substring(0, len);
|
---|
| 842 | string end = s.Substring(s.Length - len, len);
|
---|
| 843 | return start + placeholder + end;
|
---|
| 844 | }
|
---|
| 845 |
|
---|
[4997] | 846 | private static void TestIntSampling() {
|
---|
| 847 | System.Random rand = new System.Random();
|
---|
| 848 | int lower = 10;
|
---|
| 849 | int upper = 20;
|
---|
[5207] | 850 | int stepsize = 1;
|
---|
[4997] | 851 | for (int i = 0; i < 100; i++) {
|
---|
| 852 | int val;
|
---|
| 853 | do {
|
---|
| 854 | val = rand.Next(lower / stepsize, upper / stepsize + 1) * stepsize;
|
---|
| 855 | } while (val < lower || val > upper);
|
---|
| 856 | Console.WriteLine(val);
|
---|
| 857 | }
|
---|
| 858 | }
|
---|
| 859 |
|
---|
| 860 | private static void TestDoubleSampling() {
|
---|
| 861 | System.Random rand = new System.Random();
|
---|
| 862 | double lower = 2;
|
---|
| 863 | double upper = 3;
|
---|
| 864 | double stepsize = 0.6;
|
---|
| 865 | for (int i = 0; i < 100; i++) {
|
---|
| 866 | double val;
|
---|
| 867 | do {
|
---|
| 868 | val = Math.Round((rand.NextDouble() * (upper - lower) + lower) / stepsize, 0) * stepsize;
|
---|
| 869 | } while (val < lower || val > upper);
|
---|
| 870 | Console.WriteLine(val);
|
---|
| 871 | }
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 | private static IEnumerable<IItem> GetValidValues(IValueParameter valueParameter) {
|
---|
| 875 | return ApplicationManager.Manager.GetInstances(valueParameter.DataType).Select(x => (IItem)x).OrderBy(x => x.ItemName);
|
---|
| 876 | }
|
---|
| 877 | }
|
---|
[5144] | 878 |
|
---|
| 879 | public class Node {
|
---|
| 880 | public string Name { get; set; }
|
---|
| 881 | public int ActualValue { get; set; }
|
---|
| 882 | public int[] PossibleValues { get; set; }
|
---|
| 883 | public List<Node> ChildNodes { get; set; }
|
---|
| 884 |
|
---|
| 885 | public Node(string name) {
|
---|
| 886 | this.Name = name;
|
---|
| 887 | PossibleValues = new int[] { 1, 2, 3 };
|
---|
| 888 | ChildNodes = new List<Node>();
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | public void Init() {
|
---|
| 892 | this.ActualValue = PossibleValues.First();
|
---|
| 893 | foreach (var child in ChildNodes) {
|
---|
| 894 | child.Init();
|
---|
| 895 | }
|
---|
| 896 | }
|
---|
| 897 |
|
---|
| 898 | public override string ToString() {
|
---|
| 899 | StringBuilder sb = new StringBuilder();
|
---|
| 900 | sb.Append(string.Format("{0}:{1}", this.Name, this.ActualValue));
|
---|
| 901 | if (this.ChildNodes.Count() > 0) {
|
---|
| 902 | sb.Append(" (");
|
---|
| 903 | var lst = new List<string>();
|
---|
| 904 | foreach (Node child in ChildNodes) {
|
---|
| 905 | lst.Add(child.ToString());
|
---|
| 906 | }
|
---|
| 907 | sb.Append(string.Join(", ", lst.ToArray()));
|
---|
| 908 | sb.Append(")");
|
---|
| 909 | }
|
---|
| 910 |
|
---|
| 911 | return sb.ToString();
|
---|
| 912 | }
|
---|
| 913 | }
|
---|
| 914 |
|
---|
| 915 | public class NodeEnumerator : IEnumerator<Node> {
|
---|
| 916 | private Node node;
|
---|
| 917 | private List<IEnumerator> enumerators;
|
---|
| 918 |
|
---|
| 919 | public NodeEnumerator(Node node) {
|
---|
| 920 | this.node = node;
|
---|
| 921 | this.enumerators = new List<IEnumerator>();
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | public Node Current {
|
---|
| 925 | get { return node; }
|
---|
| 926 | }
|
---|
| 927 | object IEnumerator.Current {
|
---|
| 928 | get { return Current; }
|
---|
| 929 | }
|
---|
| 930 |
|
---|
| 931 | public void Dispose() { }
|
---|
| 932 |
|
---|
| 933 | public bool MoveNext() {
|
---|
| 934 | int i = 0;
|
---|
| 935 | bool ok = false;
|
---|
[5207] | 936 | while (!ok && i < enumerators.Count) {
|
---|
| 937 | if (enumerators[i].MoveNext()) {
|
---|
[5144] | 938 | ok = true;
|
---|
| 939 | } else {
|
---|
| 940 | i++;
|
---|
| 941 | }
|
---|
| 942 | }
|
---|
| 943 |
|
---|
| 944 | if (ok) {
|
---|
[5207] | 945 | for (int k = i - 1; k >= 0; k--) {
|
---|
[5144] | 946 | enumerators[k].Reset();
|
---|
| 947 | enumerators[k].MoveNext();
|
---|
| 948 | }
|
---|
| 949 | } else {
|
---|
| 950 | return false;
|
---|
| 951 | }
|
---|
| 952 |
|
---|
| 953 | node.ActualValue = (int)enumerators[0].Current;
|
---|
| 954 | return true;
|
---|
| 955 | }
|
---|
| 956 |
|
---|
| 957 | public void Reset() {
|
---|
| 958 | enumerators.Clear();
|
---|
| 959 | enumerators.Add(node.PossibleValues.GetEnumerator());
|
---|
| 960 | enumerators[0].Reset();
|
---|
| 961 |
|
---|
| 962 | foreach (var child in node.ChildNodes) {
|
---|
| 963 | var enumerator = new NodeEnumerator(child);
|
---|
| 964 | enumerator.Reset();
|
---|
| 965 | enumerator.MoveNext();
|
---|
| 966 | enumerators.Add(enumerator);
|
---|
| 967 | }
|
---|
| 968 | }
|
---|
| 969 | }
|
---|
[4997] | 970 | }
|
---|