#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Data;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Problems.GeneralizedQuadraticAssignment;
using HeuristicLab.Random;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests {
///
///This is a test class for GQAPNMoveEvaluatorTest and is intended
///to contain all GQAPNMoveEvaluatorTest Unit Tests
///
[TestClass()]
public class GQAPNMoveEvaluatorTest {
private const int Equipments = 10, Locations = 5;
private static DoubleMatrix symmetricWeights, asymmetricWeights, nonZeroDiagonalWeights;
private static DoubleMatrix symmetricDistances, asymmetricDistances, nonZeroDiagonalDistances;
private static DoubleMatrix installationCosts;
private static DoubleArray demands, capacities;
private static DoubleValue transportationCosts, overbookedCapacityPenalty;
private static IntegerVector assignment;
private static MersenneTwister random;
private TestContext testContextInstance;
///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext {
get { return testContextInstance; }
set { testContextInstance = value; }
}
#region Additional test attributes
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext) {
random = new MersenneTwister();
symmetricDistances = new DoubleMatrix(Locations, Locations);
symmetricWeights = new DoubleMatrix(Equipments, Equipments);
asymmetricDistances = new DoubleMatrix(Locations, Locations);
asymmetricWeights = new DoubleMatrix(Equipments, Equipments);
nonZeroDiagonalDistances = new DoubleMatrix(Locations, Locations);
nonZeroDiagonalWeights = new DoubleMatrix(Equipments, Equipments);
for (int i = 0; i < Equipments - 1; i++) {
for (int j = i + 1; j < Equipments; j++) {
symmetricWeights[i, j] = random.Next(Equipments * 100);
symmetricWeights[j, i] = symmetricWeights[i, j];
asymmetricWeights[i, j] = random.Next(Equipments * 100);
asymmetricWeights[j, i] = random.Next(Equipments * 100);
nonZeroDiagonalWeights[i, j] = random.Next(Equipments * 100);
nonZeroDiagonalWeights[j, i] = random.Next(Equipments * 100);
}
nonZeroDiagonalWeights[i, i] = random.Next(Equipments * 100);
}
for (int i = 0; i < Locations - 1; i++) {
for (int j = i + 1; j < Locations; j++) {
symmetricDistances[i, j] = random.Next(Locations * 100);
symmetricDistances[j, i] = symmetricDistances[i, j];
asymmetricDistances[i, j] = random.Next(Locations * 100);
asymmetricDistances[j, i] = random.Next(Locations * 100);
nonZeroDiagonalDistances[i, j] = random.Next(Locations * 100);
nonZeroDiagonalDistances[j, i] = random.Next(Locations * 100);
}
nonZeroDiagonalDistances[i, i] = random.Next(Locations * 100);
}
installationCosts = new DoubleMatrix(Equipments, Locations);
for (int i = 0; i < Equipments; i++) {
for (int j = 0; j < Locations; j++) {
installationCosts[i, j] = random.Next(0, 10);
}
}
demands = new DoubleArray(Equipments);
for (int i = 0; i < Equipments; i++) {
demands[i] = random.Next(1, 10);
}
capacities = new DoubleArray(Locations);
for (int j = 0; j < Locations; j++) {
capacities[j] = random.Next(1, 10) * ((double)Equipments / (double)Locations) * 1.5;
}
int index = random.Next(Locations);
if (nonZeroDiagonalDistances[index, index] == 0)
nonZeroDiagonalDistances[index, index] = random.Next(1, Equipments * 100);
index = random.Next(Equipments);
if (nonZeroDiagonalWeights[index, index] == 0)
nonZeroDiagonalWeights[index, index] = random.Next(1, Equipments * 100);
transportationCosts = new DoubleValue(random.NextDouble() * 10);
overbookedCapacityPenalty = new DoubleValue(1000 * random.NextDouble() + 100);
assignment = new IntegerVector(Equipments, random, 0, Locations);
}
#endregion
///
///A test for Evaluate
///
[TestMethod()]
public void EvaluateTest() {
for (int i = 0; i < 500; i++) {
NMove currentMove = StochasticNMoveSingleMoveGenerator.GenerateUpToN(random, assignment, 3, capacities);
IntegerVector prevAssignment = (IntegerVector)assignment.Clone();
NMoveMaker.Apply(assignment, currentMove);
double before = GQAPEvaluator.Evaluate(prevAssignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
double after = GQAPEvaluator.Evaluate(assignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
double moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, symmetricWeights, symmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on symmetric matrices: " + Environment.NewLine
+ "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
before = GQAPEvaluator.Evaluate(prevAssignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
after = GQAPEvaluator.Evaluate(assignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, asymmetricWeights, asymmetricDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on asymmetric matrices: " + Environment.NewLine
+ "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
before = GQAPEvaluator.Evaluate(prevAssignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
after = GQAPEvaluator.Evaluate(assignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
moveDiff = GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, nonZeroDiagonalWeights, nonZeroDiagonalDistances, installationCosts, demands, capacities, transportationCosts, overbookedCapacityPenalty);
Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on non-zero diagonal matrices: " + Environment.NewLine
+ "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
}
}
}
}