1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Algorithms.EvolutionStrategy;
|
---|
26 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
27 | using HeuristicLab.Algorithms.LocalSearch;
|
---|
28 | using HeuristicLab.Algorithms.ParticleSwarmOptimization;
|
---|
29 | using HeuristicLab.Algorithms.SimulatedAnnealing;
|
---|
30 | using HeuristicLab.Algorithms.TabuSearch;
|
---|
31 | using HeuristicLab.Algorithms.VariableNeighborhoodSearch;
|
---|
32 | using HeuristicLab.Data;
|
---|
33 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
34 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
35 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
36 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
37 | using HeuristicLab.Optimization;
|
---|
38 | using HeuristicLab.Optimization.Operators;
|
---|
39 | using HeuristicLab.ParallelEngine;
|
---|
40 | using HeuristicLab.Persistence.Default.Xml;
|
---|
41 | using HeuristicLab.Problems.ArtificialAnt;
|
---|
42 | using HeuristicLab.Problems.DataAnalysis;
|
---|
43 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
44 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
|
---|
45 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
46 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
47 | using HeuristicLab.Problems.Instances.TSPLIB;
|
---|
48 | using HeuristicLab.Problems.Knapsack;
|
---|
49 | using HeuristicLab.Problems.TestFunctions;
|
---|
50 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
51 | using HeuristicLab.Problems.VehicleRouting;
|
---|
52 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
53 | using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
|
---|
54 | using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
|
---|
55 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
56 | using HeuristicLab.Selection;
|
---|
57 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
58 | using HeuristicLab.Problems.Instances.VehicleRouting;
|
---|
59 | using HeuristicLab.Problems.Instances;
|
---|
60 |
|
---|
61 |
|
---|
62 | namespace HeuristicLab_33.Tests {
|
---|
63 | [TestClass]
|
---|
64 | [DeploymentItem(@"HeuristicLab-3.3/Resources/C101.opt.txt")]
|
---|
65 | [DeploymentItem(@"HeuristicLab-3.3/Resources/C101.txt")]
|
---|
66 | public class SamplesTest {
|
---|
67 | #region GA
|
---|
68 | #region TSP
|
---|
69 | [TestMethod]
|
---|
70 | public void CreateGaTspSampleTest() {
|
---|
71 | var ga = CreateGaTspSample();
|
---|
72 | XmlGenerator.Serialize(ga, "../../GA_TSP.hl");
|
---|
73 | }
|
---|
74 | [TestMethod]
|
---|
75 | public void RunGaTspSampleTest() {
|
---|
76 | var ga = CreateGaTspSample();
|
---|
77 | ga.SetSeedRandomly.Value = false;
|
---|
78 | RunAlgorithm(ga);
|
---|
79 | Assert.AreEqual(12332, GetDoubleResult(ga, "BestQuality"));
|
---|
80 | Assert.AreEqual(13123.2, GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
81 | Assert.AreEqual(14538, GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
82 | Assert.AreEqual(99100, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
83 | }
|
---|
84 |
|
---|
85 | private GeneticAlgorithm CreateGaTspSample() {
|
---|
86 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
87 | #region Problem Configuration
|
---|
88 | var provider = new TSPLIBTSPInstanceProvider();
|
---|
89 | var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single();
|
---|
90 | TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
|
---|
91 | tspProblem.Load(provider.LoadData(instance));
|
---|
92 | tspProblem.UseDistanceMatrix.Value = true;
|
---|
93 | #endregion
|
---|
94 | #region Algorithm Configuration
|
---|
95 | ga.Name = "Genetic Algorithm - TSP";
|
---|
96 | ga.Description = "A genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
|
---|
97 | ga.Problem = tspProblem;
|
---|
98 | ConfigureGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator>(
|
---|
99 | ga, 100, 1, 1000, 0.05);
|
---|
100 |
|
---|
101 | ga.Analyzer.Operators.SetItemCheckedState(ga.Analyzer.Operators
|
---|
102 | .OfType<TSPAlleleFrequencyAnalyzer>()
|
---|
103 | .Single(), false);
|
---|
104 | ga.Analyzer.Operators.SetItemCheckedState(ga.Analyzer.Operators
|
---|
105 | .OfType<TSPPopulationDiversityAnalyzer>()
|
---|
106 | .Single(), false);
|
---|
107 | #endregion
|
---|
108 | return ga;
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 | #region VRP
|
---|
112 | [TestMethod]
|
---|
113 | public void CreateGaVrpSampleTest() {
|
---|
114 | var ga = CreateGaVrpSample();
|
---|
115 | XmlGenerator.Serialize(ga, "../../GA_VRP.hl");
|
---|
116 | }
|
---|
117 |
|
---|
118 | [TestMethod]
|
---|
119 | public void RunGaVrpSampleTest() {
|
---|
120 | var ga = CreateGaVrpSample();
|
---|
121 | ga.SetSeedRandomly.Value = false;
|
---|
122 | RunAlgorithm(ga);
|
---|
123 | Assert.AreEqual(1828.9368669428338, GetDoubleResult(ga, "BestQuality"));
|
---|
124 | Assert.AreEqual(1830.1444308908331, GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
125 | Assert.AreEqual(1871.7128510304112, GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
126 | Assert.AreEqual(99100, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
127 | }
|
---|
128 |
|
---|
129 | private GeneticAlgorithm CreateGaVrpSample() {
|
---|
130 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
131 | #region Problem Configuration
|
---|
132 | VehicleRoutingProblem vrpProblem = new VehicleRoutingProblem();
|
---|
133 |
|
---|
134 | SolomonFormatInstanceProvider instanceProvider = new SolomonInstanceProvider();
|
---|
135 | IVRPData data = instanceProvider.LoadData("C101.txt", "C101.opt.txt");
|
---|
136 | vrpProblem.Load(data);
|
---|
137 | vrpProblem.Name = "C101 VRP (imported from Solomon)";
|
---|
138 | vrpProblem.Description = "Represents a Vehicle Routing Problem.";
|
---|
139 | CVRPTWProblemInstance instance = vrpProblem.ProblemInstance as CVRPTWProblemInstance;
|
---|
140 | instance.DistanceFactor.Value = 1;
|
---|
141 | instance.FleetUsageFactor.Value = 100;
|
---|
142 | instance.OverloadPenalty.Value = 100;
|
---|
143 | instance.TardinessPenalty.Value = 100;
|
---|
144 | instance.TimeFactor.Value = 0;
|
---|
145 | vrpProblem.MaximizationParameter.Value.Value = false;
|
---|
146 | instance.UseDistanceMatrix.Value = true;
|
---|
147 | instance.Vehicles.Value = 25;
|
---|
148 | #endregion
|
---|
149 | #region Algorithm Configuration
|
---|
150 | ga.Name = "Genetic Algorithm - VRP";
|
---|
151 | ga.Description = "A genetic algorithm which solves the \"C101\" vehicle routing problem (imported from Solomon)";
|
---|
152 | ga.Problem = vrpProblem;
|
---|
153 | ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiVRPSolutionCrossover, MultiVRPSolutionManipulator>(
|
---|
154 | ga, 100, 1, 1000, 0.05, 3);
|
---|
155 |
|
---|
156 | var xOver = (MultiVRPSolutionCrossover)ga.Crossover;
|
---|
157 | foreach (var op in xOver.Operators) {
|
---|
158 | xOver.Operators.SetItemCheckedState(op, false);
|
---|
159 | }
|
---|
160 | xOver.Operators.SetItemCheckedState(xOver.Operators
|
---|
161 | .OfType<PotvinRouteBasedCrossover>()
|
---|
162 | .Single(), true);
|
---|
163 | xOver.Operators.SetItemCheckedState(xOver.Operators
|
---|
164 | .OfType<PotvinSequenceBasedCrossover>()
|
---|
165 | .Single(), true);
|
---|
166 |
|
---|
167 | var manipulator = (MultiVRPSolutionManipulator)ga.Mutator;
|
---|
168 | foreach (var op in manipulator.Operators) {
|
---|
169 | manipulator.Operators.SetItemCheckedState(op, false);
|
---|
170 | }
|
---|
171 | manipulator.Operators.SetItemCheckedState(manipulator.Operators
|
---|
172 | .OfType<PotvinOneLevelExchangeMainpulator>()
|
---|
173 | .Single(), true);
|
---|
174 | manipulator.Operators.SetItemCheckedState(manipulator.Operators
|
---|
175 | .OfType<PotvinTwoLevelExchangeManipulator>()
|
---|
176 | .Single(), true);
|
---|
177 | #endregion
|
---|
178 | return ga;
|
---|
179 | }
|
---|
180 | #endregion
|
---|
181 | #region ArtificialAnt
|
---|
182 | [TestMethod]
|
---|
183 | public void CreateGpArtificialAntSampleTest() {
|
---|
184 | var ga = CreateGpArtificialAntSample();
|
---|
185 | XmlGenerator.Serialize(ga, "../../SGP_SantaFe.hl");
|
---|
186 | }
|
---|
187 |
|
---|
188 | [TestMethod]
|
---|
189 | public void RunGpArtificialAntSampleTest() {
|
---|
190 | var ga = CreateGpArtificialAntSample();
|
---|
191 | ga.SetSeedRandomly.Value = false;
|
---|
192 | RunAlgorithm(ga);
|
---|
193 | Assert.AreEqual(67, GetDoubleResult(ga, "BestQuality"));
|
---|
194 | Assert.AreEqual(45.813, GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
195 | Assert.AreEqual(0, GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
196 | Assert.AreEqual(50950, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
197 | }
|
---|
198 |
|
---|
199 | public GeneticAlgorithm CreateGpArtificialAntSample() {
|
---|
200 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
201 | #region Problem Configuration
|
---|
202 | ArtificialAntProblem antProblem = new ArtificialAntProblem();
|
---|
203 | antProblem.BestKnownQuality.Value = 89;
|
---|
204 | antProblem.MaxExpressionDepth.Value = 10;
|
---|
205 | antProblem.MaxExpressionLength.Value = 100;
|
---|
206 | antProblem.MaxFunctionArguments.Value = 3;
|
---|
207 | antProblem.MaxFunctionDefinitions.Value = 3;
|
---|
208 | antProblem.MaxTimeSteps.Value = 600;
|
---|
209 | #endregion
|
---|
210 | #region Algorithm Configuration
|
---|
211 | ga.Name = "Genetic Programming - Artificial Ant";
|
---|
212 | ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
|
---|
213 | ga.Problem = antProblem;
|
---|
214 | ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
|
---|
215 | ga, 1000, 1, 50, 0.15, 5);
|
---|
216 | var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
|
---|
217 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
218 | .OfType<FullTreeShaker>()
|
---|
219 | .Single(), false);
|
---|
220 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
221 | .OfType<OnePointShaker>()
|
---|
222 | .Single(), false);
|
---|
223 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
224 | .OfType<ArgumentDeleter>()
|
---|
225 | .Single(), false);
|
---|
226 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
227 | .OfType<SubroutineDeleter>()
|
---|
228 | .Single(), false);
|
---|
229 | #endregion
|
---|
230 | return ga;
|
---|
231 | }
|
---|
232 | #endregion
|
---|
233 | #region Symbolic Regression
|
---|
234 | [TestMethod]
|
---|
235 | public void CreateGpSymbolicRegressionSampleTest() {
|
---|
236 | var ga = CreateGpSymbolicRegressionSample();
|
---|
237 | XmlGenerator.Serialize(ga, "../../SGP_SymbReg.hl");
|
---|
238 | }
|
---|
239 | [TestMethod]
|
---|
240 | public void RunGpSymbolicRegressionSampleTest() {
|
---|
241 | var ga = CreateGpSymbolicRegressionSample();
|
---|
242 | ga.SetSeedRandomly.Value = false;
|
---|
243 | RunAlgorithm(ga);
|
---|
244 | Assert.AreEqual(0.78855594192122458, GetDoubleResult(ga, "BestQuality"), 1E-8);
|
---|
245 | Assert.AreEqual(0.61395271071681523, GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
|
---|
246 | Assert.AreEqual(0, GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
|
---|
247 | Assert.AreEqual(50950, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
248 | }
|
---|
249 |
|
---|
250 | private GeneticAlgorithm CreateGpSymbolicRegressionSample() {
|
---|
251 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
252 | #region Problem Configuration
|
---|
253 | SymbolicRegressionSingleObjectiveProblem symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
|
---|
254 | symbRegProblem.Name = "Tower Symbolic Regression Problem";
|
---|
255 | symbRegProblem.Description = "Tower Dataset (downloaded from: http://vanillamodeling.com/realproblems.html)";
|
---|
256 | RegressionRealWorldInstanceProvider provider = new RegressionRealWorldInstanceProvider();
|
---|
257 | var instance = provider.GetDataDescriptors().Where(x => x.Name.Equals("TowerData")).Single();
|
---|
258 | var towerProblemData = (RegressionProblemData)provider.LoadData(instance);
|
---|
259 | towerProblemData.TargetVariableParameter.Value = towerProblemData.TargetVariableParameter.ValidValues
|
---|
260 | .First(v => v.Value == "towerResponse");
|
---|
261 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
262 | towerProblemData.InputVariables.Single(x => x.Value == "x1"), true);
|
---|
263 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
264 | towerProblemData.InputVariables.Single(x => x.Value == "x7"), false);
|
---|
265 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
266 | towerProblemData.InputVariables.Single(x => x.Value == "x11"), false);
|
---|
267 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
268 | towerProblemData.InputVariables.Single(x => x.Value == "x16"), false);
|
---|
269 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
270 | towerProblemData.InputVariables.Single(x => x.Value == "x21"), false);
|
---|
271 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
272 | towerProblemData.InputVariables.Single(x => x.Value == "x25"), false);
|
---|
273 | towerProblemData.InputVariables.SetItemCheckedState(
|
---|
274 | towerProblemData.InputVariables.Single(x => x.Value == "towerResponse"), false);
|
---|
275 | towerProblemData.TrainingPartition.Start = 0;
|
---|
276 | towerProblemData.TrainingPartition.End = 4000;
|
---|
277 | towerProblemData.TestPartition.Start = 4000;
|
---|
278 | towerProblemData.TestPartition.End = 4999;
|
---|
279 | towerProblemData.Name = "Data imported from towerData.txt";
|
---|
280 | 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";
|
---|
281 | symbRegProblem.ProblemData = towerProblemData;
|
---|
282 |
|
---|
283 | // configure grammar
|
---|
284 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
285 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
286 | grammar.Symbols.OfType<VariableCondition>().Single().InitialFrequency = 0.0;
|
---|
287 | var varSymbol = grammar.Symbols.OfType<Variable>().Where(x => !(x is LaggedVariable)).Single();
|
---|
288 | varSymbol.WeightMu = 1.0;
|
---|
289 | varSymbol.WeightSigma = 1.0;
|
---|
290 | varSymbol.WeightManipulatorMu = 0.0;
|
---|
291 | varSymbol.WeightManipulatorSigma = 0.05;
|
---|
292 | varSymbol.MultiplicativeWeightManipulatorSigma = 0.03;
|
---|
293 | var constSymbol = grammar.Symbols.OfType<Constant>().Single();
|
---|
294 | constSymbol.MaxValue = 20;
|
---|
295 | constSymbol.MinValue = -20;
|
---|
296 | constSymbol.ManipulatorMu = 0.0;
|
---|
297 | constSymbol.ManipulatorSigma = 1;
|
---|
298 | constSymbol.MultiplicativeManipulatorSigma = 0.03;
|
---|
299 | symbRegProblem.SymbolicExpressionTreeGrammar = grammar;
|
---|
300 |
|
---|
301 | // configure remaining problem parameters
|
---|
302 | symbRegProblem.BestKnownQuality.Value = 0.97;
|
---|
303 | symbRegProblem.FitnessCalculationPartition.Start = 0;
|
---|
304 | symbRegProblem.FitnessCalculationPartition.End = 2800;
|
---|
305 | symbRegProblem.ValidationPartition.Start = 2800;
|
---|
306 | symbRegProblem.ValidationPartition.End = 4000;
|
---|
307 | symbRegProblem.RelativeNumberOfEvaluatedSamples.Value = 1;
|
---|
308 | symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 150;
|
---|
309 | symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 12;
|
---|
310 | symbRegProblem.MaximumFunctionDefinitions.Value = 0;
|
---|
311 | symbRegProblem.MaximumFunctionArguments.Value = 0;
|
---|
312 |
|
---|
313 | symbRegProblem.EvaluatorParameter.Value = new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator();
|
---|
314 | #endregion
|
---|
315 | #region Algorithm Configuration
|
---|
316 | ga.Problem = symbRegProblem;
|
---|
317 | ga.Name = "Genetic Programming - Symbolic Regression";
|
---|
318 | ga.Description = "A standard genetic programming algorithm to solve a symbolic regression problem (tower dataset)";
|
---|
319 | ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
|
---|
320 | ga, 1000, 1, 50, 0.15, 5);
|
---|
321 | var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
|
---|
322 | mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
|
---|
323 | mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
|
---|
324 |
|
---|
325 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
326 | ga.Analyzer.Operators
|
---|
327 | .OfType<SymbolicRegressionSingleObjectiveOverfittingAnalyzer>()
|
---|
328 | .Single(), false);
|
---|
329 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
330 | ga.Analyzer.Operators
|
---|
331 | .OfType<SymbolicDataAnalysisAlleleFrequencyAnalyzer>()
|
---|
332 | .First(), false);
|
---|
333 | #endregion
|
---|
334 | return ga;
|
---|
335 | }
|
---|
336 | #endregion
|
---|
337 | #region Symbolic Classification
|
---|
338 | [TestMethod]
|
---|
339 | public void CreateGpSymbolicClassificationSampleTest() {
|
---|
340 | var ga = CreateGpSymbolicClassificationSample();
|
---|
341 | XmlGenerator.Serialize(ga, "../../SGP_SymbClass.hl");
|
---|
342 | }
|
---|
343 |
|
---|
344 | [TestMethod]
|
---|
345 | public void RunGpSymbolicClassificationSampleTest() {
|
---|
346 | var ga = CreateGpSymbolicClassificationSample();
|
---|
347 | ga.SetSeedRandomly.Value = false;
|
---|
348 | RunAlgorithm(ga);
|
---|
349 | Assert.AreEqual(0.13775264138895371, GetDoubleResult(ga, "BestQuality"), 1E-8);
|
---|
350 | Assert.AreEqual(14.232802217120254, GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
|
---|
351 | Assert.AreEqual(104.24339008411457, GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
|
---|
352 | Assert.AreEqual(100900, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
353 | }
|
---|
354 |
|
---|
355 | private GeneticAlgorithm CreateGpSymbolicClassificationSample() {
|
---|
356 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
357 | #region Problem Configuration
|
---|
358 | SymbolicClassificationSingleObjectiveProblem symbClassProblem = new SymbolicClassificationSingleObjectiveProblem();
|
---|
359 | symbClassProblem.Name = "Mammography Classification Problem";
|
---|
360 | symbClassProblem.Description = "Mammography dataset imported from the UCI machine learning repository (http://archive.ics.uci.edu/ml/datasets/Mammographic+Mass)";
|
---|
361 | ClassificationRealWorldInstanceProvider provider = new ClassificationRealWorldInstanceProvider();
|
---|
362 | var instance = provider.GetDataDescriptors().Where(x => x.Name.Equals("Mammography")).Single();
|
---|
363 | var mammoData = (ClassificationProblemData)provider.LoadData(instance);
|
---|
364 | mammoData.TargetVariableParameter.Value = mammoData.TargetVariableParameter.ValidValues
|
---|
365 | .First(v => v.Value == "Severity");
|
---|
366 | mammoData.InputVariables.SetItemCheckedState(
|
---|
367 | mammoData.InputVariables.Single(x => x.Value == "BI-RADS"), false);
|
---|
368 | mammoData.InputVariables.SetItemCheckedState(
|
---|
369 | mammoData.InputVariables.Single(x => x.Value == "Age"), true);
|
---|
370 | mammoData.InputVariables.SetItemCheckedState(
|
---|
371 | mammoData.InputVariables.Single(x => x.Value == "Shape"), true);
|
---|
372 | mammoData.InputVariables.SetItemCheckedState(
|
---|
373 | mammoData.InputVariables.Single(x => x.Value == "Margin"), true);
|
---|
374 | mammoData.InputVariables.SetItemCheckedState(
|
---|
375 | mammoData.InputVariables.Single(x => x.Value == "Density"), true);
|
---|
376 | mammoData.InputVariables.SetItemCheckedState(
|
---|
377 | mammoData.InputVariables.Single(x => x.Value == "Severity"), false);
|
---|
378 | mammoData.TrainingPartition.Start = 0;
|
---|
379 | mammoData.TrainingPartition.End = 800;
|
---|
380 | mammoData.TestPartition.Start = 800;
|
---|
381 | mammoData.TestPartition.End = 961;
|
---|
382 | mammoData.Name = "Data imported from mammographic_masses.csv";
|
---|
383 | mammoData.Description = "Original dataset: http://archive.ics.uci.edu/ml/datasets/Mammographic+Mass, missing values have been replaced with median values.";
|
---|
384 | symbClassProblem.ProblemData = mammoData;
|
---|
385 |
|
---|
386 | // configure grammar
|
---|
387 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
388 | grammar.ConfigureAsDefaultClassificationGrammar();
|
---|
389 | grammar.Symbols.OfType<VariableCondition>().Single().Enabled = false;
|
---|
390 | var varSymbol = grammar.Symbols.OfType<Variable>().Where(x => !(x is LaggedVariable)).Single();
|
---|
391 | varSymbol.WeightMu = 1.0;
|
---|
392 | varSymbol.WeightSigma = 1.0;
|
---|
393 | varSymbol.WeightManipulatorMu = 0.0;
|
---|
394 | varSymbol.WeightManipulatorSigma = 0.05;
|
---|
395 | varSymbol.MultiplicativeWeightManipulatorSigma = 0.03;
|
---|
396 | var constSymbol = grammar.Symbols.OfType<Constant>().Single();
|
---|
397 | constSymbol.MaxValue = 20;
|
---|
398 | constSymbol.MinValue = -20;
|
---|
399 | constSymbol.ManipulatorMu = 0.0;
|
---|
400 | constSymbol.ManipulatorSigma = 1;
|
---|
401 | constSymbol.MultiplicativeManipulatorSigma = 0.03;
|
---|
402 | symbClassProblem.SymbolicExpressionTreeGrammar = grammar;
|
---|
403 |
|
---|
404 | // configure remaining problem parameters
|
---|
405 | symbClassProblem.BestKnownQuality.Value = 0.0;
|
---|
406 | symbClassProblem.FitnessCalculationPartition.Start = 0;
|
---|
407 | symbClassProblem.FitnessCalculationPartition.End = 400;
|
---|
408 | symbClassProblem.ValidationPartition.Start = 400;
|
---|
409 | symbClassProblem.ValidationPartition.End = 800;
|
---|
410 | symbClassProblem.RelativeNumberOfEvaluatedSamples.Value = 1;
|
---|
411 | symbClassProblem.MaximumSymbolicExpressionTreeLength.Value = 100;
|
---|
412 | symbClassProblem.MaximumSymbolicExpressionTreeDepth.Value = 10;
|
---|
413 | symbClassProblem.MaximumFunctionDefinitions.Value = 0;
|
---|
414 | symbClassProblem.MaximumFunctionArguments.Value = 0;
|
---|
415 | symbClassProblem.EvaluatorParameter.Value = new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator();
|
---|
416 | #endregion
|
---|
417 | #region Algorithm Configuration
|
---|
418 | ga.Problem = symbClassProblem;
|
---|
419 | ga.Name = "Genetic Programming - Symbolic Classification";
|
---|
420 | ga.Description = "A standard genetic programming algorithm to solve a classification problem (Mammographic+Mass dataset)";
|
---|
421 | ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(
|
---|
422 | ga, 1000, 1, 100, 0.15, 5
|
---|
423 | );
|
---|
424 |
|
---|
425 | var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator;
|
---|
426 | mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1;
|
---|
427 | mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0;
|
---|
428 |
|
---|
429 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
430 | ga.Analyzer.Operators
|
---|
431 | .OfType<SymbolicClassificationSingleObjectiveOverfittingAnalyzer>()
|
---|
432 | .Single(), false);
|
---|
433 | ga.Analyzer.Operators.SetItemCheckedState(
|
---|
434 | ga.Analyzer.Operators
|
---|
435 | .OfType<SymbolicDataAnalysisAlleleFrequencyAnalyzer>()
|
---|
436 | .First(), false);
|
---|
437 | #endregion
|
---|
438 | return ga;
|
---|
439 | }
|
---|
440 | #endregion
|
---|
441 | #endregion
|
---|
442 |
|
---|
443 | #region ES
|
---|
444 | #region Griewank
|
---|
445 | [TestMethod]
|
---|
446 | public void CreateEsGriewankSampleTest() {
|
---|
447 | var es = CreateEsGriewankSample();
|
---|
448 | XmlGenerator.Serialize(es, "../../ES_Griewank.hl");
|
---|
449 | }
|
---|
450 | [TestMethod]
|
---|
451 | public void RunEsGriewankSampleTest() {
|
---|
452 | var es = CreateEsGriewankSample();
|
---|
453 | es.SetSeedRandomly.Value = false;
|
---|
454 | RunAlgorithm(es);
|
---|
455 | Assert.AreEqual(0, GetDoubleResult(es, "BestQuality"));
|
---|
456 | Assert.AreEqual(0, GetDoubleResult(es, "CurrentAverageQuality"));
|
---|
457 | Assert.AreEqual(0, GetDoubleResult(es, "CurrentWorstQuality"));
|
---|
458 | Assert.AreEqual(100020, GetIntResult(es, "EvaluatedSolutions"));
|
---|
459 | }
|
---|
460 |
|
---|
461 | private EvolutionStrategy CreateEsGriewankSample() {
|
---|
462 | EvolutionStrategy es = new EvolutionStrategy();
|
---|
463 | #region Problem Configuration
|
---|
464 | SingleObjectiveTestFunctionProblem problem = new SingleObjectiveTestFunctionProblem();
|
---|
465 |
|
---|
466 | problem.ProblemSize.Value = 10;
|
---|
467 | problem.EvaluatorParameter.Value = new GriewankEvaluator();
|
---|
468 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
469 | problem.Maximization.Value = false;
|
---|
470 | problem.Bounds = new DoubleMatrix(new double[,] { { -600, 600 } });
|
---|
471 | problem.BestKnownQuality.Value = 0;
|
---|
472 | problem.BestKnownSolutionParameter.Value = new RealVector(10);
|
---|
473 | problem.Name = "Single Objective Test Function";
|
---|
474 | problem.Description = "Test function with real valued inputs and a single objective.";
|
---|
475 | #endregion
|
---|
476 | #region Algorithm Configuration
|
---|
477 | es.Name = "Evolution Strategy - Griewank";
|
---|
478 | es.Description = "An evolution strategy which solves the 10-dimensional Griewank test function";
|
---|
479 | es.Problem = problem;
|
---|
480 | ConfigureEvolutionStrategyParameters<AverageCrossover, NormalAllPositionsManipulator,
|
---|
481 | StdDevStrategyVectorCreator, StdDevStrategyVectorCrossover, StdDevStrategyVectorManipulator>(
|
---|
482 | es, 20, 500, 2, 200, false);
|
---|
483 |
|
---|
484 | StdDevStrategyVectorCreator strategyCreator = (StdDevStrategyVectorCreator)es.StrategyParameterCreator;
|
---|
485 | strategyCreator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1, 20 } });
|
---|
486 |
|
---|
487 | StdDevStrategyVectorManipulator strategyManipulator = (StdDevStrategyVectorManipulator)es.StrategyParameterManipulator;
|
---|
488 | strategyManipulator.BoundsParameter.Value = new DoubleMatrix(new double[,] { { 1E-12, 30 } });
|
---|
489 | strategyManipulator.GeneralLearningRateParameter.Value = new DoubleValue(0.22360679774997896);
|
---|
490 | strategyManipulator.LearningRateParameter.Value = new DoubleValue(0.39763536438352531);
|
---|
491 | #endregion
|
---|
492 | return es;
|
---|
493 | }
|
---|
494 | #endregion
|
---|
495 | #endregion
|
---|
496 |
|
---|
497 | #region Island GA
|
---|
498 | #region TSP
|
---|
499 | [TestMethod]
|
---|
500 | public void CreateIslandGaTspSampleTest() {
|
---|
501 | var ga = CreateIslandGaTspSample();
|
---|
502 | XmlGenerator.Serialize(ga, "../../IslandGA_TSP.hl");
|
---|
503 | }
|
---|
504 | [TestMethod]
|
---|
505 | public void RunIslandGaTspSampleTest() {
|
---|
506 | var ga = CreateIslandGaTspSample();
|
---|
507 | ga.SetSeedRandomly.Value = false;
|
---|
508 | RunAlgorithm(ga);
|
---|
509 | Assert.AreEqual(9918, GetDoubleResult(ga, "BestQuality"));
|
---|
510 | Assert.AreEqual(10324.64, GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
511 | Assert.AreEqual(11823, GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
512 | Assert.AreEqual(495500, GetIntResult(ga, "EvaluatedSolutions"));
|
---|
513 | }
|
---|
514 |
|
---|
515 | private IslandGeneticAlgorithm CreateIslandGaTspSample() {
|
---|
516 | IslandGeneticAlgorithm ga = new IslandGeneticAlgorithm();
|
---|
517 | #region Problem Configuration
|
---|
518 | var provider = new TSPLIBTSPInstanceProvider();
|
---|
519 | var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single();
|
---|
520 | TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
|
---|
521 | tspProblem.Load(provider.LoadData(instance));
|
---|
522 | tspProblem.UseDistanceMatrix.Value = true;
|
---|
523 | #endregion
|
---|
524 | #region Algorithm Configuration
|
---|
525 | ga.Name = "Island Genetic Algorithm - TSP";
|
---|
526 | ga.Description = "An island genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
|
---|
527 | ga.Problem = tspProblem;
|
---|
528 | ConfigureIslandGeneticAlgorithmParameters<ProportionalSelector, OrderCrossover2, InversionManipulator,
|
---|
529 | UnidirectionalRingMigrator, BestSelector, WorstReplacer>(
|
---|
530 | ga, 100, 1, 1000, 0.05, 5, 50, 0.25);
|
---|
531 |
|
---|
532 | ga.Analyzer.Operators.SetItemCheckedState(ga.Analyzer.Operators
|
---|
533 | .OfType<TSPAlleleFrequencyAnalyzer>()
|
---|
534 | .Single(), false);
|
---|
535 | ga.Analyzer.Operators.SetItemCheckedState(ga.Analyzer.Operators
|
---|
536 | .OfType<TSPPopulationDiversityAnalyzer>()
|
---|
537 | .Single(), false);
|
---|
538 | #endregion
|
---|
539 | return ga;
|
---|
540 | }
|
---|
541 | #endregion
|
---|
542 | #endregion
|
---|
543 |
|
---|
544 | #region LS
|
---|
545 | #region Knapsack
|
---|
546 | [TestMethod]
|
---|
547 | public void CreateLocalSearchKnapsackSampleTest() {
|
---|
548 | var ls = CreateLocalSearchKnapsackSample();
|
---|
549 | XmlGenerator.Serialize(ls, "../../LS_Knapsack.hl");
|
---|
550 | }
|
---|
551 | [TestMethod]
|
---|
552 | public void RunLocalSearchKnapsackSampleTest() {
|
---|
553 | var ls = CreateLocalSearchKnapsackSample();
|
---|
554 | ls.SetSeedRandomly.Value = false;
|
---|
555 | RunAlgorithm(ls);
|
---|
556 | Assert.AreEqual(345, GetDoubleResult(ls, "BestQuality"));
|
---|
557 | Assert.AreEqual(340.70731707317071, GetDoubleResult(ls, "CurrentAverageQuality"));
|
---|
558 | Assert.AreEqual(337, GetDoubleResult(ls, "CurrentWorstQuality"));
|
---|
559 | Assert.AreEqual(82000, GetIntResult(ls, "EvaluatedMoves"));
|
---|
560 | }
|
---|
561 |
|
---|
562 | private LocalSearch CreateLocalSearchKnapsackSample() {
|
---|
563 | LocalSearch ls = new LocalSearch();
|
---|
564 | #region Problem Configuration
|
---|
565 | KnapsackProblem problem = new KnapsackProblem();
|
---|
566 | problem.BestKnownQuality = new DoubleValue(362);
|
---|
567 | problem.BestKnownSolution = new HeuristicLab.Encodings.BinaryVectorEncoding.BinaryVector(new bool[] {
|
---|
568 | 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});
|
---|
569 | problem.EvaluatorParameter.Value = new KnapsackEvaluator();
|
---|
570 | problem.SolutionCreatorParameter.Value = new RandomBinaryVectorCreator();
|
---|
571 | problem.KnapsackCapacity.Value = 297;
|
---|
572 | problem.Maximization.Value = true;
|
---|
573 | problem.Penalty.Value = 1;
|
---|
574 | problem.Values = new IntArray(new int[] {
|
---|
575 | 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});
|
---|
576 | problem.Weights = new IntArray(new int[] {
|
---|
577 | 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});
|
---|
578 | problem.Name = "Knapsack Problem";
|
---|
579 | problem.Description = "Represents a Knapsack problem.";
|
---|
580 | #endregion
|
---|
581 | #region Algorithm Configuration
|
---|
582 | ls.Name = "Local Search - Knapsack";
|
---|
583 | ls.Description = "A local search algorithm that solves a randomly generated Knapsack problem";
|
---|
584 | ls.Problem = problem;
|
---|
585 | ls.MaximumIterations.Value = 1000;
|
---|
586 | ls.MoveEvaluator = ls.MoveEvaluatorParameter.ValidValues
|
---|
587 | .OfType<KnapsackOneBitflipMoveEvaluator>()
|
---|
588 | .Single();
|
---|
589 | ls.MoveGenerator = ls.MoveGeneratorParameter.ValidValues
|
---|
590 | .OfType<ExhaustiveOneBitflipMoveGenerator>()
|
---|
591 | .Single();
|
---|
592 | ls.MoveMaker = ls.MoveMakerParameter.ValidValues
|
---|
593 | .OfType<OneBitflipMoveMaker>()
|
---|
594 | .Single();
|
---|
595 | ls.SampleSize.Value = 100;
|
---|
596 | ls.Seed.Value = 0;
|
---|
597 | ls.SetSeedRandomly.Value = true;
|
---|
598 | #endregion
|
---|
599 | ls.Engine = new ParallelEngine();
|
---|
600 | return ls;
|
---|
601 | }
|
---|
602 | #endregion
|
---|
603 | #endregion
|
---|
604 |
|
---|
605 | #region PSO
|
---|
606 | #region Schwefel
|
---|
607 | [TestMethod]
|
---|
608 | public void CreatePsoSchwefelSampleTest() {
|
---|
609 | var pso = CreatePsoSchwefelSample();
|
---|
610 | XmlGenerator.Serialize(pso, "../../PSO_Schwefel.hl");
|
---|
611 | }
|
---|
612 | [TestMethod]
|
---|
613 | public void RunPsoSchwefelSampleTest() {
|
---|
614 | var pso = CreatePsoSchwefelSample();
|
---|
615 | pso.SetSeedRandomly.Value = false;
|
---|
616 | RunAlgorithm(pso);
|
---|
617 | if (!Environment.Is64BitProcess) {
|
---|
618 | Assert.AreEqual(118.44027985932837, GetDoubleResult(pso, "BestQuality"));
|
---|
619 | Assert.AreEqual(140.71570105946438, GetDoubleResult(pso, "CurrentAverageQuality"));
|
---|
620 | Assert.AreEqual(220.956806502853, GetDoubleResult(pso, "CurrentWorstQuality"));
|
---|
621 | Assert.AreEqual(1000, GetIntResult(pso, "Iterations"));
|
---|
622 | } else {
|
---|
623 | Assert.AreEqual(118.43958282879345, GetDoubleResult(pso, "BestQuality"));
|
---|
624 | Assert.AreEqual(139.43946864779372, GetDoubleResult(pso, "CurrentAverageQuality"));
|
---|
625 | Assert.AreEqual(217.14654589055152, GetDoubleResult(pso, "CurrentWorstQuality"));
|
---|
626 | Assert.AreEqual(1000, GetIntResult(pso, "Iterations"));
|
---|
627 | }
|
---|
628 | }
|
---|
629 | private ParticleSwarmOptimization CreatePsoSchwefelSample() {
|
---|
630 | ParticleSwarmOptimization pso = new ParticleSwarmOptimization();
|
---|
631 | #region Problem Configuration
|
---|
632 | var problem = new SingleObjectiveTestFunctionProblem();
|
---|
633 | problem.BestKnownQuality.Value = 0.0;
|
---|
634 | problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 420.968746, 420.968746 });
|
---|
635 | problem.Bounds = new DoubleMatrix(new double[,] { { -500, 500 } });
|
---|
636 | problem.EvaluatorParameter.Value = new SchwefelEvaluator();
|
---|
637 | problem.Maximization.Value = false;
|
---|
638 | problem.ProblemSize.Value = 2;
|
---|
639 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
640 | #endregion
|
---|
641 | #region Algorithm Configuration
|
---|
642 | pso.Name = "Particle Swarm Optimization - Schwefel";
|
---|
643 | 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)";
|
---|
644 | pso.Problem = problem;
|
---|
645 | pso.Inertia.Value = 10;
|
---|
646 | pso.MaxIterations.Value = 1000;
|
---|
647 | pso.NeighborBestAttraction.Value = 0.5;
|
---|
648 | pso.PersonalBestAttraction.Value = -0.01;
|
---|
649 | pso.SwarmSize.Value = 50;
|
---|
650 |
|
---|
651 | var inertiaUpdater = pso.InertiaUpdaterParameter.ValidValues
|
---|
652 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
653 | .Single();
|
---|
654 | inertiaUpdater.StartValueParameter.Value = new DoubleValue(10);
|
---|
655 | inertiaUpdater.EndValueParameter.Value = new DoubleValue(1);
|
---|
656 | pso.InertiaUpdater = inertiaUpdater;
|
---|
657 |
|
---|
658 | pso.ParticleCreator = pso.ParticleCreatorParameter.ValidValues
|
---|
659 | .OfType<RealVectorParticleCreator>()
|
---|
660 | .Single();
|
---|
661 | var swarmUpdater = pso.SwarmUpdaterParameter.ValidValues
|
---|
662 | .OfType<RealVectorSwarmUpdater>()
|
---|
663 | .Single();
|
---|
664 | swarmUpdater.VelocityBoundsIndexParameter.ActualName = "Iterations";
|
---|
665 | swarmUpdater.VelocityBoundsParameter.Value = new DoubleMatrix(new double[,] { { -10, 10 } });
|
---|
666 | swarmUpdater.VelocityBoundsStartValueParameter.Value = new DoubleValue(10.0);
|
---|
667 | swarmUpdater.VelocityBoundsEndValueParameter.Value = new DoubleValue(1.0);
|
---|
668 | swarmUpdater.VelocityBoundsScalingOperatorParameter.Value = swarmUpdater.VelocityBoundsScalingOperatorParameter.ValidValues
|
---|
669 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
670 | .Single();
|
---|
671 |
|
---|
672 | pso.TopologyInitializer = null;
|
---|
673 | pso.TopologyUpdater = null;
|
---|
674 | pso.SwarmUpdater = swarmUpdater;
|
---|
675 | pso.Seed.Value = 0;
|
---|
676 | pso.SetSeedRandomly.Value = true;
|
---|
677 | #endregion
|
---|
678 | pso.Engine = new ParallelEngine();
|
---|
679 | return pso;
|
---|
680 | }
|
---|
681 | #endregion
|
---|
682 | #endregion
|
---|
683 |
|
---|
684 | #region SA
|
---|
685 | #region Rastrigin
|
---|
686 | [TestMethod]
|
---|
687 | public void CreateSimulatedAnnealingRastriginSampleTest() {
|
---|
688 | var sa = CreateSimulatedAnnealingRastriginSample();
|
---|
689 | XmlGenerator.Serialize(sa, "../../SA_Rastrigin.hl");
|
---|
690 | }
|
---|
691 | [TestMethod]
|
---|
692 | public void RunSimulatedAnnealingRastriginSampleTest() {
|
---|
693 | var sa = CreateSimulatedAnnealingRastriginSample();
|
---|
694 | sa.SetSeedRandomly.Value = false;
|
---|
695 | RunAlgorithm(sa);
|
---|
696 | Assert.AreEqual(0.00014039606034543795, GetDoubleResult(sa, "BestQuality"));
|
---|
697 | Assert.AreEqual(5000, GetIntResult(sa, "EvaluatedMoves"));
|
---|
698 | }
|
---|
699 | private SimulatedAnnealing CreateSimulatedAnnealingRastriginSample() {
|
---|
700 | SimulatedAnnealing sa = new SimulatedAnnealing();
|
---|
701 | #region Problem Configuration
|
---|
702 | var problem = new SingleObjectiveTestFunctionProblem();
|
---|
703 | problem.BestKnownQuality.Value = 0.0;
|
---|
704 | problem.BestKnownSolutionParameter.Value = new RealVector(new double[] { 0, 0 });
|
---|
705 | problem.Bounds = new DoubleMatrix(new double[,] { { -5.12, 5.12 } });
|
---|
706 | problem.EvaluatorParameter.Value = new RastriginEvaluator();
|
---|
707 | problem.Maximization.Value = false;
|
---|
708 | problem.ProblemSize.Value = 2;
|
---|
709 | problem.SolutionCreatorParameter.Value = new UniformRandomRealVectorCreator();
|
---|
710 | #endregion
|
---|
711 | #region Algorithm Configuration
|
---|
712 | sa.Name = "Simulated Annealing - Rastrigin";
|
---|
713 | sa.Description = "A simulated annealing algorithm that solves the 2-dimensional Rastrigin test function";
|
---|
714 | sa.Problem = problem;
|
---|
715 | var annealingOperator = sa.AnnealingOperatorParameter.ValidValues
|
---|
716 | .OfType<ExponentialDiscreteDoubleValueModifier>()
|
---|
717 | .Single();
|
---|
718 | annealingOperator.StartIndexParameter.Value = new IntValue(0);
|
---|
719 | sa.AnnealingOperator = annealingOperator;
|
---|
720 |
|
---|
721 | sa.EndTemperature.Value = 1E-6;
|
---|
722 | sa.InnerIterations.Value = 50;
|
---|
723 | sa.MaximumIterations.Value = 100;
|
---|
724 | var moveEvaluator = sa.MoveEvaluatorParameter.ValidValues
|
---|
725 | .OfType<RastriginAdditiveMoveEvaluator>()
|
---|
726 | .Single();
|
---|
727 | moveEvaluator.A.Value = 10;
|
---|
728 | sa.MoveEvaluator = moveEvaluator;
|
---|
729 |
|
---|
730 | var moveGenerator = sa.MoveGeneratorParameter.ValidValues
|
---|
731 | .OfType<StochasticNormalMultiMoveGenerator>()
|
---|
732 | .Single();
|
---|
733 | moveGenerator.SigmaParameter.Value = new DoubleValue(1);
|
---|
734 | sa.MoveGenerator = moveGenerator;
|
---|
735 |
|
---|
736 | sa.MoveMaker = sa.MoveMakerParameter.ValidValues
|
---|
737 | .OfType<AdditiveMoveMaker>()
|
---|
738 | .Single();
|
---|
739 |
|
---|
740 | sa.Seed.Value = 0;
|
---|
741 | sa.SetSeedRandomly.Value = true;
|
---|
742 | sa.StartTemperature.Value = 1;
|
---|
743 | #endregion
|
---|
744 | sa.Engine = new ParallelEngine();
|
---|
745 | return sa;
|
---|
746 | }
|
---|
747 | #endregion
|
---|
748 | #endregion
|
---|
749 |
|
---|
750 | #region TS
|
---|
751 | #region TSP
|
---|
752 | [TestMethod]
|
---|
753 | public void CreateTabuSearchTspSampleTest() {
|
---|
754 | var ts = CreateTabuSearchTspSample();
|
---|
755 | XmlGenerator.Serialize(ts, "../../TS_TSP.hl");
|
---|
756 | }
|
---|
757 | [TestMethod]
|
---|
758 | public void RunTabuSearchTspSampleTest() {
|
---|
759 | var ts = CreateTabuSearchTspSample();
|
---|
760 | ts.SetSeedRandomly.Value = false;
|
---|
761 | RunAlgorithm(ts);
|
---|
762 | Assert.AreEqual(6441, GetDoubleResult(ts, "BestQuality"));
|
---|
763 | Assert.AreEqual(7401.666666666667, GetDoubleResult(ts, "CurrentAverageQuality"));
|
---|
764 | Assert.AreEqual(8418, GetDoubleResult(ts, "CurrentWorstQuality"));
|
---|
765 | Assert.AreEqual(750000, GetIntResult(ts, "EvaluatedMoves"));
|
---|
766 | }
|
---|
767 |
|
---|
768 | private TabuSearch CreateTabuSearchTspSample() {
|
---|
769 | TabuSearch ts = new TabuSearch();
|
---|
770 | #region Problem Configuration
|
---|
771 | var provider = new TSPLIBTSPInstanceProvider();
|
---|
772 | var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single();
|
---|
773 | TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
|
---|
774 | tspProblem.Load(provider.LoadData(instance));
|
---|
775 | tspProblem.UseDistanceMatrix.Value = true;
|
---|
776 | #endregion
|
---|
777 | #region Algorithm Configuration
|
---|
778 | ts.Name = "Tabu Search - TSP";
|
---|
779 | ts.Description = "A tabu search algorithm that solves the \"ch130\" TSP (imported from TSPLIB)";
|
---|
780 | ts.Problem = tspProblem;
|
---|
781 |
|
---|
782 | ts.MaximumIterations.Value = 1000;
|
---|
783 | // move generator has to be set first
|
---|
784 | var moveGenerator = ts.MoveGeneratorParameter.ValidValues
|
---|
785 | .OfType<StochasticInversionMultiMoveGenerator>()
|
---|
786 | .Single();
|
---|
787 | ts.MoveGenerator = moveGenerator;
|
---|
788 | var moveEvaluator = ts.MoveEvaluatorParameter.ValidValues
|
---|
789 | .OfType<TSPInversionMoveRoundedEuclideanPathEvaluator>()
|
---|
790 | .Single();
|
---|
791 | ts.MoveEvaluator = moveEvaluator;
|
---|
792 | var moveMaker = ts.MoveMakerParameter.ValidValues
|
---|
793 | .OfType<InversionMoveMaker>()
|
---|
794 | .Single();
|
---|
795 | ts.MoveMaker = moveMaker;
|
---|
796 | ts.SampleSize.Value = 750;
|
---|
797 | ts.Seed.Value = 0;
|
---|
798 | ts.SetSeedRandomly.Value = true;
|
---|
799 |
|
---|
800 | var tabuChecker = ts.TabuCheckerParameter.ValidValues
|
---|
801 | .OfType<InversionMoveSoftTabuCriterion>()
|
---|
802 | .Single();
|
---|
803 | tabuChecker.UseAspirationCriterion.Value = true;
|
---|
804 | ts.TabuChecker = tabuChecker;
|
---|
805 |
|
---|
806 | var tabuMaker = ts.TabuMakerParameter.ValidValues
|
---|
807 | .OfType<InversionMoveTabuMaker>()
|
---|
808 | .Single();
|
---|
809 | ts.TabuMaker = tabuMaker;
|
---|
810 | ts.TabuTenure.Value = 60;
|
---|
811 |
|
---|
812 | ts.Analyzer.Operators.SetItemCheckedState(ts.Analyzer.Operators
|
---|
813 | .OfType<TSPAlleleFrequencyAnalyzer>()
|
---|
814 | .Single(), false);
|
---|
815 | ts.Analyzer.Operators.SetItemCheckedState(ts.Analyzer.Operators
|
---|
816 | .OfType<TSPPopulationDiversityAnalyzer>()
|
---|
817 | .Single(), false);
|
---|
818 | #endregion
|
---|
819 | ts.Engine = new ParallelEngine();
|
---|
820 | return ts;
|
---|
821 | }
|
---|
822 | #endregion
|
---|
823 | #endregion
|
---|
824 |
|
---|
825 | #region VNS
|
---|
826 | #region TSP
|
---|
827 | [TestMethod]
|
---|
828 | public void CreateVnsTspSampleTest() {
|
---|
829 | var vns = CreateVnsTspSample();
|
---|
830 | XmlGenerator.Serialize(vns, "../../VNS_TSP.hl");
|
---|
831 | }
|
---|
832 | [TestMethod]
|
---|
833 | public void RunVnsTspSampleTest() {
|
---|
834 | var vns = CreateVnsTspSample();
|
---|
835 | vns.SetSeedRandomly = false;
|
---|
836 | RunAlgorithm(vns);
|
---|
837 | Assert.AreEqual(867, GetDoubleResult(vns, "BestQuality"));
|
---|
838 | Assert.AreEqual(867, GetDoubleResult(vns, "CurrentAverageQuality"));
|
---|
839 | Assert.AreEqual(867, GetDoubleResult(vns, "CurrentWorstQuality"));
|
---|
840 | Assert.AreEqual(12975173, GetIntResult(vns, "EvaluatedSolutions"));
|
---|
841 | }
|
---|
842 |
|
---|
843 | private VariableNeighborhoodSearch CreateVnsTspSample() {
|
---|
844 | VariableNeighborhoodSearch vns = new VariableNeighborhoodSearch();
|
---|
845 | #region Problem Configuration
|
---|
846 | TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
|
---|
847 | tspProblem.BestKnownSolution = new Permutation(PermutationTypes.Absolute, new int[] {
|
---|
848 | 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,
|
---|
849 | });
|
---|
850 | tspProblem.Coordinates = new DoubleMatrix(new double[,] {
|
---|
851 | {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}
|
---|
852 | });
|
---|
853 | tspProblem.BestKnownQuality = new DoubleValue(867);
|
---|
854 |
|
---|
855 | tspProblem.EvaluatorParameter.Value = new TSPRoundedEuclideanPathEvaluator();
|
---|
856 | tspProblem.SolutionCreatorParameter.Value = new RandomPermutationCreator();
|
---|
857 | tspProblem.UseDistanceMatrix.Value = true;
|
---|
858 | tspProblem.Name = "Funny TSP";
|
---|
859 | tspProblem.Description = "Represents a symmetric Traveling Salesman Problem.";
|
---|
860 | #endregion
|
---|
861 | #region Algorithm Configuration
|
---|
862 | vns.Name = "Variable Neighborhood Search - TSP";
|
---|
863 | vns.Description = "A variable neighborhood search algorithm which solves a funny TSP instance";
|
---|
864 | vns.Problem = tspProblem;
|
---|
865 |
|
---|
866 | var localImprovement = vns.LocalImprovementParameter.ValidValues
|
---|
867 | .OfType<LocalSearchImprovementOperator>()
|
---|
868 | .Single();
|
---|
869 | // move generator has to be set first
|
---|
870 | localImprovement.MoveGenerator = localImprovement.MoveGeneratorParameter.ValidValues
|
---|
871 | .OfType<StochasticInversionMultiMoveGenerator>()
|
---|
872 | .Single();
|
---|
873 | localImprovement.MoveEvaluator = localImprovement.MoveEvaluatorParameter.ValidValues
|
---|
874 | .OfType<TSPInversionMoveRoundedEuclideanPathEvaluator>()
|
---|
875 | .Single();
|
---|
876 | localImprovement.MoveMaker = localImprovement.MoveMakerParameter.ValidValues
|
---|
877 | .OfType<InversionMoveMaker>()
|
---|
878 | .Single();
|
---|
879 | localImprovement.SampleSizeParameter.Value = new IntValue(500);
|
---|
880 | vns.LocalImprovement = localImprovement;
|
---|
881 |
|
---|
882 | vns.LocalImprovementMaximumIterations = 150;
|
---|
883 | vns.MaximumIterations = 25;
|
---|
884 | vns.Seed = 0;
|
---|
885 | vns.SetSeedRandomly = true;
|
---|
886 | var shakingOperator = vns.ShakingOperatorParameter.ValidValues
|
---|
887 | .OfType<PermutationShakingOperator>()
|
---|
888 | .Single();
|
---|
889 | shakingOperator.Operators.SetItemCheckedState(shakingOperator.Operators
|
---|
890 | .OfType<Swap2Manipulator>()
|
---|
891 | .Single(), false);
|
---|
892 | shakingOperator.Operators.SetItemCheckedState(shakingOperator.Operators
|
---|
893 | .OfType<Swap3Manipulator>()
|
---|
894 | .Single(), false);
|
---|
895 | vns.ShakingOperator = shakingOperator;
|
---|
896 | vns.Analyzer.Operators.SetItemCheckedState(vns.Analyzer.Operators
|
---|
897 | .OfType<TSPAlleleFrequencyAnalyzer>()
|
---|
898 | .Single(), false);
|
---|
899 | vns.Analyzer.Operators.SetItemCheckedState(vns.Analyzer.Operators
|
---|
900 | .OfType<TSPPopulationDiversityAnalyzer>()
|
---|
901 | .Single(), false);
|
---|
902 | #endregion
|
---|
903 | vns.Engine = new ParallelEngine();
|
---|
904 | return vns;
|
---|
905 | }
|
---|
906 | #endregion
|
---|
907 | #endregion
|
---|
908 |
|
---|
909 | #region Helpers
|
---|
910 | private void ConfigureEvolutionStrategyParameters<R, M, SC, SR, SM>(EvolutionStrategy es, int popSize, int children, int parentsPerChild, int maxGens, bool plusSelection)
|
---|
911 | where R : ICrossover
|
---|
912 | where M : IManipulator
|
---|
913 | where SC : IStrategyParameterCreator
|
---|
914 | where SR : IStrategyParameterCrossover
|
---|
915 | where SM : IStrategyParameterManipulator {
|
---|
916 | es.PopulationSize.Value = popSize;
|
---|
917 | es.Children.Value = children;
|
---|
918 | es.ParentsPerChild.Value = parentsPerChild;
|
---|
919 | es.MaximumGenerations.Value = maxGens;
|
---|
920 | es.PlusSelection.Value = false;
|
---|
921 |
|
---|
922 | es.Seed.Value = 0;
|
---|
923 | es.SetSeedRandomly.Value = true;
|
---|
924 |
|
---|
925 | es.Recombinator = es.RecombinatorParameter.ValidValues
|
---|
926 | .OfType<R>()
|
---|
927 | .Single();
|
---|
928 |
|
---|
929 | es.Mutator = es.MutatorParameter.ValidValues
|
---|
930 | .OfType<M>()
|
---|
931 | .Single();
|
---|
932 |
|
---|
933 | es.StrategyParameterCreator = es.StrategyParameterCreatorParameter.ValidValues
|
---|
934 | .OfType<SC>()
|
---|
935 | .Single();
|
---|
936 | es.StrategyParameterCrossover = es.StrategyParameterCrossoverParameter.ValidValues
|
---|
937 | .OfType<SR>()
|
---|
938 | .Single();
|
---|
939 | es.StrategyParameterManipulator = es.StrategyParameterManipulatorParameter.ValidValues
|
---|
940 | .OfType<SM>()
|
---|
941 | .Single();
|
---|
942 | es.Engine = new ParallelEngine();
|
---|
943 | }
|
---|
944 |
|
---|
945 | private void ConfigureGeneticAlgorithmParameters<S, C, M>(GeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int tournGroupSize = 0)
|
---|
946 | where S : ISelector
|
---|
947 | where C : ICrossover
|
---|
948 | where M : IManipulator {
|
---|
949 | ga.Elites.Value = elites;
|
---|
950 | ga.MaximumGenerations.Value = maxGens;
|
---|
951 | ga.MutationProbability.Value = mutationRate;
|
---|
952 | ga.PopulationSize.Value = popSize;
|
---|
953 | ga.Seed.Value = 0;
|
---|
954 | ga.SetSeedRandomly.Value = true;
|
---|
955 | ga.Selector = ga.SelectorParameter.ValidValues
|
---|
956 | .OfType<S>()
|
---|
957 | .First();
|
---|
958 |
|
---|
959 | ga.Crossover = ga.CrossoverParameter.ValidValues
|
---|
960 | .OfType<C>()
|
---|
961 | .First();
|
---|
962 |
|
---|
963 | ga.Mutator = ga.MutatorParameter.ValidValues
|
---|
964 | .OfType<M>()
|
---|
965 | .First();
|
---|
966 |
|
---|
967 | var tSelector = ga.Selector as TournamentSelector;
|
---|
968 | if (tSelector != null) {
|
---|
969 | tSelector.GroupSizeParameter.Value.Value = tournGroupSize;
|
---|
970 | }
|
---|
971 | ga.Engine = new ParallelEngine();
|
---|
972 | }
|
---|
973 |
|
---|
974 | 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)
|
---|
975 | where S : ISelector
|
---|
976 | where C : ICrossover
|
---|
977 | where M : IManipulator
|
---|
978 | where Mi : IMigrator
|
---|
979 | where MiS : ISelector
|
---|
980 | where MiR : IReplacer {
|
---|
981 | ga.Elites.Value = elites;
|
---|
982 | ga.MaximumGenerations.Value = maxGens;
|
---|
983 | ga.MutationProbability.Value = mutationRate;
|
---|
984 | ga.PopulationSize.Value = popSize;
|
---|
985 | ga.NumberOfIslands.Value = numberOfIslands;
|
---|
986 | ga.MigrationInterval.Value = migrationInterval;
|
---|
987 | ga.MigrationRate.Value = migrationRate;
|
---|
988 | ga.Seed.Value = 0;
|
---|
989 | ga.SetSeedRandomly.Value = true;
|
---|
990 | ga.Selector = ga.SelectorParameter.ValidValues
|
---|
991 | .OfType<S>()
|
---|
992 | .Single();
|
---|
993 |
|
---|
994 | ga.Crossover = ga.CrossoverParameter.ValidValues
|
---|
995 | .OfType<C>()
|
---|
996 | .Single();
|
---|
997 |
|
---|
998 | ga.Mutator = ga.MutatorParameter.ValidValues
|
---|
999 | .OfType<M>()
|
---|
1000 | .Single();
|
---|
1001 | ga.Migrator = ga.MigratorParameter.ValidValues
|
---|
1002 | .OfType<Mi>()
|
---|
1003 | .Single();
|
---|
1004 | ga.EmigrantsSelector = ga.EmigrantsSelectorParameter.ValidValues
|
---|
1005 | .OfType<MiS>()
|
---|
1006 | .Single();
|
---|
1007 | ga.ImmigrationReplacer = ga.ImmigrationReplacerParameter.ValidValues
|
---|
1008 | .OfType<MiR>()
|
---|
1009 | .Single();
|
---|
1010 | ga.Engine = new ParallelEngine();
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 |
|
---|
1014 | private void RunAlgorithm(IAlgorithm a) {
|
---|
1015 | var trigger = new EventWaitHandle(false, EventResetMode.ManualReset);
|
---|
1016 | Exception ex = null;
|
---|
1017 | a.Stopped += (src, e) => { trigger.Set(); };
|
---|
1018 | a.ExceptionOccurred += (src, e) => { ex = e.Value; trigger.Set(); };
|
---|
1019 | a.Prepare();
|
---|
1020 | a.Start();
|
---|
1021 | trigger.WaitOne();
|
---|
1022 |
|
---|
1023 | Assert.AreEqual(ex, null);
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | private double GetDoubleResult(IAlgorithm a, string resultName) {
|
---|
1027 | return ((DoubleValue)a.Results[resultName].Value).Value;
|
---|
1028 | }
|
---|
1029 | private int GetIntResult(IAlgorithm a, string resultName) {
|
---|
1030 | return ((IntValue)a.Results[resultName].Value).Value;
|
---|
1031 | }
|
---|
1032 | #endregion
|
---|
1033 | }
|
---|
1034 | }
|
---|