Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2898_GeneralizedAdditiveModels/HeuristicLab.Tests/HeuristicLab-3.3/Samples/AlpsSampleTest.cs @ 16105

Last change on this file since 16105 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.IO;
23using System.Linq;
24using HeuristicLab.Algorithms.ALPS;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence.Default.Xml;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
30using HeuristicLab.Problems.Instances.DataAnalysis;
31using HeuristicLab.Problems.Instances.TSPLIB;
32using HeuristicLab.Problems.TravelingSalesman;
33using HeuristicLab.Selection;
34using Microsoft.VisualStudio.TestTools.UnitTesting;
35
36namespace HeuristicLab.Tests {
37  [TestClass]
38  public class AlpsTspSampleTest {
39    private const string TspSampleFileName = "ALPSGA_TSP";
40    private const string SymRegSampleFileName = "ALPSGP_SymReg";
41
42    [TestMethod]
43    [TestCategory("Samples.Create")]
44    [TestProperty("Time", "medium")]
45    public void CreateAlpsGaTspSampleTest() {
46      var alpsGa = CreateAlpsGaTspSample();
47      string path = Path.Combine(SamplesUtils.SamplesDirectory, TspSampleFileName + SamplesUtils.SampleFileExtension);
48      XmlGenerator.Serialize(alpsGa, path);
49    }
50
51    [TestMethod]
52    [TestCategory("Samples.Create")]
53    [TestProperty("Time", "medium")]
54    public void CreateAlpsGaSymRegSampleTest() {
55      var alpsGa = CreateAlpsGaSymRegSample();
56      string path = Path.Combine(SamplesUtils.SamplesDirectory, SymRegSampleFileName + SamplesUtils.SampleFileExtension);
57      XmlGenerator.Serialize(alpsGa, path);
58    }
59
60    [TestMethod]
61    [TestCategory("Samples.Execute")]
62    [TestProperty("Time", "long")]
63    public void RunAlpsGaTspSampleTest() {
64      var alpsGa = CreateAlpsGaTspSample();
65      alpsGa.SetSeedRandomly.Value = false;
66      SamplesUtils.RunAlgorithm(alpsGa);
67      Assert.AreEqual(7967, SamplesUtils.GetDoubleResult(alpsGa, "BestQuality"));
68      Assert.AreEqual(17565.174444444445, SamplesUtils.GetDoubleResult(alpsGa, "CurrentAverageQuality"));
69      Assert.AreEqual(50295, SamplesUtils.GetDoubleResult(alpsGa, "CurrentWorstQuality"));
70      Assert.AreEqual(621900, SamplesUtils.GetIntResult(alpsGa, "EvaluatedSolutions"));
71    }
72
73    [TestMethod]
74    [TestCategory("Samples.Execute")]
75    [TestProperty("Time", "long")]
76    public void RunAlpsGaSymRegSampleTest() {
77      var alpsGa = CreateAlpsGaSymRegSample();
78      alpsGa.SetSeedRandomly.Value = false;
79      SamplesUtils.RunAlgorithm(alpsGa);
80      Assert.AreEqual(265855, SamplesUtils.GetIntResult(alpsGa, "EvaluatedSolutions"));
81    }
82
83    private AlpsGeneticAlgorithm CreateAlpsGaTspSample() {
84      AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
85      #region Problem Configuration
86      var provider = new TSPLIBTSPInstanceProvider();
87      var instance = provider.GetDataDescriptors().Single(x => x.Name == "ch130");
88      TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
89      tspProblem.Load(provider.LoadData(instance));
90      tspProblem.UseDistanceMatrix.Value = true;
91      #endregion
92      #region Algorithm Configuration
93      alpsGa.Name = "ALPS Genetic Algorithm - TSP";
94      alpsGa.Description = "An age-layered population structure genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
95      alpsGa.Problem = tspProblem;
96      SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, MultiPermutationCrossover, InversionManipulator>(alpsGa,
97        numberOfLayers: 1000,
98        popSize: 100,
99        mutationRate: 0.05,
100        elites: 1,
101        plusSelection: true,
102        agingScheme: AgingScheme.Polynomial,
103        ageGap: 20,
104        ageInheritance: 1.0,
105        maxGens: 1000);
106      var checkedCrossovers = new[] { typeof(EdgeRecombinationCrossover), typeof(MaximalPreservativeCrossover), typeof(OrderCrossover2) };
107      var multiCrossover = (MultiPermutationCrossover)alpsGa.Crossover;
108      var crossovers = multiCrossover.Operators.Where(c => checkedCrossovers.Any(cc => cc.IsInstanceOfType(c))).ToList();
109      foreach (var c in multiCrossover.Operators)
110        multiCrossover.Operators.SetItemCheckedState(c, crossovers.Contains(c));
111      #endregion
112      return alpsGa;
113    }
114
115    private AlpsGeneticAlgorithm CreateAlpsGaSymRegSample() {
116      AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
117      #region Problem Configuration
118      var provider = new VladislavlevaInstanceProvider();
119      var instance = provider.GetDataDescriptors().Single(x => x.Name.StartsWith("Vladislavleva-5 F5"));
120      var symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
121      symbRegProblem.Load(provider.LoadData(instance));
122
123      symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 35;
124      symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 35;
125
126      var grammar = (TypeCoherentExpressionGrammar)symbRegProblem.SymbolicExpressionTreeGrammar;
127      grammar.Symbols.OfType<Exponential>().Single().Enabled = false;
128      grammar.Symbols.OfType<Logarithm>().Single().Enabled = false;
129
130      #endregion
131      #region Algorithm Configuration
132      alpsGa.Name = "ALPS Genetic Programming - Symbolic Regression";
133      alpsGa.Description = "An ALPS-GP to solve a symbolic regression problem (Vladislavleva-5 dataset)";
134      alpsGa.Problem = symbRegProblem;
135      SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(alpsGa,
136        numberOfLayers: 1000,
137        popSize: 100,
138        mutationRate: 0.25,
139        elites: 1,
140        plusSelection: false,
141        agingScheme: AgingScheme.Polynomial,
142        ageGap: 15,
143        ageInheritance: 1.0,
144        maxGens: 500);
145      #endregion
146      return alpsGa;
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.