[13223] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13223] | 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;
|
---|
[17105] | 24 | using HEAL.Attic;
|
---|
[13223] | 25 | using HeuristicLab.Algorithms.ALPS;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[13260] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 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 |
|
---|
[17105] | 42 | private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
| 43 |
|
---|
[13223] | 44 | [TestMethod]
|
---|
| 45 | [TestCategory("Samples.Create")]
|
---|
| 46 | [TestProperty("Time", "medium")]
|
---|
| 47 | public void CreateAlpsGaTspSampleTest() {
|
---|
| 48 | var alpsGa = CreateAlpsGaTspSample();
|
---|
[13260] | 49 | string path = Path.Combine(SamplesUtils.SamplesDirectory, TspSampleFileName + SamplesUtils.SampleFileExtension);
|
---|
[17105] | 50 | serializer.Serialize(alpsGa, path);
|
---|
[13223] | 51 | }
|
---|
[13260] | 52 |
|
---|
[13223] | 53 | [TestMethod]
|
---|
[13260] | 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);
|
---|
[17105] | 59 | serializer.Serialize(alpsGa, path);
|
---|
[13260] | 60 | }
|
---|
| 61 |
|
---|
| 62 | [TestMethod]
|
---|
[13223] | 63 | [TestCategory("Samples.Execute")]
|
---|
| 64 | [TestProperty("Time", "long")]
|
---|
| 65 | public void RunAlpsGaTspSampleTest() {
|
---|
[13260] | 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 | }
|
---|
[13223] | 74 |
|
---|
[13260] | 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"));
|
---|
[13223] | 83 | }
|
---|
| 84 |
|
---|
[13260] | 85 | private AlpsGeneticAlgorithm CreateAlpsGaTspSample() {
|
---|
[13223] | 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;
|
---|
[13260] | 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));
|
---|
[13223] | 113 | #endregion
|
---|
| 114 | return alpsGa;
|
---|
| 115 | }
|
---|
[13260] | 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 | }
|
---|
[13223] | 150 | }
|
---|
| 151 | }
|
---|