Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab-3.3/Samples/AlpsSampleTest.cs @ 17696

Last change on this file since 17696 was 17254, checked in by abeham, 5 years ago

#2521: Added context lookup parameter

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