#region License Information /* HeuristicLab * Copyright (C) 2002-2018 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.Linq; using System.Threading; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Random; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Tests { [TestClass] public class ApproximateLocalSearchTest { [TestMethod] public void ApproximateLocalSearchApplyTest() { CollectionAssert.AreEqual(new [] { 2, 0, 1, 1, 2, 3, 0, 3, 0, 0 }, assignment.ToArray()); var evaluation = instance.Evaluate(assignment); Assert.AreEqual(3985258, evaluation.FlowCosts); Assert.AreEqual(30, evaluation.InstallationCosts); Assert.AreEqual(0, evaluation.ExcessDemand); var quality = instance.ToSingleObjective(evaluation); Assert.AreEqual(15489822.781533258, quality, 1e-9); var evaluatedSolutions = 0; ApproximateLocalSearch.Apply(random, assignment, ref quality, ref evaluation, 10, 0.5, 100, instance, out evaluatedSolutions); Assert.AreEqual(61, evaluatedSolutions); CollectionAssert.AreEqual(new[] { 2, 0, 0, 0, 2, 1, 0, 3, 0, 0 }, assignment.ToArray()); Assert.AreEqual(10167912.633734789, quality, 1e-9); } private const int Equipments = 10, Locations = 5; private static GQAPInstance instance; private static IntegerVector assignment; private static MersenneTwister random; [ClassInitialize()] public static void MyClassInitialize(TestContext testContext) { random = new MersenneTwister(42); var symmetricDistances = new DoubleMatrix(Locations, Locations); var symmetricWeights = 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]; } } 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]; } } var 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); } } var demands = new DoubleArray(Equipments); for (int i = 0; i < Equipments; i++) { demands[i] = random.Next(1, 10); } var capacities = new DoubleArray(Locations); for (int j = 0; j < Locations; j++) { capacities[j] = random.Next(1, 10) * ((double)Equipments / (double)Locations) * 2; } var transportationCosts = random.NextDouble() * 10; var overbookedCapacityPenalty = 1000 * random.NextDouble() + 100; instance = new GQAPInstance() { Capacities = capacities, Demands = demands, InstallationCosts = installationCosts, PenaltyLevel = overbookedCapacityPenalty, TransportationCosts = transportationCosts, Weights = symmetricWeights, Distances = symmetricDistances }; assignment = GreedyRandomizedSolutionCreator.CreateSolution(random, instance, 100, false, CancellationToken.None); } } }