Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab-3.3/Samples/TabuSearchTspSampleTest.cs

Last change on this file was 17254, checked in by abeham, 5 years ago

#2521: Added context lookup parameter

  • Refactored tests
  • Fixed duplicate GUID
File size: 4.1 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      var tspProblem = new TSP();
64      tspProblem.Load(provider.LoadData(instance));
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<TSPInversionMoveEvaluator>()
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.