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