Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Tests/HeuristicLab.Problems.QuadraticAssignment-3.3/QAPLIBInstancesTest.cs @ 6878

Last change on this file since 6878 was 6878, checked in by abeham, 13 years ago

#1614

  • updated branch from trunk
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 Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace HeuristicLab.Problems.QuadraticAssignment.Tests_33 {
32  [TestClass]
33  public class QAPLIBInstancesTest {
34    [TestMethod]
35    public void LoadAllEmbeddedInstances() {
36      QuadraticAssignmentProblem qap = new QuadraticAssignmentProblem();
37      StringBuilder failedInstances = new StringBuilder();
38      foreach (string instance in qap.EmbeddedInstances) {
39        try {
40          qap.LoadEmbeddedInstance(instance);
41        }
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        }
75        catch {
76          Assert.Fail("Not all instances load correctly");
77        }
78        if (qap.BestKnownSolution != null) {
79          double quality = double.NaN;
80          try {
81            quality = QAPEvaluator.Apply(qap.BestKnownSolution, qap.Weights, qap.Distances);
82          }
83          catch (Exception ex) {
84            failedInstances.AppendLine("An unknown problem occurred evaluating solution of instance " + instance + ": " + ex.Message);
85          }
86          if (!quality.IsAlmost(qap.BestKnownQuality.Value)) {
87            failedInstances.AppendLine(instance + ": Reported quality: " + qap.BestKnownQuality.Value.ToString() + ", evaluated fitness: " + quality.ToString() + ".");
88          }
89        } else if (qap.BestKnownQuality != null) {
90          failedInstances.AppendLine(instance + ": The solution failed to load, only the quality value is available!");
91        }
92
93      }
94      Assert.IsTrue(failedInstances.Length == 0, "Following instances report divergent fitness values: " + Environment.NewLine + failedInstances.ToString());
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.