#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.IO; using System.Linq; using HeuristicLab.Algorithms.TabuSearch; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Persistence.Default.Xml; using HeuristicLab.Problems.Instances.TSPLIB; using HeuristicLab.Problems.TravelingSalesman; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Tests { /// /// Summary description for TabuSearchTspSampleTest /// [TestClass] public class TabuSearchTspSampleTest { private const string samplesDirectory = SamplesUtils.Directory; [ClassInitialize] public static void MyClassInitialize(TestContext testContext) { if (!Directory.Exists(samplesDirectory)) Directory.CreateDirectory(samplesDirectory); } [TestMethod] [TestCategory("Samples.Create")] [TestProperty("Time", "medium")] public void CreateTabuSearchTspSampleTest() { var ts = CreateTabuSearchTspSample(); XmlGenerator.Serialize(ts, @"Samples\TS_TSP.hl"); } [TestMethod] [TestCategory("Samples.Execute")] [TestProperty("Time", "long")] public void RunTabuSearchTspSampleTest() { var ts = CreateTabuSearchTspSample(); ts.SetSeedRandomly.Value = false; SamplesUtils.RunAlgorithm(ts); Assert.AreEqual(6294, SamplesUtils.GetDoubleResult(ts, "BestQuality")); Assert.AreEqual(7380.0386666666664, SamplesUtils.GetDoubleResult(ts, "CurrentAverageQuality")); Assert.AreEqual(8328, SamplesUtils.GetDoubleResult(ts, "CurrentWorstQuality")); Assert.AreEqual(750000, SamplesUtils.GetIntResult(ts, "EvaluatedMoves")); } private TabuSearch CreateTabuSearchTspSample() { TabuSearch ts = new TabuSearch(); #region Problem Configuration var provider = new TSPLIBTSPInstanceProvider(); var instance = provider.GetDataDescriptors().Where(x => x.Name == "ch130").Single(); TravelingSalesmanProblem tspProblem = new TravelingSalesmanProblem(); tspProblem.Load(provider.LoadData(instance)); tspProblem.UseDistanceMatrix.Value = true; #endregion #region Algorithm Configuration ts.Name = "Tabu Search - TSP"; ts.Description = "A tabu search algorithm that solves the \"ch130\" TSP (imported from TSPLIB)"; ts.Problem = tspProblem; ts.MaximumIterations.Value = 1000; // move generator has to be set first var moveGenerator = ts.MoveGeneratorParameter.ValidValues .OfType() .Single(); ts.MoveGenerator = moveGenerator; var moveEvaluator = ts.MoveEvaluatorParameter.ValidValues .OfType() .Single(); ts.MoveEvaluator = moveEvaluator; var moveMaker = ts.MoveMakerParameter.ValidValues .OfType() .Single(); ts.MoveMaker = moveMaker; ts.SampleSize.Value = 750; ts.Seed.Value = 0; ts.SetSeedRandomly.Value = true; var tabuChecker = ts.TabuCheckerParameter.ValidValues .OfType() .Single(); tabuChecker.UseAspirationCriterion.Value = true; ts.TabuChecker = tabuChecker; var tabuMaker = ts.TabuMakerParameter.ValidValues .OfType() .Single(); ts.TabuMaker = tabuMaker; ts.TabuTenure.Value = 60; #endregion ts.Engine = new ParallelEngine.ParallelEngine(); return ts; } } }