1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
25 | using HeuristicLab.Problems.GeneralizedQuadraticAssignment;
|
---|
26 | using HeuristicLab.Random;
|
---|
27 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
28 |
|
---|
29 | namespace UnitTests {
|
---|
30 | /// <summary>
|
---|
31 | ///This is a test class for GQAPNMoveEvaluatorTest and is intended
|
---|
32 | ///to contain all GQAPNMoveEvaluatorTest Unit Tests
|
---|
33 | ///</summary>
|
---|
34 | [TestClass()]
|
---|
35 | public class GQAPNMoveEvaluatorTest {
|
---|
36 | private const int Equipments = 10, Locations = 5;
|
---|
37 | private static DoubleMatrix symmetricWeights, asymmetricWeights, nonZeroDiagonalWeights;
|
---|
38 | private static DoubleMatrix symmetricDistances, asymmetricDistances, nonZeroDiagonalDistances;
|
---|
39 | private static DoubleMatrix installationCosts;
|
---|
40 | private static DoubleArray demands, capacities;
|
---|
41 | private static DoubleValue transportationCosts, overbookedCapacityPenalty;
|
---|
42 | private static IntegerVector assignment;
|
---|
43 | private static MersenneTwister random;
|
---|
44 |
|
---|
45 | private TestContext testContextInstance;
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | ///Gets or sets the test context which provides
|
---|
49 | ///information about and functionality for the current test run.
|
---|
50 | ///</summary>
|
---|
51 | public TestContext TestContext {
|
---|
52 | get { return testContextInstance; }
|
---|
53 | set { testContextInstance = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | #region Additional test attributes
|
---|
57 | [ClassInitialize()]
|
---|
58 | public static void MyClassInitialize(TestContext testContext) {
|
---|
59 | random = new MersenneTwister();
|
---|
60 | symmetricDistances = new DoubleMatrix(Locations, Locations);
|
---|
61 | symmetricWeights = new DoubleMatrix(Equipments, Equipments);
|
---|
62 | asymmetricDistances = new DoubleMatrix(Locations, Locations);
|
---|
63 | asymmetricWeights = new DoubleMatrix(Equipments, Equipments);
|
---|
64 | nonZeroDiagonalDistances = new DoubleMatrix(Locations, Locations);
|
---|
65 | nonZeroDiagonalWeights = new DoubleMatrix(Equipments, Equipments);
|
---|
66 | for (int i = 0; i < Equipments - 1; i++) {
|
---|
67 | for (int j = i + 1; j < Equipments; j++) {
|
---|
68 | symmetricWeights[i, j] = random.Next(Equipments * 100);
|
---|
69 | symmetricWeights[j, i] = symmetricWeights[i, j];
|
---|
70 | asymmetricWeights[i, j] = random.Next(Equipments * 100);
|
---|
71 | asymmetricWeights[j, i] = random.Next(Equipments * 100);
|
---|
72 | nonZeroDiagonalWeights[i, j] = random.Next(Equipments * 100);
|
---|
73 | nonZeroDiagonalWeights[j, i] = random.Next(Equipments * 100);
|
---|
74 | }
|
---|
75 | nonZeroDiagonalWeights[i, i] = random.Next(Equipments * 100);
|
---|
76 | }
|
---|
77 | for (int i = 0; i < Locations - 1; i++) {
|
---|
78 | for (int j = i + 1; j < Locations; j++) {
|
---|
79 | symmetricDistances[i, j] = random.Next(Locations * 100);
|
---|
80 | symmetricDistances[j, i] = symmetricDistances[i, j];
|
---|
81 | asymmetricDistances[i, j] = random.Next(Locations * 100);
|
---|
82 | asymmetricDistances[j, i] = random.Next(Locations * 100);
|
---|
83 | nonZeroDiagonalDistances[i, j] = random.Next(Locations * 100);
|
---|
84 | nonZeroDiagonalDistances[j, i] = random.Next(Locations * 100);
|
---|
85 | }
|
---|
86 | nonZeroDiagonalDistances[i, i] = random.Next(Locations * 100);
|
---|
87 | }
|
---|
88 | installationCosts = new DoubleMatrix(Equipments, Locations);
|
---|
89 | for (int i = 0; i < Equipments; i++) {
|
---|
90 | for (int j = 0; j < Locations; j++) {
|
---|
91 | installationCosts[i, j] = random.Next(0, 10);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | demands = new DoubleArray(Equipments);
|
---|
95 | for (int i = 0; i < Equipments; i++) {
|
---|
96 | demands[i] = random.Next(1, 10);
|
---|
97 | }
|
---|
98 | capacities = new DoubleArray(Locations);
|
---|
99 | for (int j = 0; j < Locations; j++) {
|
---|
100 | capacities[j] = random.Next(1, 10) * ((double)Equipments / (double)Locations) * 1.5;
|
---|
101 | }
|
---|
102 | int index = random.Next(Locations);
|
---|
103 | if (nonZeroDiagonalDistances[index, index] == 0)
|
---|
104 | nonZeroDiagonalDistances[index, index] = random.Next(1, Equipments * 100);
|
---|
105 | index = random.Next(Equipments);
|
---|
106 | if (nonZeroDiagonalWeights[index, index] == 0)
|
---|
107 | nonZeroDiagonalWeights[index, index] = random.Next(1, Equipments * 100);
|
---|
108 |
|
---|
109 | transportationCosts = new DoubleValue(random.NextDouble() * 10);
|
---|
110 | overbookedCapacityPenalty = new DoubleValue(1000 * random.NextDouble() + 100);
|
---|
111 | assignment = new IntegerVector(Equipments, random, 0, Locations);
|
---|
112 | }
|
---|
113 | #endregion
|
---|
114 |
|
---|
115 |
|
---|
116 | /// <summary>
|
---|
117 | ///A test for Evaluate
|
---|
118 | ///</summary>
|
---|
119 | [TestMethod()]
|
---|
120 | public void EvaluateTest() {
|
---|
121 | for (int i = 0; i < 500; i++) {
|
---|
122 | NMove currentMove = StochasticNMoveSingleMoveGenerator.GenerateUpToN(random, assignment, 3, capacities);
|
---|
123 | IntegerVector prevAssignment = (IntegerVector)assignment.Clone();
|
---|
124 | NMoveMaker.Apply(assignment, currentMove);
|
---|
125 | double before = GQAPEvaluator.Evaluate(prevAssignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
126 | double after = GQAPEvaluator.Evaluate(assignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
127 | double moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
128 | Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on symmetric matrices: " + Environment.NewLine
|
---|
129 | + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
|
---|
130 |
|
---|
131 | before = GQAPEvaluator.Evaluate(prevAssignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
132 | after = GQAPEvaluator.Evaluate(assignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
133 | moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
134 | Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on asymmetric matrices: " + Environment.NewLine
|
---|
135 | + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
|
---|
136 |
|
---|
137 | before = GQAPEvaluator.Evaluate(prevAssignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
138 | after = GQAPEvaluator.Evaluate(assignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
139 | moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
|
---|
140 | Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on non-zero diagonal matrices: " + Environment.NewLine
|
---|
141 | + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|