Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/AlpsTspSampleTest.cs @ 13223

Last change on this file since 13223 was 13223, checked in by pfleck, 8 years ago

#2269 Added ALPS TSP test sample.

File size: 4.1 KB
Line 
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
22using System.IO;
23using System.Linq;
24using HeuristicLab.Algorithms.ALPS;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Persistence.Default.Xml;
27using HeuristicLab.Problems.Instances.TSPLIB;
28using HeuristicLab.Problems.TravelingSalesman;
29using HeuristicLab.Selection;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31
32namespace HeuristicLab.Tests {
33  [TestClass]
34  public class AlpsTspSampleTest {
35    private const string SampleFileName = "ALPSGA_TSP";
36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreateAlpsGaTspSampleTest() {
41      var alpsGa = CreateAlpsGaTspSample();
42      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
43      XmlGenerator.Serialize(alpsGa, path);
44    }
45    [TestMethod]
46    [TestCategory("Samples.Execute")]
47    [TestProperty("Time", "long")]
48    public void RunAlpsGaTspSampleTest() {
49      var alpsGaE = CreateAlpsGaTspSample(plusSelection: false);
50      alpsGaE.SetSeedRandomly.Value = false;
51      SamplesUtils.RunAlgorithm(alpsGaE);
52      Assert.AreEqual(23661, SamplesUtils.GetDoubleResult(alpsGaE, "BestQuality"));
53      Assert.AreEqual(35131.1625, SamplesUtils.GetDoubleResult(alpsGaE, "CurrentAverageQuality"));
54      Assert.AreEqual(49699, SamplesUtils.GetDoubleResult(alpsGaE, "CurrentWorstQuality"));
55      Assert.AreEqual(26640, SamplesUtils.GetIntResult(alpsGaE, "EvaluatedSolutions"));
56
57      var alpsGaPs = CreateAlpsGaTspSample(plusSelection: true);
58      alpsGaPs.SetSeedRandomly.Value = false;
59      SamplesUtils.RunAlgorithm(alpsGaPs);
60      Assert.AreEqual(21365, SamplesUtils.GetDoubleResult(alpsGaPs, "BestQuality"));
61      Assert.AreEqual(30774.695, SamplesUtils.GetDoubleResult(alpsGaPs, "CurrentAverageQuality"));
62      Assert.AreEqual(49699, SamplesUtils.GetDoubleResult(alpsGaPs, "CurrentWorstQuality"));
63      Assert.AreEqual(26900, SamplesUtils.GetIntResult(alpsGaPs, "EvaluatedSolutions"));
64    }
65
66    private AlpsGeneticAlgorithm CreateAlpsGaTspSample(bool plusSelection = false) {
67      AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
68      #region Problem Configuration
69      var provider = new TSPLIBTSPInstanceProvider();
70      var instance = provider.GetDataDescriptors().Single(x => x.Name == "ch130");
71      TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
72      tspProblem.Load(provider.LoadData(instance));
73      tspProblem.UseDistanceMatrix.Value = true;
74      #endregion
75      #region Algorithm Configuration
76      alpsGa.Name = "ALPS Genetic Algorithm - TSP";
77      alpsGa.Description = "An age-layered population structure genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
78      alpsGa.Problem = tspProblem;
79      SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, OrderCrossover2, InversionManipulator>(alpsGa,
80        numberOfLayers: 10,
81        popSize: 100,
82        mutationRate: 0.05,
83        elites: 1,
84        plusSelection: plusSelection,
85        agingScheme: AgingScheme.Polynomial,
86        ageGap: 20,
87        ageInheritance: 1.0,
88        maxGens: 100);
89      #endregion
90      return alpsGa;
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.