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

Last change on this file since 5822 was 5822, checked in by gkronber, 11 years ago

#1418 added unit test for symbolic regression with standard GP.

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      ga.SetSeedRandomly.Value = false;
61      ga.Seed.Value = 0;
62
63      ga.Prepare();
64      ga.Start();
65      trigger.WaitOne();
66      if (ex != null) throw ex;
67
68      TestContext.WriteLine("Runtime: {0}", ga.ExecutionTime.ToString());
69
70      double expectedBestQuality = 0.69403710871428215;
71      double expectedAverageQuality = 0.4189061979501727;
72      double expectedWorstQuality = 0;
73      double bestQuality = (ga.Results["CurrentBestQuality"].Value as DoubleValue).Value;
74      double averageQuality = (ga.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
75      double worstQuality = (ga.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
76
77      TestContext.WriteLine("");
78      TestContext.WriteLine("CurrentBestQuality: {0} (should be {1})", bestQuality, expectedBestQuality);
79      TestContext.WriteLine("CurrentAverageQuality: {0} (should be {1})", averageQuality, expectedAverageQuality);
80      TestContext.WriteLine("CurrentWorstQuality: {0} (should be {1})", worstQuality, expectedWorstQuality);
81
82      Assert.AreEqual(bestQuality, expectedBestQuality);
83      Assert.AreEqual(averageQuality, expectedAverageQuality);
84      Assert.AreEqual(worstQuality, expectedWorstQuality);
85    }
86
87    private void ga_ExceptionOccurred(object sender, EventArgs<Exception> e) {
88      ex = e.Value;
89    }
90
91    private void ga_Stopped(object sender, EventArgs e) {
92      trigger.Set();
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.