[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;
|
---|
[5927] | 16 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[6090] | 17 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[6017] | 18 | //using HeuristicLab.Hive.ExperimentManager;
|
---|
[5144] | 19 | using HeuristicLab.Optimization;
|
---|
[5653] | 20 | using HeuristicLab.Parameters;
|
---|
[4997] | 21 | using HeuristicLab.PluginInfrastructure;
|
---|
[5653] | 22 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
[5927] | 23 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[4997] | 24 | using HeuristicLab.Problems.MetaOptimization;
|
---|
[5144] | 25 | using HeuristicLab.Problems.TestFunctions;
|
---|
[5009] | 26 | using HeuristicLab.Random;
|
---|
[5023] | 27 | using HeuristicLab.Selection;
|
---|
[4997] | 28 |
|
---|
| 29 | namespace HeuristicLab.MetaOptimization.Test {
|
---|
| 30 | class Program {
|
---|
[5522] | 31 | static void Main(string[] args) {
|
---|
| 32 | PluginManager pm = new PluginManager(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
|
---|
| 33 | pm.DiscoverAndCheckPlugins();
|
---|
[8820] | 34 | pm.Run(pm.Applications.Where(x => x.Name == "TestApp").SingleOrDefault(), CommandLineArgumentHandling.GetArguments(args));
|
---|
[5522] | 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [Plugin("TestPlugin", "1.0.0.0")]
|
---|
| 39 | [PluginFile("HeuristicLab.MetaOptimization.Test.exe", PluginFileType.Assembly)]
|
---|
| 40 | public class TestPlugin : PluginBase { }
|
---|
| 41 |
|
---|
| 42 | [Application("TestApp")]
|
---|
| 43 | public class TestApp : ApplicationBase {
|
---|
[6090] | 44 | //private static int metaAlgorithmPopulationSize = 30;
|
---|
[5212] | 45 | //private static int metaAlgorithmMaxGenerations = 30;
|
---|
[6090] | 46 | //private static int metaProblemRepetitions = 3;
|
---|
| 47 | //private static int baseAlgorithmMaxGenerations = 500;
|
---|
| 48 | //private static double mutationProbability = 0.10;
|
---|
[5009] | 49 |
|
---|
[6489] | 50 | private static int metaAlgorithmPopulationSize = 3;
|
---|
[6197] | 51 | private static int metaAlgorithmMaxGenerations = 10;
|
---|
[6090] | 52 | private static double metaAlgorithmMutationProbability = 0.10;
|
---|
[6489] | 53 | private static int metaProblemRepetitions = 2;
|
---|
| 54 | private static int baseAlgorithmMaxGenerations = 1;
|
---|
| 55 | private static int baseAlgorithmPopulationSize = 5;
|
---|
[6197] | 56 |
|
---|
[8820] | 57 | public override void Run(ICommandLineArgument[] args) {
|
---|
[5267] | 58 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
[5522] | 59 |
|
---|
[5207] | 60 | //TestTableBuilder();
|
---|
[5087] | 61 | //TestShorten();
|
---|
| 62 |
|
---|
[6197] | 63 |
|
---|
[6445] | 64 | bool y = ParameterConfiguration.IsSubclassOfRawGeneric(typeof(Tuple<,,,>), typeof(Tuple<,>));
|
---|
| 65 | bool a = ParameterConfiguration.IsSubclassOfRawGeneric(typeof(Tuple<>), typeof(Tuple<int>));
|
---|
| 66 |
|
---|
| 67 |
|
---|
[6197] | 68 | var x = typeof(float).IsPrimitive;
|
---|
| 69 |
|
---|
[5522] | 70 | //TestSimilarities(); return;
|
---|
[4997] | 71 | //TestIntSampling();
|
---|
[5207] | 72 | //TestDoubleSampling(); return;
|
---|
[5110] | 73 | //TestTypeDiscovery();
|
---|
[6090] | 74 | //TestOperators(); return;
|
---|
[5144] | 75 | //TestCombinations();
|
---|
| 76 | //TestCombinations2();
|
---|
| 77 | //TestCombinations3();
|
---|
[5184] | 78 | //TestEnumeratorCollectionEnumerator();
|
---|
[5522] | 79 | //TestCombinations4(); return;
|
---|
[5653] | 80 | //TestAlgorithmPerformanceIssue(); return;
|
---|
[5262] | 81 | //TestWaitAny();
|
---|
[5267] | 82 | //TestExecutionTimeUpdateInvervalPerformance();
|
---|
[5277] | 83 | //TestMemoryConsumption();
|
---|
[5293] | 84 | //TestNormalCrossover();
|
---|
[5303] | 85 | //TestItemDictionary();
|
---|
[5927] | 86 |
|
---|
| 87 | //TestSymbolicDataAnalysisGrammar(); return;
|
---|
[6489] | 88 | //TestObjectGraphObjectsTraversal(); return;
|
---|
[6197] | 89 | //TestParameterizedItem(); return;
|
---|
[5927] | 90 |
|
---|
[6090] | 91 | //MetaOptimizationProblem metaOptimizationProblem = LoadOptimizationProblem("Meta Optimization Problem (Genetic Programming - Symbolic Regression 3.4 scaled).hl");
|
---|
| 92 | //var algorithmVc = metaOptimizationProblem.ParameterConfigurationTree;
|
---|
| 93 |
|
---|
[6489] | 94 | //var metaOptimizationProblem = new MetaOptimizationProblem();
|
---|
| 95 | //var algorithmVc = SetupGAAlgorithm(typeof(GeneticAlgorithm), metaOptimizationProblem);
|
---|
[6090] | 96 |
|
---|
[6489] | 97 | // import classification 4 problems
|
---|
| 98 | var metaOptimizationProblem = ((MetaOptimizationProblem)ContentManager.Load("MetaOpt GA,OSGA,Classification (melanoma,respiratory,wisconsin,prostata),MSE-Evaluator_test.hl"));
|
---|
| 99 | metaOptimizationProblem.QualityMeasureNameParameter.Value.Value = "Best training solution.Mean squared error (test)";
|
---|
| 100 |
|
---|
[5110] | 101 | metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
|
---|
[6489] | 102 | GeneticAlgorithm metaLevelAlgorithm = GetSequentialMetaGA(metaOptimizationProblem);
|
---|
| 103 | //GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
|
---|
[5212] | 104 | //GeneticAlgorithm metaLevelAlgorithm = GetHiveParallelMetaGA(metaOptimizationProblem);
|
---|
[5087] | 105 | //EvolutionStrategy metaLevelAlgorithm = GetMetaES(metaOptimizationProblem);
|
---|
[5009] | 106 |
|
---|
[6197] | 107 | //Console.WriteLine("Press enter to start"); Console.ReadLine();
|
---|
[6017] | 108 | //TestConfiguration(algorithmVc, typeof(GeneticAlgorithm), metaOptimizationProblem.Problems.First());
|
---|
[5144] | 109 |
|
---|
[6489] | 110 | //Console.WriteLine("Press enter to start"); Console.ReadLine();
|
---|
[5009] | 111 | TestOptimization(metaLevelAlgorithm);
|
---|
| 112 |
|
---|
| 113 | //TestMemoryLeak(metaLevelAlgorithm);
|
---|
| 114 |
|
---|
| 115 | Console.ReadLine();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[6197] | 118 | private void TestParameterizedItem() {
|
---|
| 119 | var value = new MyParameterizedItem();
|
---|
| 120 | Console.WriteLine("P1=1;; " + value.ToString());
|
---|
| 121 |
|
---|
| 122 | var vc = new ParameterizedValueConfiguration(value, typeof(MyParameterizedItem), true);
|
---|
| 123 | vc.Optimize = true;
|
---|
| 124 | ((IntValue)vc.ParameterConfigurations.Single(x => x.Name == "P1").ActualValue.Value).Value = 22;
|
---|
| 125 | Console.WriteLine("P1=22;; " + value.ToString());
|
---|
| 126 | vc.ParameterConfigurations.Single(x => x.Name == "P1").ActualValue.Value = new IntValue(33);
|
---|
| 127 | Console.WriteLine("P1=33;; " + value.ToString());
|
---|
| 128 | vc.Parameterize(value);
|
---|
| 129 | Console.WriteLine("P1=33;; " + value.ToString());
|
---|
[7137] | 130 |
|
---|
[6197] | 131 | Console.ReadLine();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private void TestObjectGraphObjectsTraversal() {
|
---|
| 135 | //var obj = new GeneticAlgorithm();
|
---|
| 136 | var obj = ContentManager.Load("Genetic Programming - Symbolic Regression 3.4_scaled_paused.hl");
|
---|
| 137 | Console.WriteLine("loaded");
|
---|
| 138 |
|
---|
| 139 | for (int i = 0; i < 10; i++) {
|
---|
| 140 | var sw = new Stopwatch();
|
---|
| 141 | sw.Start();
|
---|
| 142 | var objects = obj.GetObjectGraphObjects().ToArray();
|
---|
| 143 | sw.Stop();
|
---|
| 144 |
|
---|
| 145 | var typeCount = GetTypeCount(objects).ToArray();
|
---|
| 146 | Console.WriteLine("objects: {0}", objects.Count());
|
---|
| 147 | Console.WriteLine(sw.Elapsed);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | private IOrderedEnumerable<KeyValuePair<Type, long>> GetTypeCount(object[] objects) {
|
---|
| 152 | var dict = new Dictionary<Type, long>();
|
---|
| 153 | foreach (var item in objects) {
|
---|
| 154 | var t = item.GetType();
|
---|
| 155 | if (!dict.ContainsKey(t))
|
---|
| 156 | dict.Add(t, 0);
|
---|
| 157 | dict[t]++;
|
---|
| 158 | }
|
---|
| 159 | return dict.OrderByDescending(x => x.Value);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[6090] | 162 | private MetaOptimizationProblem LoadOptimizationProblem(string filename) {
|
---|
| 163 | return (MetaOptimizationProblem)ContentManager.Load(filename);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[5927] | 166 | private void TestSymbolicDataAnalysisGrammar() {
|
---|
| 167 | var random = new MersenneTwister();
|
---|
| 168 |
|
---|
| 169 | var grammar1 = new TypeCoherentExpressionGrammar();
|
---|
| 170 | var grammar2 = new TypeCoherentExpressionGrammar();
|
---|
[6090] | 171 |
|
---|
[6017] | 172 | Console.WriteLine("========== Grammar1: ==========");
|
---|
| 173 | PrintGrammar(grammar1);
|
---|
| 174 | //Console.WriteLine("========== Grammar2: ==========");
|
---|
| 175 | //PrintGrammar(grammar2);
|
---|
[5927] | 176 |
|
---|
[6017] | 177 | var vc1 = new SymbolicExpressionGrammarValueConfiguration(grammar1);
|
---|
[6018] | 178 |
|
---|
| 179 | string info = vc1.ParameterInfoString;
|
---|
| 180 |
|
---|
[6017] | 181 | ConfigureSymbolicExpressionGrammarVc(vc1);
|
---|
[6018] | 182 |
|
---|
| 183 | info = vc1.ParameterInfoString;
|
---|
| 184 |
|
---|
| 185 |
|
---|
[6017] | 186 | var vc2 = new SymbolicExpressionGrammarValueConfiguration(grammar2);
|
---|
| 187 | ConfigureSymbolicExpressionGrammarVc(vc2);
|
---|
[5927] | 188 |
|
---|
[6017] | 189 | vc1.Mutate(random, new MutateDelegate(ParameterConfigurationManipulator.Mutate), new UniformIntValueManipulator(), new UniformDoubleValueManipulator());
|
---|
| 190 | vc1.Parameterize(grammar1);
|
---|
| 191 |
|
---|
| 192 | Console.WriteLine("========== Grammar1 (mutated): ==========");
|
---|
[5927] | 193 | PrintGrammar(grammar1);
|
---|
| 194 |
|
---|
[6017] | 195 | vc1.Cross(random, vc2, new CrossDelegate(ParameterConfigurationCrossover.Cross), new DiscreteIntValueCrossover(), new AverageDoubleValueCrossover());
|
---|
| 196 | vc1.Parameterize(grammar1);
|
---|
[5927] | 197 |
|
---|
[6017] | 198 | Console.WriteLine("========== Grammar1 (crossed): ==========");
|
---|
| 199 | PrintGrammar(grammar1);
|
---|
[5927] | 200 |
|
---|
[6017] | 201 | //RealVector v1 = GetInitialFrequenciesAsRealVector(grammar1);
|
---|
| 202 | //RealVector v2 = GetInitialFrequenciesAsRealVector(grammar2);
|
---|
[5927] | 203 |
|
---|
[6017] | 204 | //for (int i = 0; i < 10; i++) {
|
---|
| 205 | // RealVector v3 = DiscreteCrossover.Apply(random, new ItemArray<RealVector>(new List<RealVector> { v1, v2 }));
|
---|
[5927] | 206 |
|
---|
[6017] | 207 | // var grammar3 = new TypeCoherentExpressionGrammar();
|
---|
| 208 | // SetInitialFrequenciesFromRealVector(grammar3, v3);
|
---|
[5927] | 209 |
|
---|
[6017] | 210 | // Console.WriteLine("\n========== Crossed: ==========");
|
---|
| 211 | // PrintGrammar(grammar3);
|
---|
| 212 | //}
|
---|
| 213 |
|
---|
[5927] | 214 | }
|
---|
| 215 |
|
---|
| 216 | private static void PrintGrammar(TypeCoherentExpressionGrammar grammar) {
|
---|
| 217 | foreach (var symbol in grammar.Symbols) {
|
---|
| 218 | Console.WriteLine("{0} ({1})", symbol.ToString(), symbol.InitialFrequency);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | private static RealVector GetInitialFrequenciesAsRealVector(TypeCoherentExpressionGrammar grammar) {
|
---|
| 223 | var vector = new RealVector(grammar.Symbols.Count());
|
---|
| 224 | for (int i = 0; i < grammar.Symbols.Count(); i++) {
|
---|
| 225 | vector[i] = grammar.Symbols.ElementAt(i).InitialFrequency;
|
---|
| 226 | }
|
---|
| 227 | return vector;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | private static void SetInitialFrequenciesFromRealVector(TypeCoherentExpressionGrammar grammar, RealVector vector) {
|
---|
| 231 | for (int i = 0; i < grammar.Symbols.Count(); i++) {
|
---|
| 232 | grammar.Symbols.ElementAt(i).InitialFrequency = vector[i];
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[5522] | 236 | private static void TestSimilarities() {
|
---|
| 237 | Console.WriteLine("\nDoubleRange:");
|
---|
| 238 | var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(10), new DoubleValue(1));
|
---|
| 239 | var a = new DoubleValue(5.0);
|
---|
| 240 |
|
---|
| 241 | for (double d = 0; d < 10; d += 0.1) {
|
---|
| 242 | var similarity = doubleRange.CalculateSimilarity(a, new DoubleValue(d));
|
---|
| 243 | Console.WriteLine("{0}: {1}", d, similarity);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | Console.WriteLine("\nPecentRange:");
|
---|
| 247 | var percentRange = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(1));
|
---|
| 248 | var b = new PercentValue(0.05);
|
---|
| 249 |
|
---|
| 250 | for (double d = 0; d < 1; d += 0.01) {
|
---|
| 251 | var similarity = percentRange.CalculateSimilarity(b, new PercentValue(d));
|
---|
| 252 | Console.WriteLine("{0}: {1}", d, similarity);
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | Console.WriteLine("\nIntRange:");
|
---|
| 256 | var intRange = new IntValueRange(new IntValue(50), new IntValue(100), new IntValue(1));
|
---|
| 257 | var c = new IntValue(90);
|
---|
| 258 |
|
---|
| 259 | for (int i = 0; i < 100; i++) {
|
---|
| 260 | var similarity = intRange.CalculateSimilarity(c, new IntValue(i));
|
---|
| 261 | Console.WriteLine("{0}: {1}", i, similarity);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | Console.WriteLine("\nValueConfigurations:");
|
---|
| 265 | var vc1 = SetupGAAlgorithm(typeof(GeneticAlgorithm), new MetaOptimizationProblem());
|
---|
[5665] | 266 | vc1.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "Elites").Optimize = true;
|
---|
| 267 | vc1.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "PopulationSize").Optimize = true;
|
---|
| 268 | vc1.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").Optimize = true;
|
---|
| 269 | vc1.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "Selector").Optimize = true;
|
---|
[5522] | 270 |
|
---|
[5653] | 271 | var vc2 = (ParameterConfigurationTree)vc1.Clone();
|
---|
[5522] | 272 | Console.WriteLine("Assert(1): {0}", vc1.CalculateSimilarity(vc2));
|
---|
| 273 |
|
---|
[5665] | 274 | ((IntValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "PopulationSize").ValueConfigurations[0].ActualValue.Value).Value = 75;
|
---|
[5522] | 275 | Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
|
---|
| 276 |
|
---|
[5665] | 277 | ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.15;
|
---|
[5522] | 278 | Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
|
---|
| 279 |
|
---|
[5665] | 280 | ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.25;
|
---|
[5522] | 281 | Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
|
---|
[5665] | 282 | ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.35;
|
---|
[5522] | 283 | Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
|
---|
[5665] | 284 | ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.45;
|
---|
[5522] | 285 | Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
|
---|
[5665] | 286 | ((PercentValue)vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MutationProbability").ValueConfigurations[0].ActualValue.Value).Value = 0.55;
|
---|
[5522] | 287 | Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
|
---|
| 288 |
|
---|
[5665] | 289 | vc2.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "Selector").ActualValueConfigurationIndex = 3;
|
---|
[5522] | 290 | Console.WriteLine("{0}", vc1.CalculateSimilarity(vc2));
|
---|
| 291 |
|
---|
| 292 | var random = new Random.MersenneTwister(0);
|
---|
| 293 | for (int i = 0; i < 10; i++) {
|
---|
| 294 | vc2.Randomize(random);
|
---|
| 295 | Console.WriteLine("Randomized: {0}", vc1.CalculateSimilarity(vc2));
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[5303] | 299 | private static void TestItemDictionary() {
|
---|
| 300 | var dict = new ItemDictionary<StringValue, RunCollection>();
|
---|
| 301 | dict.Add(new StringValue("a"), new RunCollection());
|
---|
| 302 | dict.Add(new StringValue("b"), new RunCollection());
|
---|
| 303 | dict.Add(new StringValue("c"), new RunCollection());
|
---|
| 304 |
|
---|
| 305 | Console.WriteLine(dict.ContainsKey(new StringValue("a")));
|
---|
| 306 | Console.WriteLine(dict.Count(x => x.Key.Value == "a"));
|
---|
| 307 |
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[5293] | 310 | private static void TestNormalCrossover() {
|
---|
| 311 | var random = new MersenneTwister();
|
---|
| 312 | double d1 = 0.5;
|
---|
| 313 | double d2 = 0.6;
|
---|
| 314 | var doubleRange = new DoubleValueRange(new DoubleValue(0.0), new DoubleValue(1.0), new DoubleValue(0.01));
|
---|
| 315 |
|
---|
| 316 | using (var sw = new StreamWriter("normalCrossover-DoubleValue.txt")) {
|
---|
| 317 | for (int i = 0; i < 10000; i++) {
|
---|
| 318 | sw.WriteLine(NormalDoubleValueCrossover.ApplyStatic(random, new DoubleValue(d1), new DoubleValue(d2), doubleRange));
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | int i1 = 180;
|
---|
| 323 | int i2 = 160;
|
---|
| 324 | var intRange = new IntValueRange(new IntValue(100), new IntValue(200), new IntValue(1));
|
---|
| 325 |
|
---|
| 326 | using (var sw = new StreamWriter("normalCrossover-IntValue.txt")) {
|
---|
| 327 | for (int i = 0; i < 10000; i++) {
|
---|
[5522] | 328 | sw.WriteLine(NormalIntValueCrossover.ApplyStatic(random, new IntValue(i1), new IntValue(i2), intRange));
|
---|
[5293] | 329 | }
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[5267] | 333 | private static void TestMemoryConsumption() {
|
---|
| 334 | Queue<TimeSpan> latestExecutionTimes = new Queue<TimeSpan>();
|
---|
| 335 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 336 | ga.PopulationSize.Value = 3;
|
---|
| 337 | ga.MaximumGenerations.Value = 1;
|
---|
| 338 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
[5313] | 339 | throw new NotImplementedException("TODO: set ga properties correctly");
|
---|
[5267] | 340 |
|
---|
| 341 | MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
|
---|
| 342 | metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
|
---|
[6090] | 343 | GeneticAlgorithm metaLevelAlgorithm = GetSequentialMetaGA(metaOptimizationProblem);
|
---|
[5313] | 344 | ParameterConfigurationTree algorithmVc = SetupGAAlgorithm(typeof(GeneticAlgorithm), metaOptimizationProblem);
|
---|
[5267] | 345 | Stopwatch sw = new Stopwatch();
|
---|
| 346 |
|
---|
| 347 | var algs = new List<IAlgorithm>();
|
---|
| 348 | for (int i = 0; i < 10000; i++) {
|
---|
| 349 | sw.Start();
|
---|
| 350 | GeneticAlgorithm clonedGa = (GeneticAlgorithm)ga.Clone();
|
---|
| 351 | clonedGa.Name = "CLONED GA";
|
---|
| 352 | algorithmVc.Parameterize(clonedGa);
|
---|
| 353 | algs.Add(clonedGa);
|
---|
| 354 | sw.Reset();
|
---|
[5277] | 355 | ContentManager.Save((IStorableContent)metaLevelAlgorithm, "alg_" + i + ".hl", true);
|
---|
[5267] | 356 | Console.WriteLine("Cloned alg #{0}", i);
|
---|
| 357 | }
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[5262] | 360 | private static void TestExecutionTimeUpdateInvervalPerformance() {
|
---|
| 361 | TableBuilder tb = new TableBuilder("Tasks", "Interval", "TotalExecutionTime", "AvgExecutionTime", "TimeElapsed", "TotalTimeElapsed", "Speedup", "ExecutionTimeChangedCount", "RealExecutionTimeUpdate(ms)");
|
---|
[5267] | 362 | int tasks = 4;
|
---|
[5262] | 363 | int repetitions = 3;
|
---|
| 364 |
|
---|
| 365 | // warmup
|
---|
| 366 | RepeatExecuteParallel(3, 1, 1, tb);
|
---|
| 367 | tb.AppendRow("--", "--", "--", "--", "--", "--", "--", "--", "--");
|
---|
[5277] | 368 | RepeatExecuteParallel(repetitions, tasks, 1, tb);
|
---|
| 369 | RepeatExecuteParallel(repetitions, tasks, 2.5, tb);
|
---|
[5262] | 370 | RepeatExecuteParallel(repetitions, tasks, 5, tb);
|
---|
| 371 | RepeatExecuteParallel(repetitions, tasks, 10, tb);
|
---|
| 372 | RepeatExecuteParallel(repetitions, tasks, 25, tb);
|
---|
| 373 | RepeatExecuteParallel(repetitions, tasks, 50, tb);
|
---|
[5277] | 374 | RepeatExecuteParallel(repetitions, tasks, 100, tb);
|
---|
| 375 | RepeatExecuteParallel(repetitions, tasks, 250, tb);
|
---|
| 376 | RepeatExecuteParallel(repetitions, tasks, 500, tb);
|
---|
[5262] | 377 | RepeatExecuteParallel(repetitions, tasks, 1000, tb);
|
---|
[5277] | 378 | RepeatExecuteParallel(repetitions, tasks, 2500, tb);
|
---|
[5262] | 379 | RepeatExecuteParallel(repetitions, tasks, 5000, tb);
|
---|
| 380 |
|
---|
| 381 | using (var sw = new StreamWriter("TestExecutionTimeUpdateInvervalPerformance.txt")) {
|
---|
| 382 | sw.Write(tb.ToString());
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | private static GeneticAlgorithm CreateGA() {
|
---|
| 387 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 388 | ga.Problem = new SingleObjectiveTestFunctionProblem() { ProblemSize = new IntValue(250) };
|
---|
| 389 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 390 | ga.SetSeedRandomly.Value = false;
|
---|
| 391 | ga.Seed.Value = 0;
|
---|
| 392 | return ga;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | private static void RepeatExecuteParallel(int repetitions, int tasks, double executionTimeUpdateIntervalMs, TableBuilder tb) {
|
---|
| 396 | for (int i = 0; i < repetitions; i++) {
|
---|
[5277] | 397 | ExecuteParallel(tasks, executionTimeUpdateIntervalMs, tb);
|
---|
| 398 | Console.Clear();
|
---|
[5262] | 399 | Console.WriteLine(tb.ToString());
|
---|
| 400 | }
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | private static void ExecuteParallel(int taskCount, double executionTimeUpdateIntervalMs, TableBuilder tb) {
|
---|
| 404 | Task<TimeSpan>[] tasks = new Task<TimeSpan>[taskCount];
|
---|
| 405 | EngineAlgorithm[] algs = new EngineAlgorithm[taskCount];
|
---|
| 406 | for (int i = 0; i < taskCount; i++) {
|
---|
| 407 | GeneticAlgorithm alg = CreateGA();
|
---|
[5267] | 408 | //((Engine)alg.Engine).ExecutionTimeUpdateInterval = TimeSpan.FromMilliseconds(executionTimeUpdateIntervalMs);
|
---|
[5262] | 409 | algs[i] = alg;
|
---|
| 410 | }
|
---|
| 411 | Console.WriteLine("Creating algs finished.");
|
---|
| 412 |
|
---|
| 413 | for (int i = 0; i < taskCount; i++) {
|
---|
| 414 | tasks[i] = new Task<TimeSpan>((alg) => {
|
---|
| 415 | Console.WriteLine("Task {0} started.", Task.CurrentId);
|
---|
[6024] | 416 | var cancellationTokenSource = new CancellationTokenSource();
|
---|
[5277] | 417 |
|
---|
[5262] | 418 | Stopwatch swx = new Stopwatch();
|
---|
| 419 | swx.Start();
|
---|
| 420 | ((EngineAlgorithm)alg).ExecutionTimeChanged += new EventHandler(Program_ExecutionTimeChanged);
|
---|
[6024] | 421 | ((EngineAlgorithm)alg).StartSync(cancellationTokenSource.Token);
|
---|
[5262] | 422 | ((EngineAlgorithm)alg).ExecutionTimeChanged -= new EventHandler(Program_ExecutionTimeChanged);
|
---|
| 423 | swx.Stop();
|
---|
| 424 | Console.WriteLine("Task {0} finished.", Task.CurrentId);
|
---|
| 425 | return swx.Elapsed;
|
---|
| 426 | }, algs[i]);
|
---|
| 427 | }
|
---|
| 428 | Console.WriteLine("Creating tasks finished.");
|
---|
| 429 | counter = 0;
|
---|
| 430 | Stopwatch sw = new Stopwatch();
|
---|
| 431 | sw.Start();
|
---|
| 432 | foreach (var task in tasks) task.Start();
|
---|
| 433 | Task.WaitAll(tasks);
|
---|
| 434 | sw.Stop();
|
---|
| 435 |
|
---|
| 436 | if (!algs.All(alg => alg.ExecutionState == ExecutionState.Stopped))
|
---|
| 437 | throw new Exception("Not all algs stopped properly");
|
---|
| 438 |
|
---|
| 439 | if (!algs.All(alg => ((DoubleValue)alg.Results["BestQuality"].Value).Value == ((DoubleValue)algs.First().Results["BestQuality"].Value).Value))
|
---|
| 440 | throw new Exception("Not all algs have the same resutls");
|
---|
| 441 |
|
---|
| 442 | if (tb != null) {
|
---|
| 443 | double totalExecutionTimeMilliseconds = algs.Select(x => x.ExecutionTime.TotalMilliseconds).Sum();
|
---|
| 444 | double totalMilliseconds = tasks.Select(t => t.Result.TotalMilliseconds).Sum();
|
---|
| 445 | tb.AppendRow(
|
---|
[5277] | 446 | taskCount.ToString(),
|
---|
| 447 | executionTimeUpdateIntervalMs.ToString(),
|
---|
[5262] | 448 | TimeSpan.FromMilliseconds(totalExecutionTimeMilliseconds).ToString(),
|
---|
[5277] | 449 | TimeSpan.FromMilliseconds(totalExecutionTimeMilliseconds / taskCount).ToString(),
|
---|
[5262] | 450 | sw.Elapsed.ToString(),
|
---|
| 451 | TimeSpan.FromMilliseconds(totalMilliseconds).ToString(),
|
---|
| 452 | (totalMilliseconds / sw.ElapsedMilliseconds).ToString("0.00"),
|
---|
| 453 | counter.ToString(),
|
---|
[5277] | 454 | (totalExecutionTimeMilliseconds / counter).ToString("0.00"));
|
---|
[5262] | 455 | }
|
---|
| 456 | tasks = null;
|
---|
| 457 | algs = null;
|
---|
| 458 | GC.Collect();
|
---|
| 459 | Console.WriteLine("Test finished.");
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | private static int counter = 0;
|
---|
| 463 | static void Program_ExecutionTimeChanged(object sender, EventArgs e) {
|
---|
| 464 | System.Threading.Interlocked.Increment(ref counter);
|
---|
| 465 | }
|
---|
| 466 |
|
---|
[5231] | 467 | private static void TestWaitAny() {
|
---|
| 468 | System.Random rand = new System.Random();
|
---|
| 469 | var tasks = new List<Task<int>>();
|
---|
| 470 | for (int i = 0; i < 10; i++) {
|
---|
| 471 | tasks.Add(Task.Factory.StartNew<int>((x) => {
|
---|
| 472 | int sleep = ((int)x - 10) * -1000;
|
---|
| 473 | Console.WriteLine("sleeping: {0} ms", sleep);
|
---|
| 474 | Thread.Sleep(0); // make context switch
|
---|
| 475 | Thread.Sleep(sleep);
|
---|
| 476 | return (int)x * (int)x;
|
---|
| 477 | }, i));
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | // --> WaitAll processes tasks lazy but in order.
|
---|
| 481 | Task.WaitAll();
|
---|
| 482 | foreach (var task in tasks) {
|
---|
| 483 | Console.WriteLine(task.Result);
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | // -> WaitAny processes any finished task first. but the finished task needs to be removed from list in order to process all tasks
|
---|
| 487 | //for (int i = 0; i < 10; i++) {
|
---|
| 488 | // var tasksArray = tasks.ToArray();
|
---|
| 489 | // var task = tasksArray[Task.WaitAny(tasksArray)];
|
---|
| 490 | // Console.WriteLine(task.Result);
|
---|
| 491 | // tasks.Remove(task);
|
---|
| 492 | //}
|
---|
| 493 |
|
---|
| 494 | Console.WriteLine("Finished TestWaitAny");
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[5207] | 497 | private static void TestAlgorithmPerformanceIssue() {
|
---|
| 498 | Queue<TimeSpan> latestExecutionTimes = new Queue<TimeSpan>();
|
---|
| 499 | int size = 10;
|
---|
[5653] | 500 | var random = new Random.MersenneTwister(0);
|
---|
[5207] | 501 |
|
---|
| 502 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
[6017] | 503 | ga.PopulationSize.Value = 5;
|
---|
| 504 | ga.MaximumGenerations.Value = 5;
|
---|
[5207] | 505 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
[5653] | 506 | ga.Problem = new SingleObjectiveTestFunctionProblem();
|
---|
[5207] | 507 |
|
---|
[6017] | 508 | //MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
|
---|
| 509 | ////metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
|
---|
| 510 | //GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
|
---|
| 511 | //ParameterConfigurationTree algorithmVc = SetupGAAlgorithm(typeof(GeneticAlgorithm), metaOptimizationProblem);
|
---|
| 512 | //algorithmVc.Randomize(random);
|
---|
[5207] | 513 | Stopwatch sw = new Stopwatch();
|
---|
| 514 |
|
---|
[5653] | 515 | var algs = new Queue<IAlgorithm>(); // keep them in memory
|
---|
| 516 | // -> BINGO! -> .NET cannot hold more than 16 algorithms with their ThreadLocal<T> objects efficiently,
|
---|
| 517 | // so if they are kept in memory, runtime at the 17. execution drops significantly
|
---|
| 518 | // because creating ThreadLocal<T> takes all the runtime.
|
---|
| 519 | // when the algs are not stored in a list however this effect does not occur.
|
---|
| 520 |
|
---|
[5927] | 521 |
|
---|
[6090] | 522 | for (int i = 0; i < 10000; i++) {
|
---|
[5207] | 523 | GeneticAlgorithm clonedGa = (GeneticAlgorithm)ga.Clone();
|
---|
| 524 | clonedGa.Name = "CLONED GA";
|
---|
[5653] | 525 | //algorithmVc.Randomize(random);
|
---|
| 526 | //algorithmVc.Parameterize(clonedGa);
|
---|
[5207] | 527 | clonedGa.Prepare(true);
|
---|
[5653] | 528 | sw.Start();
|
---|
| 529 | algs.Enqueue(clonedGa);
|
---|
| 530 |
|
---|
[6024] | 531 | var cancellationTokenSource = new CancellationTokenSource();
|
---|
[6017] | 532 | //if (algs.Count > 24)
|
---|
| 533 | // algs.Dequeue();
|
---|
[6024] | 534 | clonedGa.StartSync(cancellationTokenSource.Token);
|
---|
[5207] | 535 | sw.Stop();
|
---|
| 536 | latestExecutionTimes.Enqueue(sw.Elapsed);
|
---|
| 537 | Console.WriteLine("{0}: {1} ({2})", i, sw.Elapsed, latestExecutionTimes.Count > size ? TimeSpan.FromMilliseconds(latestExecutionTimes.Average(t => t.TotalMilliseconds)).ToString() : "-");
|
---|
| 538 | if (latestExecutionTimes.Count > size) {
|
---|
| 539 | latestExecutionTimes.Dequeue();
|
---|
| 540 | }
|
---|
| 541 | sw.Reset();
|
---|
[6090] | 542 | //Console.ReadLine();
|
---|
[5207] | 543 | }
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | private static void TestTableBuilder() {
|
---|
| 547 | TableBuilder tb = new TableBuilder("column_1", "col2", "col3");
|
---|
| 548 | tb.AppendRow("1", "humpi", "0.23124");
|
---|
| 549 | tb.AppendRow("2", "sf", "0.23124");
|
---|
| 550 | tb.AppendRow("5", "humpi dampti", "0.224");
|
---|
| 551 | tb.AppendRow("10", "egon asdf", "0.4");
|
---|
| 552 | tb.AppendRow("15", "MichaelizcMultiVfds", "0.23124564");
|
---|
| 553 | Console.WriteLine(tb.ToString());
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | private static void TestToInfoString(IValueConfiguration algorithmVc) {
|
---|
[5144] | 557 | var random = new MersenneTwister();
|
---|
[5184] | 558 | Console.WriteLine(algorithmVc.ParameterInfoString);
|
---|
[5144] | 559 | algorithmVc.Randomize(random);
|
---|
[5184] | 560 | Console.WriteLine(algorithmVc.ParameterInfoString);
|
---|
[5144] | 561 | algorithmVc.Randomize(random);
|
---|
[5184] | 562 | Console.WriteLine(algorithmVc.ParameterInfoString);
|
---|
[5144] | 563 | algorithmVc.Randomize(random);
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | private static void TestCombinations() {
|
---|
| 567 | Console.WriteLine("IntRange 3-18:3");
|
---|
| 568 | IntValueRange intRange = new IntValueRange(new IntValue(3), new IntValue(18), new IntValue(3));
|
---|
| 569 | foreach (var val in intRange.GetCombinations()) {
|
---|
| 570 | Console.WriteLine(val);
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | Console.WriteLine("DoubleRange 1.0-2.5:0.5");
|
---|
| 574 | var dblRange = new DoubleValueRange(new DoubleValue(0.7), new DoubleValue(2.8), new DoubleValue(0.5));
|
---|
| 575 | foreach (var val in dblRange.GetCombinations()) {
|
---|
| 576 | Console.WriteLine(val);
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | Console.WriteLine("PercentRange 33%-66%:33%");
|
---|
| 580 | var pctRange = new PercentValueRange(new PercentValue(0.32), new PercentValue(0.98), new PercentValue(0.33));
|
---|
| 581 | foreach (var val in pctRange.GetCombinations()) {
|
---|
| 582 | Console.WriteLine(val);
|
---|
| 583 | }
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | private static void TestCombinations3() {
|
---|
| 587 | Node root = new Node("root");
|
---|
| 588 | root.ChildNodes.Add(new Node("root.n1"));
|
---|
| 589 | root.ChildNodes.Add(new Node("root.n2"));
|
---|
| 590 | Node n3 = new Node("root.n3");
|
---|
| 591 | n3.ChildNodes.Add(new Node("root.n3.n1"));
|
---|
| 592 | n3.ChildNodes.Add(new Node("root.n3.n2"));
|
---|
| 593 | root.ChildNodes.Add(n3);
|
---|
| 594 |
|
---|
| 595 | Console.WriteLine(root.ToString());
|
---|
| 596 | Console.WriteLine("--");
|
---|
| 597 | int cnt = 0;
|
---|
| 598 | var enumerator = new NodeEnumerator(root);
|
---|
| 599 | enumerator.Reset();
|
---|
| 600 | while (enumerator.MoveNext()) {
|
---|
| 601 | Console.WriteLine(enumerator.Current.ToString());
|
---|
| 602 | cnt++;
|
---|
| 603 | }
|
---|
| 604 | Console.WriteLine("count: " + cnt);
|
---|
| 605 | }
|
---|
| 606 |
|
---|
[5184] | 607 | private static void TestEnumeratorCollectionEnumerator() {
|
---|
| 608 | IEnumerable<int> list1 = new int[] { 1, 2, 3, 4, 5 };
|
---|
| 609 | IEnumerable<int> list2 = new int[] { 10, 20, 30 };
|
---|
| 610 | IEnumerable<int> list3 = new int[] { 300, 400, 500 };
|
---|
| 611 |
|
---|
| 612 | var enumerators = new List<IEnumerator>();
|
---|
| 613 |
|
---|
| 614 | EnumeratorCollectionEnumerator<int> enu = new EnumeratorCollectionEnumerator<int>();
|
---|
| 615 | enu.AddEnumerator(list1.GetEnumerator());
|
---|
| 616 | enu.AddEnumerator(list2.GetEnumerator());
|
---|
| 617 | enu.AddEnumerator(list3.GetEnumerator());
|
---|
| 618 | enu.Reset();
|
---|
| 619 | while (enu.MoveNext()) {
|
---|
| 620 | Console.WriteLine(enu.Current);
|
---|
| 621 | }
|
---|
| 622 | }
|
---|
| 623 |
|
---|
[5144] | 624 | private static void TestCombinations4() {
|
---|
| 625 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 626 | ga.Problem = new SingleObjectiveTestFunctionProblem();
|
---|
[5184] | 627 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 628 |
|
---|
[5665] | 629 | ParameterConfigurationTree vc = new ParameterConfigurationTree(ga, new SingleObjectiveTestFunctionProblem());
|
---|
[5144] | 630 |
|
---|
[5184] | 631 | ConfigurePopulationSize(vc, 20, 100, 20);
|
---|
[5144] | 632 | //ConfigureMutationRate(vc, 0.10, 0.60, 0.10);
|
---|
[5361] | 633 | ConfigureMutationOperator(vc);
|
---|
| 634 | //ConfigureSelectionOperator(vc, true);
|
---|
[5144] | 635 |
|
---|
| 636 | int count = 0;
|
---|
| 637 | IEnumerator enumerator = new ParameterCombinationsEnumerator(vc);
|
---|
| 638 | enumerator.Reset();
|
---|
| 639 | while (enumerator.MoveNext()) {
|
---|
| 640 | var current = (IValueConfiguration)enumerator.Current;
|
---|
| 641 | count++;
|
---|
[5184] | 642 | Console.WriteLine(current.ParameterInfoString);
|
---|
[5144] | 643 | }
|
---|
| 644 | Console.WriteLine("You are about to create {0} algorithms.", count);
|
---|
[5207] | 645 |
|
---|
[5184] | 646 | Experiment experiment = vc.GenerateExperiment(ga);
|
---|
| 647 | //foreach (var opt in experiment.Optimizers) {
|
---|
| 648 | // Console.WriteLine(opt.Name);
|
---|
| 649 | //}
|
---|
[5144] | 650 |
|
---|
[5184] | 651 | experiment.Prepare();
|
---|
| 652 | experiment.Start();
|
---|
| 653 |
|
---|
| 654 | while (experiment.ExecutionState != ExecutionState.Stopped) {
|
---|
| 655 | Thread.Sleep(500);
|
---|
[5144] | 656 | }
|
---|
| 657 | }
|
---|
| 658 |
|
---|
[5111] | 659 | private static void TestOperators() {
|
---|
| 660 | IRandom random = new MersenneTwister();
|
---|
| 661 |
|
---|
[6038] | 662 | var doubleRange = new DoubleValueRange(new DoubleValue(0), new DoubleValue(1), new DoubleValue(0.001));
|
---|
[5111] | 663 | using (var sw = new StreamWriter("out-DoubleValue.txt")) {
|
---|
| 664 | for (int i = 0; i < 10000; i++) {
|
---|
[6038] | 665 | var val = new DoubleValue(0.0);
|
---|
[5111] | 666 | NormalDoubleValueManipulator.ApplyStatic(random, val, doubleRange);
|
---|
[5144] | 667 |
|
---|
[5111] | 668 | sw.WriteLine(val);
|
---|
[6038] | 669 | Debug.Assert(val.Value >= 0.0 && val.Value <= 1.0);
|
---|
[5111] | 670 | }
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | var percentRange = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.001));
|
---|
| 674 | using (var sw = new StreamWriter("out-PercentValue.txt")) {
|
---|
| 675 | for (int i = 0; i < 10000; i++) {
|
---|
| 676 | var val = new PercentValue(0.5);
|
---|
| 677 | NormalDoubleValueManipulator.ApplyStatic(random, val, percentRange.AsDoubleValueRange());
|
---|
| 678 | sw.WriteLine(val);
|
---|
| 679 | }
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | var intRange = new IntValueRange(new IntValue(0), new IntValue(100), new IntValue(1));
|
---|
| 683 | using (var sw = new StreamWriter("out-IntValue.txt")) {
|
---|
| 684 | for (int i = 0; i < 10000; i++) {
|
---|
| 685 | var val = new IntValue(50);
|
---|
| 686 | UniformIntValueManipulator.ApplyStatic(random, val, intRange);
|
---|
| 687 | sw.WriteLine(val);
|
---|
| 688 | }
|
---|
| 689 | }
|
---|
| 690 |
|
---|
[6038] | 691 | using (var sw = new StreamWriter("out-DoubleValueCrossed.txt")) {
|
---|
| 692 | for (int i = 0; i < 10000; i++) {
|
---|
| 693 | var val1 = new DoubleValue(0.0);
|
---|
| 694 | var val2 = new DoubleValue(0.5);
|
---|
| 695 | var val3 = NormalDoubleValueCrossover.ApplyStatic(random, val1, val2, doubleRange);
|
---|
| 696 |
|
---|
| 697 | sw.WriteLine(val3);
|
---|
| 698 | Debug.Assert(val3.Value >= 0.0 && val3.Value <= 1.0);
|
---|
| 699 | }
|
---|
| 700 | }
|
---|
| 701 |
|
---|
[5111] | 702 | Console.ReadLine();
|
---|
| 703 | }
|
---|
| 704 |
|
---|
[5110] | 705 | private static void TestTypeDiscovery() {
|
---|
| 706 | var items = ApplicationManager.Manager.GetInstances(typeof(DoubleArray)).ToArray();
|
---|
| 707 |
|
---|
| 708 | foreach (var item in items) {
|
---|
| 709 | Console.WriteLine(item.ToString());
|
---|
| 710 | }
|
---|
| 711 | }
|
---|
| 712 |
|
---|
[5009] | 713 | private static void TestMemoryLeak(GeneticAlgorithm metaLevelAlgorithm) {
|
---|
[5144] | 714 | IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).ParameterConfigurationTree;
|
---|
[5009] | 715 |
|
---|
| 716 | Console.WriteLine("Starting Memory Test...");
|
---|
| 717 | Console.ReadLine();
|
---|
| 718 |
|
---|
[5023] | 719 | var clones = new List<object>();
|
---|
[5009] | 720 | for (int i = 0; i < 1000; i++) {
|
---|
| 721 | var clone = algorithmVc.Clone();
|
---|
[5023] | 722 | clones.Add(clone);
|
---|
[5009] | 723 | }
|
---|
| 724 |
|
---|
| 725 | Console.WriteLine("Finished. Now GC...");
|
---|
| 726 | Console.ReadLine();
|
---|
| 727 |
|
---|
| 728 | GC.Collect();
|
---|
| 729 |
|
---|
| 730 | Console.WriteLine("Finished!");
|
---|
| 731 | Console.ReadLine();
|
---|
| 732 | }
|
---|
| 733 |
|
---|
[6090] | 734 | private static GeneticAlgorithm GetSequentialMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
[5009] | 735 | GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm();
|
---|
| 736 | metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
|
---|
| 737 | metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
|
---|
| 738 |
|
---|
| 739 | metaLevelAlgorithm.Problem = metaOptimizationProblem;
|
---|
| 740 | metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
|
---|
[5207] | 741 |
|
---|
[5303] | 742 | metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Where(x => x.GetType() == typeof(ParameterConfigurationOnePositionsManipulator)).Single();
|
---|
| 743 | //metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Where(x => x.GetType() == typeof(ParameterConfigurationAllPositionsManipulator)).Single();
|
---|
[5207] | 744 |
|
---|
[6090] | 745 | metaLevelAlgorithm.MutationProbability.Value = metaAlgorithmMutationProbability;
|
---|
[5313] | 746 | //metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(LinearRankSelector)).Single();
|
---|
| 747 | //metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(TournamentSelector)).Single();
|
---|
| 748 | //metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(GenderSpecificSelector)).Single();
|
---|
| 749 | //metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(BestSelector)).Single();
|
---|
[6090] | 750 | metaLevelAlgorithm.Selector = ((OptionalConstrainedValueParameter<ISelector>)((IAlgorithm)metaLevelAlgorithm).Parameters["Selector"]).ValidValues.Where(x => x.GetType() == typeof(TournamentSelector)).Single();
|
---|
[5023] | 751 |
|
---|
[6090] | 752 | metaLevelAlgorithm.SetSeedRandomly.Value = false;
|
---|
| 753 | //metaLevelAlgorithm.Seed.Value = new MersenneTwister().Next(0, 1000000);
|
---|
| 754 | metaLevelAlgorithm.Seed.Value = 527875;
|
---|
| 755 |
|
---|
[5009] | 756 | return metaLevelAlgorithm;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
[5207] | 759 | private static GeneticAlgorithm GetParallelMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
[6090] | 760 | GeneticAlgorithm metaLevelAlgorithm = GetSequentialMetaGA(metaOptimizationProblem);
|
---|
[5207] | 761 | metaLevelAlgorithm.Engine = new ParallelEngine.ParallelEngine();
|
---|
[5212] | 762 | return metaLevelAlgorithm;
|
---|
| 763 | }
|
---|
[5207] | 764 |
|
---|
[6017] | 765 | //private static GeneticAlgorithm GetHiveParallelMetaGA(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
| 766 | // GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
|
---|
| 767 | // metaLevelAlgorithm.Engine = new HiveEngine.HiveEngine();
|
---|
[7137] | 768 | // HiveServiceLocator.Instance.ClientFacadePool.UserName = "cneumuel";
|
---|
| 769 | // HiveServiceLocator.Instance.ClientFacadePool.Password = "cneumuel";
|
---|
| 770 | // HiveServiceLocator.Instance.StreamedClientFacadePool.UserName = "cneumuel";
|
---|
| 771 | // HiveServiceLocator.Instance.StreamedClientFacadePool.Password = "cneumuel";
|
---|
[6017] | 772 | // return metaLevelAlgorithm;
|
---|
| 773 | //}
|
---|
[5207] | 774 |
|
---|
[5087] | 775 | private static EvolutionStrategy GetMetaES(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
| 776 | EvolutionStrategy metaLevelAlgorithm = new EvolutionStrategy();
|
---|
| 777 | metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
|
---|
| 778 | metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
|
---|
| 779 |
|
---|
| 780 | metaLevelAlgorithm.Problem = metaOptimizationProblem;
|
---|
| 781 | metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
|
---|
| 782 |
|
---|
[5184] | 783 | metaLevelAlgorithm.Mutator = ((OptionalConstrainedValueParameter<IManipulator>)((IAlgorithm)metaLevelAlgorithm).Parameters["Mutator"]).ValidValues.Last();
|
---|
[5087] | 784 |
|
---|
| 785 | return metaLevelAlgorithm;
|
---|
| 786 | }
|
---|
| 787 |
|
---|
[5313] | 788 | private static ParameterConfigurationTree SetupGAAlgorithm(Type baseLevelAlgorithmType, MetaOptimizationProblem metaOptimizationProblem) {
|
---|
| 789 | metaOptimizationProblem.AlgorithmType.Value = baseLevelAlgorithmType;
|
---|
[6197] | 790 |
|
---|
[6421] | 791 | //metaOptimizationProblem.ProblemType.Value = typeof(SingleObjectiveTestFunctionProblem);
|
---|
[5927] | 792 | //metaOptimizationProblem.Problems.Clear();
|
---|
[5653] | 793 | //metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
|
---|
| 794 | // Evaluator = new GriewankEvaluator(),
|
---|
[5927] | 795 | // ProblemSize = new IntValue(2)
|
---|
| 796 | //});
|
---|
| 797 | //metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
|
---|
| 798 | // Evaluator = new GriewankEvaluator(),
|
---|
[6090] | 799 | // ProblemSize = new IntValue(50)
|
---|
[5653] | 800 | //});
|
---|
| 801 | //metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() {
|
---|
| 802 | // Evaluator = new GriewankEvaluator(),
|
---|
| 803 | // ProblemSize = new IntValue(500)
|
---|
| 804 | //});
|
---|
[5087] | 805 |
|
---|
[6090] | 806 | //metaOptimizationProblem.ProblemType.Value = typeof(SymbolicRegressionSingleObjectiveProblem);
|
---|
| 807 | //metaOptimizationProblem.Maximization.Value = true;
|
---|
| 808 |
|
---|
| 809 | // tower problem
|
---|
[6197] | 810 | //metaOptimizationProblem.ImportAlgorithm((IAlgorithm)ContentManager.Load("Genetic Programming - Symbolic Regression 3.4_scaled.hl"));
|
---|
| 811 | //metaOptimizationProblem.Maximization.Value = true;
|
---|
[6090] | 812 |
|
---|
[6421] | 813 | // tsp
|
---|
[6489] | 814 | //metaOptimizationProblem.ProblemType.Value = typeof(TravelingSalesmanProblem);
|
---|
[6421] | 815 |
|
---|
[5927] | 816 | ParameterConfigurationTree algorithmVc = metaOptimizationProblem.ParameterConfigurationTree;
|
---|
| 817 | ((IntValue)algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "MaximumGenerations").ActualValue.Value).Value = baseAlgorithmMaxGenerations;
|
---|
[6090] | 818 | ((IntValue)algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Single(x => x.Name == "PopulationSize").ActualValue.Value).Value = baseAlgorithmPopulationSize;
|
---|
[6197] | 819 |
|
---|
[6489] | 820 | //ConfigurePopulationSize(algorithmVc, 10, 100, 1);
|
---|
| 821 | //ConfigureMutationRate(algorithmVc, 0.0, 1.0, 0.01);
|
---|
| 822 | //ConfigureMutationOperator(algorithmVc);
|
---|
| 823 | //ConfigureElites(algorithmVc, 0, 5, 1);
|
---|
| 824 | //ConfigureSelectionOperator(algorithmVc, true);
|
---|
[5927] | 825 |
|
---|
[6197] | 826 | //ConfigureSymbolicExpressionGrammar(algorithmVc);
|
---|
[5927] | 827 |
|
---|
[5009] | 828 | return algorithmVc;
|
---|
| 829 | }
|
---|
[4997] | 830 |
|
---|
[5927] | 831 | private static void ConfigureSymbolicExpressionGrammar(ParameterConfigurationTree vc) {
|
---|
| 832 | var pc = vc.ProblemConfiguration.ParameterConfigurations.Single(x => x.Name == "SymbolicExpressionTreeGrammar");
|
---|
| 833 | pc.Optimize = true;
|
---|
| 834 |
|
---|
| 835 | SymbolicExpressionGrammarValueConfiguration symbolicExpressionGrammarVc = null;
|
---|
| 836 | foreach (var valconf in pc.ValueConfigurations) {
|
---|
| 837 | if (valconf.ActualValue.Value.ItemName != "TypeCoherentExpressionGrammar") {
|
---|
| 838 | pc.ValueConfigurations.SetItemCheckedState(valconf, false);
|
---|
| 839 | } else {
|
---|
| 840 | symbolicExpressionGrammarVc = valconf as SymbolicExpressionGrammarValueConfiguration;
|
---|
| 841 | }
|
---|
| 842 | }
|
---|
| 843 |
|
---|
[6017] | 844 | ConfigureSymbolicExpressionGrammarVc(symbolicExpressionGrammarVc);
|
---|
[5927] | 845 | }
|
---|
| 846 |
|
---|
[6017] | 847 | private static void ConfigureSymbolicExpressionGrammarVc(SymbolicExpressionGrammarValueConfiguration symbolicExpressionGrammarVc) {
|
---|
| 848 | symbolicExpressionGrammarVc.Optimize = true;
|
---|
[6090] | 849 |
|
---|
| 850 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Addition", 1.0);
|
---|
| 851 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Subtraction", 1.0);
|
---|
| 852 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Multiplication", 1.0);
|
---|
| 853 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Division", 1.0);
|
---|
| 854 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Average", 1.0);
|
---|
| 855 |
|
---|
| 856 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "IfThenElse", 0.0);
|
---|
| 857 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "GreaterThan", 0.0);
|
---|
| 858 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "LessThan", 0.0);
|
---|
| 859 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "And", 0.0);
|
---|
| 860 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Or", 0.0);
|
---|
| 861 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Not", 0.0);
|
---|
| 862 |
|
---|
| 863 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Sine", 0.0);
|
---|
| 864 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Cosine", 0.0);
|
---|
| 865 | SetInitialFrequencyValue(symbolicExpressionGrammarVc, "Tangent", 0.0);
|
---|
| 866 |
|
---|
| 867 | OptimizeInitialFrequency(symbolicExpressionGrammarVc, "Logarithm", "InitialFrequency", 0.0, 5.0, 0.01);
|
---|
| 868 | OptimizeInitialFrequency(symbolicExpressionGrammarVc, "Power", "InitialFrequency", 0.0, 5.0, 0.01);
|
---|
| 869 | OptimizeInitialFrequency(symbolicExpressionGrammarVc, "Root", "InitialFrequency", 0.0, 5.0, 0.01);
|
---|
| 870 |
|
---|
| 871 | OptimizeInitialFrequency(symbolicExpressionGrammarVc, "Variable", "WeightSigma", 0.01, 10.0, 0.01);
|
---|
| 872 | OptimizeInitialFrequency(symbolicExpressionGrammarVc, "Variable", "WeightManipulatorSigma", 0.01, 10.0, 0.01);
|
---|
| 873 | OptimizeInitialFrequency(symbolicExpressionGrammarVc, "Variable", "MultiplicativeWeightManipulatorSigma", 0.01, 10.0, 0.01);
|
---|
| 874 |
|
---|
| 875 | OptimizeInitialFrequency(symbolicExpressionGrammarVc, "Constant", "ManipulatorSigma", 0.01, 10.0, 0.01);
|
---|
| 876 | OptimizeInitialFrequency(symbolicExpressionGrammarVc, "Constant", "MultiplicativeManipulatorSigma", 0.01, 10.0, 0.01);
|
---|
[6017] | 877 | }
|
---|
| 878 |
|
---|
[6090] | 879 | private static void SetInitialFrequencyValue(SymbolicExpressionGrammarValueConfiguration symbolicExpressionGrammarVc, string symbolName, double value) {
|
---|
| 880 | ((Symbol)symbolicExpressionGrammarVc.ParameterConfigurations.Single(x => x.Name == symbolName).ActualValue.Value).InitialFrequency = value;
|
---|
| 881 | }
|
---|
| 882 |
|
---|
| 883 | private static void OptimizeInitialFrequency(SymbolicExpressionGrammarValueConfiguration symbolicExpressionGrammarVc, string symbolName, string parameterName, double lower, double upper, double step) {
|
---|
| 884 | var pc = symbolicExpressionGrammarVc.ParameterConfigurations.Single(x => x.Name == symbolName);
|
---|
| 885 | pc.Optimize = true;
|
---|
| 886 | var vc = (SymbolValueConfiguration)pc.ValueConfigurations.Single();
|
---|
| 887 | var parameterPc = vc.ParameterConfigurations.Single(x => x.Name == parameterName);
|
---|
| 888 | parameterPc.Optimize = true;
|
---|
| 889 | parameterPc.ValueConfigurations.Clear();
|
---|
| 890 | var rvc = new RangeValueConfiguration(new DoubleValue(5.0), typeof(DoubleValue));
|
---|
| 891 | rvc.Optimize = true;
|
---|
| 892 | ((DoubleValueRange)rvc.RangeConstraint).LowerBound.Value = lower;
|
---|
| 893 | ((DoubleValueRange)rvc.RangeConstraint).UpperBound.Value = upper;
|
---|
| 894 | ((DoubleValueRange)rvc.RangeConstraint).StepSize.Value = step;
|
---|
| 895 | parameterPc.ValueConfigurations.Add(rvc);
|
---|
| 896 | }
|
---|
| 897 |
|
---|
[5927] | 898 | private static void TestConfiguration(ParameterConfigurationTree algorithmVc, Type baseLevelAlgorithmType, IProblem problem) {
|
---|
[5023] | 899 | IRandom rand = new FastRandom(0);
|
---|
[5927] | 900 | var baseLevelAlgorithm = (GeneticAlgorithm)MetaOptimizationUtil.CreateParameterizedAlgorithmInstance(algorithmVc, baseLevelAlgorithmType, problem);
|
---|
| 901 |
|
---|
[4997] | 902 | // set random values
|
---|
| 903 | for (int i = 0; i < 10; i++) {
|
---|
[5653] | 904 | var clonedVc = (ParameterConfigurationTree)algorithmVc.Clone();
|
---|
[5111] | 905 | GeneticAlgorithm newAlg = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
|
---|
[5009] | 906 | clonedVc.Randomize(rand);
|
---|
[5111] | 907 | clonedVc.Parameterize(newAlg);
|
---|
[5009] | 908 | Console.WriteLine(string.Format("PopSize: original: {0}, randomized: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
|
---|
[4997] | 909 | Console.WriteLine(string.Format("MutRate: original: {0}, randomized: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
|
---|
[5009] | 910 | Console.WriteLine(string.Format("MutOp: original: {0}, randomized: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
|
---|
[5023] | 911 | Console.WriteLine(string.Format("SelOp: original: {0}, randomized: {1}", baseLevelAlgorithm.Selector, newAlg.Selector));
|
---|
[5111] | 912 | //Console.WriteLine(string.Format("GrSi: original: {0}, randomized: {1}", "?", ((TournamentSelector)newAlg.Selector).GroupSizeParameter.Value));
|
---|
[5023] | 913 | Console.WriteLine("---");
|
---|
[4997] | 914 | }
|
---|
| 915 |
|
---|
[5023] | 916 | Console.WriteLine("=======================");
|
---|
| 917 | algorithmVc.Randomize(rand);
|
---|
| 918 | algorithmVc.Parameterize(baseLevelAlgorithm);
|
---|
[4997] | 919 | // mutate
|
---|
| 920 | for (int i = 0; i < 10; i++) {
|
---|
[5653] | 921 | var clonedVc = (ParameterConfigurationTree)algorithmVc.Clone();
|
---|
[5111] | 922 | GeneticAlgorithm newAlg = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
|
---|
[5277] | 923 | ParameterConfigurationManipulator.Apply(rand, clonedVc, new UniformIntValueManipulator(), new NormalDoubleValueManipulator());
|
---|
| 924 | clonedVc.Parameterize(newAlg);
|
---|
[5144] | 925 |
|
---|
[5009] | 926 | Console.WriteLine(string.Format("PopSize: original: {0}, mutated: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
|
---|
[4997] | 927 | Console.WriteLine(string.Format("MutRate: original: {0}, mutated: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
|
---|
[5009] | 928 | Console.WriteLine(string.Format("MutOp: original: {0}, mutated: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
|
---|
[5023] | 929 | Console.WriteLine(string.Format("SelOp: original: {0}, mutated: {1}", baseLevelAlgorithm.Selector, newAlg.Selector));
|
---|
[5111] | 930 | //Console.WriteLine(string.Format("GrSi: original: {0}, mutated: {1}", ((TournamentSelector)baseLevelAlgorithm.Selector).GroupSizeParameter.Value, ((TournamentSelector)newAlg.Selector).GroupSizeParameter.Value));
|
---|
[5023] | 931 | Console.WriteLine("---");
|
---|
[4997] | 932 | }
|
---|
| 933 |
|
---|
[5023] | 934 | Console.WriteLine("=======================");
|
---|
[4997] | 935 | // cross
|
---|
| 936 | for (int i = 0; i < 10; i++) {
|
---|
[5653] | 937 | var clonedVc1 = (ParameterConfigurationTree)algorithmVc.Clone();
|
---|
| 938 | var clonedVc2 = (ParameterConfigurationTree)algorithmVc.Clone();
|
---|
[5023] | 939 |
|
---|
[5111] | 940 | GeneticAlgorithm first = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
|
---|
| 941 | GeneticAlgorithm second = (GeneticAlgorithm)baseLevelAlgorithm.Clone();
|
---|
[5023] | 942 |
|
---|
[5009] | 943 | clonedVc1.Randomize(rand);
|
---|
[5023] | 944 | clonedVc1.Parameterize(first);
|
---|
[4997] | 945 |
|
---|
[5023] | 946 | clonedVc2.Randomize(rand);
|
---|
| 947 | clonedVc2.Parameterize(second);
|
---|
[4997] | 948 |
|
---|
| 949 | var popSizeBefore = first.PopulationSize.Value;
|
---|
| 950 | var mutRateBefore = first.MutationProbability.Value;
|
---|
[5009] | 951 | var mutOpBefore = first.Mutator;
|
---|
[5023] | 952 | var selOpBefore = first.Selector;
|
---|
[5111] | 953 | //var groupSizeBefore = ((TournamentSelector)first.Selector).GroupSizeParameter.Value.Value;
|
---|
[4997] | 954 |
|
---|
[5111] | 955 | //clonedVc1.Cross(clonedVc2, rand); todo
|
---|
[5277] | 956 |
|
---|
| 957 | ParameterConfigurationCrossover.Apply(rand, clonedVc1, clonedVc2, new DiscreteIntValueCrossover(), new AverageDoubleValueCrossover());
|
---|
[5023] | 958 | clonedVc1.Parameterize(first);
|
---|
[5009] | 959 |
|
---|
| 960 | Console.WriteLine(string.Format("PopSize: first: {0}, second: {1}, crossed: {2}", popSizeBefore, second.PopulationSize, first.PopulationSize));
|
---|
[4997] | 961 | Console.WriteLine(string.Format("MutRate: first: {0}, second: {1}, crossed: {2}", mutRateBefore, second.MutationProbability, first.MutationProbability));
|
---|
[5023] | 962 | Console.WriteLine(string.Format("MutOp: first: {0}, second: {1}, crossed: {2}", mutOpBefore, second.Mutator, first.Mutator));
|
---|
| 963 | Console.WriteLine(string.Format("SelOp: first: {0}, second: {1}, crossed: {2}", selOpBefore, second.Selector, first.Selector));
|
---|
[5111] | 964 | //Console.WriteLine(string.Format("GrSi: first: {0}, second: {1}, crossed: {2}", groupSizeBefore, ((TournamentSelector)second.Selector).GroupSizeParameter.Value, ((TournamentSelector)first.Selector).GroupSizeParameter.Value));
|
---|
[5023] | 965 | Console.WriteLine("---");
|
---|
[4997] | 966 | }
|
---|
[5023] | 967 | Console.WriteLine("=======================");
|
---|
[5009] | 968 | }
|
---|
[4997] | 969 |
|
---|
[5653] | 970 | private static void ConfigureMutationOperator(ParameterConfigurationTree algorithmVc) {
|
---|
[5665] | 971 | var mutationOperator = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "Mutator").SingleOrDefault();
|
---|
[5009] | 972 | mutationOperator.Optimize = true;
|
---|
| 973 |
|
---|
| 974 | // uncheck multiMutator to avoid Michalewicz issue
|
---|
[5927] | 975 | //var multiMutator = mutationOperator.ValueConfigurations.Where(x => x.ActualValue.Value != null && x.ActualValue.Value.ItemName.StartsWith("Multi")).SingleOrDefault();
|
---|
| 976 | //if (multiMutator != null) {
|
---|
| 977 | // mutationOperator.ValueConfigurations.SetItemCheckedState(multiMutator, false);
|
---|
| 978 | //}
|
---|
[5361] | 979 |
|
---|
[5653] | 980 | // add another normal - don't do this with 'new', because ActualNames will not be set correctly. It should be copied from an existing one
|
---|
| 981 | // mutationOperator.ValueConfigurations.Add(new ParameterizedValueConfiguration(new NormalAllPositionsManipulator(), typeof(NormalAllPositionsManipulator)), true);
|
---|
[4997] | 982 | }
|
---|
| 983 |
|
---|
[5653] | 984 | private static void ConfigureSelectionOperator(ParameterConfigurationTree algorithmVc, bool configureTournamenSize) {
|
---|
[5665] | 985 | var selectionOperatorPc = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "Selector").SingleOrDefault();
|
---|
[5023] | 986 | selectionOperatorPc.Optimize = true;
|
---|
| 987 |
|
---|
| 988 | foreach (var vc in selectionOperatorPc.ValueConfigurations) {
|
---|
| 989 | if (vc.ActualValue.ValueDataType == typeof(TournamentSelector)) {
|
---|
| 990 | selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
|
---|
[5144] | 991 | if (configureTournamenSize) {
|
---|
| 992 | vc.Optimize = true;
|
---|
[5653] | 993 | ConfigureTournamentGroupSize((ParameterizedValueConfiguration)vc);
|
---|
[5144] | 994 | }
|
---|
[5087] | 995 | } else if (vc.ActualValue.ValueDataType == typeof(RandomSelector)) {
|
---|
| 996 | selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
|
---|
[5023] | 997 | } else {
|
---|
[5087] | 998 | selectionOperatorPc.ValueConfigurations.SetItemCheckedState(vc, true);
|
---|
[5023] | 999 | }
|
---|
| 1000 | }
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
[5653] | 1003 | private static void ConfigureTournamentGroupSize(ParameterizedValueConfiguration tournamentVc) {
|
---|
[5023] | 1004 | var groupSizePc = tournamentVc.ParameterConfigurations.Where(x => x.ParameterName == "GroupSize").SingleOrDefault();
|
---|
| 1005 | groupSizePc.Optimize = true;
|
---|
[5653] | 1006 | var groupSizeVc = (RangeValueConfiguration)groupSizePc.ValueConfigurations.First();
|
---|
| 1007 | groupSizeVc.Optimize = true;
|
---|
| 1008 | groupSizeVc.RangeConstraint.LowerBound = new IntValue(0);
|
---|
| 1009 | groupSizeVc.RangeConstraint.UpperBound = new IntValue(10);
|
---|
| 1010 | groupSizeVc.RangeConstraint.StepSize = new IntValue(1);
|
---|
[5023] | 1011 | }
|
---|
| 1012 |
|
---|
[5665] | 1013 | private static void ConfigurePopulationSize(ParameterConfigurationTree algorithmVc, int lower, int upper, int stepsize) {
|
---|
| 1014 | var populationSizePc = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "PopulationSize").SingleOrDefault();
|
---|
[4997] | 1015 | populationSizePc.Optimize = true;
|
---|
[5653] | 1016 | var populationSizeVc = (RangeValueConfiguration)populationSizePc.ValueConfigurations.First();
|
---|
[4997] | 1017 | populationSizeVc.Optimize = true;
|
---|
[5144] | 1018 | populationSizeVc.RangeConstraint.LowerBound = new IntValue(lower);
|
---|
| 1019 | populationSizeVc.RangeConstraint.UpperBound = new IntValue(upper);
|
---|
| 1020 | populationSizeVc.RangeConstraint.StepSize = new IntValue(stepsize);
|
---|
[4997] | 1021 | }
|
---|
| 1022 |
|
---|
[5665] | 1023 | private static void ConfigureMutationRate(ParameterConfigurationTree algorithmVc, double lower, double upper, double stepsize) {
|
---|
| 1024 | var mutationRatePc = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "MutationProbability").SingleOrDefault();
|
---|
[4997] | 1025 | mutationRatePc.Optimize = true;
|
---|
[5653] | 1026 | var mutationRateVc = (RangeValueConfiguration)mutationRatePc.ValueConfigurations.First();
|
---|
[4997] | 1027 | mutationRateVc.Optimize = true;
|
---|
[5144] | 1028 | mutationRateVc.RangeConstraint.LowerBound = new PercentValue(lower);
|
---|
| 1029 | mutationRateVc.RangeConstraint.UpperBound = new PercentValue(upper);
|
---|
| 1030 | mutationRateVc.RangeConstraint.StepSize = new PercentValue(stepsize);
|
---|
[4997] | 1031 | }
|
---|
| 1032 |
|
---|
[5665] | 1033 | private static void ConfigureElites(ParameterConfigurationTree algorithmVc, int from, int to, int stepSize) {
|
---|
| 1034 | var elitesPc = algorithmVc.AlgorithmConfiguration.ParameterConfigurations.Where(x => x.Name == "Elites").SingleOrDefault();
|
---|
[5023] | 1035 | elitesPc.Optimize = true;
|
---|
[5653] | 1036 | var elitesVc = (RangeValueConfiguration)elitesPc.ValueConfigurations.First();
|
---|
[5023] | 1037 | elitesVc.Optimize = true;
|
---|
[5184] | 1038 | elitesVc.RangeConstraint.LowerBound = new IntValue(from);
|
---|
| 1039 | elitesVc.RangeConstraint.UpperBound = new IntValue(to);
|
---|
| 1040 | elitesVc.RangeConstraint.StepSize = new IntValue(stepSize);
|
---|
[5023] | 1041 | }
|
---|
| 1042 |
|
---|
[5087] | 1043 | private static void TestOptimization(EngineAlgorithm metaLevelAlgorithm) {
|
---|
[5023] | 1044 | string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Results");
|
---|
[5087] | 1045 | if (!Directory.Exists(path))
|
---|
[5023] | 1046 | Directory.CreateDirectory(path);
|
---|
[5087] | 1047 | string id = DateTime.Now.ToString("yyyy.MM.dd - HH;mm;ss,ffff");
|
---|
| 1048 | string resultPath = Path.Combine(path, string.Format("{0} - Result.hl", id));
|
---|
| 1049 | string outputPath = Path.Combine(path, string.Format("{0} - Console.txt", id));
|
---|
[5023] | 1050 |
|
---|
[6090] | 1051 | ContentManager.Save((IStorableContent)metaLevelAlgorithm, resultPath + "-before.hl", true);
|
---|
[6197] | 1052 |
|
---|
[5023] | 1053 | using (var sw = new StreamWriter(outputPath)) {
|
---|
[5087] | 1054 | sw.AutoFlush = true;
|
---|
| 1055 |
|
---|
| 1056 | StringBuilder sb1 = new StringBuilder();
|
---|
[6489] | 1057 | sb1.AppendFormat("Meta-PopulationSize: {0}\n", metaAlgorithmPopulationSize);
|
---|
| 1058 | sb1.AppendFormat("Meta-MaxGenerations: {0}\n", metaAlgorithmMaxGenerations);
|
---|
| 1059 | sb1.AppendFormat("Meta-Repetitions : {0}\n", metaProblemRepetitions);
|
---|
| 1060 | sb1.AppendFormat("Meta-MutProb : {0}\n", ((GeneticAlgorithm)metaLevelAlgorithm).MutationProbability.Value);
|
---|
| 1061 | sb1.AppendFormat("Meta-Seed : {0}\n", ((GeneticAlgorithm)metaLevelAlgorithm).Seed.Value);
|
---|
| 1062 | sb1.AppendFormat("Base-MaxGenerations: {0}\n", baseAlgorithmMaxGenerations);
|
---|
[6090] | 1063 |
|
---|
[5207] | 1064 | sb1.AppendLine("Problems:");
|
---|
| 1065 | foreach (var prob in ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).Problems) {
|
---|
| 1066 | sb1.Append(prob.Name);
|
---|
| 1067 | var sotf = prob as SingleObjectiveTestFunctionProblem;
|
---|
| 1068 | if (sotf != null) {
|
---|
| 1069 | sb1.AppendFormat(" {0}", sotf.ProblemSize.Value);
|
---|
| 1070 | }
|
---|
| 1071 | sb1.AppendLine();
|
---|
| 1072 | }
|
---|
[5087] | 1073 | sw.WriteLine(sb1.ToString());
|
---|
| 1074 | Console.WriteLine(sb1.ToString());
|
---|
[5184] | 1075 | metaLevelAlgorithm.Stopped += new EventHandler(metaLevelAlgorithm_Stopped);
|
---|
| 1076 | metaLevelAlgorithm.Paused += new EventHandler(metaLevelAlgorithm_Paused);
|
---|
| 1077 | metaLevelAlgorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(metaLevelAlgorithm_ExceptionOccurred);
|
---|
[5087] | 1078 |
|
---|
[5023] | 1079 | metaLevelAlgorithm.Start();
|
---|
| 1080 | int i = 0;
|
---|
| 1081 | int currentGeneration = -1;
|
---|
| 1082 | do {
|
---|
[5277] | 1083 | Thread.Sleep(1000);
|
---|
[5087] | 1084 | if (metaLevelAlgorithm.Results.ContainsKey("Generations") && ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value != currentGeneration) {
|
---|
[5281] | 1085 | while (metaLevelAlgorithm.Results.Count < 6) Thread.Sleep(1000);
|
---|
[5087] | 1086 | StringBuilder sb = new StringBuilder();
|
---|
| 1087 | sb.AppendLine(DateTime.Now.ToLongTimeString());
|
---|
| 1088 | sb.AppendLine("=================================");
|
---|
[5023] | 1089 |
|
---|
[5111] | 1090 | sb.AppendLine(metaLevelAlgorithm.ExecutionState.ToString());
|
---|
[5281] | 1091 | ResultCollection rsClone = null;
|
---|
| 1092 | while (rsClone == null) {
|
---|
| 1093 | try {
|
---|
| 1094 | rsClone = (ResultCollection)metaLevelAlgorithm.Results.Clone();
|
---|
| 1095 | }
|
---|
| 1096 | catch { }
|
---|
| 1097 | }
|
---|
[5207] | 1098 | foreach (var result in rsClone) {
|
---|
[5087] | 1099 | sb.AppendLine(result.ToString());
|
---|
| 1100 | if (result.Name == "Population") {
|
---|
| 1101 | RunCollection rc = (RunCollection)result.Value;
|
---|
[5212] | 1102 | var orderedRuns = rc.OrderBy(x => x.Results["AverageQualityNormalized"]);
|
---|
[5087] | 1103 |
|
---|
[6090] | 1104 | //TableBuilder tb = new TableBuilder("QNorm", "Qualities"/*, "PoSi"*/ /*,"MutRa"*/ /*,"Eli", "SelOp",*/ /*"MutOp"*//*, "NrSelSubScopes"*/);
|
---|
| 1105 | //foreach (IRun run in orderedRuns) {
|
---|
| 1106 | // //string selector;
|
---|
| 1107 | // //if (run.Parameters["Selector"] is TournamentSelector) {
|
---|
| 1108 | // // selector = string.Format("{0} ({1})", run.Parameters["Selector"].ToString(), ((TournamentSelector)run.Parameters["Selector"]).GroupSizeParameter.Value.ToString());
|
---|
| 1109 | // //} else {
|
---|
| 1110 | // // selector = string.Format("{0}", run.Parameters["Selector"].ToString());
|
---|
| 1111 | // //}
|
---|
| 1112 |
|
---|
| 1113 | // tb.AppendRow(
|
---|
| 1114 | // ((DoubleValue)run.Results["AverageQualityNormalized"]).Value.ToString("#0.0000")
|
---|
| 1115 | // ,((DoubleArray)run.Results["RunsAverageQualities"]).ToString()
|
---|
| 1116 | // //,((IntValue)run.Parameters["PopulationSize"]).Value.ToString()
|
---|
| 1117 | // //,((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.0000")
|
---|
| 1118 | // //,((IntValue)run.Parameters["Elites"]).Value.ToString()
|
---|
| 1119 | // //,Shorten(selector, 20)
|
---|
| 1120 | // //,Shorten(run.Parameters.ContainsKey("Mutator") ? run.Parameters["Mutator"].ToString() : "null", 40)
|
---|
| 1121 | // //,((ISelector)run.Parameters["Selector"]).NumberOfSelectedSubScopesParameter.Value.ToString()
|
---|
| 1122 | // );
|
---|
| 1123 | //}
|
---|
| 1124 | //sb.AppendLine(tb.ToString());
|
---|
| 1125 |
|
---|
| 1126 | var tb = new TableBuilder("QNorm", "Qualities", "StdDevs", "Evaluations", "Parameters");
|
---|
[5087] | 1127 | foreach (IRun run in orderedRuns) {
|
---|
[5207] | 1128 | tb.AppendRow(
|
---|
[5927] | 1129 | ((DoubleValue)run.Results["AverageQualityNormalized"]).Value.ToString("#0.0000")
|
---|
[6090] | 1130 | , ((DoubleArray)run.Results["RunsAverageQualities"]).ToString()
|
---|
| 1131 | , ((DoubleArray)run.Results["RunsQualityStandardDeviations"]).ToString()
|
---|
| 1132 | , ((DoubleArray)run.Results["RunsAverageEvaluatedSolutions"]).ToString()
|
---|
| 1133 | , run.Name
|
---|
[5653] | 1134 | );
|
---|
[5023] | 1135 | }
|
---|
[5207] | 1136 | sb.AppendLine(tb.ToString());
|
---|
[5087] | 1137 | }
|
---|
| 1138 | } // foreach
|
---|
[5207] | 1139 | //Console.Clear();
|
---|
[5087] | 1140 | Console.WriteLine(sb.ToString());
|
---|
| 1141 | sw.WriteLine(sb.ToString());
|
---|
| 1142 | currentGeneration = ((IntValue)metaLevelAlgorithm.Results["Generations"].Value).Value;
|
---|
| 1143 | } // if
|
---|
[5207] | 1144 | //if (i % 30 == 0) GC.Collect();
|
---|
[5087] | 1145 | i++;
|
---|
[5023] | 1146 | } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped);
|
---|
| 1147 | }
|
---|
[5009] | 1148 |
|
---|
[5023] | 1149 | Console.WriteLine();
|
---|
| 1150 | Console.WriteLine("Storing...");
|
---|
| 1151 |
|
---|
[5087] | 1152 | ContentManager.Save((IStorableContent)metaLevelAlgorithm, resultPath, true);
|
---|
[5009] | 1153 | Console.WriteLine("Finished");
|
---|
| 1154 | }
|
---|
| 1155 |
|
---|
[5184] | 1156 | private static void metaLevelAlgorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 1157 | Console.WriteLine("metaLevelAlgorithm_ExceptionOccurred");
|
---|
[5212] | 1158 | Console.WriteLine(e.Value.ToString());
|
---|
| 1159 | if (e.Value.InnerException != null) {
|
---|
| 1160 | Console.WriteLine(e.Value.InnerException.ToString());
|
---|
| 1161 | }
|
---|
[5184] | 1162 | }
|
---|
| 1163 |
|
---|
| 1164 | private static void metaLevelAlgorithm_Paused(object sender, EventArgs e) {
|
---|
| 1165 | Console.WriteLine("metaLevelAlgorithm_Paused");
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 | private static void metaLevelAlgorithm_Stopped(object sender, EventArgs e) {
|
---|
| 1169 | Console.WriteLine("metaLevelAlgorithm_Stopped");
|
---|
| 1170 | }
|
---|
| 1171 |
|
---|
[5087] | 1172 | private static void TestShorten() {
|
---|
| 1173 | int n = 8;
|
---|
| 1174 | Console.WriteLine(Shorten("1", n));
|
---|
| 1175 | Console.WriteLine(Shorten("12", n));
|
---|
| 1176 | Console.WriteLine(Shorten("123", n));
|
---|
| 1177 | Console.WriteLine(Shorten("1234", n));
|
---|
| 1178 | Console.WriteLine(Shorten("12345", n));
|
---|
| 1179 | Console.WriteLine(Shorten("123456", n));
|
---|
| 1180 | Console.WriteLine(Shorten("1234567", n));
|
---|
| 1181 | Console.WriteLine(Shorten("12345678", n));
|
---|
| 1182 | Console.WriteLine(Shorten("123456789", n));
|
---|
| 1183 | Console.WriteLine(Shorten("1234567890", n));
|
---|
| 1184 | Console.WriteLine(Shorten("12345678901", n));
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
| 1187 | private static string Shorten(string s, int n) {
|
---|
| 1188 | string placeholder = "..";
|
---|
| 1189 | if (s.Length <= n) return s;
|
---|
| 1190 | int len = n / 2 - placeholder.Length / 2;
|
---|
| 1191 | string start = s.Substring(0, len);
|
---|
| 1192 | string end = s.Substring(s.Length - len, len);
|
---|
| 1193 | return start + placeholder + end;
|
---|
| 1194 | }
|
---|
| 1195 |
|
---|
[4997] | 1196 | private static void TestIntSampling() {
|
---|
| 1197 | System.Random rand = new System.Random();
|
---|
| 1198 | int lower = 10;
|
---|
| 1199 | int upper = 20;
|
---|
[5207] | 1200 | int stepsize = 1;
|
---|
[4997] | 1201 | for (int i = 0; i < 100; i++) {
|
---|
| 1202 | int val;
|
---|
| 1203 | do {
|
---|
| 1204 | val = rand.Next(lower / stepsize, upper / stepsize + 1) * stepsize;
|
---|
| 1205 | } while (val < lower || val > upper);
|
---|
| 1206 | Console.WriteLine(val);
|
---|
| 1207 | }
|
---|
| 1208 | }
|
---|
| 1209 |
|
---|
| 1210 | private static void TestDoubleSampling() {
|
---|
[6038] | 1211 | var random = new MersenneTwister();
|
---|
| 1212 | double lower = 0;
|
---|
| 1213 | double upper = 1;
|
---|
| 1214 | double stepsize = 0.0000001;
|
---|
| 1215 | DoubleValueRange range = new DoubleValueRange(new DoubleValue(lower), new DoubleValue(upper), new DoubleValue(stepsize));
|
---|
| 1216 |
|
---|
| 1217 | using (var sw = new StreamWriter("out-DoubleValue.txt")) {
|
---|
| 1218 | for (int i = 0; i < 10000; i++) {
|
---|
| 1219 | var val = range.GetRandomValue(random);
|
---|
| 1220 | Debug.Assert(val.Value >= lower && val.Value <= upper);
|
---|
| 1221 | sw.WriteLine(val);
|
---|
| 1222 | }
|
---|
[4997] | 1223 | }
|
---|
| 1224 | }
|
---|
| 1225 |
|
---|
| 1226 | private static IEnumerable<IItem> GetValidValues(IValueParameter valueParameter) {
|
---|
| 1227 | return ApplicationManager.Manager.GetInstances(valueParameter.DataType).Select(x => (IItem)x).OrderBy(x => x.ItemName);
|
---|
| 1228 | }
|
---|
| 1229 | }
|
---|
[5144] | 1230 |
|
---|
| 1231 | public class Node {
|
---|
| 1232 | public string Name { get; set; }
|
---|
| 1233 | public int ActualValue { get; set; }
|
---|
| 1234 | public int[] PossibleValues { get; set; }
|
---|
| 1235 | public List<Node> ChildNodes { get; set; }
|
---|
| 1236 |
|
---|
| 1237 | public Node(string name) {
|
---|
| 1238 | this.Name = name;
|
---|
| 1239 | PossibleValues = new int[] { 1, 2, 3 };
|
---|
| 1240 | ChildNodes = new List<Node>();
|
---|
| 1241 | }
|
---|
| 1242 |
|
---|
| 1243 | public void Init() {
|
---|
| 1244 | this.ActualValue = PossibleValues.First();
|
---|
| 1245 | foreach (var child in ChildNodes) {
|
---|
| 1246 | child.Init();
|
---|
| 1247 | }
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 | public override string ToString() {
|
---|
| 1251 | StringBuilder sb = new StringBuilder();
|
---|
| 1252 | sb.Append(string.Format("{0}:{1}", this.Name, this.ActualValue));
|
---|
| 1253 | if (this.ChildNodes.Count() > 0) {
|
---|
| 1254 | sb.Append(" (");
|
---|
| 1255 | var lst = new List<string>();
|
---|
| 1256 | foreach (Node child in ChildNodes) {
|
---|
| 1257 | lst.Add(child.ToString());
|
---|
| 1258 | }
|
---|
| 1259 | sb.Append(string.Join(", ", lst.ToArray()));
|
---|
| 1260 | sb.Append(")");
|
---|
| 1261 | }
|
---|
| 1262 |
|
---|
| 1263 | return sb.ToString();
|
---|
| 1264 | }
|
---|
| 1265 | }
|
---|
| 1266 |
|
---|
| 1267 | public class NodeEnumerator : IEnumerator<Node> {
|
---|
| 1268 | private Node node;
|
---|
| 1269 | private List<IEnumerator> enumerators;
|
---|
| 1270 |
|
---|
| 1271 | public NodeEnumerator(Node node) {
|
---|
| 1272 | this.node = node;
|
---|
| 1273 | this.enumerators = new List<IEnumerator>();
|
---|
| 1274 | }
|
---|
| 1275 |
|
---|
| 1276 | public Node Current {
|
---|
| 1277 | get { return node; }
|
---|
| 1278 | }
|
---|
| 1279 | object IEnumerator.Current {
|
---|
| 1280 | get { return Current; }
|
---|
| 1281 | }
|
---|
| 1282 |
|
---|
| 1283 | public void Dispose() { }
|
---|
| 1284 |
|
---|
| 1285 | public bool MoveNext() {
|
---|
| 1286 | int i = 0;
|
---|
| 1287 | bool ok = false;
|
---|
[5207] | 1288 | while (!ok && i < enumerators.Count) {
|
---|
| 1289 | if (enumerators[i].MoveNext()) {
|
---|
[5144] | 1290 | ok = true;
|
---|
| 1291 | } else {
|
---|
| 1292 | i++;
|
---|
| 1293 | }
|
---|
| 1294 | }
|
---|
| 1295 |
|
---|
| 1296 | if (ok) {
|
---|
[5207] | 1297 | for (int k = i - 1; k >= 0; k--) {
|
---|
[5144] | 1298 | enumerators[k].Reset();
|
---|
| 1299 | enumerators[k].MoveNext();
|
---|
| 1300 | }
|
---|
| 1301 | } else {
|
---|
| 1302 | return false;
|
---|
| 1303 | }
|
---|
| 1304 |
|
---|
| 1305 | node.ActualValue = (int)enumerators[0].Current;
|
---|
| 1306 | return true;
|
---|
| 1307 | }
|
---|
| 1308 |
|
---|
| 1309 | public void Reset() {
|
---|
| 1310 | enumerators.Clear();
|
---|
| 1311 | enumerators.Add(node.PossibleValues.GetEnumerator());
|
---|
| 1312 | enumerators[0].Reset();
|
---|
| 1313 |
|
---|
| 1314 | foreach (var child in node.ChildNodes) {
|
---|
| 1315 | var enumerator = new NodeEnumerator(child);
|
---|
| 1316 | enumerator.Reset();
|
---|
| 1317 | enumerator.MoveNext();
|
---|
| 1318 | enumerators.Add(enumerator);
|
---|
| 1319 | }
|
---|
| 1320 | }
|
---|
| 1321 | }
|
---|
[6197] | 1322 |
|
---|
| 1323 | class MyParameterizedItem : ParameterizedNamedItem {
|
---|
| 1324 | public MyParameterizedItem() {
|
---|
| 1325 | this.Parameters.Add(new ValueParameter<IntValue>("P1", new IntValue(1)));
|
---|
| 1326 | this.Parameters.Add(new ValueParameter<IntValue>("P2", new IntValue(2)));
|
---|
| 1327 | this.Parameters.Add(new ValueParameter<IntValue>("P3", new IntValue(3)));
|
---|
| 1328 | this.Parameters.Add(new ValueParameter<MyOtherParameterizedItem>("P4", new MyOtherParameterizedItem()));
|
---|
| 1329 | }
|
---|
| 1330 |
|
---|
| 1331 | protected MyParameterizedItem(MyParameterizedItem original, Cloner cloner)
|
---|
| 1332 | : base(original, cloner) {
|
---|
| 1333 | }
|
---|
| 1334 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 1335 | return new MyParameterizedItem(this, cloner);
|
---|
| 1336 | }
|
---|
| 1337 |
|
---|
| 1338 | public override string ToString() {
|
---|
| 1339 | return string.Format("P1: {0}, P2: {1}, P3: {2}, P4: {3}", Parameters["P1"].ActualValue, Parameters["P2"].ActualValue, Parameters["P3"].ActualValue, Parameters["P4"].ActualValue);
|
---|
| 1340 | }
|
---|
| 1341 | }
|
---|
| 1342 |
|
---|
| 1343 | class MyOtherParameterizedItem : ParameterizedNamedItem {
|
---|
| 1344 | public MyOtherParameterizedItem() {
|
---|
| 1345 | this.Parameters.Add(new ValueParameter<IntValue>("PP1", new IntValue(1)));
|
---|
| 1346 | this.Parameters.Add(new ValueParameter<IntValue>("PP2", new IntValue(2)));
|
---|
| 1347 | this.Parameters.Add(new ValueParameter<IntValue>("PP3", new IntValue(3)));
|
---|
| 1348 | }
|
---|
| 1349 |
|
---|
| 1350 | protected MyOtherParameterizedItem(MyOtherParameterizedItem original, Cloner cloner)
|
---|
| 1351 | : base(original, cloner) {
|
---|
| 1352 | }
|
---|
| 1353 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 1354 | return new MyOtherParameterizedItem(this, cloner);
|
---|
| 1355 | }
|
---|
| 1356 |
|
---|
| 1357 | public override string ToString() {
|
---|
| 1358 | return string.Format("PP1: {0}, PP2: {1}, PP3: {2}", Parameters["PP1"].ActualValue, Parameters["PP2"].ActualValue, Parameters["PP3"].ActualValue);
|
---|
| 1359 | }
|
---|
| 1360 | }
|
---|
[4997] | 1361 | }
|
---|