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