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