Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Tests/HeuristicLab-3.3/Samples/LocalSearchKnapsackSampleTest.cs @ 11507

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