source: branches/2936_GQAPIntegration/HeuristicLab.Tests/HeuristicLab.Problems.GeneralizedQuadraticAssignment-3.3/ApproximateLocalSearchTest.cs @ 16077

Last change on this file since 16077 was 16077, checked in by abeham, 12 months ago

#2936: Added integration branch

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
23using System.Threading;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.IntegerVectorEncoding;
26using HeuristicLab.Random;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28
29namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Tests {
30  [TestClass]
31  public class ApproximateLocalSearchTest {
32
33    [TestMethod]
34    public void ApproximateLocalSearchApplyTest() {
35      CollectionAssert.AreEqual(new [] { 2, 0, 1, 1, 2, 3, 0, 3, 0, 0 }, assignment.ToArray());
36
37      var evaluation = instance.Evaluate(assignment);
38      Assert.AreEqual(3985258, evaluation.FlowCosts);
39      Assert.AreEqual(30, evaluation.InstallationCosts);
40      Assert.AreEqual(0, evaluation.ExcessDemand);
41
42      var quality = instance.ToSingleObjective(evaluation);
43      Assert.AreEqual(15489822.781533258, quality, 1e-9);
44
45      var evaluatedSolutions = 0;
46      ApproximateLocalSearch.Apply(random, assignment, ref quality,
47        ref evaluation, 10, 0.5, 100, instance,
48        out evaluatedSolutions);
49      Assert.AreEqual(61, evaluatedSolutions);
50      CollectionAssert.AreEqual(new[] { 2, 0, 0, 0, 2, 1, 0, 3, 0, 0 }, assignment.ToArray());
51      Assert.AreEqual(10167912.633734789, quality, 1e-9);
52    }
53
54    private const int Equipments = 10, Locations = 5;
55    private static GQAPInstance instance;
56    private static IntegerVector assignment;
57    private static MersenneTwister random;
58   
59    [ClassInitialize()]
60    public static void MyClassInitialize(TestContext testContext) {
61      random = new MersenneTwister(42);
62      var symmetricDistances = new DoubleMatrix(Locations, Locations);
63      var symmetricWeights = new DoubleMatrix(Equipments, Equipments);
64      for (int i = 0; i < Equipments - 1; i++) {
65        for (int j = i + 1; j < Equipments; j++) {
66          symmetricWeights[i, j] = random.Next(Equipments * 100);
67          symmetricWeights[j, i] = symmetricWeights[i, j];
68        }
69      }
70      for (int i = 0; i < Locations - 1; i++) {
71        for (int j = i + 1; j < Locations; j++) {
72          symmetricDistances[i, j] = random.Next(Locations * 100);
73          symmetricDistances[j, i] = symmetricDistances[i, j];
74        }
75      }
76      var installationCosts = new DoubleMatrix(Equipments, Locations);
77      for (int i = 0; i < Equipments; i++) {
78        for (int j = 0; j < Locations; j++) {
79          installationCosts[i, j] = random.Next(0, 10);
80        }
81      }
82      var demands = new DoubleArray(Equipments);
83      for (int i = 0; i < Equipments; i++) {
84        demands[i] = random.Next(1, 10);
85      }
86      var capacities = new DoubleArray(Locations);
87      for (int j = 0; j < Locations; j++) {
88        capacities[j] = random.Next(1, 10) * ((double)Equipments / (double)Locations) * 2;
89      }
90
91      var transportationCosts = random.NextDouble() * 10;
92      var overbookedCapacityPenalty = 1000 * random.NextDouble() + 100;
93
94      instance = new GQAPInstance() {
95        Capacities = capacities,
96        Demands = demands,
97        InstallationCosts = installationCosts,
98        PenaltyLevel = overbookedCapacityPenalty,
99        TransportationCosts = transportationCosts,
100        Weights = symmetricWeights,
101        Distances = symmetricDistances
102      };
103
104      assignment = GreedyRandomizedSolutionCreator.CreateSolution(random, instance, 100, false, CancellationToken.None);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.