1 | using System;
|
---|
2 | using System.Text;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Problems.QuadraticAssignment;
|
---|
5 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
6 |
|
---|
7 | namespace Tests {
|
---|
8 | [TestClass]
|
---|
9 | public class QAPLIBInstancesTest {
|
---|
10 | [TestMethod]
|
---|
11 | public void LoadAllEmbeddedInstances() {
|
---|
12 | QuadraticAssignmentProblem qap = new QuadraticAssignmentProblem();
|
---|
13 | StringBuilder failedInstances = new StringBuilder();
|
---|
14 | foreach (string instance in qap.EmbeddedInstances) {
|
---|
15 | try {
|
---|
16 | qap.LoadEmbeddedInstance(instance);
|
---|
17 | } catch (Exception ex) {
|
---|
18 | failedInstances.AppendLine(instance + ": " + ex.Message);
|
---|
19 | }
|
---|
20 | }
|
---|
21 | Assert.IsTrue(failedInstances.Length == 0, "Following instances failed to load: " + Environment.NewLine + failedInstances.ToString());
|
---|
22 | }
|
---|
23 |
|
---|
24 | [TestMethod]
|
---|
25 | public void TestReportedSolutionQuality() {
|
---|
26 | StringBuilder failedInstances = new StringBuilder();
|
---|
27 | QuadraticAssignmentProblem qap = new QuadraticAssignmentProblem();
|
---|
28 | foreach (string instance in qap.EmbeddedInstances) {
|
---|
29 | try {
|
---|
30 | qap.LoadEmbeddedInstance(instance);
|
---|
31 | } catch {
|
---|
32 | Assert.Fail("Not all instances load correctly");
|
---|
33 | }
|
---|
34 | if (qap.BestKnownSolution != null) {
|
---|
35 | double quality = double.NaN;
|
---|
36 | try {
|
---|
37 | quality = QAPEvaluator.Apply(qap.BestKnownSolution, qap.Weights, qap.Distances);
|
---|
38 | } catch (Exception ex) {
|
---|
39 | failedInstances.AppendLine("An unknown problem occurred evaluating solution of instance " + instance + ": " + ex.Message);
|
---|
40 | }
|
---|
41 | if (!quality.IsAlmost(qap.BestKnownQuality.Value)) {
|
---|
42 | failedInstances.AppendLine(instance + ": Reported quality: " + qap.BestKnownQuality.Value.ToString() + ", evaluated fitness: " + quality.ToString() + ".");
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | }
|
---|
47 | Assert.IsTrue(failedInstances.Length == 0, "Following instances report divergent fitness values: " + Environment.NewLine + failedInstances.ToString());
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|