Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/SamplesTest.cs @ 9928

Last change on this file since 9928 was 9885, checked in by mkommend, 11 years ago

#2088: Merged all changesets regarding the unit test restructuring in the stable branch.

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