Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab/3.3/Tests/GeneticAlgorithmTest.cs @ 11299

Last change on this file since 11299 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.2 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.Threading;
24using HeuristicLab.Algorithms.GeneticAlgorithm;
25using HeuristicLab.Common;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.Xml;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace HeuristicLab_33.Tests {
31  [TestClass]
32  public class GeneticAlgorithmTest {
33    public GeneticAlgorithmTest() { }
34
35    private TestContext testContextInstance;
36
37    /// <summary>
38    ///Gets or sets the test context which provides
39    ///information about and functionality for the current test run.
40    ///</summary>
41    public TestContext TestContext {
42      get {
43        return testContextInstance;
44      }
45      set {
46        testContextInstance = value;
47      }
48    }
49
50    #region Additional test attributes
51    //
52    // You can use the following additional attributes as you write your tests:
53    //
54    // Use ClassInitialize to run code before running the first test in the class
55    // [ClassInitialize()]
56    // public static void MyClassInitialize(TestContext testContext) { }
57    //
58    // Use ClassCleanup to run code after all tests in a class have run
59    // [ClassCleanup()]
60    // public static void MyClassCleanup() { }
61    //
62    // Use TestInitialize to run code before running each test
63    // [TestInitialize()]
64    // public void MyTestInitialize() { }
65    //
66    // Use TestCleanup to run code after each test has run
67    // [TestCleanup()]
68    // public void MyTestCleanup() { }
69    //
70    #endregion
71
72    private EventWaitHandle trigger = new AutoResetEvent(false);
73    private Exception ex;
74
75    [TestMethod]
76    [DeploymentItem(@"GA_TSP.hl")]
77    public void GeneticAlgorithmPerformanceTest() {
78      ex = null;
79      GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize("GA_TSP.hl");
80      ga.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ga_ExceptionOccurred);
81      ga.Stopped += new EventHandler(ga_Stopped);
82      ga.SetSeedRandomly.Value = false;
83      ga.Seed.Value = 0;
84
85      ga.Prepare();
86      ga.Start();
87      trigger.WaitOne();
88      if (ex != null) throw ex;
89
90      TestContext.WriteLine("Runtime: {0}", ga.ExecutionTime.ToString());
91
92      double expectedBestQuality = 12332.0;
93      double expectedAverageQuality = 13123.2;
94      double expectedWorstQuality = 14538.0;
95      double bestQuality = (ga.Results["CurrentBestQuality"].Value as DoubleValue).Value;
96      double averageQuality = (ga.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
97      double worstQuality = (ga.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
98
99      TestContext.WriteLine("");
100      TestContext.WriteLine("CurrentBestQuality: {0} (should be {1})", bestQuality, expectedBestQuality);
101      TestContext.WriteLine("CurrentAverageQuality: {0} (should be {1})", averageQuality, expectedAverageQuality);
102      TestContext.WriteLine("CurrentWorstQuality: {0} (should be {1})", worstQuality, expectedWorstQuality);
103
104      Assert.AreEqual(bestQuality, expectedBestQuality);
105      Assert.AreEqual(averageQuality, expectedAverageQuality);
106      Assert.AreEqual(worstQuality, expectedWorstQuality);
107    }
108
109    private void ga_ExceptionOccurred(object sender, EventArgs<Exception> e) {
110      ex = e.Value;
111    }
112
113    private void ga_Stopped(object sender, EventArgs e) {
114      trigger.Set();
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.