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