Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/SamplesTest.cs @ 8775

Last change on this file since 8775 was 8775, checked in by ascheibe, 12 years ago

#1331 added Scatter Search sample

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