[16077] | 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 |
|
---|
| 22 | using System.Linq;
|
---|
[15512] | 23 | using System.Threading;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 26 | using HeuristicLab.Random;
|
---|
| 27 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 28 |
|
---|
[16077] | 29 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Tests {
|
---|
[15512] | 30 | [TestClass]
|
---|
| 31 | public class ApproximateLocalSearchTest {
|
---|
| 32 |
|
---|
| 33 | [TestMethod]
|
---|
| 34 | public void ApproximateLocalSearchApplyTest() {
|
---|
[15562] | 35 | CollectionAssert.AreEqual(new [] { 2, 0, 1, 1, 2, 3, 0, 3, 0, 0 }, assignment.ToArray());
|
---|
[15512] | 36 |
|
---|
| 37 | var evaluation = instance.Evaluate(assignment);
|
---|
[15562] | 38 | Assert.AreEqual(3985258, evaluation.FlowCosts);
|
---|
| 39 | Assert.AreEqual(30, evaluation.InstallationCosts);
|
---|
[15512] | 40 | Assert.AreEqual(0, evaluation.ExcessDemand);
|
---|
| 41 |
|
---|
[15553] | 42 | var quality = instance.ToSingleObjective(evaluation);
|
---|
[15562] | 43 | Assert.AreEqual(15489822.781533258, quality, 1e-9);
|
---|
[15512] | 44 |
|
---|
[15553] | 45 | var evaluatedSolutions = 0;
|
---|
| 46 | ApproximateLocalSearch.Apply(random, assignment, ref quality,
|
---|
[15562] | 47 | ref evaluation, 10, 0.5, 100, instance,
|
---|
[15553] | 48 | out evaluatedSolutions);
|
---|
[15562] | 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);
|
---|
[15512] | 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 | }
|
---|