Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab-3.3/Samples/AlpsSampleTest.cs @ 17607

Last change on this file since 17607 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.6 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      TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
91      tspProblem.Load(provider.LoadData(instance));
92      tspProblem.UseDistanceMatrix.Value = true;
93      #endregion
94      #region Algorithm Configuration
95      alpsGa.Name = "ALPS Genetic Algorithm - TSP";
96      alpsGa.Description = "An age-layered population structure genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
97      alpsGa.Problem = tspProblem;
98      SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, MultiPermutationCrossover, InversionManipulator>(alpsGa,
99        numberOfLayers: 1000,
100        popSize: 100,
101        mutationRate: 0.05,
102        elites: 1,
103        plusSelection: true,
104        agingScheme: AgingScheme.Polynomial,
105        ageGap: 20,
106        ageInheritance: 1.0,
107        maxGens: 1000);
108      var checkedCrossovers = new[] { typeof(EdgeRecombinationCrossover), typeof(MaximalPreservativeCrossover), typeof(OrderCrossover2) };
109      var multiCrossover = (MultiPermutationCrossover)alpsGa.Crossover;
110      var crossovers = multiCrossover.Operators.Where(c => checkedCrossovers.Any(cc => cc.IsInstanceOfType(c))).ToList();
111      foreach (var c in multiCrossover.Operators)
112        multiCrossover.Operators.SetItemCheckedState(c, crossovers.Contains(c));
113      #endregion
114      return alpsGa;
115    }
116
117    private AlpsGeneticAlgorithm CreateAlpsGaSymRegSample() {
118      AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
119      #region Problem Configuration
120      var provider = new VladislavlevaInstanceProvider();
121      var instance = provider.GetDataDescriptors().Single(x => x.Name.StartsWith("Vladislavleva-5 F5"));
122      var symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
123      symbRegProblem.Load(provider.LoadData(instance));
124
125      symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 35;
126      symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 35;
127
128      var grammar = (TypeCoherentExpressionGrammar)symbRegProblem.SymbolicExpressionTreeGrammar;
129      grammar.Symbols.OfType<Exponential>().Single().Enabled = false;
130      grammar.Symbols.OfType<Logarithm>().Single().Enabled = false;
131
132      #endregion
133      #region Algorithm Configuration
134      alpsGa.Name = "ALPS Genetic Programming - Symbolic Regression";
135      alpsGa.Description = "An ALPS-GP to solve a symbolic regression problem (Vladislavleva-5 dataset)";
136      alpsGa.Problem = symbRegProblem;
137      SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(alpsGa,
138        numberOfLayers: 1000,
139        popSize: 100,
140        mutationRate: 0.25,
141        elites: 1,
142        plusSelection: false,
143        agingScheme: AgingScheme.Polynomial,
144        ageGap: 15,
145        ageInheritance: 1.0,
146        maxGens: 500);
147      #endregion
148      return alpsGa;
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.