Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Tests/HeuristicLab-3.3/SamplesTest.cs @ 7615

Last change on this file since 7615 was 7615, checked in by gkronber, 13 years ago

#1081 merged r7462:7609 from trunk into time series branch

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