Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Tests/HeuristicLab-3.3/Samples/LocalSearchKnapsackSampleTest.cs @ 18183

Last change on this file since 18183 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.0 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.LocalSearch;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Problems.Knapsack;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace HeuristicLab.Tests {
32  [TestClass]
33  public class LocalSearchKnapsackSampleTest {
34    private const string SampleFileName = "LS_Knapsack";
35    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreateLocalSearchKnapsackSampleTest() {
41      var ls = CreateLocalSearchKnapsackSample();
42      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
43      serializer.Serialize(ls, path);
44    }
45    [TestMethod]
46    [TestCategory("Samples.Execute")]
47    [TestProperty("Time", "medium")]
48    public void RunLocalSearchKnapsackSampleTest() {
49      var ls = CreateLocalSearchKnapsackSample();
50      ls.SetSeedRandomly.Value = false;
51      SamplesUtils.RunAlgorithm(ls);
52      Assert.AreEqual(345, SamplesUtils.GetDoubleResult(ls, "BestQuality"));
53      Assert.AreEqual(340.70731707317071, SamplesUtils.GetDoubleResult(ls, "CurrentAverageQuality"));
54      Assert.AreEqual(337, SamplesUtils.GetDoubleResult(ls, "CurrentWorstQuality"));
55      Assert.AreEqual(82000, SamplesUtils.GetIntResult(ls, "EvaluatedMoves"));
56    }
57
58    private LocalSearch CreateLocalSearchKnapsackSample() {
59      LocalSearch ls = new LocalSearch();
60      #region Problem Configuration
61      KnapsackProblem problem = new KnapsackProblem();
62      problem.BestKnownQuality = new DoubleValue(362);
63      problem.BestKnownSolution = new HeuristicLab.Encodings.BinaryVectorEncoding.BinaryVector(new bool[] {
64       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});
65      problem.EvaluatorParameter.Value = new KnapsackEvaluator();
66      problem.SolutionCreatorParameter.Value = new RandomBinaryVectorCreator();
67      problem.KnapsackCapacity.Value = 297;
68      problem.Maximization.Value = true;
69      problem.Penalty.Value = 1;
70      problem.Values = new IntArray(new int[] {
71  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});
72      problem.Weights = new IntArray(new int[] {
73 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});
74      problem.Name = "Knapsack Problem";
75      problem.Description = "Represents a Knapsack problem.";
76      #endregion
77      #region Algorithm Configuration
78      ls.Name = "Local Search - Knapsack";
79      ls.Description = "A local search algorithm that solves a randomly generated Knapsack problem";
80      ls.Problem = problem;
81      ls.MaximumIterations.Value = 1000;
82      ls.MoveEvaluator = ls.MoveEvaluatorParameter.ValidValues
83        .OfType<KnapsackOneBitflipMoveEvaluator>()
84        .Single();
85      ls.MoveGenerator = ls.MoveGeneratorParameter.ValidValues
86        .OfType<ExhaustiveOneBitflipMoveGenerator>()
87        .Single();
88      ls.MoveMaker = ls.MoveMakerParameter.ValidValues
89        .OfType<OneBitflipMoveMaker>()
90        .Single();
91      ls.SampleSize.Value = 100;
92      ls.Seed.Value = 0;
93      ls.SetSeedRandomly.Value = true;
94      #endregion
95      ls.Engine = new ParallelEngine.ParallelEngine();
96      return ls;
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.