[13223] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 22 | using System.IO;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Algorithms.ALPS;
|
---|
| 25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[13260] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[13223] | 27 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[13260] | 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
| 30 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
[13223] | 31 | using HeuristicLab.Problems.Instances.TSPLIB;
|
---|
| 32 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
| 33 | using HeuristicLab.Selection;
|
---|
| 34 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Tests {
|
---|
| 37 | [TestClass]
|
---|
| 38 | public class AlpsTspSampleTest {
|
---|
[13260] | 39 | private const string TspSampleFileName = "ALPSGA_TSP";
|
---|
| 40 | private const string SymRegSampleFileName = "ALPSGP_SymReg";
|
---|
[13223] | 41 |
|
---|
| 42 | [TestMethod]
|
---|
| 43 | [TestCategory("Samples.Create")]
|
---|
| 44 | [TestProperty("Time", "medium")]
|
---|
| 45 | public void CreateAlpsGaTspSampleTest() {
|
---|
| 46 | var alpsGa = CreateAlpsGaTspSample();
|
---|
[13260] | 47 | string path = Path.Combine(SamplesUtils.SamplesDirectory, TspSampleFileName + SamplesUtils.SampleFileExtension);
|
---|
[13223] | 48 | XmlGenerator.Serialize(alpsGa, path);
|
---|
| 49 | }
|
---|
[13260] | 50 |
|
---|
[13223] | 51 | [TestMethod]
|
---|
[13260] | 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]
|
---|
[13223] | 61 | [TestCategory("Samples.Execute")]
|
---|
| 62 | [TestProperty("Time", "long")]
|
---|
| 63 | public void RunAlpsGaTspSampleTest() {
|
---|
[13260] | 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 | }
|
---|
[13223] | 72 |
|
---|
[13260] | 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"));
|
---|
[13223] | 81 | }
|
---|
| 82 |
|
---|
[13260] | 83 | private AlpsGeneticAlgorithm CreateAlpsGaTspSample() {
|
---|
[13223] | 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;
|
---|
[13260] | 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));
|
---|
[13223] | 111 | #endregion
|
---|
| 112 | return alpsGa;
|
---|
| 113 | }
|
---|
[13260] | 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 | }
|
---|
[13223] | 148 | }
|
---|
| 149 | }
|
---|