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