Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Tests/HeuristicLab-3.3/SamplesTest.cs @ 10632

Last change on this file since 10632 was 10324, checked in by gkronber, 11 years ago

#2106: changed methods for sorting in ObservableArray and ObservableList to use a stable sort (via Enumerable.OrderBy()). This is implemented as extension methods in HeuristicLab.Common. This implementation requires additional memory O(n).
The unit tests for tabu search had to be updated as the stable sort changes the results of the sample.
(minor bug fix in TestRandom)

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