[11450] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11450] | 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 |
|
---|
| 22 | using System.IO;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Algorithms.LocalSearch;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 27 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 28 | using HeuristicLab.Problems.Knapsack;
|
---|
| 29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Tests {
|
---|
| 32 | [TestClass]
|
---|
| 33 | public class LocalSearchKnapsackSampleTest {
|
---|
[11514] | 34 | private const string SampleFileName = "LS_Knapsack";
|
---|
[11450] | 35 |
|
---|
| 36 | [TestMethod]
|
---|
| 37 | [TestCategory("Samples.Create")]
|
---|
| 38 | [TestProperty("Time", "medium")]
|
---|
| 39 | public void CreateLocalSearchKnapsackSampleTest() {
|
---|
| 40 | var ls = CreateLocalSearchKnapsackSample();
|
---|
[11514] | 41 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
| 42 | XmlGenerator.Serialize(ls, path);
|
---|
[11450] | 43 | }
|
---|
| 44 | [TestMethod]
|
---|
| 45 | [TestCategory("Samples.Execute")]
|
---|
| 46 | [TestProperty("Time", "medium")]
|
---|
| 47 | public void RunLocalSearchKnapsackSampleTest() {
|
---|
| 48 | var ls = CreateLocalSearchKnapsackSample();
|
---|
| 49 | ls.SetSeedRandomly.Value = false;
|
---|
| 50 | SamplesUtils.RunAlgorithm(ls);
|
---|
| 51 | Assert.AreEqual(345, SamplesUtils.GetDoubleResult(ls, "BestQuality"));
|
---|
| 52 | Assert.AreEqual(340.70731707317071, SamplesUtils.GetDoubleResult(ls, "CurrentAverageQuality"));
|
---|
| 53 | Assert.AreEqual(337, SamplesUtils.GetDoubleResult(ls, "CurrentWorstQuality"));
|
---|
| 54 | Assert.AreEqual(82000, SamplesUtils.GetIntResult(ls, "EvaluatedMoves"));
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | private LocalSearch CreateLocalSearchKnapsackSample() {
|
---|
| 58 | LocalSearch ls = new LocalSearch();
|
---|
| 59 | #region Problem Configuration
|
---|
| 60 | KnapsackProblem problem = new KnapsackProblem();
|
---|
[13408] | 61 | problem.BestKnownQuality = 362;
|
---|
[11450] | 62 | problem.BestKnownSolution = new HeuristicLab.Encodings.BinaryVectorEncoding.BinaryVector(new bool[] {
|
---|
| 63 | true , false, false, true , true , true , true , true , false, true , true , true , true , true , true , false, true , false, true , true , false, true , true , false, true , false, true , true , true , false, true , true , false, true , true , false, true , false, true , true , true , true , true , true , true , true , true , true , true , true , true , false, true , false, false, true , true , false, true , true , true , true , true , true , true , true , false, true , false, true , true , true , true , false, true , true , true , true , true , true , true , true});
|
---|
[13469] | 64 | problem.Encoding.SolutionCreator = new RandomBinaryVectorCreator();
|
---|
[13408] | 65 | problem.KnapsackCapacity = 297;
|
---|
[11450] | 66 | problem.Values = new IntArray(new int[] {
|
---|
| 67 | 6, 1, 1, 6, 7, 8, 7, 4, 2, 5, 2, 6, 7, 8, 7, 1, 7, 1, 9, 4, 2, 6, 5, 3, 5, 3, 3, 6, 5, 2, 4, 9, 4, 5, 7, 1, 4, 3, 5, 5, 8, 3, 6, 7, 3, 9, 7, 7, 5, 5, 7, 1, 4, 4, 3, 9, 5, 1, 6, 2, 2, 6, 1, 6, 5, 4, 4, 7, 1, 8, 9, 9, 7, 4, 3, 8, 7, 5, 7, 4, 4, 5});
|
---|
| 68 | problem.Weights = new IntArray(new int[] {
|
---|
| 69 | 1, 9, 3, 6, 5, 3, 8, 1, 7, 4, 2, 1, 2, 7, 9, 9, 8, 4, 9, 2, 4, 8, 3, 7, 5, 7, 5, 5, 1, 9, 8, 7, 8, 9, 1, 3, 3, 8, 8, 5, 1, 2, 4, 3, 6, 9, 4, 4, 9, 7, 4, 5, 1, 9, 7, 6, 7, 4, 7, 1, 2, 1, 2, 9, 8, 6, 8, 4, 7, 6, 7, 5, 3, 9, 4, 7, 4, 6, 1, 2, 5, 4});
|
---|
| 70 | problem.Name = "Knapsack Problem";
|
---|
| 71 | problem.Description = "Represents a Knapsack problem.";
|
---|
| 72 | #endregion
|
---|
| 73 | #region Algorithm Configuration
|
---|
| 74 | ls.Name = "Local Search - Knapsack";
|
---|
| 75 | ls.Description = "A local search algorithm that solves a randomly generated Knapsack problem";
|
---|
| 76 | ls.Problem = problem;
|
---|
| 77 | ls.MaximumIterations.Value = 1000;
|
---|
| 78 | ls.MoveEvaluator = ls.MoveEvaluatorParameter.ValidValues
|
---|
| 79 | .OfType<KnapsackOneBitflipMoveEvaluator>()
|
---|
| 80 | .Single();
|
---|
| 81 | ls.MoveGenerator = ls.MoveGeneratorParameter.ValidValues
|
---|
| 82 | .OfType<ExhaustiveOneBitflipMoveGenerator>()
|
---|
| 83 | .Single();
|
---|
| 84 | ls.MoveMaker = ls.MoveMakerParameter.ValidValues
|
---|
| 85 | .OfType<OneBitflipMoveMaker>()
|
---|
| 86 | .Single();
|
---|
| 87 | ls.SampleSize.Value = 100;
|
---|
| 88 | ls.Seed.Value = 0;
|
---|
| 89 | ls.SetSeedRandomly.Value = true;
|
---|
| 90 | #endregion
|
---|
| 91 | ls.Engine = new ParallelEngine.ParallelEngine();
|
---|
| 92 | return ls;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|