Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/GeneticAlgorithmTest.cs @ 9922

Last change on this file since 9922 was 9885, checked in by mkommend, 11 years ago

#2088: Merged all changesets regarding the unit test restructuring in the stable branch.

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