Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GeSymbolicRegressionSampleTest.cs @ 11514

Last change on this file since 11514 was 11514, checked in by jkarder, 9 years ago

#2211:

  • updated/added unit tests
    • added AssemblyInitialize method to load all plugins, create output directories for (script) samples and initialize the MainForm
    • script code is now stored in test resource files
    • refactored unit tests
  • updated (script) samples
  • added Test.cmd
File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.IO;
23using System.Linq;
24using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;
25using HeuristicLab.Persistence.Default.Xml;
26using HeuristicLab.Problems.DataAnalysis.Symbolic;
27using HeuristicLab.Problems.Instances.DataAnalysis;
28using HeuristicLab.Selection;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace HeuristicLab.Tests {
32  [TestClass]
33  public class GeSymbolicRegressionSampleTest {
34    #region artificial ant
35    private const string GeArtificialAntSampleFileName = "GE_ArtificialAnt";
36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreateGeArtificialAntSampleTest() {
41      var geaa = CreateGeArtificialAntSample();
42      string path = Path.Combine(SamplesUtils.SamplesDirectory, GeArtificialAntSampleFileName + SamplesUtils.SampleFileExtension);
43      XmlGenerator.Serialize(geaa, path);
44    }
45
46    [TestMethod]
47    [TestCategory("Samples.Execute")]
48    [TestProperty("Time", "long")]
49    public void RunGeArtificalAntSampleTest() {
50      var ga = CreateGeArtificialAntSample();
51      ga.SetSeedRandomly.Value = false;
52      SamplesUtils.RunAlgorithm(ga);
53    }
54
55    public OffspringSelectionGeneticAlgorithm CreateGeArtificialAntSample() {
56      OffspringSelectionGeneticAlgorithm ga = new OffspringSelectionGeneticAlgorithm();
57
58      #region Problem Configuration
59      var problem = new HeuristicLab.Problems.GrammaticalEvolution.GEArtificialAntProblem();
60      #endregion
61      #region Algorithm Configuration
62      ga.Name = "Grammatical Evolution - Artificial Ant (SantaFe)";
63      ga.Description = "Grammatical evolution algorithm for solving a artificial ant problem";
64      ga.Problem = problem;
65      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, Encodings.IntegerVectorEncoding.SinglePointCrossover, Encodings.IntegerVectorEncoding.UniformOnePositionManipulator>(
66        ga, 200, 1, 50, 0.05, 200);
67      #endregion
68
69      return ga;
70    }
71    #endregion
72
73    #region symbolic regression
74    private const string GeSymbolicRegressionSampleFileName = "GE_SymbReg";
75
76    [TestMethod]
77    [TestCategory("Samples.Create")]
78    [TestProperty("Time", "medium")]
79    public void CreateGeSymbolicRegressionSampleTest() {
80      var geSymbReg = CreateGeSymbolicRegressionSample();
81      string path = Path.Combine(SamplesUtils.SamplesDirectory, GeSymbolicRegressionSampleFileName + SamplesUtils.SampleFileExtension);
82      XmlGenerator.Serialize(geSymbReg, path);
83    }
84
85    [TestMethod]
86    [TestCategory("Samples.Execute")]
87    [TestProperty("Time", "long")]
88    public void RunGeSymbolicRegressionSampleTest() {
89      var ga = CreateGeSymbolicRegressionSample();
90      ga.SetSeedRandomly.Value = false;
91      SamplesUtils.RunAlgorithm(ga);
92    }
93
94    public OffspringSelectionGeneticAlgorithm CreateGeSymbolicRegressionSample() {
95      var ga = new OffspringSelectionGeneticAlgorithm();
96
97      #region Problem Configuration
98      var problem = new HeuristicLab.Problems.GrammaticalEvolution.GESymbolicRegressionSingleObjectiveProblem();
99
100      #endregion
101      #region Algorithm Configuration
102      ga.Name = "Grammatical Evolution - Symbolic Regression (Poly-10)";
103      ga.Description = "Grammatical evolution algorithm for solving a symbolic regression problem problem";
104      ga.Problem = problem;
105      problem.Load(new PolyTen().GenerateRegressionData());
106
107      // must occur after loading problem data because the grammar creates symbols for random constants once the data is loaded
108      var consts = problem.SymbolicExpressionTreeGrammar.AllowedSymbols.OfType<Constant>().ToList();
109      foreach (var c in consts) {
110        problem.SymbolicExpressionTreeGrammar.RemoveSymbol(c);
111      }
112
113      SamplesUtils.ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, Encodings.IntegerVectorEncoding.SinglePointCrossover, Encodings.IntegerVectorEncoding.UniformOnePositionManipulator>(
114        ga, 1000, 1, 50, 0.05, 200);
115      #endregion
116
117      return ga;
118    }
119    #endregion
120  }
121}
Note: See TracBrowser for help on using the repository browser.