Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17197 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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