1 | using System.Linq;
|
---|
2 | using System.Threading;
|
---|
3 | using HeuristicLab.Data;
|
---|
4 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
5 | using HeuristicLab.Problems.GeneralizedQuadraticAssignment;
|
---|
6 | using HeuristicLab.Random;
|
---|
7 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
8 |
|
---|
9 | namespace UnitTests {
|
---|
10 | [TestClass]
|
---|
11 | public class ApproximateLocalSearchTest {
|
---|
12 |
|
---|
13 | [TestMethod]
|
---|
14 | public void ApproximateLocalSearchApplyTest() {
|
---|
15 | CollectionAssert.AreEqual(new [] { 2, 0, 1, 1, 2, 3, 0, 3, 0, 0 }, assignment.ToArray());
|
---|
16 |
|
---|
17 | var evaluation = instance.Evaluate(assignment);
|
---|
18 | Assert.AreEqual(3985258, evaluation.FlowCosts);
|
---|
19 | Assert.AreEqual(30, evaluation.InstallationCosts);
|
---|
20 | Assert.AreEqual(0, evaluation.ExcessDemand);
|
---|
21 |
|
---|
22 | var quality = instance.ToSingleObjective(evaluation);
|
---|
23 | Assert.AreEqual(15489822.781533258, quality, 1e-9);
|
---|
24 |
|
---|
25 | var evaluatedSolutions = 0;
|
---|
26 | ApproximateLocalSearch.Apply(random, assignment, ref quality,
|
---|
27 | ref evaluation, 10, 0.5, 100, instance,
|
---|
28 | out evaluatedSolutions);
|
---|
29 | Assert.AreEqual(61, evaluatedSolutions);
|
---|
30 | CollectionAssert.AreEqual(new[] { 2, 0, 0, 0, 2, 1, 0, 3, 0, 0 }, assignment.ToArray());
|
---|
31 | Assert.AreEqual(10167912.633734789, quality, 1e-9);
|
---|
32 | }
|
---|
33 |
|
---|
34 | private const int Equipments = 10, Locations = 5;
|
---|
35 | private static GQAPInstance instance;
|
---|
36 | private static IntegerVector assignment;
|
---|
37 | private static MersenneTwister random;
|
---|
38 |
|
---|
39 | [ClassInitialize()]
|
---|
40 | public static void MyClassInitialize(TestContext testContext) {
|
---|
41 | random = new MersenneTwister(42);
|
---|
42 | var symmetricDistances = new DoubleMatrix(Locations, Locations);
|
---|
43 | var symmetricWeights = new DoubleMatrix(Equipments, Equipments);
|
---|
44 | for (int i = 0; i < Equipments - 1; i++) {
|
---|
45 | for (int j = i + 1; j < Equipments; j++) {
|
---|
46 | symmetricWeights[i, j] = random.Next(Equipments * 100);
|
---|
47 | symmetricWeights[j, i] = symmetricWeights[i, j];
|
---|
48 | }
|
---|
49 | }
|
---|
50 | for (int i = 0; i < Locations - 1; i++) {
|
---|
51 | for (int j = i + 1; j < Locations; j++) {
|
---|
52 | symmetricDistances[i, j] = random.Next(Locations * 100);
|
---|
53 | symmetricDistances[j, i] = symmetricDistances[i, j];
|
---|
54 | }
|
---|
55 | }
|
---|
56 | var installationCosts = new DoubleMatrix(Equipments, Locations);
|
---|
57 | for (int i = 0; i < Equipments; i++) {
|
---|
58 | for (int j = 0; j < Locations; j++) {
|
---|
59 | installationCosts[i, j] = random.Next(0, 10);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | var demands = new DoubleArray(Equipments);
|
---|
63 | for (int i = 0; i < Equipments; i++) {
|
---|
64 | demands[i] = random.Next(1, 10);
|
---|
65 | }
|
---|
66 | var capacities = new DoubleArray(Locations);
|
---|
67 | for (int j = 0; j < Locations; j++) {
|
---|
68 | capacities[j] = random.Next(1, 10) * ((double)Equipments / (double)Locations) * 2;
|
---|
69 | }
|
---|
70 |
|
---|
71 | var transportationCosts = random.NextDouble() * 10;
|
---|
72 | var overbookedCapacityPenalty = 1000 * random.NextDouble() + 100;
|
---|
73 |
|
---|
74 | instance = new GQAPInstance() {
|
---|
75 | Capacities = capacities,
|
---|
76 | Demands = demands,
|
---|
77 | InstallationCosts = installationCosts,
|
---|
78 | PenaltyLevel = overbookedCapacityPenalty,
|
---|
79 | TransportationCosts = transportationCosts,
|
---|
80 | Weights = symmetricWeights,
|
---|
81 | Distances = symmetricDistances
|
---|
82 | };
|
---|
83 |
|
---|
84 | assignment = GreedyRandomizedSolutionCreator.CreateSolution(random, instance, 100, false, CancellationToken.None);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|