1 | using System.Collections.Generic;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.PluginInfrastructure;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Problems.MetaOptimization;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using System;
|
---|
11 | using System.Threading;
|
---|
12 | using HeuristicLab.Random;
|
---|
13 | using HeuristicLab.Optimization;
|
---|
14 |
|
---|
15 | namespace HeuristicLab.MetaOptimization.Test {
|
---|
16 | class Program {
|
---|
17 | private static int metaAlgorithmPopulationSize = 20;
|
---|
18 | private static int metaAlgorithmMaxGenerations = 100;
|
---|
19 |
|
---|
20 | private static int baseAlgorithmPopulationSize = 20;
|
---|
21 | private static int baseAlgorithmMaxGenerations = 30;
|
---|
22 |
|
---|
23 | static void Main(string[] args) {
|
---|
24 | //TestIntSampling();
|
---|
25 | //TestDoubleSampling();
|
---|
26 |
|
---|
27 | GeneticAlgorithm baseLevelAlgorithm = new GeneticAlgorithm();
|
---|
28 | MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
|
---|
29 | GeneticAlgorithm metaLevelAlgorithm = GetMetaAlgorithm(metaOptimizationProblem);
|
---|
30 |
|
---|
31 | IValueConfiguration algorithmVc = SetupAlgorithm(baseLevelAlgorithm, metaOptimizationProblem);
|
---|
32 |
|
---|
33 | Console.WriteLine("Press enter to start");
|
---|
34 | Console.ReadLine();
|
---|
35 | TestConfiguration(algorithmVc, baseLevelAlgorithm);
|
---|
36 |
|
---|
37 | Console.WriteLine("Press enter to start");
|
---|
38 | Console.ReadLine();
|
---|
39 | TestOptimization(metaLevelAlgorithm);
|
---|
40 |
|
---|
41 | //TestMemoryLeak(metaLevelAlgorithm);
|
---|
42 |
|
---|
43 | Console.ReadLine();
|
---|
44 | }
|
---|
45 |
|
---|
46 | private static void TestMemoryLeak(GeneticAlgorithm metaLevelAlgorithm) {
|
---|
47 | IValueConfiguration algorithmVc = ((MetaOptimizationProblem)metaLevelAlgorithm.Problem).AlgorithmParameterConfiguration;
|
---|
48 |
|
---|
49 | Console.WriteLine("Starting Memory Test...");
|
---|
50 | Console.ReadLine();
|
---|
51 |
|
---|
52 | for (int i = 0; i < 1000; i++) {
|
---|
53 | var clone = algorithmVc.Clone();
|
---|
54 | }
|
---|
55 |
|
---|
56 | Console.WriteLine("Finished. Now GC...");
|
---|
57 | Console.ReadLine();
|
---|
58 |
|
---|
59 | GC.Collect();
|
---|
60 |
|
---|
61 | Console.WriteLine("Finished!");
|
---|
62 | Console.ReadLine();
|
---|
63 | }
|
---|
64 |
|
---|
65 | private static GeneticAlgorithm GetMetaAlgorithm(MetaOptimizationProblem metaOptimizationProblem) {
|
---|
66 | GeneticAlgorithm metaLevelAlgorithm = new GeneticAlgorithm();
|
---|
67 | metaLevelAlgorithm.PopulationSize.Value = metaAlgorithmPopulationSize;
|
---|
68 | metaLevelAlgorithm.MaximumGenerations.Value = metaAlgorithmMaxGenerations;
|
---|
69 |
|
---|
70 | metaLevelAlgorithm.Problem = metaOptimizationProblem;
|
---|
71 | metaLevelAlgorithm.Engine = new SequentialEngine.SequentialEngine();
|
---|
72 | return metaLevelAlgorithm;
|
---|
73 | }
|
---|
74 |
|
---|
75 | private static IValueConfiguration SetupAlgorithm(GeneticAlgorithm baseLevelAlgorithm, MetaOptimizationProblem metaOptimizationProblem) {
|
---|
76 | baseLevelAlgorithm.Problem = new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem() { ProblemSize = new IntValue(2000) };
|
---|
77 | baseLevelAlgorithm.PopulationSize.Value = baseAlgorithmPopulationSize;
|
---|
78 | baseLevelAlgorithm.MaximumGenerations.Value = baseAlgorithmMaxGenerations;
|
---|
79 |
|
---|
80 | metaOptimizationProblem.Algorithm = baseLevelAlgorithm;
|
---|
81 | IValueConfiguration algorithmVc = metaOptimizationProblem.AlgorithmParameterConfiguration;
|
---|
82 | metaOptimizationProblem.Problems.Add(new HeuristicLab.Problems.TestFunctions.SingleObjectiveTestFunctionProblem());
|
---|
83 |
|
---|
84 | ConfigurePopulationSize(algorithmVc);
|
---|
85 | ConfigureMutationRate(algorithmVc);
|
---|
86 | ConfigureMutationOperator(algorithmVc);
|
---|
87 | return algorithmVc;
|
---|
88 | }
|
---|
89 |
|
---|
90 | private static void TestConfiguration(IValueConfiguration algorithmVc, GeneticAlgorithm baseLevelAlgorithm) {
|
---|
91 | IRandom rand = new MersenneTwister();
|
---|
92 | // set random values
|
---|
93 | for (int i = 0; i < 10; i++) {
|
---|
94 | IValueConfiguration clonedVc = (IValueConfiguration)algorithmVc.Clone();
|
---|
95 | clonedVc.Randomize(rand);
|
---|
96 | clonedVc.Parameterize((GeneticAlgorithm)clonedVc.ActualValue.Value);
|
---|
97 | GeneticAlgorithm newAlg = (GeneticAlgorithm)clonedVc.ActualValue.Value;
|
---|
98 | Console.WriteLine(string.Format("PopSize: original: {0}, randomized: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
|
---|
99 | Console.WriteLine(string.Format("MutRate: original: {0}, randomized: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
|
---|
100 | Console.WriteLine(string.Format("MutOp: original: {0}, randomized: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
|
---|
101 | }
|
---|
102 |
|
---|
103 | // mutate
|
---|
104 | for (int i = 0; i < 10; i++) {
|
---|
105 | IValueConfiguration clonedVc = (IValueConfiguration)algorithmVc.Clone();
|
---|
106 | clonedVc.Mutate(rand);
|
---|
107 | clonedVc.Parameterize((GeneticAlgorithm)clonedVc.ActualValue.Value);
|
---|
108 | GeneticAlgorithm newAlg = (GeneticAlgorithm)clonedVc.ActualValue.Value;
|
---|
109 | Console.WriteLine(string.Format("PopSize: original: {0}, mutated: {1}", baseLevelAlgorithm.PopulationSize, newAlg.PopulationSize));
|
---|
110 | Console.WriteLine(string.Format("MutRate: original: {0}, mutated: {1}", baseLevelAlgorithm.MutationProbability, newAlg.MutationProbability));
|
---|
111 | Console.WriteLine(string.Format("MutOp: original: {0}, mutated: {1}", baseLevelAlgorithm.Mutator, newAlg.Mutator));
|
---|
112 | }
|
---|
113 |
|
---|
114 | // cross
|
---|
115 | for (int i = 0; i < 10; i++) {
|
---|
116 | IValueConfiguration clonedVc1 = (IValueConfiguration)algorithmVc.Clone();
|
---|
117 | clonedVc1.Randomize(rand);
|
---|
118 | clonedVc1.Parameterize(baseLevelAlgorithm);
|
---|
119 |
|
---|
120 | IValueConfiguration clonedVc2 = (IValueConfiguration)algorithmVc.Clone();
|
---|
121 | GeneticAlgorithm first = (GeneticAlgorithm)clonedVc1.ActualValue.Value;
|
---|
122 | GeneticAlgorithm second = (GeneticAlgorithm)clonedVc2.ActualValue.Value;
|
---|
123 |
|
---|
124 | var popSizeBefore = first.PopulationSize.Value;
|
---|
125 | var mutRateBefore = first.MutationProbability.Value;
|
---|
126 | var mutOpBefore = first.Mutator;
|
---|
127 |
|
---|
128 | clonedVc1.Cross(clonedVc2, rand);
|
---|
129 | clonedVc1.Parameterize((GeneticAlgorithm)clonedVc2.ActualValue.Value);
|
---|
130 |
|
---|
131 | Console.WriteLine(string.Format("PopSize: first: {0}, second: {1}, crossed: {2}", popSizeBefore, second.PopulationSize, first.PopulationSize));
|
---|
132 | Console.WriteLine(string.Format("MutRate: first: {0}, second: {1}, crossed: {2}", mutRateBefore, second.MutationProbability, first.MutationProbability));
|
---|
133 | Console.WriteLine(string.Format("MutRate: first: {0}, second: {1}, crossed: {2}", mutOpBefore, second.Mutator, first.Mutator));
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private static void ConfigureMutationOperator(IValueConfiguration algorithmVc) {
|
---|
138 | var mutationOperator = algorithmVc.ParameterConfigurations.Where(x => x.Name == "Mutator").SingleOrDefault();
|
---|
139 | mutationOperator.Optimize = true;
|
---|
140 |
|
---|
141 | // uncheck multiMutator to avoid Michalewicz issue
|
---|
142 | var multiMutator = mutationOperator.ValueConfigurations.Where(x => x.ActualValue.Value.ItemName.StartsWith("Multi")).SingleOrDefault();
|
---|
143 | if (multiMutator != null) {
|
---|
144 | mutationOperator.ValueConfigurations.SetItemCheckedState(multiMutator, false);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | private static void ConfigurePopulationSize(IValueConfiguration algorithmVc) {
|
---|
149 | var populationSizePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "PopulationSize").SingleOrDefault();
|
---|
150 | populationSizePc.Optimize = true;
|
---|
151 | var populationSizeVc = populationSizePc.ValueConfigurations.First();
|
---|
152 | populationSizeVc.Optimize = true;
|
---|
153 | populationSizeVc.RangeConstraint.LowerBound = new IntValue(0);
|
---|
154 | populationSizeVc.RangeConstraint.UpperBound = new IntValue(100);
|
---|
155 | populationSizeVc.RangeConstraint.StepSize = new IntValue(1);
|
---|
156 | }
|
---|
157 |
|
---|
158 | private static void ConfigureMutationRate(IValueConfiguration algorithmVc) {
|
---|
159 | var mutationRatePc = algorithmVc.ParameterConfigurations.Where(x => x.Name == "MutationProbability").SingleOrDefault();
|
---|
160 | mutationRatePc.Optimize = true;
|
---|
161 | var mutationRateVc = mutationRatePc.ValueConfigurations.First();
|
---|
162 | mutationRateVc.Optimize = true;
|
---|
163 | mutationRateVc.RangeConstraint.LowerBound = new PercentValue(0.0);
|
---|
164 | mutationRateVc.RangeConstraint.UpperBound = new PercentValue(1.0);
|
---|
165 | mutationRateVc.RangeConstraint.StepSize = new PercentValue(0.01);
|
---|
166 | }
|
---|
167 |
|
---|
168 | private static void TestOptimization(GeneticAlgorithm metaLevelAlgorithm) {
|
---|
169 | metaLevelAlgorithm.Start();
|
---|
170 | do {
|
---|
171 | Thread.Sleep(1000);
|
---|
172 | Console.Clear();
|
---|
173 | try {
|
---|
174 | foreach (var result in metaLevelAlgorithm.Results) {
|
---|
175 | Console.WriteLine(result.ToString());
|
---|
176 | if (result.Name == "Population") {
|
---|
177 | RunCollection rc = (RunCollection)result.Value;
|
---|
178 | var orderedRuns = rc.OrderBy(x => x.Results["BestQuality"]);
|
---|
179 | foreach (IRun run in orderedRuns) {
|
---|
180 | Console.WriteLine("Q: {0} PoSi: {1} MuRa: {2} MuOp: {3}",
|
---|
181 | ((DoubleValue)run.Results["BestQuality"]).Value.ToString("0.00").PadLeft(4, ' '),
|
---|
182 | ((IntValue)run.Parameters["PopulationSize"]).Value.ToString().PadLeft(3, ' '),
|
---|
183 | ((DoubleValue)run.Parameters["MutationProbability"]).Value.ToString("0.00").PadRight(3, ' '),
|
---|
184 | run.Parameters["Mutator"]);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | catch { }
|
---|
190 | } while (metaLevelAlgorithm.ExecutionState != ExecutionState.Stopped);
|
---|
191 |
|
---|
192 | Console.WriteLine("Finished");
|
---|
193 | }
|
---|
194 |
|
---|
195 | private static void TestIntSampling() {
|
---|
196 | System.Random rand = new System.Random();
|
---|
197 | int lower = 10;
|
---|
198 | int upper = 20;
|
---|
199 | int stepsize = 7;
|
---|
200 | for (int i = 0; i < 100; i++) {
|
---|
201 | int val;
|
---|
202 | do {
|
---|
203 | val = rand.Next(lower / stepsize, upper / stepsize + 1) * stepsize;
|
---|
204 | } while (val < lower || val > upper);
|
---|
205 | Console.WriteLine(val);
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | private static void TestDoubleSampling() {
|
---|
210 | System.Random rand = new System.Random();
|
---|
211 | double lower = 2;
|
---|
212 | double upper = 3;
|
---|
213 | double stepsize = 0.6;
|
---|
214 | for (int i = 0; i < 100; i++) {
|
---|
215 | double val;
|
---|
216 | do {
|
---|
217 | val = Math.Round((rand.NextDouble() * (upper - lower) + lower) / stepsize, 0) * stepsize;
|
---|
218 | } while (val < lower || val > upper);
|
---|
219 | Console.WriteLine(val);
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | private static IEnumerable<IItem> GetValidValues(IValueParameter valueParameter) {
|
---|
224 | return ApplicationManager.Manager.GetInstances(valueParameter.DataType).Select(x => (IItem)x).OrderBy(x => x.ItemName);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|