Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/UnitTests/GQAPNMoveEvaluatorTest.cs @ 11419

Last change on this file since 11419 was 7970, checked in by abeham, 12 years ago

#1614: restructured architecture to allow for different evaluator with different penalty strategies

File size: 7.9 KB
Line 
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
22using System;
23using HeuristicLab.Data;
24using HeuristicLab.Encodings.IntegerVectorEncoding;
25using HeuristicLab.Problems.GeneralizedQuadraticAssignment;
26using HeuristicLab.Random;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28
29namespace 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 double 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 = random.NextDouble() * 10;
110      overbookedCapacityPenalty = 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      var evaluator = new GQAPAdditivePenaltyEvaluator();
122      for (int i = 0; i < 500; i++) {
123        NMove currentMove = StochasticNMoveSingleMoveGenerator.GenerateUpToN(random, assignment, 3, capacities);
124        IntegerVector prevAssignment = (IntegerVector)assignment.Clone();
125        NMoveMaker.Apply(assignment, currentMove);
126        double before = evaluator.Evaluate(prevAssignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, 0);
127        double after = evaluator.Evaluate(assignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, 0);
128        double moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, 0, evaluator);
129        Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on symmetric matrices: " + Environment.NewLine
130          + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
131
132        before = evaluator.Evaluate(prevAssignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, 0);
133        after = evaluator.Evaluate(assignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, 0);
134        moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, 0, evaluator);
135        Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on asymmetric matrices: " + Environment.NewLine
136          + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
137
138        before = evaluator.Evaluate(prevAssignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, 0);
139        after = evaluator.Evaluate(assignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, 0);
140        moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, 0, evaluator);
141        Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on non-zero diagonal matrices: " + Environment.NewLine
142          + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
143      }
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.