Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Tests/HeuristicLab-3.3/Samples/TabuSearchTspSampleTest.cs @ 11655

Last change on this file since 11655 was 11450, checked in by bburlacu, 10 years ago

#2211: Separated samples class into separate test classes. Added scripts unit tests (grid search classification/regression).

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