Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/3.3/Tests/SymbolicRegressionTest.cs @ 5836

Last change on this file since 5836 was 5836, checked in by mkommend, 13 years ago

#1418: Fixed Symbolic Regression Unit Test.

File size: 3.4 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 SymbolicRegressionTest {
33    public SymbolicRegressionTest() { }
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    private EventWaitHandle trigger = new AutoResetEvent(false);
51    private Exception ex;
52
53    [TestMethod]
54    [DeploymentItem(@"GA_SymbReg.hl")]
55    public void SymbolicRegressionPerformanceTest() {
56      ex = null;
57      GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize("GA_SymbReg.hl");
58      ga.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ga_ExceptionOccurred);
59      ga.Stopped += new EventHandler(ga_Stopped);
60
61      ga.Prepare();
62      ga.Start();
63      trigger.WaitOne();
64      if (ex != null) throw ex;
65
66      TestContext.WriteLine("Runtime: {0}", ga.ExecutionTime.ToString());
67
68      double expectedBestQuality = 0.69403710871428215;
69      double expectedAverageQuality = 0.4189061979501727;
70      double expectedWorstQuality = 0;
71      double bestQuality = (ga.Results["CurrentBestQuality"].Value as DoubleValue).Value;
72      double averageQuality = (ga.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
73      double worstQuality = (ga.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
74
75      TestContext.WriteLine("");
76      TestContext.WriteLine("CurrentBestQuality: {0} (should be {1})", bestQuality, expectedBestQuality);
77      TestContext.WriteLine("CurrentAverageQuality: {0} (should be {1})", averageQuality, expectedAverageQuality);
78      TestContext.WriteLine("CurrentWorstQuality: {0} (should be {1})", worstQuality, expectedWorstQuality);
79
80      Assert.AreEqual(bestQuality, expectedBestQuality);
81      Assert.AreEqual(averageQuality, expectedAverageQuality);
82      Assert.AreEqual(worstQuality, expectedWorstQuality);
83    }
84
85    private void ga_ExceptionOccurred(object sender, EventArgs<Exception> e) {
86      ex = e.Value;
87    }
88
89    private void ga_Stopped(object sender, EventArgs e) {
90      trigger.Set();
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.