1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.IO;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
27 | using HeuristicLab.Algorithms.EvolutionStrategy;
|
---|
28 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
29 | using HeuristicLab.Algorithms.LocalSearch;
|
---|
30 | using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
|
---|
31 | using HeuristicLab.Algorithms.ParticleSwarmOptimization;
|
---|
32 | using HeuristicLab.Algorithms.RAPGA;
|
---|
33 | using HeuristicLab.Algorithms.ScatterSearch;
|
---|
34 | using HeuristicLab.Algorithms.SimulatedAnnealing;
|
---|
35 | using HeuristicLab.Algorithms.TabuSearch;
|
---|
36 | using HeuristicLab.Algorithms.VariableNeighborhoodSearch;
|
---|
37 | using HeuristicLab.Data;
|
---|
38 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
39 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
40 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
41 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
42 | using HeuristicLab.Encodings.ScheduleEncoding.JobSequenceMatrix;
|
---|
43 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
44 | using HeuristicLab.Optimization;
|
---|
45 | using HeuristicLab.Optimization.Operators;
|
---|
46 | using HeuristicLab.Persistence.Default.Xml;
|
---|
47 | using HeuristicLab.Problems.ArtificialAnt;
|
---|
48 | using HeuristicLab.Problems.DataAnalysis;
|
---|
49 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
50 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
|
---|
51 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
52 | using HeuristicLab.Problems.Instances;
|
---|
53 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
54 | using HeuristicLab.Problems.Instances.TSPLIB;
|
---|
55 | using HeuristicLab.Problems.Instances.VehicleRouting;
|
---|
56 | using HeuristicLab.Problems.Knapsack;
|
---|
57 | using HeuristicLab.Problems.Scheduling;
|
---|
58 | using HeuristicLab.Problems.TestFunctions;
|
---|
59 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
60 | using HeuristicLab.Problems.VehicleRouting;
|
---|
61 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
62 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
63 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
64 | using HeuristicLab.Selection;
|
---|
65 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
66 | using StdDevStrategyVectorCreator = HeuristicLab.Encodings.RealVectorEncoding.StdDevStrategyVectorCreator;
|
---|
67 | using StdDevStrategyVectorCrossover = HeuristicLab.Encodings.RealVectorEncoding.StdDevStrategyVectorCrossover;
|
---|
68 | using StdDevStrategyVectorManipulator = HeuristicLab.Encodings.RealVectorEncoding.StdDevStrategyVectorManipulator;
|
---|
69 |
|
---|
70 |
|
---|
71 | namespace HeuristicLab.Tests {
|
---|
72 | [TestClass]
|
---|
73 | public class SamplesTest {
|
---|
74 | private const string samplesDirectory = @"Samples\";
|
---|
75 |
|
---|
76 | [ClassInitialize]
|
---|
77 | public static void MyClassInitialize(TestContext testContext) {
|
---|
78 | if (!Directory.Exists(samplesDirectory))
|
---|
79 | Directory.CreateDirectory(samplesDirectory);
|
---|
80 | }
|
---|
81 |
|
---|
82 | #region GA
|
---|
83 | #region TSP
|
---|
84 | [TestMethod]
|
---|
85 | [TestCategory("Samples.Create")]
|
---|
86 | [TestProperty("Time", "medium")]
|
---|
87 | public void CreateGaTspSampleTest() {
|
---|
88 | var ga = CreateGaTspSample();
|
---|
89 | XmlGenerator.Serialize(ga, @"Samples\GA_TSP.hl");
|
---|
90 | }
|
---|
91 | [TestMethod]
|
---|
92 | [TestCategory("Samples.Execute")]
|
---|
93 | [TestProperty("Time", "long")]
|
---|
94 | public void RunGaTspSampleTest() {
|
---|
95 | var ga = CreateGaTspSample();
|
---|
96 | ga.SetSeedRandomly.Value = false;
|
---|
97 | RunAlgorithm(ga);
|
---|
98 | Assert.AreEqual(12332, GetDoubleResult(ga, "BestQuality"));
|
---|
99 | Assert.AreEqual(13123.2, GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
100 | Assert.AreEqual(14538, GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
101 | Assert.AreEqual(99100, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
102 | }
|
---|
103 |
|
---|
104 | private GeneticAlgorithm CreateGaTspSample() {
|
---|
105 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
106 | #region Problem Configuration
|
---|
107 | var provider = new TSPLIBTSPInstanceProvider();
|
---|
108 | var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single();
|
---|
109 | TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
|
---|
110 | tspProblem.Load(provider.LoadData(instance));
|
---|
111 | tspProblem.UseDistanceMatrix.Value = true;
|
---|
112 | #endregion
|
---|
113 | #region Algorithm Configuration
|
---|
114 | ga.Name = "Genetic Algorithm - TSP";
|
---|
115 | ga.Description = "A genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
|
---|
116 | ga.Problem = tspProblem;
|
---|
117 | ConfigureGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator>(
|
---|
118 | ga, 100, 1, 1000, 0.05);
|
---|
119 | #endregion
|
---|
120 | return ga;
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 | #region VRP
|
---|
124 | [TestMethod]
|
---|
125 | [TestCategory("Samples.Create")]
|
---|
126 | [TestProperty("Time", "medium")]
|
---|
127 | public void CreateGaVrpSampleTest() {
|
---|
128 | var ga = CreateGaVrpSample();
|
---|
129 | XmlGenerator.Serialize(ga, @"Samples\GA_VRP.hl");
|
---|
130 | }
|
---|
131 |
|
---|
132 | [TestMethod]
|
---|
133 | [TestCategory("Samples.Execute")]
|
---|
134 | [TestProperty("Time", "long")]
|
---|
135 | public void RunGaVrpSampleTest() {
|
---|
136 | var ga = CreateGaVrpSample();
|
---|
137 | ga.SetSeedRandomly.Value = false;
|
---|
138 | RunAlgorithm(ga);
|
---|
139 | Assert.AreEqual(1828.9368669428338, GetDoubleResult(ga, "BestQuality"));
|
---|
140 | Assert.AreEqual(1830.1444308908331, GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
141 | Assert.AreEqual(1871.7128510304112, GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
142 | Assert.AreEqual(99100, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
143 | }
|
---|
144 |
|
---|
145 | private GeneticAlgorithm CreateGaVrpSample() {
|
---|
146 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
147 | #region Problem Configuration
|
---|
148 | VehicleRoutingProblem vrpProblem = new VehicleRoutingProblem();
|
---|
149 |
|
---|
150 | SolomonFormatInstanceProvider instanceProvider = new SolomonInstanceProvider();
|
---|
151 | CVRPTWData data = instanceProvider.Import(@"Test Resources\C101.txt", @"Test Resources\C101.opt.txt") as CVRPTWData;
|
---|
152 | vrpProblem.Load(data);
|
---|
153 | vrpProblem.Name = "C101 VRP (imported from Solomon)";
|
---|
154 | vrpProblem.Description = "Represents a Vehicle Routing Problem.";
|
---|
155 | CVRPTWProblemInstance instance = vrpProblem.ProblemInstance as CVRPTWProblemInstance;
|
---|
156 | instance.DistanceFactor.Value = 1;
|
---|
157 | instance.FleetUsageFactor.Value = 100;
|
---|
158 | instance.OverloadPenalty.Value = 100;
|
---|
159 | instance.TardinessPenalty.Value = 100;
|
---|
160 | instance.TimeFactor.Value = 0;
|
---|
161 | vrpProblem.MaximizationParameter.Value.Value = false;
|
---|
162 | instance.UseDistanceMatrix.Value = true;
|
---|
163 | instance.Vehicles.Value = 25;
|
---|
164 | #endregion
|
---|
165 | #region Algorithm Configuration
|
---|
166 | ga.Name = "Genetic Algorithm - VRP";
|
---|
167 | ga.Description = "A genetic algorithm which solves the \"C101\" vehicle routing problem (imported from Solomon)";
|
---|
168 | ga.Problem = vrpProblem;
|
---|
169 | ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiVRPSolutionCrossover, MultiVRPSolutionManipulator>(
|
---|
170 | ga, 100, 1, 1000, 0.05, 3);
|
---|
171 |
|
---|
172 | var xOver = (MultiVRPSolutionCrossover)ga.Crossover;
|
---|
173 | foreach (var op in xOver.Operators) {
|
---|
174 | xOver.Operators.SetItemCheckedState(op, false);
|
---|
175 | }
|
---|
176 | xOver.Operators.SetItemCheckedState(xOver.Operators
|
---|
177 | .OfType<PotvinRouteBasedCrossover>()
|
---|
178 | .Single(), true);
|
---|
179 | xOver.Operators.SetItemCheckedState(xOver.Operators
|
---|
180 | .OfType<PotvinSequenceBasedCrossover>()
|
---|
181 | .Single(), true);
|
---|
182 |
|
---|
183 | var manipulator = (MultiVRPSolutionManipulator)ga.Mutator;
|
---|
184 | foreach (var op in manipulator.Operators) {
|
---|
185 | manipulator.Operators.SetItemCheckedState(op, false);
|
---|
186 | }
|
---|
187 | manipulator.Operators.SetItemCheckedState(manipulator.Operators
|
---|
188 | .OfType<PotvinOneLevelExchangeMainpulator>()
|
---|
189 | .Single(), true);
|
---|
190 | manipulator.Operators.SetItemCheckedState(manipulator.Operators
|
---|
191 | .OfType<PotvinTwoLevelExchangeManipulator>()
|
---|
192 | .Single(), true);
|
---|
193 | #endregion
|
---|
194 | return ga;
|
---|
195 | }
|
---|
196 | #endregion
|
---|
197 | #region ArtificialAnt
|
---|
198 | [TestMethod]
|
---|
199 | [TestCategory("Samples.Create")]
|
---|
200 | [TestProperty("Time", "medium")]
|
---|
201 | public void CreateGpArtificialAntSampleTest() {
|
---|
202 | var ga = CreateGpArtificialAntSample();
|
---|
203 | XmlGenerator.Serialize(ga, @"Samples\SGP_SantaFe.hl");
|
---|
204 | }
|
---|
205 |
|
---|
206 | [TestMethod]
|
---|
207 | [TestCategory("Samples.Execute")]
|
---|
208 | [TestProperty("Time", "long")]
|
---|
209 | public void RunGpArtificialAntSampleTest() {
|
---|
210 | var ga = CreateGpArtificialAntSample();
|
---|
211 | ga.SetSeedRandomly.Value = false;
|
---|
212 | RunAlgorithm(ga);
|
---|
213 | Assert.AreEqual(81, GetDoubleResult(ga, "BestQuality"));
|
---|
214 | Assert.AreEqual(48.19, GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
215 | Assert.AreEqual(0, GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
216 | Assert.AreEqual(50950, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
217 | }
|
---|
218 |
|
---|
219 | public GeneticAlgorithm CreateGpArtificialAntSample() {
|
---|
220 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
221 | #region Problem Configuration
|
---|
222 | ArtificialAntProblem antProblem = new ArtificialAntProblem();
|
---|
223 | antProblem.BestKnownQuality.Value = 89;
|
---|
224 | antProblem.MaxExpressionDepth.Value = 10;
|
---|
225 | antProblem.MaxExpressionLength.Value = 100;
|
---|
226 | antProblem.MaxFunctionArguments.Value = 3;
|
---|
227 | antProblem.MaxFunctionDefinitions.Value = 3;
|
---|
228 | antProblem.MaxTimeSteps.Value = 600;
|
---|
229 | #endregion
|
---|
230 | #region Algorithm Configuration
|
---|
231 | ga.Name = "Genetic Programming - Artificial Ant";
|
---|
232 | ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
|
---|
233 | ga.Problem = antProblem;
|
---|
234 | ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
|
---|
235 | ga, 1000, 1, 50, 0.15, 5);
|
---|
236 | var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
|
---|
237 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
238 | .OfType<FullTreeShaker>()
|
---|
239 | .Single(), false);
|
---|
240 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
241 | .OfType<OnePointShaker>()
|
---|
242 | .Single(), false);
|
---|
243 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
244 | .OfType<ArgumentDeleter>()
|
---|
245 | .Single(), false);
|
---|
246 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
247 | .OfType<SubroutineDeleter>()
|
---|
248 | .Single(), false);
|
---|
249 | #endregion
|
---|
250 | return ga;
|
---|
251 | }
|
---|
252 | #endregion
|
---|
253 | #region Symbolic Regression
|
---|
254 | [TestMethod]
|
---|
255 | [TestCategory("Samples.Create")]
|
---|
256 | [TestProperty("Time", "medium")]
|
---|
257 | public void CreateGpSymbolicRegressionSampleTest() {
|
---|
258 | var ga = CreateGpSymbolicRegressionSample();
|
---|
259 | XmlGenerator.Serialize(ga, @"Samples\SGP_SymbReg.hl");
|
---|
260 | }
|
---|
261 | [TestMethod]
|
---|
262 | [TestCategory("Samples.Execute")]
|
---|
263 | [TestProperty("Time", "long")]
|
---|
264 | public void RunGpSymbolicRegressionSampleTest() {
|
---|
265 | var ga = CreateGpSymbolicRegressionSample();
|
---|
266 | ga.SetSeedRandomly.Value = false;
|
---|
267 | RunAlgorithm(ga);
|
---|
268 | Assert.AreEqual(0.858344291534625, GetDoubleResult(ga, "BestQuality"), 1E-8);
|
---|
269 | Assert.AreEqual(0.56758466520692641, GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
|
---|
270 | Assert.AreEqual(0, GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
|
---|
271 | Assert.AreEqual(50950, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
272 | var bestTrainingSolution = (IRegressionSolution)ga.Results["Best training solution"].Value;
|
---|
273 | Assert.AreEqual(0.85504801557844745, bestTrainingSolution.TrainingRSquared, 1E-8);
|
---|
274 | Assert.AreEqual(0.86259381948647817, bestTrainingSolution.TestRSquared, 1E-8);
|
---|
275 | var bestValidationSolution = (IRegressionSolution)ga.Results["Best validation solution"].Value;
|
---|
276 | Assert.AreEqual(0.84854338315539746, bestValidationSolution.TrainingRSquared, 1E-8);
|
---|
277 | Assert.AreEqual(0.8662813452656678, bestValidationSolution.TestRSquared, 1E-8);
|
---|
278 | }
|
---|
279 |
|
---|
280 | private GeneticAlgorithm CreateGpSymbolicRegressionSample() {
|
---|
281 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
282 | #region Problem Configuration
|
---|
283 | SymbolicRegressionSingleObjectiveProblem symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
|
---|
284 | symbRegProblem.Name = "Tower Symbolic Regression Problem";
|
---|
285 | symbRegProblem.Description = "Tower Dataset (downloaded from: http://www.symbolicregression.com/?q=towerProblem)";
|
---|
286 | RegressionRealWorldInstanceProvider provider = new RegressionRealWorldInstanceProvider();
|
---|
287 | var instance = provider.GetDataDescriptors().Where(x => x.Name.Equals("Tower")).Single();
|
---|
288 | var towerProblemData = (RegressionProblemData)provider.LoadData(instance);
|
---|
289 | towerProblemData.TargetVariableParameter.Value = towerProblemData.TargetVariableParameter.ValidValues
|
---|
290 | .First(v => v.Value == "towerResponse");
|
---|
291 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
292 | towerProblemData.InputVariables.Single(x => x.Value == "x1"), true);
|
---|
293 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
294 | towerProblemData.InputVariables.Single(x => x.Value == "x7"), false);
|
---|
295 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
296 | towerProblemData.InputVariables.Single(x => x.Value == "x11"), false);
|
---|
297 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
298 | towerProblemData.InputVariables.Single(x => x.Value == "x16"), false);
|
---|
299 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
300 | towerProblemData.InputVariables.Single(x => x.Value == "x21"), false);
|
---|
301 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
302 | towerProblemData.InputVariables.Single(x => x.Value == "x25"), false);
|
---|
303 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
304 | towerProblemData.InputVariables.Single(x => x.Value == "towerResponse"), false);
|
---|
305 | towerProblemData.TrainingPartition.Start = 0;
|
---|
306 | towerProblemData.TrainingPartition.End = 3136;
|
---|
307 | towerProblemData.TestPartition.Start = 3136;
|
---|
308 | towerProblemData.TestPartition.End = 4999;
|
---|
309 | towerProblemData.Name = "Data imported from towerData.txt";
|
---|
310 | towerProblemData.Description = "Chemical concentration at top of distillation tower, dataset downloaded from: http://vanillamodeling.com/realproblems.html, best R² achieved with nu-SVR = 0.97";
|
---|
311 | symbRegProblem.ProblemData = towerProblemData;
|
---|
312 |
|
---|
313 | // configure grammar
|
---|
314 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
315 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
316 | grammar.Symbols.OfType<VariableCondition>().Single().InitialFrequency = 0.0;
|
---|
317 | var varSymbol = grammar.Symbols.OfType<Variable>().Where(x => !(x is LaggedVariable)).Single();
|
---|
318 | varSymbol.WeightMu = 1.0;
|
---|
319 | varSymbol.WeightSigma = 1.0;
|
---|
320 | varSymbol.WeightManipulatorMu = 0.0;
|
---|
321 | varSymbol.WeightManipulatorSigma = 0.05;
|
---|
322 | varSymbol.MultiplicativeWeightManipulatorSigma = 0.03;
|
---|
323 | var constSymbol = grammar.Symbols.OfType<Constant>().Single();
|
---|
324 | constSymbol.MaxValue = 20;
|
---|
325 | constSymbol.MinValue = -20;
|
---|
326 | constSymbol.ManipulatorMu = 0.0;
|
---|
327 | constSymbol.ManipulatorSigma = 1;
|
---|
328 | constSymbol.MultiplicativeManipulatorSigma = 0.03;
|
---|
329 | symbRegProblem.SymbolicExpressionTreeGrammar = grammar;
|
---|
330 |
|
---|
331 | // configure remaining problem parameters
|
---|
332 | symbRegProblem.BestKnownQuality.Value = 0.97;
|
---|
333 | symbRegProblem.FitnessCalculationPartition.Start = 0;
|
---|
334 | symbRegProblem.FitnessCalculationPartition.End = 2300;
|
---|
335 | symbRegProblem.ValidationPartition.Start = 2300;
|
---|
336 | symbRegProblem.ValidationPartition.End = 3136;
|
---|
337 | symbRegProblem.RelativeNumberOfEvaluatedSamples.Value = 1;
|
---|
338 | symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 150;
|
---|
339 | symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 12;
|
---|
340 | symbRegProblem.MaximumFunctionDefinitions.Value = 0;
|
---|
341 | symbRegProblem.MaximumFunctionArguments.Value = 0;
|
---|
342 |
|
---|
343 | symbRegProblem.EvaluatorParameter.Value = new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator();
|
---|
344 | #endregion
|
---|
345 | #region Algorithm Configuration
|
---|
346 | ga.Problem = symbRegProblem;
|
---|
347 | ga.Name = "Genetic Programming - Symbolic Regression";
|
---|
348 | ga.Description = "A standard genetic programming algorithm to solve a symbolic regression problem (tower dataset)";
|
---|
349 | ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
|
---|
350 | ga, 1000, 1, 50, 0.15, 5);
|
---|
351 | var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
|
---|
352 | mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
|
---|
353 | mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
|
---|
354 |
|
---|
355 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
356 | ga.Analyzer.Operators
|
---|
357 | .OfType<SymbolicRegressionSingleObjectiveOverfittingAnalyzer>()
|
---|
358 | .Single(), false);
|
---|
359 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
360 | ga.Analyzer.Operators
|
---|
361 | .OfType<SymbolicDataAnalysisAlleleFrequencyAnalyzer>()
|
---|
362 | .First(), false);
|
---|
363 | #endregion
|
---|
364 | return ga;
|
---|
365 | }
|
---|
366 | #endregion
|
---|
367 | #region Symbolic Classification
|
---|
368 | [TestMethod]
|
---|
369 | [TestCategory("Samples.Create")]
|
---|
370 | [TestProperty("Time", "medium")]
|
---|
371 | public void CreateGpSymbolicClassificationSampleTest() {
|
---|
372 | var ga = CreateGpSymbolicClassificationSample();
|
---|
373 | XmlGenerator.Serialize(ga, @"Samples\SGP_SymbClass.hl");
|
---|
374 | }
|
---|
375 |
|
---|
376 | [TestMethod]
|
---|
377 | [TestCategory("Samples.Execute")]
|
---|
378 | [TestProperty("Time", "long")]
|
---|
379 | public void RunGpSymbolicClassificationSampleTest() {
|
---|
380 | var ga = CreateGpSymbolicClassificationSample();
|
---|
381 | ga.SetSeedRandomly.Value = false;
|
---|
382 | RunAlgorithm(ga);
|
---|
383 | Assert.AreEqual(0.141880203907627, GetDoubleResult(ga, "BestQuality"), 1E-8);
|
---|
384 | Assert.AreEqual(4.3246992327753295, GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
|
---|
385 | Assert.AreEqual(100.62175156249987, GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
|
---|
386 | Assert.AreEqual(100900, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
387 | var bestTrainingSolution = (IClassificationSolution)ga.Results["Best training solution"].Value;
|
---|
388 | Assert.AreEqual(0.80875, bestTrainingSolution.TrainingAccuracy, 1E-8);
|
---|
389 | Assert.AreEqual(0.795031055900621, bestTrainingSolution.TestAccuracy, 1E-8);
|
---|
390 | var bestValidationSolution = (IClassificationSolution)ga.Results["Best validation solution"].Value;
|
---|
391 | Assert.AreEqual(0.81375, bestValidationSolution.TrainingAccuracy, 1E-8);
|
---|
392 | Assert.AreEqual(0.788819875776398, bestValidationSolution.TestAccuracy, 1E-8);
|
---|
393 | }
|
---|
394 |
|
---|
395 | private GeneticAlgorithm CreateGpSymbolicClassificationSample() {
|
---|
396 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
397 | #region Problem Configuration
|
---|
398 | SymbolicClassificationSingleObjectiveProblem symbClassProblem = new SymbolicClassificationSingleObjectiveProblem();
|
---|
399 | symbClassProblem.Name = "Mammography Classification Problem";
|
---|
400 | symbClassProblem.Description = "Mammography dataset imported from the UCI machine learning repository (http://archive.ics.uci.edu/ml/datasets/Mammographic+Mass)";
|
---|
401 | UCIInstanceProvider provider = new UCIInstanceProvider();
|
---|
402 | var instance = provider.GetDataDescriptors().Where(x => x.Name.Equals("Mammography, M. Elter, 2007")).Single();
|
---|
403 | var mammoData = (ClassificationProblemData)provider.LoadData(instance);
|
---|
404 | mammoData.TargetVariableParameter.Value = mammoData.TargetVariableParameter.ValidValues
|
---|
405 | .First(v => v.Value == "Severity");
|
---|
406 | mammoData.InputVariables.SetItemCheckedState(
|
---|
407 | mammoData.InputVariables.Single(x => x.Value == "BI-RADS"), false);
|
---|
408 | mammoData.InputVariables.SetItemCheckedState(
|
---|
409 | mammoData.InputVariables.Single(x => x.Value == "Age"), true);
|
---|
410 | mammoData.InputVariables.SetItemCheckedState(
|
---|
411 | mammoData.InputVariables.Single(x => x.Value == "Shape"), true);
|
---|
412 | mammoData.InputVariables.SetItemCheckedState(
|
---|
413 | mammoData.InputVariables.Single(x => x.Value == "Margin"), true);
|
---|
414 | mammoData.InputVariables.SetItemCheckedState(
|
---|
415 | mammoData.InputVariables.Single(x => x.Value == "Density"), true);
|
---|
416 | mammoData.InputVariables.SetItemCheckedState(
|
---|
417 | mammoData.InputVariables.Single(x => x.Value == "Severity"), false);
|
---|
418 | mammoData.TrainingPartition.Start = 0;
|
---|
419 | mammoData.TrainingPartition.End = 800;
|
---|
420 | mammoData.TestPartition.Start = 800;
|
---|
421 | mammoData.TestPartition.End = 961;
|
---|
422 | mammoData.Name = "Data imported from mammographic_masses.csv";
|
---|
423 | mammoData.Description = "Original dataset: http://archive.ics.uci.edu/ml/datasets/Mammographic+Mass, missing values have been replaced with median values.";
|
---|
424 | symbClassProblem.ProblemData = mammoData;
|
---|
425 |
|
---|
426 | // configure grammar
|
---|
427 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
428 | grammar.ConfigureAsDefaultClassificationGrammar();
|
---|
429 | grammar.Symbols.OfType<VariableCondition>().Single().Enabled = false;
|
---|
430 | var varSymbol = grammar.Symbols.OfType<Variable>().Where(x => !(x is LaggedVariable)).Single();
|
---|
431 | varSymbol.WeightMu = 1.0;
|
---|
432 | varSymbol.WeightSigma = 1.0;
|
---|
433 | varSymbol.WeightManipulatorMu = 0.0;
|
---|
434 | varSymbol.WeightManipulatorSigma = 0.05;
|
---|
435 | varSymbol.MultiplicativeWeightManipulatorSigma = 0.03;
|
---|
436 | var constSymbol = grammar.Symbols.OfType<Constant>().Single();
|
---|
437 | constSymbol.MaxValue = 20;
|
---|
438 | constSymbol.MinValue = -20;
|
---|
439 | constSymbol.ManipulatorMu = 0.0;
|
---|
440 | constSymbol.ManipulatorSigma = 1;
|
---|
441 | constSymbol.MultiplicativeManipulatorSigma = 0.03;
|
---|
442 | symbClassProblem.SymbolicExpressionTreeGrammar = grammar;
|
---|
443 |
|
---|
444 | // configure remaining problem parameters
|
---|
445 | symbClassProblem.BestKnownQuality.Value = 0.0;
|
---|
446 | symbClassProblem.FitnessCalculationPartition.Start = 0;
|
---|
447 | symbClassProblem.FitnessCalculationPartition.End = 400;
|
---|
448 | symbClassProblem.ValidationPartition.Start = 400;
|
---|
449 | symbClassProblem.ValidationPartition.End = 800;
|
---|
450 | symbClassProblem.RelativeNumberOfEvaluatedSamples.Value = 1;
|
---|
451 | symbClassProblem.MaximumSymbolicExpressionTreeLength.Value = 100;
|
---|
452 | symbClassProblem.MaximumSymbolicExpressionTreeDepth.Value = 10;
|
---|
453 | symbClassProblem.MaximumFunctionDefinitions.Value = 0;
|
---|
454 | symbClassProblem.MaximumFunctionArguments.Value = 0;
|
---|
455 | symbClassProblem.EvaluatorParameter.Value = new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator();
|
---|
456 | #endregion
|
---|
457 | #region Algorithm Configuration
|
---|
458 | ga.Problem = symbClassProblem;
|
---|
459 | ga.Name = "Genetic Programming - Symbolic Classification";
|
---|
460 | ga.Description = "A standard genetic programming algorithm to solve a classification problem (Mammographic+Mass dataset)";
|
---|
461 | ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
|
---|
462 | ga, 1000, 1, 100, 0.15, 5
|
---|
463 | );
|
---|
464 |
|
---|
465 | var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
|
---|
466 | mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
|
---|
467 | mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
|
---|
468 |
|
---|
469 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
470 | ga.Analyzer.Operators
|
---|
471 | .OfType<SymbolicClassificationSingleObjectiveOverfittingAnalyzer>()
|
---|
472 | .Single(), false);
|
---|
473 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
474 | ga.Analyzer.Operators
|
---|
475 | .OfType<SymbolicDataAnalysisAlleleFrequencyAnalyzer>()
|
---|
476 | .First(), false);
|
---|
477 | #endregion
|
---|
478 | return ga;
|
---|
479 | }
|
---|
480 | #endregion
|
---|
481 | #region LawnMower
|
---|
482 | [TestMethod]
|
---|
483 | [TestCategory("Samples.Execute")]
|
---|
484 | [TestProperty("Time", "long")]
|
---|
485 | public void RunGpLawnMowerSampleTest() {
|
---|
486 | var ga = CreateGpLawnMowerSample();
|
---|
487 | ga.SetSeedRandomly.Value = false;
|
---|
488 | RunAlgorithm(ga);
|
---|
489 | }
|
---|
490 |
|
---|
491 | public GeneticAlgorithm CreateGpLawnMowerSample() {
|
---|
492 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
493 | #region Problem Configuration
|
---|
494 | var problem = new HeuristicLab.Problems.LawnMower.Problem();
|
---|
495 | #endregion
|
---|
496 | #region Algorithm Configuration
|
---|
497 | ga.Name = "Genetic Programming - Lawn Mower";
|
---|
498 | ga.Description = "A standard genetic programming algorithm to solve the lawn mower problem";
|
---|
499 | ga.Problem = problem;
|
---|
500 | ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
|
---|
501 | ga, 1000, 1, 50, 0.25, 5);
|
---|
502 | var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
|
---|
503 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
504 | .OfType<OnePointShaker>()
|
---|
505 | .Single(), false);
|
---|
506 | #endregion
|
---|
507 | return ga;
|
---|
508 | }
|
---|
509 | #endregion
|
---|
510 | #endregion
|
---|
511 |
|
---|
512 | #region ES
|
---|
513 | #region Griewank
|
---|
514 | [TestMethod]
|
---|
515 | [TestCategory("Samples.Create")]
|
---|
516 | [TestProperty("Time", "medium")]
|
---|
517 | public void CreateEsGriewankSampleTest() {
|
---|
518 | var es = CreateEsGriewankSample();
|
---|
519 | XmlGenerator.Serialize(es, @"Samples\ES_Griewank.hl");
|
---|
520 | }
|
---|
521 | [TestMethod]
|
---|
522 | [TestCategory("Samples.Execute")]
|
---|
523 | [TestProperty("Time", "long")]
|
---|
524 | public void RunEsGriewankSampleTest() {
|
---|
525 | var es = CreateEsGriewankSample();
|
---|
526 | es.SetSeedRandomly.Value = false;
|
---|
527 | RunAlgorithm(es);
|
---|
528 | Assert.AreEqual(0, GetDoubleResult(es, "BestQuality"));
|
---|
529 | Assert.AreEqual(0, GetDoubleResult(es, "CurrentAverageQuality"));
|
---|
530 | Assert.AreEqual(0, GetDoubleResult(es, "CurrentWorstQuality"));
|
---|
531 | Assert.AreEqual(100020, GetIntResult(es, "EvaluatedSolutions"));
|
---|
532 | }
|
---|
533 |
|
---|
534 | private EvolutionStrategy CreateEsGriewankSample() {
|
---|
535 | EvolutionStrategy es = new EvolutionStrategy();
|
---|
536 | #region Problem Configuration
|
---|
537 | SingleObjectiveTestFunctionProblem problem = new SingleObjectiveTestFunctionProblem();
|
---|
538 |
|
---|
539 | problem.ProblemSize.Value = 10;
|
---|
540 | problem.EvaluatorParameter.Value = new GriewankEvaluator();
|
---|
541 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
542 | problem.Maximization.Value = false;
|
---|
543 | problem.Bounds = new DoubleMatrix(new double[,] { { -600, 600 } });
|
---|
544 | problem.BestKnownQuality.Value = 0;
|
---|
545 | problem.BestKnownSolutionParameter.Value = new RealVector(10);
|
---|
546 | problem.Name = "Single Objective Test Function";
|
---|
547 | problem.Description = "Test function with real valued inputs and a single objective.";
|
---|
548 | #endregion
|
---|
549 | #region Algorithm Configuration
|
---|
550 | es.Name = "Evolution Strategy - Griewank";
|
---|
551 | es.Description = "An evolution strategy which solves the 10-dimensional Griewank test function";
|
---|
552 | es.Problem = problem;
|
---|
553 | ConfigureEvolutionStrategyParameters<AverageCrossover, NormalAllPositionsManipulator,
|
---|
554 | StdDevStrategyVectorCreator, StdDevStrategyVectorCrossover, StdDevStrategyVectorManipulator>(
|
---|
555 | es, 20, 500, 2, 200, false);
|
---|
556 |
|
---|
557 | StdDevStrategyVectorCreator strategyCreator = (StdDevStrategyVectorCreator)es.StrategyParameterCreator;
|
---|
558 | strategyCreator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1, 20 } });
|
---|
559 |
|
---|
560 | StdDevStrategyVectorManipulator strategyManipulator = (StdDevStrategyVectorManipulator)es.StrategyParameterManipulator;
|
---|
561 | strategyManipulator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1E-12, 30 } });
|
---|
562 | strategyManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.22360679774997896);
|
---|
563 | strategyManipulator.LearningRateParameter.Value = new DoubleValue(0.39763536438352531);
|
---|
564 | #endregion
|
---|
565 | return es;
|
---|
566 | }
|
---|
567 | #endregion
|
---|
568 | #endregion
|
---|
569 |
|
---|
570 | #region Island GA
|
---|
571 | #region TSP
|
---|
572 | [TestMethod]
|
---|
573 | [TestCategory("Samples.Create")]
|
---|
574 | [TestProperty("Time", "medium")]
|
---|
575 | public void CreateIslandGaTspSampleTest() {
|
---|
576 | var ga = CreateIslandGaTspSample();
|
---|
577 | XmlGenerator.Serialize(ga, @"Samples\IslandGA_TSP.hl");
|
---|
578 | }
|
---|
579 | [TestMethod]
|
---|
580 | [TestCategory("Samples.Execute")]
|
---|
581 | [TestProperty("Time", "long")]
|
---|
582 | public void RunIslandGaTspSampleTest() {
|
---|
583 | var ga = CreateIslandGaTspSample();
|
---|
584 | ga.SetSeedRandomly.Value = false;
|
---|
585 | RunAlgorithm(ga);
|
---|
586 | Assert.AreEqual(9918, GetDoubleResult(ga, "BestQuality"));
|
---|
587 | Assert.AreEqual(10324.64, GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
588 | Assert.AreEqual(11823, GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
589 | Assert.AreEqual(495500, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
590 | }
|
---|
591 |
|
---|
592 | private IslandGeneticAlgorithm CreateIslandGaTspSample() {
|
---|
593 | IslandGeneticAlgorithm ga = new IslandGeneticAlgorithm();
|
---|
594 | #region Problem Configuration
|
---|
595 | var provider = new TSPLIBTSPInstanceProvider();
|
---|
596 | var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single();
|
---|
597 | TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
|
---|
598 | tspProblem.Load(provider.LoadData(instance));
|
---|
599 | tspProblem.UseDistanceMatrix.Value = true;
|
---|
600 | #endregion
|
---|
601 | #region Algorithm Configuration
|
---|
602 | ga.Name = "Island Genetic Algorithm - TSP";
|
---|
603 | ga.Description = "An island genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
|
---|
604 | ga.Problem = tspProblem;
|
---|
605 | ConfigureIslandGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator,
|
---|
606 | UnidirectionalRingMigrator, BestSelector, WorstReplacer>(
|
---|
607 | ga, 100, 1, 1000, 0.05, 5, 50, 0.25);
|
---|
608 | #endregion
|
---|
609 | return ga;
|
---|
610 | }
|
---|
611 | #endregion
|
---|
612 | #endregion
|
---|
613 |
|
---|
614 | #region LS
|
---|
615 | #region Knapsack
|
---|
616 | [TestMethod]
|
---|
617 | [TestCategory("Samples.Create")]
|
---|
618 | [TestProperty("Time", "medium")]
|
---|
619 | public void CreateLocalSearchKnapsackSampleTest() {
|
---|
620 | var ls = CreateLocalSearchKnapsackSample();
|
---|
621 | XmlGenerator.Serialize(ls, @"Samples\LS_Knapsack.hl");
|
---|
622 | }
|
---|
623 | [TestMethod]
|
---|
624 | [TestCategory("Samples.Execute")]
|
---|
625 | [TestProperty("Time", "medium")]
|
---|
626 | public void RunLocalSearchKnapsackSampleTest() {
|
---|
627 | var ls = CreateLocalSearchKnapsackSample();
|
---|
628 | ls.SetSeedRandomly.Value = false;
|
---|
629 | RunAlgorithm(ls);
|
---|
630 | Assert.AreEqual(345, GetDoubleResult(ls, "BestQuality"));
|
---|
631 | Assert.AreEqual(340.70731707317071, GetDoubleResult(ls, "CurrentAverageQuality"));
|
---|
632 | Assert.AreEqual(337, GetDoubleResult(ls, "CurrentWorstQuality"));
|
---|
633 | Assert.AreEqual(82000, GetIntResult(ls, "EvaluatedMoves"));
|
---|
634 | }
|
---|
635 |
|
---|
636 | private LocalSearch CreateLocalSearchKnapsackSample() {
|
---|
637 | LocalSearch ls = new LocalSearch();
|
---|
638 | #region Problem Configuration
|
---|
639 | KnapsackProblem problem = new KnapsackProblem();
|
---|
640 | problem.BestKnownQuality = new DoubleValue(362);
|
---|
641 | problem.BestKnownSolution = new HeuristicLab.Encodings.BinaryVectorEncoding.BinaryVector(new bool[] {
|
---|
642 | true , false, false, true , true , true , true , true , false, true , true , true , true , true , true , false, true , false, true , true , false, true , true , false, true , false, true , true , true , false, true , true , false, true , true , false, true , false, true , true , true , true , true , true , true , true , true , true , true , true , true , false, true , false, false, true , true , false, true , true , true , true , true , true , true , true , false, true , false, true , true , true , true , false, true , true , true , true , true , true , true , true});
|
---|
643 | problem.EvaluatorParameter.Value = new KnapsackEvaluator();
|
---|
644 | problem.SolutionCreatorParameter.Value = new RandomBinaryVectorCreator();
|
---|
645 | problem.KnapsackCapacity.Value = 297;
|
---|
646 | problem.Maximization.Value = true;
|
---|
647 | problem.Penalty.Value = 1;
|
---|
648 | problem.Values = new IntArray(new int[] {
|
---|
649 | 6, 1, 1, 6, 7, 8, 7, 4, 2, 5, 2, 6, 7, 8, 7, 1, 7, 1, 9, 4, 2, 6, 5, 3, 5, 3, 3, 6, 5, 2, 4, 9, 4, 5, 7, 1, 4, 3, 5, 5, 8, 3, 6, 7, 3, 9, 7, 7, 5, 5, 7, 1, 4, 4, 3, 9, 5, 1, 6, 2, 2, 6, 1, 6, 5, 4, 4, 7, 1, 8, 9, 9, 7, 4, 3, 8, 7, 5, 7, 4, 4, 5});
|
---|
650 | problem.Weights = new IntArray(new int[] {
|
---|
651 | 1, 9, 3, 6, 5, 3, 8, 1, 7, 4, 2, 1, 2, 7, 9, 9, 8, 4, 9, 2, 4, 8, 3, 7, 5, 7, 5, 5, 1, 9, 8, 7, 8, 9, 1, 3, 3, 8, 8, 5, 1, 2, 4, 3, 6, 9, 4, 4, 9, 7, 4, 5, 1, 9, 7, 6, 7, 4, 7, 1, 2, 1, 2, 9, 8, 6, 8, 4, 7, 6, 7, 5, 3, 9, 4, 7, 4, 6, 1, 2, 5, 4});
|
---|
652 | problem.Name = "Knapsack Problem";
|
---|
653 | problem.Description = "Represents a Knapsack problem.";
|
---|
654 | #endregion
|
---|
655 | #region Algorithm Configuration
|
---|
656 | ls.Name = "Local Search - Knapsack";
|
---|
657 | ls.Description = "A local search algorithm that solves a randomly generated Knapsack problem";
|
---|
658 | ls.Problem = problem;
|
---|
659 | ls.MaximumIterations.Value = 1000;
|
---|
660 | ls.MoveEvaluator = ls.MoveEvaluatorParameter.ValidValues
|
---|
661 | .OfType<KnapsackOneBitflipMoveEvaluator>()
|
---|
662 | .Single();
|
---|
663 | ls.MoveGenerator = ls.MoveGeneratorParameter.ValidValues
|
---|
664 | .OfType<ExhaustiveOneBitflipMoveGenerator>()
|
---|
665 | .Single();
|
---|
666 | ls.MoveMaker = ls.MoveMakerParameter.ValidValues
|
---|
667 | .OfType<OneBitflipMoveMaker>()
|
---|
668 | .Single();
|
---|
669 | ls.SampleSize.Value = 100;
|
---|
670 | ls.Seed.Value = 0;
|
---|
671 | ls.SetSeedRandomly.Value = true;
|
---|
672 | #endregion
|
---|
673 | ls.Engine = new ParallelEngine.ParallelEngine();
|
---|
674 | return ls;
|
---|
675 | }
|
---|
676 | #endregion
|
---|
677 | #endregion
|
---|
678 |
|
---|
679 | #region PSO
|
---|
680 | #region Schwefel
|
---|
681 | [TestMethod]
|
---|
682 | [TestCategory("Samples.Create")]
|
---|
683 | [TestProperty("Time", "medium")]
|
---|
684 | public void CreatePsoSchwefelSampleTest() {
|
---|
685 | var pso = CreatePsoSchwefelSample();
|
---|
686 | XmlGenerator.Serialize(pso, @"Samples\PSO_Schwefel.hl");
|
---|
687 | }
|
---|
688 | [TestMethod]
|
---|
689 | [TestCategory("Samples.Execute")]
|
---|
690 | [TestProperty("Time", "medium")]
|
---|
691 | public void RunPsoSchwefelSampleTest() {
|
---|
692 | var pso = CreatePsoSchwefelSample();
|
---|
693 | pso.SetSeedRandomly.Value = false;
|
---|
694 | RunAlgorithm(pso);
|
---|
695 | if (!Environment.Is64BitProcess) {
|
---|
696 | Assert.AreEqual(118.44027985932837, GetDoubleResult(pso, "BestQuality"));
|
---|
697 | Assert.AreEqual(140.71570105946438, GetDoubleResult(pso, "CurrentAverageQuality"));
|
---|
698 | Assert.AreEqual(220.956806502853, GetDoubleResult(pso, "CurrentWorstQuality"));
|
---|
699 | Assert.AreEqual(1000, GetIntResult(pso, "Iterations"));
|
---|
700 | } else {
|
---|
701 | Assert.AreEqual(118.43958282879345, GetDoubleResult(pso, "BestQuality"));
|
---|
702 | Assert.AreEqual(139.43946864779372, GetDoubleResult(pso, "CurrentAverageQuality"));
|
---|
703 | Assert.AreEqual(217.14654589055152, GetDoubleResult(pso, "CurrentWorstQuality"));
|
---|
704 | Assert.AreEqual(1000, GetIntResult(pso, "Iterations"));
|
---|
705 | }
|
---|
706 | }
|
---|
707 | private ParticleSwarmOptimization CreatePsoSchwefelSample() {
|
---|
708 | ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
|
---|
709 | #region Problem Configuration
|
---|
710 | var problem = new SingleObjectiveTestFunctionProblem();
|
---|
711 | problem.BestKnownQuality.Value = 0.0;
|
---|
712 | problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 420.968746, 420.968746 });
|
---|
713 | problem.Bounds = new DoubleMatrix(new double[,] { { -500, 500 } });
|
---|
714 | problem.EvaluatorParameter.Value = new SchwefelEvaluator();
|
---|
715 | problem.Maximization.Value = false;
|
---|
716 | problem.ProblemSize.Value = 2;
|
---|
717 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
718 | #endregion
|
---|
719 | #region Algorithm Configuration
|
---|
720 | pso.Name = "Particle Swarm Optimization - Schwefel";
|
---|
721 | pso.Description = "A particle swarm optimization algorithm which solves the 2-dimensional Schwefel test function (based on the description in Pedersen, M.E.H. (2010). PhD thesis. University of Southampton)";
|
---|
722 | pso.Problem = problem;
|
---|
723 | pso.Inertia.Value = 10;
|
---|
724 | pso.MaxIterations.Value = 1000;
|
---|
725 | pso.NeighborBestAttraction.Value = 0.5;
|
---|
726 | pso.PersonalBestAttraction.Value = -0.01;
|
---|
727 | pso.SwarmSize.Value = 50;
|
---|
728 |
|
---|
729 | var inertiaUpdater = pso.InertiaUpdaterParameter.ValidValues
|
---|
730 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
731 | .Single();
|
---|
732 | inertiaUpdater.StartValueParameter.Value = new DoubleValue(10);
|
---|
733 | inertiaUpdater.EndValueParameter.Value = new DoubleValue(1);
|
---|
734 | pso.InertiaUpdater = inertiaUpdater;
|
---|
735 |
|
---|
736 | pso.ParticleCreator = pso.ParticleCreatorParameter.ValidValues
|
---|
737 | .OfType<RealVectorParticleCreator>()
|
---|
738 | .Single();
|
---|
739 | var swarmUpdater = pso.SwarmUpdaterParameter.ValidValues
|
---|
740 | .OfType<RealVectorSwarmUpdater>()
|
---|
741 | .Single();
|
---|
742 | swarmUpdater.VelocityBoundsIndexParameter.ActualName = "Iterations";
|
---|
743 | swarmUpdater.VelocityBoundsParameter.Value = new DoubleMatrix(new double[,] { { -10, 10 } });
|
---|
744 | swarmUpdater.VelocityBoundsStartValueParameter.Value = new DoubleValue(10.0);
|
---|
745 | swarmUpdater.VelocityBoundsEndValueParameter.Value = new DoubleValue(1.0);
|
---|
746 | swarmUpdater.VelocityBoundsScalingOperatorParameter.Value = swarmUpdater.VelocityBoundsScalingOperatorParameter.ValidValues
|
---|
747 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
748 | .Single();
|
---|
749 |
|
---|
750 | pso.TopologyInitializer = null;
|
---|
751 | pso.TopologyUpdater = null;
|
---|
752 | pso.SwarmUpdater = swarmUpdater;
|
---|
753 | pso.Seed.Value = 0;
|
---|
754 | pso.SetSeedRandomly.Value = true;
|
---|
755 | #endregion
|
---|
756 | pso.Engine = new ParallelEngine.ParallelEngine();
|
---|
757 | return pso;
|
---|
758 | }
|
---|
759 | #endregion
|
---|
760 | #endregion
|
---|
761 |
|
---|
762 | #region SA
|
---|
763 | #region Rastrigin
|
---|
764 | [TestMethod]
|
---|
765 | [TestCategory("Samples.Create")]
|
---|
766 | [TestProperty("Time", "medium")]
|
---|
767 | public void CreateSimulatedAnnealingRastriginSampleTest() {
|
---|
768 | var sa = CreateSimulatedAnnealingRastriginSample();
|
---|
769 | XmlGenerator.Serialize(sa, @"Samples\SA_Rastrigin.hl");
|
---|
770 | }
|
---|
771 | [TestMethod]
|
---|
772 | [TestCategory("Samples.Execute")]
|
---|
773 | [TestProperty("Time", "medium")]
|
---|
774 | public void RunSimulatedAnnealingRastriginSampleTest() {
|
---|
775 | var sa = CreateSimulatedAnnealingRastriginSample();
|
---|
776 | sa.SetSeedRandomly.Value = false;
|
---|
777 | RunAlgorithm(sa);
|
---|
778 | Assert.AreEqual(0.00014039606034543795, GetDoubleResult(sa, "BestQuality"));
|
---|
779 | Assert.AreEqual(5000, GetIntResult(sa, "EvaluatedMoves"));
|
---|
780 | }
|
---|
781 | private SimulatedAnnealing CreateSimulatedAnnealingRastriginSample() {
|
---|
782 | SimulatedAnnealing sa = new SimulatedAnnealing();
|
---|
783 | #region Problem Configuration
|
---|
784 | var problem = new SingleObjectiveTestFunctionProblem();
|
---|
785 | problem.BestKnownQuality.Value = 0.0;
|
---|
786 | problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 0, 0 });
|
---|
787 | problem.Bounds = new DoubleMatrix(new double[,] { { -5.12, 5.12 } });
|
---|
788 | problem.EvaluatorParameter.Value = new RastriginEvaluator();
|
---|
789 | problem.Maximization.Value = false;
|
---|
790 | problem.ProblemSize.Value = 2;
|
---|
791 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
792 | #endregion
|
---|
793 | #region Algorithm Configuration
|
---|
794 | sa.Name = "Simulated Annealing - Rastrigin";
|
---|
795 | sa.Description = "A simulated annealing algorithm that solves the 2-dimensional Rastrigin test function";
|
---|
796 | sa.Problem = problem;
|
---|
797 | var annealingOperator = sa.AnnealingOperatorParameter.ValidValues
|
---|
798 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
799 | .Single();
|
---|
800 | annealingOperator.StartIndexParameter.Value = new IntValue(0);
|
---|
801 | sa.AnnealingOperator = annealingOperator;
|
---|
802 |
|
---|
803 | sa.EndTemperature.Value = 1E-6;
|
---|
804 | sa.InnerIterations.Value = 50;
|
---|
805 | sa.MaximumIterations.Value = 100;
|
---|
806 | var moveEvaluator = sa.MoveEvaluatorParameter.ValidValues
|
---|
807 | .OfType<RastriginAdditiveMoveEvaluator>()
|
---|
808 | .Single();
|
---|
809 | moveEvaluator.A.Value = 10;
|
---|
810 | sa.MoveEvaluator = moveEvaluator;
|
---|
811 |
|
---|
812 | var moveGenerator = sa.MoveGeneratorParameter.ValidValues
|
---|
813 | .OfType<StochasticNormalMultiMoveGenerator>()
|
---|
814 | .Single();
|
---|
815 | moveGenerator.SigmaParameter.Value = new DoubleValue(1);
|
---|
816 | sa.MoveGenerator = moveGenerator;
|
---|
817 |
|
---|
818 | sa.MoveMaker = sa.MoveMakerParameter.ValidValues
|
---|
819 | .OfType<AdditiveMoveMaker>()
|
---|
820 | .Single();
|
---|
821 |
|
---|
822 | sa.Seed.Value = 0;
|
---|
823 | sa.SetSeedRandomly.Value = true;
|
---|
824 | sa.StartTemperature.Value = 1;
|
---|
825 | #endregion
|
---|
826 | sa.Engine = new ParallelEngine.ParallelEngine();
|
---|
827 | return sa;
|
---|
828 | }
|
---|
829 | #endregion
|
---|
830 | #endregion
|
---|
831 |
|
---|
832 | #region TS
|
---|
833 | #region TSP
|
---|
834 | [TestMethod]
|
---|
835 | [TestCategory("Samples.Create")]
|
---|
836 | [TestProperty("Time", "medium")]
|
---|
837 | public void CreateTabuSearchTspSampleTest() {
|
---|
838 | var ts = CreateTabuSearchTspSample();
|
---|
839 | XmlGenerator.Serialize(ts, @"Samples\TS_TSP.hl");
|
---|
840 | }
|
---|
841 | [TestMethod]
|
---|
842 | [TestCategory("Samples.Execute")]
|
---|
843 | [TestProperty("Time", "long")]
|
---|
844 | public void RunTabuSearchTspSampleTest() {
|
---|
845 | var ts = CreateTabuSearchTspSample();
|
---|
846 | ts.SetSeedRandomly.Value = false;
|
---|
847 | RunAlgorithm(ts);
|
---|
848 | Assert.AreEqual(6294, GetDoubleResult(ts, "BestQuality"));
|
---|
849 | Assert.AreEqual(7380.0386666666664, GetDoubleResult(ts, "CurrentAverageQuality"));
|
---|
850 | Assert.AreEqual(8328, GetDoubleResult(ts, "CurrentWorstQuality"));
|
---|
851 | Assert.AreEqual(750000, GetIntResult(ts, "EvaluatedMoves"));
|
---|
852 | }
|
---|
853 |
|
---|
854 | private TabuSearch CreateTabuSearchTspSample() {
|
---|
855 | TabuSearch ts = new TabuSearch();
|
---|
856 | #region Problem Configuration
|
---|
857 | var provider = new TSPLIBTSPInstanceProvider();
|
---|
858 | var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single();
|
---|
859 | TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
|
---|
860 | tspProblem.Load(provider.LoadData(instance));
|
---|
861 | tspProblem.UseDistanceMatrix.Value = true;
|
---|
862 | #endregion
|
---|
863 | #region Algorithm Configuration
|
---|
864 | ts.Name = "Tabu Search - TSP";
|
---|
865 | ts.Description = "A tabu search algorithm that solves the \"ch130\" TSP (imported from TSPLIB)";
|
---|
866 | ts.Problem = tspProblem;
|
---|
867 |
|
---|
868 | ts.MaximumIterations.Value = 1000;
|
---|
869 | // move generator has to be set first
|
---|
870 | var moveGenerator = ts.MoveGeneratorParameter.ValidValues
|
---|
871 | .OfType<StochasticInversionMultiMoveGenerator>()
|
---|
872 | .Single();
|
---|
873 | ts.MoveGenerator = moveGenerator;
|
---|
874 | var moveEvaluator = ts.MoveEvaluatorParameter.ValidValues
|
---|
875 | .OfType<TSPInversionMoveRoundedEuclideanPathEvaluator>()
|
---|
876 | .Single();
|
---|
877 | ts.MoveEvaluator = moveEvaluator;
|
---|
878 | var moveMaker = ts.MoveMakerParameter.ValidValues
|
---|
879 | .OfType<InversionMoveMaker>()
|
---|
880 | .Single();
|
---|
881 | ts.MoveMaker = moveMaker;
|
---|
882 | ts.SampleSize.Value = 750;
|
---|
883 | ts.Seed.Value = 0;
|
---|
884 | ts.SetSeedRandomly.Value = true;
|
---|
885 |
|
---|
886 | var tabuChecker = ts.TabuCheckerParameter.ValidValues
|
---|
887 | .OfType<InversionMoveSoftTabuCriterion>()
|
---|
888 | .Single();
|
---|
889 | tabuChecker.UseAspirationCriterion.Value = true;
|
---|
890 | ts.TabuChecker = tabuChecker;
|
---|
891 |
|
---|
892 | var tabuMaker = ts.TabuMakerParameter.ValidValues
|
---|
893 | .OfType<InversionMoveTabuMaker>()
|
---|
894 | .Single();
|
---|
895 | ts.TabuMaker = tabuMaker;
|
---|
896 | ts.TabuTenure.Value = 60;
|
---|
897 |
|
---|
898 | #endregion
|
---|
899 | ts.Engine = new ParallelEngine.ParallelEngine();
|
---|
900 | return ts;
|
---|
901 | }
|
---|
902 | #endregion
|
---|
903 |
|
---|
904 | #region VRP
|
---|
905 | [TestMethod]
|
---|
906 | [TestCategory("Samples.Create")]
|
---|
907 | [TestProperty("Time", "medium")]
|
---|
908 | public void CreateTabuSearchVRPSampleTest() {
|
---|
909 | var vrp = CreateTabuSearchVrpSample();
|
---|
910 | XmlGenerator.Serialize(vrp, @"Samples\TS_VRP.hl");
|
---|
911 | }
|
---|
912 | [TestMethod]
|
---|
913 | [TestCategory("Samples.Execute")]
|
---|
914 | [TestProperty("Time", "long")]
|
---|
915 | public void RunTabuSearchVRPSampleTest() {
|
---|
916 | var vrp = CreateTabuSearchVrpSample();
|
---|
917 | vrp.SetSeedRandomly.Value = false;
|
---|
918 | RunAlgorithm(vrp);
|
---|
919 | Assert.AreEqual(1473, GetDoubleResult(vrp, "BestQuality"));
|
---|
920 | Assert.AreEqual(2102.1192622950812, GetDoubleResult(vrp, "CurrentAverageQuality"));
|
---|
921 | Assert.AreEqual(4006, GetDoubleResult(vrp, "CurrentWorstQuality"));
|
---|
922 | Assert.AreEqual(119072, GetIntResult(vrp, "EvaluatedMoves"));
|
---|
923 | }
|
---|
924 |
|
---|
925 | private TabuSearch CreateTabuSearchVrpSample() {
|
---|
926 | TabuSearch ts = new TabuSearch();
|
---|
927 | #region Problem Configuration
|
---|
928 | var provider = new AugeratInstanceProvider();
|
---|
929 | var instance = provider.GetDataDescriptors().Where(x => x.Name == "A-n62-k8").Single();
|
---|
930 | VehicleRoutingProblem vrpProblem = new VehicleRoutingProblem();
|
---|
931 | vrpProblem.Load(provider.LoadData(instance));
|
---|
932 | #endregion
|
---|
933 | #region Algorithm Configuration
|
---|
934 | ts.Name = "Tabu Search - VRP";
|
---|
935 | ts.Description = "A tabu search algorithm that solves the \"A-n62-k8\" VRP (imported from Augerat)";
|
---|
936 | ts.Problem = vrpProblem;
|
---|
937 |
|
---|
938 | ts.MaximumIterations.Value = 200;
|
---|
939 | // move generator has to be set first
|
---|
940 | var moveGenerator = ts.MoveGeneratorParameter.ValidValues
|
---|
941 | .OfType<PotvinCustomerRelocationExhaustiveMoveGenerator>()
|
---|
942 | .Single();
|
---|
943 | ts.MoveGenerator = moveGenerator;
|
---|
944 | var moveEvaluator = ts.MoveEvaluatorParameter.ValidValues
|
---|
945 | .OfType<PotvinCustomerRelocationMoveEvaluator>()
|
---|
946 | .Single();
|
---|
947 | ts.MoveEvaluator = moveEvaluator;
|
---|
948 | var moveMaker = ts.MoveMakerParameter.ValidValues
|
---|
949 | .OfType<PotvinCustomerRelocationMoveMaker>()
|
---|
950 | .Single();
|
---|
951 | ts.MoveMaker = moveMaker;
|
---|
952 | ts.SampleSize.Value = 1000;
|
---|
953 | ts.Seed.Value = 0;
|
---|
954 | ts.SetSeedRandomly.Value = true;
|
---|
955 |
|
---|
956 | var tabuChecker = ts.TabuCheckerParameter.ValidValues
|
---|
957 | .OfType<PotvinCustomerRelocationMoveTabuCriterion>()
|
---|
958 | .Single();
|
---|
959 | tabuChecker.UseAspirationCriterion.Value = false;
|
---|
960 | ts.TabuChecker = tabuChecker;
|
---|
961 |
|
---|
962 | var tabuMaker = ts.TabuMakerParameter.ValidValues
|
---|
963 | .OfType<PotvinCustomerRelocationMoveTabuMaker>()
|
---|
964 | .Single();
|
---|
965 | ts.TabuMaker = tabuMaker;
|
---|
966 | ts.TabuTenure.Value = 6;
|
---|
967 |
|
---|
968 | #endregion
|
---|
969 | ts.Engine = new ParallelEngine.ParallelEngine();
|
---|
970 | return ts;
|
---|
971 | }
|
---|
972 | #endregion
|
---|
973 | #endregion
|
---|
974 |
|
---|
975 | #region VNS
|
---|
976 | #region TSP
|
---|
977 | [TestMethod]
|
---|
978 | [TestCategory("Samples.Create")]
|
---|
979 | [TestProperty("Time", "medium")]
|
---|
980 | public void CreateVnsTspSampleTest() {
|
---|
981 | var vns = CreateVnsTspSample();
|
---|
982 | XmlGenerator.Serialize(vns, @"Samples\VNS_TSP.hl");
|
---|
983 | }
|
---|
984 | [TestMethod]
|
---|
985 | [TestCategory("Samples.Execute")]
|
---|
986 | [TestProperty("Time", "long")]
|
---|
987 | public void RunVnsTspSampleTest() {
|
---|
988 | var vns = CreateVnsTspSample();
|
---|
989 | vns.SetSeedRandomly = false;
|
---|
990 | RunAlgorithm(vns);
|
---|
991 | Assert.AreEqual(867, GetDoubleResult(vns, "BestQuality"));
|
---|
992 | Assert.AreEqual(867, GetDoubleResult(vns, "CurrentAverageQuality"));
|
---|
993 | Assert.AreEqual(867, GetDoubleResult(vns, "CurrentWorstQuality"));
|
---|
994 | Assert.AreEqual(12975173, GetIntResult(vns, "EvaluatedSolutions"));
|
---|
995 | }
|
---|
996 |
|
---|
997 | private VariableNeighborhoodSearch CreateVnsTspSample() {
|
---|
998 | VariableNeighborhoodSearch vns = new VariableNeighborhoodSearch();
|
---|
999 | #region Problem Configuration
|
---|
1000 | TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
|
---|
1001 | tspProblem.BestKnownSolution = new Permutation(PermutationTypes.Absolute, new int[] {
|
---|
1002 | 117, 65, 73, 74, 75, 76, 82, 86, 87, 94, 100, 106, 115, 120, 124, 107, 101, 108, 109, 102, 97, 90, 96, 95, 88, 89, 84, 78, 69, 57, 68, 56, 44, 55, 45, 36, 46, 37, 38, 47, 48, 59, 49, 58, 70, 77, 83, 79, 50, 80, 85, 98, 103, 110, 116, 121, 125, 133, 132, 138, 139, 146, 147, 159, 168, 169, 175, 182, 188, 201, 213, 189, 214, 221, 230, 246, 262, 276, 284, 275, 274, 261, 245, 229, 220, 228, 243, 259, 273, 282, 272, 258, 242, 257, 293, 292, 302, 310, 319, 320, 327, 326, 333, 340, 346, 339, 345, 344, 337, 338, 332, 325, 318, 309, 301, 291, 271, 251, 270, 233, 250, 269, 268, 280, 290, 300, 415, 440, 416, 417, 441, 458, 479, 418, 419, 395, 420, 442, 421, 396, 397, 422, 423, 461, 481, 502, 460, 501, 459, 480, 500, 517, 531, 516, 530, 499, 478, 457, 439, 414, 413, 412, 438, 456, 477, 498, 515, 529, 538, 547, 558, 559, 560, 548, 539, 549, 561, 562, 551, 550, 532, 540, 533, 541, 518, 534, 542, 552, 553, 554, 555, 535, 543, 556, 544, 536, 522, 505, 521, 520, 504, 519, 503, 482, 462, 463, 464, 483, 443, 465, 484, 506, 485, 507, 508, 487, 467, 486, 466, 445, 428, 444, 424, 425, 426, 427, 398, 399, 400, 381, 382, 371, 372, 401, 429, 446, 430, 402, 383, 366, 356, 357, 352, 385, 384, 403, 431, 447, 469, 468, 488, 489, 490, 470, 471, 448, 432, 433, 404, 405, 386, 373, 374, 367, 376, 375, 387, 491, 509, 537, 510, 492, 472, 449, 388, 389, 406, 450, 407, 377, 368, 359, 354, 350, 335, 324, 330, 390, 434, 451, 473, 493, 511, 523, 545, 563, 565, 567, 570, 569, 578, 577, 576, 575, 574, 573, 572, 580, 584, 583, 582, 587, 586, 585, 581, 579, 571, 568, 566, 564, 557, 546, 527, 513, 526, 525, 524, 512, 495, 494, 474, 452, 436, 409, 435, 453, 475, 496, 514, 528, 497, 455, 476, 454, 437, 411, 410, 394, 393, 392, 380, 370, 379, 408, 391, 378, 369, 364, 365, 361, 355, 351, 343, 336, 331, 317, 299, 286, 287, 278, 263, 264, 265, 223, 202, 248, 266, 279, 288, 289, 281, 267, 249, 232, 224, 216, 215, 204, 192, 193, 194, 186, 179, 185, 203, 191, 190, 177, 171, 161, 128, 135, 140, 149, 162, 150, 163, 172, 178, 173, 164, 152, 151, 141, 153, 165, 154, 142, 155, 143, 137, 136, 130, 129, 118, 114, 113, 105, 119, 123, 131, 144, 156, 157, 145, 158, 166, 167, 174, 180, 181, 187, 195, 205, 217, 226, 236, 225, 234, 252, 235, 253, 254, 255, 238, 239, 240, 241, 256, 237, 206, 207, 208, 196, 197, 198, 209, 199, 200, 211, 212, 219, 210, 218, 227, 244, 260, 283, 294, 295, 303, 296, 311, 304, 297, 298, 305, 285, 306, 314, 329, 321, 313, 312, 328, 334, 341, 347, 348, 353, 358, 362, 363, 360, 349, 342, 322, 323, 315, 316, 308, 307, 277, 247, 231, 222, 184, 183, 176, 170, 160, 148, 134, 127, 126, 111, 104, 92, 91, 71, 60, 51, 52, 40, 32, 23, 21, 20, 18, 17, 16, 14, 13, 11, 10, 7, 6, 5, 2, 1, 0, 3, 4, 31, 39, 25, 30, 35, 34, 33, 43, 54, 42, 27, 28, 29, 9, 8, 12, 15, 19, 22, 24, 26, 41, 67, 66, 64, 63, 53, 62, 61, 72, 81, 93, 99, 112, 122,
|
---|
1003 | });
|
---|
1004 | tspProblem.Coordinates = new DoubleMatrix(new double[,] {
|
---|
1005 | {48, 71}, {49, 71}, {50, 71}, {44, 70}, {45, 70}, {52, 70}, {53, 70}, {54, 70}, {41, 69}, {42, 69}, {55, 69}, {56, 69}, {40, 68}, {56, 68}, {57, 68}, {39, 67}, {57, 67}, {58, 67}, {59, 67}, {38, 66}, {59, 66}, {60, 66}, {37, 65}, {60, 65}, {36, 64}, {43, 64}, {35, 63}, {37, 63}, {41, 63}, {42, 63}, {43, 63}, {47, 63}, {61, 63}, {40, 62}, {41, 62}, {42, 62}, {43, 62}, {45, 62}, {46, 62}, {47, 62}, {62, 62}, {34, 61}, {38, 61}, {39, 61}, {42, 61}, {43, 61}, {44, 61}, {45, 61}, {46, 61}, {47, 61}, {52, 61}, {62, 61}, {63, 61}, {26, 60}, {38, 60}, {42, 60}, {43, 60}, {44, 60}, {46, 60}, {47, 60}, {63, 60}, {23, 59}, {24, 59}, {27, 59}, {29, 59}, {30, 59}, {31, 59}, {33, 59}, {42, 59}, {46, 59}, {47, 59}, {63, 59}, {21, 58}, {32, 58}, {33, 58}, {34, 58}, {35, 58}, {46, 58}, {47, 58}, {48, 58}, {53, 58}, {21, 57}, {35, 57}, {47, 57}, {48, 57}, {53, 57}, {36, 56}, {37, 56}, {46, 56}, {47, 56}, {48, 56}, {64, 56}, {65, 56}, {20, 55}, {38, 55}, {46, 55}, {47, 55}, {48, 55}, {52, 55}, {21, 54}, {40, 54}, {47, 54}, {48, 54}, {52, 54}, {65, 54}, {30, 53}, {41, 53}, {46, 53}, {47, 53}, {48, 53}, {52, 53}, {65, 53}, {21, 52}, {32, 52}, {33, 52}, {42, 52}, {51, 52}, {21, 51}, {33, 51}, {34, 51}, {43, 51}, {51, 51}, {21, 50}, {35, 50}, {44, 50}, {50, 50}, {66, 50}, {67, 50}, {21, 49}, {34, 49}, {36, 49}, {37, 49}, {46, 49}, {49, 49}, {67, 49}, {22, 48}, {36, 48}, {37, 48}, {46, 48}, {47, 48}, {22, 47}, {30, 47}, {34, 47}, {37, 47}, {38, 47}, {39, 47}, {47, 47}, {48, 47}, {67, 47}, {23, 46}, {28, 46}, {29, 46}, {30, 46}, {31, 46}, {32, 46}, {35, 46}, {37, 46}, {38, 46}, {39, 46}, {49, 46}, {67, 46}, {23, 45}, {28, 45}, {29, 45}, {31, 45}, {32, 45}, {40, 45}, {41, 45}, {49, 45}, {50, 45}, {68, 45}, {24, 44}, {29, 44}, {32, 44}, {41, 44}, {51, 44}, {68, 44}, {25, 43}, {30, 43}, {32, 43}, {42, 43}, {43, 43}, {51, 43}, {68, 43}, {69, 43}, {31, 42}, {32, 42}, {43, 42}, {52, 42}, {55, 42}, {26, 41}, {27, 41}, {31, 41}, {32, 41}, {33, 41}, {44, 41}, {45, 41}, {46, 41}, {47, 41}, {48, 41}, {49, 41}, {53, 41}, {25, 40}, {27, 40}, {32, 40}, {43, 40}, {44, 40}, {45, 40}, {46, 40}, {48, 40}, {49, 40}, {50, 40}, {51, 40}, {53, 40}, {56, 40}, {32, 39}, {33, 39}, {43, 39}, {50, 39}, {51, 39}, {54, 39}, {56, 39}, {69, 39}, {24, 38}, {32, 38}, {41, 38}, {42, 38}, {51, 38}, {52, 38}, {54, 38}, {57, 38}, {69, 38}, {31, 37}, {32, 37}, {40, 37}, {41, 37}, {42, 37}, {43, 37}, {44, 37}, {45, 37}, {46, 37}, {47, 37}, {48, 37}, {51, 37}, {52, 37}, {55, 37}, {57, 37}, {69, 37}, {24, 36}, {31, 36}, {32, 36}, {39, 36}, {40, 36}, {41, 36}, {42, 36}, {43, 36}, {45, 36}, {48, 36}, {49, 36}, {51, 36}, {53, 36}, {55, 36}, {58, 36}, {22, 35}, {23, 35}, {24, 35}, {25, 35}, {30, 35}, {31, 35}, {32, 35}, {39, 35}, {41, 35}, {49, 35}, {51, 35}, {55, 35}, {56, 35}, {58, 35}, {71, 35}, {20, 34}, {27, 34}, {30, 34}, {31, 34}, {51, 34}, {53, 34}, {57, 34}, {60, 34}, {18, 33}, {19, 33}, {29, 33}, {30, 33}, {31, 33}, {45, 33}, {46, 33}, {47, 33}, {52, 33}, {53, 33}, {55, 33}, {57, 33}, {58, 33}, {17, 32}, {30, 32}, {44, 32}, {47, 32}, {54, 32}, {57, 32}, {59, 32}, {61, 32}, {71, 32}, {72, 32}, {43, 31}, {47, 31}, {56, 31}, {58, 31}, {59, 31}, {61, 31}, {72, 31}, {74, 31}, {16, 30}, {43, 30}, {46, 30}, {47, 30}, {59, 30}, {63, 30}, {71, 30}, {75, 30}, {43, 29}, {46, 29}, {47, 29}, {59, 29}, {60, 29}, {75, 29}, {15, 28}, {43, 28}, {46, 28}, {61, 28}, {76, 28}, {15, 27}, {43, 27}, {44, 27}, {45, 27}, {46, 27}, {60, 27}, {62, 27}, {15, 26}, {43, 26}, {44, 26}, {46, 26}, {59, 26}, {60, 26}, {64, 26}, {77, 26}, {15, 25}, {58, 25}, {61, 25}, {77, 25}, {15, 24}, {53, 24}, {55, 24}, {61, 24}, {77, 24}, {62, 23}, {16, 22}, {61, 22}, {62, 22}, {15, 21}, {16, 21}, {52, 21}, {63, 21}, {77, 21}, {16, 20}, {17, 20}, {46, 20}, {47, 20}, {60, 20}, {62, 20}, {63, 20}, {65, 20}, {76, 20}, {15, 19}, {17, 19}, {18, 19}, {44, 19}, {45, 19}, {48, 19}, {53, 19}, {56, 19}, {60, 19}, {62, 19}, {67, 19}, {68, 19}, {76, 19}, {15, 18}, {18, 18}, {19, 18}, {20, 18}, {32, 18}, {33, 18}, {34, 18}, {41, 18}, {42, 18}, {43, 18}, {46, 18}, {48, 18}, {53, 18}, {59, 18}, {60, 18}, {69, 18}, {75, 18}, {16, 17}, {17, 17}, {20, 17}, {21, 17}, {22, 17}, {23, 17}, {24, 17}, {26, 17}, {28, 17}, {29, 17}, {30, 17}, {31, 17}, {32, 17}, {34, 17}, {35, 17}, {36, 17}, {37, 17}, {38, 17}, {39, 17}, {40, 17}, {44, 17}, {46, 17}, {48, 17}, {53, 17}, {56, 17}, {58, 17}, {75, 17}, {17, 16}, {18, 16}, {20, 16}, {24, 16}, {26, 16}, {27, 16}, {29, 16}, {33, 16}, {41, 16}, {42, 16}, {44, 16}, {47, 16}, {52, 16}, {57, 16}, {70, 16}, {73, 16}, {74, 16}, {17, 15}, {18, 15}, {20, 15}, {22, 15}, {24, 15}, {27, 15}, {29, 15}, {31, 15}, {33, 15}, {35, 15}, {36, 15}, {38, 15}, {39, 15}, {42, 15}, {45, 15}, {47, 15}, {52, 15}, {53, 15}, {55, 15}, {56, 15}, {70, 15}, {73, 15}, {17, 14}, {19, 14}, {21, 14}, {24, 14}, {26, 14}, {29, 14}, {31, 14}, {34, 14}, {37, 14}, {40, 14}, {42, 14}, {44, 14}, {46, 14}, {47, 14}, {53, 14}, {54, 14}, {55, 14}, {62, 14}, {70, 14}, {72, 14}, {17, 13}, {19, 13}, {21, 13}, {23, 13}, {25, 13}, {27, 13}, {30, 13}, {32, 13}, {34, 13}, {36, 13}, {38, 13}, {41, 13}, {43, 13}, {44, 13}, {45, 13}, {60, 13}, {70, 13}, {71, 13}, {18, 12}, {21, 12}, {23, 12}, {26, 12}, {28, 12}, {31, 12}, {34, 12}, {37, 12}, {39, 12}, {41, 12}, {42, 12}, {70, 12}, {18, 11}, {19, 11}, {20, 11}, {21, 11}, {24, 11}, {25, 11}, {27, 11}, {29, 11}, {31, 11}, {33, 11}, {35, 11}, {38, 11}, {41, 11}, {59, 11}, {26, 10}, {29, 10}, {32, 10}, {34, 10}, {36, 10}, {39, 10}, {40, 10}, {69, 10}, {21, 9}, {26, 9}, {28, 9}, {30, 9}, {32, 9}, {33, 9}, {35, 9}, {36, 9}, {37, 9}, {38, 9}, {39, 9}, {22, 8}, {27, 8}, {28, 8}, {29, 8}, {30, 8}, {31, 8}, {68, 8}, {23, 7}, {66, 7}, {24, 6}, {65, 6}, {25, 5}, {62, 5}, {63, 5}, {26, 4}, {55, 4}, {56, 4}, {57, 4}, {58, 4}, {59, 4}, {60, 4}, {61, 4}, {28, 3}, {53, 3}, {29, 2}, {50, 2}, {51, 2}, {52, 2}, {31, 1}, {32, 1}, {48, 1}
|
---|
1006 | });
|
---|
1007 | tspProblem.BestKnownQuality = new DoubleValue(867);
|
---|
1008 |
|
---|
1009 | tspProblem.EvaluatorParameter.Value = new TSPRoundedEuclideanPathEvaluator();
|
---|
1010 | tspProblem.SolutionCreatorParameter.Value = new RandomPermutationCreator();
|
---|
1011 | tspProblem.UseDistanceMatrix.Value = true;
|
---|
1012 | tspProblem.Name = "Funny TSP";
|
---|
1013 | tspProblem.Description = "Represents a symmetric Traveling Salesman Problem.";
|
---|
1014 | #endregion
|
---|
1015 | #region Algorithm Configuration
|
---|
1016 | vns.Name = "Variable Neighborhood Search - TSP";
|
---|
1017 | vns.Description = "A variable neighborhood search algorithm which solves a funny TSP instance";
|
---|
1018 | vns.Problem = tspProblem;
|
---|
1019 |
|
---|
1020 | var localImprovement = vns.LocalImprovementParameter.ValidValues
|
---|
1021 | .OfType<LocalSearchImprovementOperator>()
|
---|
1022 | .Single();
|
---|
1023 | // move generator has to be set first
|
---|
1024 | localImprovement.MoveGenerator = localImprovement.MoveGeneratorParameter.ValidValues
|
---|
1025 | .OfType<StochasticInversionMultiMoveGenerator>()
|
---|
1026 | .Single();
|
---|
1027 | localImprovement.MoveEvaluator = localImprovement.MoveEvaluatorParameter.ValidValues
|
---|
1028 | .OfType<TSPInversionMoveRoundedEuclideanPathEvaluator>()
|
---|
1029 | .Single();
|
---|
1030 | localImprovement.MoveMaker = localImprovement.MoveMakerParameter.ValidValues
|
---|
1031 | .OfType<InversionMoveMaker>()
|
---|
1032 | .Single();
|
---|
1033 | localImprovement.SampleSizeParameter.Value = new IntValue(500);
|
---|
1034 | vns.LocalImprovement = localImprovement;
|
---|
1035 |
|
---|
1036 | vns.LocalImprovementMaximumIterations = 150;
|
---|
1037 | vns.MaximumIterations = 25;
|
---|
1038 | vns.Seed = 0;
|
---|
1039 | vns.SetSeedRandomly = true;
|
---|
1040 | var shakingOperator = vns.ShakingOperatorParameter.ValidValues
|
---|
1041 | .OfType<PermutationShakingOperator>()
|
---|
1042 | .Single();
|
---|
1043 | shakingOperator.Operators.SetItemCheckedState(shakingOperator.Operators
|
---|
1044 | .OfType<Swap2Manipulator>()
|
---|
1045 | .Single(), false);
|
---|
1046 | shakingOperator.Operators.SetItemCheckedState(shakingOperator.Operators
|
---|
1047 | .OfType<Swap3Manipulator>()
|
---|
1048 | .Single(), false);
|
---|
1049 | vns.ShakingOperator = shakingOperator;
|
---|
1050 | #endregion
|
---|
1051 | vns.Engine = new ParallelEngine.ParallelEngine();
|
---|
1052 | return vns;
|
---|
1053 | }
|
---|
1054 | #endregion
|
---|
1055 | #endregion
|
---|
1056 |
|
---|
1057 | #region Gaussian Process Regression
|
---|
1058 | [TestMethod]
|
---|
1059 | [TestCategory("Samples.Create")]
|
---|
1060 | [TestProperty("Time", "medium")]
|
---|
1061 | public void CreateGaussianProcessRegressionSampleTest() {
|
---|
1062 | var gpr = CreateGaussianProcessRegressionSample();
|
---|
1063 | XmlGenerator.Serialize(gpr, @"Samples\GPR.hl");
|
---|
1064 | }
|
---|
1065 | [TestMethod]
|
---|
1066 | [TestCategory("Samples.Execute")]
|
---|
1067 | [TestProperty("Time", "long")]
|
---|
1068 | public void RunGaussianProcessRegressionSample() {
|
---|
1069 | var gpr = CreateGaussianProcessRegressionSample();
|
---|
1070 | gpr.SetSeedRandomly = false;
|
---|
1071 | gpr.Seed = 1618551877;
|
---|
1072 | RunAlgorithm(gpr);
|
---|
1073 | Assert.AreEqual(-940.48768748097029, GetDoubleResult(gpr, "NegativeLogLikelihood"));
|
---|
1074 | Assert.AreEqual(0.99561947047986976, GetDoubleResult(gpr, "Training R²"));
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | private GaussianProcessRegression CreateGaussianProcessRegressionSample() {
|
---|
1078 | var gpr = new GaussianProcessRegression();
|
---|
1079 | var provider = new VariousInstanceProvider();
|
---|
1080 | var instance = provider.GetDataDescriptors().Where(x => x.Name.Contains("Spatial co-evolution")).Single();
|
---|
1081 | var regProblem = new RegressionProblem();
|
---|
1082 | regProblem.Load(provider.LoadData(instance));
|
---|
1083 | #region Algorithm Configuration
|
---|
1084 | gpr.Name = "Gaussian Process Regression";
|
---|
1085 | gpr.Description = "A Gaussian process regression algorithm which solves the spatial co-evolution benchmark problem";
|
---|
1086 | gpr.Problem = regProblem;
|
---|
1087 |
|
---|
1088 | gpr.CovarianceFunction = new CovarianceSquaredExponentialIso();
|
---|
1089 | gpr.MeanFunction = new MeanConst();
|
---|
1090 | gpr.MinimizationIterations = 20;
|
---|
1091 | gpr.Seed = 0;
|
---|
1092 | gpr.SetSeedRandomly = true;
|
---|
1093 | #endregion
|
---|
1094 | gpr.Engine = new ParallelEngine.ParallelEngine();
|
---|
1095 | return gpr;
|
---|
1096 | }
|
---|
1097 | #endregion
|
---|
1098 |
|
---|
1099 | #region Scatter Search
|
---|
1100 | #region VRP
|
---|
1101 | [TestMethod]
|
---|
1102 | [TestCategory("Samples.Create")]
|
---|
1103 | [TestProperty("Time", "medium")]
|
---|
1104 | public void CreateScatterSearchVRPSampleTest() {
|
---|
1105 | var ss = CreateScatterSearchVRPSample();
|
---|
1106 | XmlGenerator.Serialize(ss, @"Samples\SS_VRP.hl");
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | [TestMethod]
|
---|
1110 | [TestCategory("Samples.Execute")]
|
---|
1111 | [TestProperty("Time", "long")]
|
---|
1112 | public void RunScatterSearchVRPSampleTest() {
|
---|
1113 | var ss = CreateScatterSearchVRPSample();
|
---|
1114 | ss.SetSeedRandomly.Value = false;
|
---|
1115 | RunAlgorithm(ss);
|
---|
1116 | Assert.AreEqual(828.93686694283383, GetDoubleResult(ss, "BestQuality"));
|
---|
1117 | Assert.AreEqual(868.63623986983077, GetDoubleResult(ss, "CurrentAverageQuality"));
|
---|
1118 | Assert.AreEqual(1048.8333559209832, GetDoubleResult(ss, "CurrentWorstQuality"));
|
---|
1119 | Assert.AreEqual(262622, GetIntResult(ss, "EvaluatedSolutions"));
|
---|
1120 | }
|
---|
1121 |
|
---|
1122 | private ScatterSearch CreateScatterSearchVRPSample() {
|
---|
1123 | #region Problem Configuration
|
---|
1124 | var provider = new SolomonInstanceProvider();
|
---|
1125 | var instance = provider.GetDataDescriptors().Single(x => x.Name == "C101");
|
---|
1126 | VehicleRoutingProblem vrpProblem = new VehicleRoutingProblem();
|
---|
1127 | vrpProblem.Load(provider.LoadData(instance));
|
---|
1128 | #endregion
|
---|
1129 |
|
---|
1130 | #region Algorithm Configuration
|
---|
1131 | ScatterSearch ss = new ScatterSearch();
|
---|
1132 | ss.Engine = new SequentialEngine.SequentialEngine();
|
---|
1133 | ss.Name = "Scatter Search - VRP";
|
---|
1134 | ss.Description = "A scatter search algorithm which solves the \"C101\" vehicle routing problem (imported from Solomon)";
|
---|
1135 | ss.Problem = vrpProblem;
|
---|
1136 |
|
---|
1137 | var improver = ss.Problem.Operators.OfType<VRPIntraRouteImprovementOperator>().First();
|
---|
1138 | improver.ImprovementAttemptsParameter.Value.Value = 15;
|
---|
1139 | improver.SampleSizeParameter.Value.Value = 10;
|
---|
1140 | ss.Improver = improver;
|
---|
1141 |
|
---|
1142 | var pathRelinker = ss.Problem.Operators.OfType<VRPPathRelinker>().First();
|
---|
1143 | pathRelinker.IterationsParameter.Value.Value = 25;
|
---|
1144 | ss.PathRelinker = pathRelinker;
|
---|
1145 |
|
---|
1146 | var similarityCalculator = ss.SimilarityCalculatorParameter.ValidValues.OfType<VRPSimilarityCalculator>().First();
|
---|
1147 | ss.SimilarityCalculator = similarityCalculator;
|
---|
1148 |
|
---|
1149 | ss.MaximumIterations.Value = 2;
|
---|
1150 | ss.PopulationSize.Value = 20;
|
---|
1151 | ss.ReferenceSetSize.Value = 10;
|
---|
1152 | ss.Seed.Value = 0;
|
---|
1153 | return ss;
|
---|
1154 | #endregion
|
---|
1155 | }
|
---|
1156 | #endregion
|
---|
1157 | #endregion
|
---|
1158 |
|
---|
1159 | #region RAPGA
|
---|
1160 | #region Scheduling
|
---|
1161 | [TestMethod]
|
---|
1162 | [TestCategory("Samples.Create")]
|
---|
1163 | [TestProperty("Time", "medium")]
|
---|
1164 | public void CreateRAPGASchedulingSampleTest() {
|
---|
1165 | var ss = CreateRAPGASchedulingSample();
|
---|
1166 | XmlGenerator.Serialize(ss, @"Samples\RAPGA_JSSP.hl");
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | [TestMethod]
|
---|
1170 | [TestCategory("Samples.Execute")]
|
---|
1171 | [TestProperty("Time", "long")]
|
---|
1172 | public void RunRAPGASchedulingSampleTest() {
|
---|
1173 | var rapga = CreateRAPGASchedulingSample();
|
---|
1174 | rapga.SetSeedRandomly.Value = false;
|
---|
1175 | RunAlgorithm(rapga);
|
---|
1176 | Assert.AreEqual(988.00, GetDoubleResult(rapga, "BestQuality"));
|
---|
1177 | Assert.AreEqual(988.00, GetDoubleResult(rapga, "CurrentAverageQuality"));
|
---|
1178 | Assert.AreEqual(988.00, GetDoubleResult(rapga, "CurrentWorstQuality"));
|
---|
1179 | Assert.AreEqual(27100, GetIntResult(rapga, "EvaluatedSolutions"));
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | private RAPGA CreateRAPGASchedulingSample() {
|
---|
1183 | #region Problem Configuration
|
---|
1184 | JobShopSchedulingProblem problem = new JobShopSchedulingProblem();
|
---|
1185 | #endregion
|
---|
1186 |
|
---|
1187 | #region Algorithm Configuration
|
---|
1188 | RAPGA rapga = new RAPGA();
|
---|
1189 | rapga.Engine = new SequentialEngine.SequentialEngine();
|
---|
1190 | rapga.Name = "RAPGA - Job Shop Scheduling";
|
---|
1191 | rapga.Description = "A relevant alleles preserving genetic algorithm which solves a job shop scheduling problem";
|
---|
1192 | rapga.Problem = problem;
|
---|
1193 | rapga.Mutator = rapga.MutatorParameter.ValidValues.OfType<JSMSwapManipulator>().First();
|
---|
1194 | rapga.Seed.Value = 0;
|
---|
1195 | return rapga;
|
---|
1196 | #endregion
|
---|
1197 | }
|
---|
1198 | #endregion
|
---|
1199 | #endregion
|
---|
1200 |
|
---|
1201 | #region grammatical evolution
|
---|
1202 | #region artificial ant
|
---|
1203 | [TestMethod]
|
---|
1204 | [TestCategory("Samples.Create")]
|
---|
1205 | [TestProperty("Time", "medium")]
|
---|
1206 | public void CreateGeArtificialAntSampleTest() {
|
---|
1207 | var geaa = CreateGeArtificialAntSample();
|
---|
1208 | XmlGenerator.Serialize(geaa, @"Samples\GE_ArtificialAnt.hl");
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | [TestMethod]
|
---|
1212 | [TestCategory("Samples.Execute")]
|
---|
1213 | [TestProperty("Time", "long")]
|
---|
1214 | public void RunGeArtificalAntSampleTest() {
|
---|
1215 | var ga = CreateGeArtificialAntSample();
|
---|
1216 | ga.SetSeedRandomly.Value = false;
|
---|
1217 | RunAlgorithm(ga);
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | public OffspringSelectionGeneticAlgorithm CreateGeArtificialAntSample() {
|
---|
1221 | OffspringSelectionGeneticAlgorithm ga = new OffspringSelectionGeneticAlgorithm();
|
---|
1222 | #region Problem Configuration
|
---|
1223 | var problem = new HeuristicLab.Problems.GrammaticalEvolution.GEArtificialAntProblem();
|
---|
1224 | #endregion
|
---|
1225 | #region Algorithm Configuration
|
---|
1226 | ga.Name = "Grammatical Evolution - Artificial Ant (SantaFe)";
|
---|
1227 | ga.Description = "Grammatical evolution algorithm for solving a artificial ant problem";
|
---|
1228 | ga.Problem = problem;
|
---|
1229 | ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, Encodings.IntegerVectorEncoding.SinglePointCrossover, Encodings.IntegerVectorEncoding.UniformOnePositionManipulator>(
|
---|
1230 | ga, 200, 1, 50, 0.05, 200);
|
---|
1231 | #endregion
|
---|
1232 | return ga;
|
---|
1233 | }
|
---|
1234 | #endregion
|
---|
1235 |
|
---|
1236 | #region symbolic regression
|
---|
1237 | #endregion
|
---|
1238 | [TestMethod]
|
---|
1239 | [TestCategory("Samples.Create")]
|
---|
1240 | [TestProperty("Time", "medium")]
|
---|
1241 | public void CreateGeSymbolicRegressionSampleTest() {
|
---|
1242 | var geSymbReg = CreateGeSymbolicRegressionSample();
|
---|
1243 | XmlGenerator.Serialize(geSymbReg, @"Samples\GE_SymbReg.hl");
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | [TestMethod]
|
---|
1247 | [TestCategory("Samples.Execute")]
|
---|
1248 | [TestProperty("Time", "long")]
|
---|
1249 | public void RunGeSymbolicRegressionSampleTest() {
|
---|
1250 | var ga = CreateGeSymbolicRegressionSample();
|
---|
1251 | ga.SetSeedRandomly.Value = false;
|
---|
1252 | RunAlgorithm(ga);
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | public OffspringSelectionGeneticAlgorithm CreateGeSymbolicRegressionSample() {
|
---|
1256 | var ga = new OffspringSelectionGeneticAlgorithm();
|
---|
1257 | #region Problem Configuration
|
---|
1258 | var problem = new HeuristicLab.Problems.GrammaticalEvolution.GESymbolicRegressionSingleObjectiveProblem();
|
---|
1259 |
|
---|
1260 | #endregion
|
---|
1261 | #region Algorithm Configuration
|
---|
1262 | ga.Name = "Grammatical Evolution - Symbolic Regression (Poly-10)";
|
---|
1263 | ga.Description = "Grammatical evolution algorithm for solving a symbolic regression problem problem";
|
---|
1264 | ga.Problem = problem;
|
---|
1265 | problem.Load(new PolyTen().GenerateRegressionData());
|
---|
1266 |
|
---|
1267 | // must occur after loading problem data because the grammar creates symbols for random constants once the data is loaded
|
---|
1268 | var consts = problem.SymbolicExpressionTreeGrammar.AllowedSymbols.OfType<Constant>().ToList();
|
---|
1269 | foreach (var c in consts) {
|
---|
1270 | problem.SymbolicExpressionTreeGrammar.RemoveSymbol(c);
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, Encodings.IntegerVectorEncoding.SinglePointCrossover, Encodings.IntegerVectorEncoding.UniformOnePositionManipulator>(
|
---|
1274 | ga, 1000, 1, 50, 0.05, 200);
|
---|
1275 | #endregion
|
---|
1276 | return ga;
|
---|
1277 | }
|
---|
1278 | #endregion
|
---|
1279 |
|
---|
1280 | #region Helpers
|
---|
1281 | private void ConfigureEvolutionStrategyParameters<R, M, SC, SR, SM>(EvolutionStrategy es, int popSize, int children, int parentsPerChild, int maxGens, bool plusSelection)
|
---|
1282 | where R : ICrossover
|
---|
1283 | where M : IManipulator
|
---|
1284 | where SC : IStrategyParameterCreator
|
---|
1285 | where SR : IStrategyParameterCrossover
|
---|
1286 | where SM : IStrategyParameterManipulator {
|
---|
1287 | es.PopulationSize.Value = popSize;
|
---|
1288 | es.Children.Value = children;
|
---|
1289 | es.ParentsPerChild.Value = parentsPerChild;
|
---|
1290 | es.MaximumGenerations.Value = maxGens;
|
---|
1291 | es.PlusSelection.Value = false;
|
---|
1292 |
|
---|
1293 | es.Seed.Value = 0;
|
---|
1294 | es.SetSeedRandomly.Value = true;
|
---|
1295 |
|
---|
1296 | es.Recombinator = es.RecombinatorParameter.ValidValues
|
---|
1297 | .OfType<R>()
|
---|
1298 | .Single();
|
---|
1299 |
|
---|
1300 | es.Mutator = es.MutatorParameter.ValidValues
|
---|
1301 | .OfType<M>()
|
---|
1302 | .Single();
|
---|
1303 |
|
---|
1304 | es.StrategyParameterCreator = es.StrategyParameterCreatorParameter.ValidValues
|
---|
1305 | .OfType<SC>()
|
---|
1306 | .Single();
|
---|
1307 | es.StrategyParameterCrossover = es.StrategyParameterCrossoverParameter.ValidValues
|
---|
1308 | .OfType<SR>()
|
---|
1309 | .Single();
|
---|
1310 | es.StrategyParameterManipulator = es.StrategyParameterManipulatorParameter.ValidValues
|
---|
1311 | .OfType<SM>()
|
---|
1312 | .Single();
|
---|
1313 | es.Engine = new ParallelEngine.ParallelEngine();
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | private void ConfigureGeneticAlgorithmParameters<S, C, M>(GeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int tournGroupSize = 0)
|
---|
1317 | where S : ISelector
|
---|
1318 | where C : ICrossover
|
---|
1319 | where M : IManipulator {
|
---|
1320 | ga.Elites.Value = elites;
|
---|
1321 | ga.MaximumGenerations.Value = maxGens;
|
---|
1322 | ga.MutationProbability.Value = mutationRate;
|
---|
1323 | ga.PopulationSize.Value = popSize;
|
---|
1324 | ga.Seed.Value = 0;
|
---|
1325 | ga.SetSeedRandomly.Value = true;
|
---|
1326 | ga.Selector = ga.SelectorParameter.ValidValues
|
---|
1327 | .OfType<S>()
|
---|
1328 | .First();
|
---|
1329 |
|
---|
1330 | ga.Crossover = ga.CrossoverParameter.ValidValues
|
---|
1331 | .OfType<C>()
|
---|
1332 | .First();
|
---|
1333 |
|
---|
1334 | ga.Mutator = ga.MutatorParameter.ValidValues
|
---|
1335 | .OfType<M>()
|
---|
1336 | .First();
|
---|
1337 |
|
---|
1338 | var tSelector = ga.Selector as TournamentSelector;
|
---|
1339 | if (tSelector != null) {
|
---|
1340 | tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
|
---|
1341 | }
|
---|
1342 | ga.Engine = new ParallelEngine.ParallelEngine();
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | private void ConfigureOsGeneticAlgorithmParameters<S, C, M>(OffspringSelectionGeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate=0.05, double maxSelPres=100, int tournGroupSize = 0)
|
---|
1346 | where S : ISelector
|
---|
1347 | where C : ICrossover
|
---|
1348 | where M : IManipulator {
|
---|
1349 | ga.Elites.Value = elites;
|
---|
1350 | ga.MaximumGenerations.Value = maxGens;
|
---|
1351 | ga.MutationProbability.Value = mutationRate;
|
---|
1352 | ga.PopulationSize.Value = popSize;
|
---|
1353 | ga.MaximumSelectionPressure.Value = maxSelPres;
|
---|
1354 | ga.Seed.Value = 0;
|
---|
1355 | ga.SetSeedRandomly.Value = true;
|
---|
1356 | ga.ComparisonFactorLowerBound.Value = 1;
|
---|
1357 | ga.ComparisonFactorUpperBound.Value = 1;
|
---|
1358 |
|
---|
1359 | ga.Selector = ga.SelectorParameter.ValidValues
|
---|
1360 | .OfType<S>()
|
---|
1361 | .First();
|
---|
1362 |
|
---|
1363 | ga.Crossover = ga.CrossoverParameter.ValidValues
|
---|
1364 | .OfType<C>()
|
---|
1365 | .First();
|
---|
1366 |
|
---|
1367 | ga.Mutator = ga.MutatorParameter.ValidValues
|
---|
1368 | .OfType<M>()
|
---|
1369 | .First();
|
---|
1370 |
|
---|
1371 | var tSelector = ga.Selector as TournamentSelector;
|
---|
1372 | if (tSelector != null) {
|
---|
1373 | tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
|
---|
1374 | }
|
---|
1375 | ga.Engine = new ParallelEngine.ParallelEngine();
|
---|
1376 | }
|
---|
1377 |
|
---|
1378 | private void ConfigureIslandGeneticAlgorithmParameters<S, C, M, Mi, MiS, MiR>(IslandGeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int numberOfIslands, int migrationInterval, double migrationRate)
|
---|
1379 | where S : ISelector
|
---|
1380 | where C : ICrossover
|
---|
1381 | where M : IManipulator
|
---|
1382 | where Mi : IMigrator
|
---|
1383 | where MiS : ISelector
|
---|
1384 | where MiR : IReplacer {
|
---|
1385 | ga.Elites.Value = elites;
|
---|
1386 | ga.MaximumGenerations.Value = maxGens;
|
---|
1387 | ga.MutationProbability.Value = mutationRate;
|
---|
1388 | ga.PopulationSize.Value = popSize;
|
---|
1389 | ga.NumberOfIslands.Value = numberOfIslands;
|
---|
1390 | ga.MigrationInterval.Value = migrationInterval;
|
---|
1391 | ga.MigrationRate.Value = migrationRate;
|
---|
1392 | ga.Seed.Value = 0;
|
---|
1393 | ga.SetSeedRandomly.Value = true;
|
---|
1394 | ga.Selector = ga.SelectorParameter.ValidValues
|
---|
1395 | .OfType<S>()
|
---|
1396 | .Single();
|
---|
1397 |
|
---|
1398 | ga.Crossover = ga.CrossoverParameter.ValidValues
|
---|
1399 | .OfType<C>()
|
---|
1400 | .Single();
|
---|
1401 |
|
---|
1402 | ga.Mutator = ga.MutatorParameter.ValidValues
|
---|
1403 | .OfType<M>()
|
---|
1404 | .Single();
|
---|
1405 | ga.Migrator = ga.MigratorParameter.ValidValues
|
---|
1406 | .OfType<Mi>()
|
---|
1407 | .Single();
|
---|
1408 | ga.EmigrantsSelector = ga.EmigrantsSelectorParameter.ValidValues
|
---|
1409 | .OfType<MiS>()
|
---|
1410 | .Single();
|
---|
1411 | ga.ImmigrationReplacer = ga.ImmigrationReplacerParameter.ValidValues
|
---|
1412 | .OfType<MiR>()
|
---|
1413 | .Single();
|
---|
1414 | ga.Engine = new ParallelEngine.ParallelEngine();
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 |
|
---|
1418 | private void RunAlgorithm(IAlgorithm a) {
|
---|
1419 | var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
|
---|
1420 | Exception ex = null;
|
---|
1421 | a.Stopped += (src, e) => { trigger.Set(); };
|
---|
1422 | a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
|
---|
1423 | a.Prepare();
|
---|
1424 | a.Start();
|
---|
1425 | trigger.WaitOne();
|
---|
1426 |
|
---|
1427 | Assert.AreEqual(ex, null);
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | private double GetDoubleResult(IAlgorithm a, string resultName) {
|
---|
1431 | return ((DoubleValue)a.Results[resultName].Value).Value;
|
---|
1432 | }
|
---|
1433 | private int GetIntResult(IAlgorithm a, string resultName) {
|
---|
1434 | return ((IntValue)a.Results[resultName].Value).Value;
|
---|
1435 | }
|
---|
1436 | #endregion
|
---|
1437 | }
|
---|
1438 | }
|
---|