Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/10/17 22:11:10 (6 years ago)
Author:
abeham
Message:

#1614: refactored code

  • change problem to derive from basic problem
  • using a combined instance class instead of individual parameters
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/UnitTests/GQAPNMoveEvaluatorTest.cs

    r7970 r15504  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3535  public class GQAPNMoveEvaluatorTest {
    3636    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;
     37    private static GQAPInstance symmetricInstance, asymmetricInstance, nonZeroDiagonalInstance;
    4238    private static IntegerVector assignment;
    4339    private static MersenneTwister random;
     
    5854    public static void MyClassInitialize(TestContext testContext) {
    5955      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);
     56      var symmetricDistances = new DoubleMatrix(Locations, Locations);
     57      var symmetricWeights = new DoubleMatrix(Equipments, Equipments);
     58      var asymmetricDistances = new DoubleMatrix(Locations, Locations);
     59      var asymmetricWeights = new DoubleMatrix(Equipments, Equipments);
     60      var nonZeroDiagonalDistances = new DoubleMatrix(Locations, Locations);
     61      var nonZeroDiagonalWeights = new DoubleMatrix(Equipments, Equipments);
    6662      for (int i = 0; i < Equipments - 1; i++) {
    6763        for (int j = i + 1; j < Equipments; j++) {
     
    8682        nonZeroDiagonalDistances[i, i] = random.Next(Locations * 100);
    8783      }
    88       installationCosts = new DoubleMatrix(Equipments, Locations);
     84      var installationCosts = new DoubleMatrix(Equipments, Locations);
    8985      for (int i = 0; i < Equipments; i++) {
    9086        for (int j = 0; j < Locations; j++) {
     
    9288        }
    9389      }
    94       demands = new DoubleArray(Equipments);
     90      var demands = new DoubleArray(Equipments);
    9591      for (int i = 0; i < Equipments; i++) {
    9692        demands[i] = random.Next(1, 10);
    9793      }
    98       capacities = new DoubleArray(Locations);
     94      var capacities = new DoubleArray(Locations);
    9995      for (int j = 0; j < Locations; j++) {
    10096        capacities[j] = random.Next(1, 10) * ((double)Equipments / (double)Locations) * 1.5;
     
    107103        nonZeroDiagonalWeights[index, index] = random.Next(1, Equipments * 100);
    108104
    109       transportationCosts = random.NextDouble() * 10;
    110       overbookedCapacityPenalty = 1000 * random.NextDouble() + 100;
     105      var transportationCosts = random.NextDouble() * 10;
     106      var overbookedCapacityPenalty = 1000 * random.NextDouble() + 100;
    111107      assignment = new IntegerVector(Equipments, random, 0, Locations);
     108
     109      symmetricInstance = new GQAPInstance() {
     110        Capacities = capacities,
     111        Demands = demands,
     112        InstallationCosts = installationCosts,
     113        PenaltyLevel = overbookedCapacityPenalty,
     114        TransportationCosts = transportationCosts,
     115        Weights = symmetricWeights,
     116        Distances = symmetricDistances
     117      };
     118      asymmetricInstance = new GQAPInstance() {
     119        Capacities = capacities,
     120        Demands = demands,
     121        InstallationCosts = installationCosts,
     122        PenaltyLevel = overbookedCapacityPenalty,
     123        TransportationCosts = transportationCosts,
     124        Weights = asymmetricWeights,
     125        Distances = asymmetricDistances
     126      };
     127      nonZeroDiagonalInstance = new GQAPInstance() {
     128        Capacities = capacities,
     129        Demands = demands,
     130        InstallationCosts = installationCosts,
     131        PenaltyLevel = overbookedCapacityPenalty,
     132        TransportationCosts = transportationCosts,
     133        Weights = nonZeroDiagonalWeights,
     134        Distances = nonZeroDiagonalDistances
     135      };
    112136    }
    113137    #endregion
     
    119143    [TestMethod()]
    120144    public void EvaluateTest() {
    121       var evaluator = new GQAPAdditivePenaltyEvaluator();
    122145      for (int i = 0; i < 500; i++) {
    123         NMove currentMove = StochasticNMoveSingleMoveGenerator.GenerateUpToN(random, assignment, 3, capacities);
     146        NMove currentMove = StochasticNMoveSingleMoveGenerator.GenerateUpToN(random, assignment, 3, symmetricInstance.Capacities);
    124147        IntegerVector prevAssignment = (IntegerVector)assignment.Clone();
    125148        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);
     149        var beforeEval = symmetricInstance.Evaluate(prevAssignment);
     150        double before = symmetricInstance.ToSingleObjective(beforeEval);
     151        double after = Evaluate(symmetricInstance, assignment);
     152        double moveDiff = symmetricInstance.ToSingleObjective(
     153          GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, beforeEval, symmetricInstance)
     154          ) - symmetricInstance.ToSingleObjective(beforeEval);
    129155        Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on symmetric matrices: " + Environment.NewLine
    130156          + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
    131157
    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);
     158        beforeEval = asymmetricInstance.Evaluate(prevAssignment);
     159        before = asymmetricInstance.ToSingleObjective(beforeEval);
     160        after = Evaluate(asymmetricInstance, assignment);
     161        moveDiff = asymmetricInstance.ToSingleObjective(
     162          GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, beforeEval, asymmetricInstance)
     163          ) - asymmetricInstance.ToSingleObjective(beforeEval);
    135164        Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on asymmetric matrices: " + Environment.NewLine
    136165          + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
    137166
    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);
     167        beforeEval = nonZeroDiagonalInstance.Evaluate(prevAssignment);
     168        before = nonZeroDiagonalInstance.ToSingleObjective(beforeEval);
     169        after = Evaluate(nonZeroDiagonalInstance, assignment);
     170        moveDiff = nonZeroDiagonalInstance.ToSingleObjective(
     171          GQAPNMoveEvaluator.Evaluate(currentMove, prevAssignment, beforeEval, nonZeroDiagonalInstance)
     172          ) - nonZeroDiagonalInstance.ToSingleObjective(beforeEval);
    141173        Assert.IsTrue(Math.Abs(moveDiff - (after - before)) < 1e-07, "Failed on non-zero diagonal matrices: " + Environment.NewLine
    142174          + "Quality changed from " + before + " to " + after + " (" + (after - before).ToString() + "), but move quality change was " + moveDiff + ".");
    143175      }
    144176    }
     177
     178    private double Evaluate(GQAPInstance instance, IntegerVector assignment) {
     179      return instance.ToSingleObjective(instance.Evaluate(assignment));
     180    }
    145181  }
    146182}
Note: See TracChangeset for help on using the changeset viewer.