Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.Tests/HeuristicLab-3.3/GeneticAlgorithmTest.cs @ 18086

Last change on this file since 18086 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Algorithms.GeneticAlgorithm;
24using HeuristicLab.Common;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.Xml;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28
29namespace HeuristicLab.Tests {
30  [TestClass]
31  public class GeneticAlgorithmTest {
32    public GeneticAlgorithmTest() { }
33
34    private TestContext testContextInstance;
35    public TestContext TestContext {
36      get { return testContextInstance; }
37      set { testContextInstance = value; }
38    }
39
40    private Exception ex;
41
42    [TestMethod]
43    [TestCategory("General")]
44    [TestProperty("Time", "long")]
45    public void GeneticAlgorithmPerformanceTest() {
46      ex = null;
47      GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize(@"Test Resources\GA_TSP.hl");
48      ga.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ga_ExceptionOccurred);
49      ga.SetSeedRandomly.Value = false;
50      ga.Seed.Value = 0;
51
52      ga.Prepare();
53      ga.Start();
54      if (ex != null) throw ex;
55
56      TestContext.WriteLine("Runtime: {0}", ga.ExecutionTime.ToString());
57
58      double expectedBestQuality = 12332.0;
59      double expectedAverageQuality = 13123.2;
60      double expectedWorstQuality = 14538.0;
61      double bestQuality = (ga.Results["CurrentBestQuality"].Value as DoubleValue).Value;
62      double averageQuality = (ga.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
63      double worstQuality = (ga.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
64
65      TestContext.WriteLine("");
66      TestContext.WriteLine("CurrentBestQuality: {0} (should be {1})", bestQuality, expectedBestQuality);
67      TestContext.WriteLine("CurrentAverageQuality: {0} (should be {1})", averageQuality, expectedAverageQuality);
68      TestContext.WriteLine("CurrentWorstQuality: {0} (should be {1})", worstQuality, expectedWorstQuality);
69
70      Assert.AreEqual(bestQuality, expectedBestQuality);
71      Assert.AreEqual(averageQuality, expectedAverageQuality);
72      Assert.AreEqual(worstQuality, expectedWorstQuality);
73    }
74
75    private void ga_ExceptionOccurred(object sender, EventArgs<Exception> e) {
76      ex = e.Value;
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.