Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12136 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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    public void GeneticAlgorithmPerformanceTest() {
48      ex = null;
49      GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize(@"Test Resources\GA_TSP.hl");
50      ga.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ga_ExceptionOccurred);
51      ga.Stopped += new EventHandler(ga_Stopped);
52      ga.SetSeedRandomly.Value = false;
53      ga.Seed.Value = 0;
54
55      ga.Prepare();
56      ga.Start();
57      trigger.WaitOne();
58      if (ex != null) throw ex;
59
60      TestContext.WriteLine("Runtime: {0}", ga.ExecutionTime.ToString());
61
62      double expectedBestQuality = 12332.0;
63      double expectedAverageQuality = 13123.2;
64      double expectedWorstQuality = 14538.0;
65      double bestQuality = (ga.Results["CurrentBestQuality"].Value as DoubleValue).Value;
66      double averageQuality = (ga.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
67      double worstQuality = (ga.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
68
69      TestContext.WriteLine("");
70      TestContext.WriteLine("CurrentBestQuality: {0} (should be {1})", bestQuality, expectedBestQuality);
71      TestContext.WriteLine("CurrentAverageQuality: {0} (should be {1})", averageQuality, expectedAverageQuality);
72      TestContext.WriteLine("CurrentWorstQuality: {0} (should be {1})", worstQuality, expectedWorstQuality);
73
74      Assert.AreEqual(bestQuality, expectedBestQuality);
75      Assert.AreEqual(averageQuality, expectedAverageQuality);
76      Assert.AreEqual(worstQuality, expectedWorstQuality);
77    }
78
79    private void ga_ExceptionOccurred(object sender, EventArgs<Exception> e) {
80      ex = e.Value;
81    }
82
83    private void ga_Stopped(object sender, EventArgs e) {
84      trigger.Set();
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.