Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/Samples/TabuSearchTspSampleTest.cs @ 15837

Last change on this file since 15837 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.TabuSearch;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Persistence.Default.Xml;
27using HeuristicLab.Problems.Instances.TSPLIB;
28using HeuristicLab.Problems.TravelingSalesman;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace HeuristicLab.Tests {
32  [TestClass]
33  public class TabuSearchTspSampleTest {
34    private const string SampleFileName = "TS_TSP";
35
36    [TestMethod]
37    [TestCategory("Samples.Create")]
38    [TestProperty("Time", "medium")]
39    public void CreateTabuSearchTspSampleTest() {
40      var ts = CreateTabuSearchTspSample();
41      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
42      XmlGenerator.Serialize(ts, path);
43    }
44    [TestMethod]
45    [TestCategory("Samples.Execute")]
46    [TestProperty("Time", "long")]
47    public void RunTabuSearchTspSampleTest() {
48      var ts = CreateTabuSearchTspSample();
49      ts.SetSeedRandomly.Value = false;
50      SamplesUtils.RunAlgorithm(ts);
51      Assert.AreEqual(6294, SamplesUtils.GetDoubleResult(ts, "BestQuality"));
52      Assert.AreEqual(7380.0386666666664, SamplesUtils.GetDoubleResult(ts, "CurrentAverageQuality"));
53      Assert.AreEqual(8328, SamplesUtils.GetDoubleResult(ts, "CurrentWorstQuality"));
54      Assert.AreEqual(750000, SamplesUtils.GetIntResult(ts, "EvaluatedMoves"));
55    }
56
57    private TabuSearch CreateTabuSearchTspSample() {
58      TabuSearch ts = new TabuSearch();
59      #region Problem Configuration
60      var provider = new TSPLIBTSPInstanceProvider();
61      var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single();
62      TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem();
63      tspProblem.Load(provider.LoadData(instance));
64      tspProblem.UseDistanceMatrix.Value = true;
65      #endregion
66      #region Algorithm Configuration
67      ts.Name = "Tabu Search - TSP";
68      ts.Description = "A tabu search algorithm that solves the \"ch130\" TSP (imported from TSPLIB)";
69      ts.Problem = tspProblem;
70
71      ts.MaximumIterations.Value = 1000;
72      // move generator has to be set first
73      var moveGenerator = ts.MoveGeneratorParameter.ValidValues
74        .OfType<StochasticInversionMultiMoveGenerator>()
75        .Single();
76      ts.MoveGenerator = moveGenerator;
77      var moveEvaluator = ts.MoveEvaluatorParameter.ValidValues
78        .OfType<TSPInversionMoveRoundedEuclideanPathEvaluator>()
79        .Single();
80      ts.MoveEvaluator = moveEvaluator;
81      var moveMaker = ts.MoveMakerParameter.ValidValues
82        .OfType<InversionMoveMaker>()
83        .Single();
84      ts.MoveMaker = moveMaker;
85      ts.SampleSize.Value = 750;
86      ts.Seed.Value = 0;
87      ts.SetSeedRandomly.Value = true;
88
89      var tabuChecker = ts.TabuCheckerParameter.ValidValues
90        .OfType<InversionMoveSoftTabuCriterion>()
91        .Single();
92      tabuChecker.UseAspirationCriterion.Value = true;
93      ts.TabuChecker = tabuChecker;
94
95      var tabuMaker = ts.TabuMakerParameter.ValidValues
96        .OfType<InversionMoveTabuMaker>()
97        .Single();
98      ts.TabuMaker = tabuMaker;
99      ts.TabuTenure.Value = 60;
100
101      #endregion
102      ts.Engine = new ParallelEngine.ParallelEngine();
103      return ts;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.