source: trunk/HeuristicLab.Tests/HeuristicLab-3.3/Samples/ShapeConstrainedRegressionSampleTest.cs @ 17968

Last change on this file since 17968 was 17968, checked in by chaider, 5 months ago

#3073 Added different values in test case for x64/86 environments

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.IO;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Algorithms.GeneticAlgorithm;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Problems.DataAnalysis;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
30using HeuristicLab.Problems.Instances.DataAnalysis;
31using HeuristicLab.Selection;
32using Microsoft.VisualStudio.TestTools.UnitTesting;
33
34namespace HeuristicLab.Tests {
35  [TestClass]
36  public class ShapeConstrainedRegressionSampleTest {
37    private const string SampleFileName = "GA_Shape_Constrained_Regression";
38    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
39
40    [TestMethod]
41    [TestCategory("Samples.Execute")]
42    [TestProperty("Time", "long")]
43    public void RunShapeConstrainedRegressionSampleTest() {
44      var ga = CreateShapeConstrainedRegressionSample();
45      ga.SetSeedRandomly.Value = false;
46      SamplesUtils.RunAlgorithm(ga);
47
48      if (Environment.Is64BitProcess) {
49        Assert.AreEqual(0.355347729912352, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
50        Assert.AreEqual(27.6606834433137, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
51        Assert.AreEqual(3359.91748220025, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
52        Assert.AreEqual(150200, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
53      } else {
54        Assert.AreEqual(0.317642788600248, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
55        Assert.AreEqual(40.9805778810063, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
56        Assert.AreEqual(3359.91748220025, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
57        Assert.AreEqual(150200, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
58      }
59    }
60
61    public static GeneticAlgorithm CreateShapeConstrainedRegressionSample() {
62      var alg = new GeneticAlgorithm();
63      var provider = new FeynmanSmallInstanceProvider(0);
64      var instance = provider.GetDataDescriptors().Where(x => x.Name.Contains("Radiated gravitational wave power: -32/5*G**4/c**5*(m1*m2)**2*(m1+m2)/r**5 | no noise")).Single();
65      var problem = new ShapeConstrainedRegressionSingleObjectiveProblem();
66      problem.Load(provider.LoadData(instance));
67      var problemData = (IShapeConstrainedRegressionProblemData)problem.ProblemData;
68      problemData.ShapeConstraints.Add(new ShapeConstraint(new Interval(double.NegativeInfinity, 0), 1.0));
69      problemData.ShapeConstraints.Add(new ShapeConstraint("G", 1, new Interval(double.NegativeInfinity, 0), 1.0));
70      problemData.ShapeConstraints.Add(new ShapeConstraint("c", 1, new Interval(0, double.PositiveInfinity), 1.0));
71      problemData.ShapeConstraints.Add(new ShapeConstraint("m1", 1, new Interval(double.NegativeInfinity, 0), 1.0));
72      problemData.ShapeConstraints.Add(new ShapeConstraint("m2", 1, new Interval(double.NegativeInfinity, 0), 1.0));
73      problemData.ShapeConstraints.Add(new ShapeConstraint("r", 1, new Interval(0, double.PositiveInfinity), 1.0));
74
75      problemData.VariableRanges.SetInterval("G", new Interval(1, 2));
76      problemData.VariableRanges.SetInterval("c", new Interval(1, 2));
77      problemData.VariableRanges.SetInterval("m1", new Interval(1, 5));
78      problemData.VariableRanges.SetInterval("m2", new Interval(1, 5));
79      problemData.VariableRanges.SetInterval("r", new Interval(1, 2));
80      problem.ProblemData = problemData;
81
82
83      #region Algorithm Configuration
84      alg.Name = "Genetic Programming - Shape constrained Regression";
85      alg.Description = "A standard genetic programming algorithm to solve a shape constrained regression problem (Radiated gravitational wave power - Feynman instance)";
86      alg.Problem = problem;
87
88      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>
89        (alg, popSize: 500, elites: 1, maxGens: 300, mutationRate: 0.15, tournGroupSize: 3);
90
91      alg.Seed.Value = 0;
92      #endregion
93
94      alg.Engine = new ParallelEngine.ParallelEngine();
95      return alg;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.