Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10872 was 10864, checked in by abeham, 11 years ago

#2106: merged r10324, r10477, r10787 to stable

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