Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Problems.QuadraticAssignment/3.3/Tests/QAPLIBInstancesTest.cs @ 6281

Last change on this file since 6281 was 5949, checked in by abeham, 14 years ago

#1330

  • some remaining problems with benchmark data, two instances had quality values and solutions that did not go together
File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Problems.QuadraticAssignment;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31
32namespace Tests {
33  [TestClass]
34  public class QAPLIBInstancesTest {
35    [TestMethod]
36    public void LoadAllEmbeddedInstances() {
37      QuadraticAssignmentProblem qap = new QuadraticAssignmentProblem();
38      StringBuilder failedInstances = new StringBuilder();
39      foreach (string instance in qap.EmbeddedInstances) {
40        try {
41          qap.LoadEmbeddedInstance(instance);
42        } catch (Exception ex) {
43          failedInstances.AppendLine(instance + ": " + ex.Message);
44        }
45      }
46      Assert.IsTrue(failedInstances.Length == 0, "Following instances failed to load: " + Environment.NewLine + failedInstances.ToString());
47    }
48
49    [TestMethod]
50    public void LoadAllEmbeddedSolutions() {
51      IEnumerable<string> solutionFiles = Assembly.GetAssembly(typeof(QuadraticAssignmentProblem))
52          .GetManifestResourceNames()
53          .Where(x => x.EndsWith(".sln"));
54      QAPLIBSolutionParser parser = new QAPLIBSolutionParser();
55      StringBuilder failedInstances = new StringBuilder();
56      foreach (string solution in solutionFiles) {
57        using (Stream stream = Assembly.GetAssembly(typeof(QuadraticAssignmentProblem)).GetManifestResourceStream(solution)) {
58          parser.Reset();
59          parser.Parse(stream, true);
60          if (parser.Error != null)
61            failedInstances.AppendLine(solution + ": " + parser.Error.Message);
62        }
63      }
64      Assert.IsTrue(failedInstances.Length == 0, "Following instances failed to load: " + Environment.NewLine + failedInstances.ToString());
65    }
66
67    [TestMethod]
68    public void TestReportedSolutionQuality() {
69      StringBuilder failedInstances = new StringBuilder();
70      QuadraticAssignmentProblem qap = new QuadraticAssignmentProblem();
71      foreach (string instance in qap.EmbeddedInstances) {
72        try {
73          qap.LoadEmbeddedInstance(instance);
74        } catch {
75          Assert.Fail("Not all instances load correctly");
76        }
77        if (qap.BestKnownSolution != null) {
78          double quality = double.NaN;
79          try {
80            quality = QAPEvaluator.Apply(qap.BestKnownSolution, qap.Weights, qap.Distances);
81          } catch (Exception ex) {
82            failedInstances.AppendLine("An unknown problem occurred evaluating solution of instance " + instance + ": " + ex.Message);
83          }
84          if (!quality.IsAlmost(qap.BestKnownQuality.Value)) {
85            failedInstances.AppendLine(instance + ": Reported quality: " + qap.BestKnownQuality.Value.ToString() + ", evaluated fitness: " + quality.ToString() + ".");
86          }
87        } else if (qap.BestKnownQuality != null) {
88          failedInstances.AppendLine(instance + ": The solution failed to load, only the quality value is available!");
89        }
90
91      }
92      Assert.IsTrue(failedInstances.Length == 0, "Following instances report divergent fitness values: " + Environment.NewLine + failedInstances.ToString());
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.