1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
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 {
|
---|
34 | private const string SampleFileName = "LS_Knapsack";
|
---|
35 |
|
---|
36 | [TestMethod]
|
---|
37 | [TestCategory("Samples.Create")]
|
---|
38 | [TestProperty("Time", "medium")]
|
---|
39 | public void CreateLocalSearchKnapsackSampleTest() {
|
---|
40 | var ls = CreateLocalSearchKnapsackSample();
|
---|
41 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
42 | XmlGenerator.Serialize(ls, path);
|
---|
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();
|
---|
61 | problem.BestKnownQuality = new DoubleValue(362);
|
---|
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});
|
---|
64 | problem.EvaluatorParameter.Value = new KnapsackEvaluator();
|
---|
65 | problem.SolutionCreatorParameter.Value = new RandomBinaryVectorCreator();
|
---|
66 | problem.KnapsackCapacity.Value = 297;
|
---|
67 | problem.Maximization.Value = true;
|
---|
68 | problem.Penalty.Value = 1;
|
---|
69 | problem.Values = new IntArray(new int[] {
|
---|
70 | 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});
|
---|
71 | problem.Weights = new IntArray(new int[] {
|
---|
72 | 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});
|
---|
73 | problem.Name = "Knapsack Problem";
|
---|
74 | problem.Description = "Represents a Knapsack problem.";
|
---|
75 | #endregion
|
---|
76 | #region Algorithm Configuration
|
---|
77 | ls.Name = "Local Search - Knapsack";
|
---|
78 | ls.Description = "A local search algorithm that solves a randomly generated Knapsack problem";
|
---|
79 | ls.Problem = problem;
|
---|
80 | ls.MaximumIterations.Value = 1000;
|
---|
81 | ls.MoveEvaluator = ls.MoveEvaluatorParameter.ValidValues
|
---|
82 | .OfType<KnapsackOneBitflipMoveEvaluator>()
|
---|
83 | .Single();
|
---|
84 | ls.MoveGenerator = ls.MoveGeneratorParameter.ValidValues
|
---|
85 | .OfType<ExhaustiveOneBitflipMoveGenerator>()
|
---|
86 | .Single();
|
---|
87 | ls.MoveMaker = ls.MoveMakerParameter.ValidValues
|
---|
88 | .OfType<OneBitflipMoveMaker>()
|
---|
89 | .Single();
|
---|
90 | ls.SampleSize.Value = 100;
|
---|
91 | ls.Seed.Value = 0;
|
---|
92 | ls.SetSeedRandomly.Value = true;
|
---|
93 | #endregion
|
---|
94 | ls.Engine = new ParallelEngine.ParallelEngine();
|
---|
95 | return ls;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|