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

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

#3073 Added constraints for ShapeConstraintsSampleTest and fixed values

File size: 4.5 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      Assert.AreEqual(0.317642788600248, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
49      Assert.AreEqual(40.9805778810063, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
50      Assert.AreEqual(3359.91748220025, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
51      Assert.AreEqual(150200, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
52    }
53
54    public static GeneticAlgorithm CreateShapeConstrainedRegressionSample() {
55      var alg = new GeneticAlgorithm();
56      var provider = new FeynmanSmallInstanceProvider(0);
57      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();
58      var problem = new ShapeConstrainedRegressionSingleObjectiveProblem();
59      problem.Load(provider.LoadData(instance));
60      var problemData = (IShapeConstrainedRegressionProblemData)problem.ProblemData;
61      problemData.ShapeConstraints.Add(new ShapeConstraint(new Interval(double.NegativeInfinity, 0), 1.0));
62      problemData.ShapeConstraints.Add(new ShapeConstraint("G", 1, new Interval(double.NegativeInfinity, 0), 1.0));
63      problemData.ShapeConstraints.Add(new ShapeConstraint("c", 1, new Interval(0, double.PositiveInfinity), 1.0));
64      problemData.ShapeConstraints.Add(new ShapeConstraint("m1", 1, new Interval(double.NegativeInfinity, 0), 1.0));
65      problemData.ShapeConstraints.Add(new ShapeConstraint("m2", 1, new Interval(double.NegativeInfinity, 0), 1.0));
66      problemData.ShapeConstraints.Add(new ShapeConstraint("r", 1, new Interval(0, double.PositiveInfinity), 1.0));
67
68      problemData.VariableRanges.SetInterval("G", new Interval(1, 2));
69      problemData.VariableRanges.SetInterval("c", new Interval(1, 2));
70      problemData.VariableRanges.SetInterval("m1", new Interval(1, 5));
71      problemData.VariableRanges.SetInterval("m2", new Interval(1, 5));
72      problemData.VariableRanges.SetInterval("r", new Interval(1, 2));
73      problem.ProblemData = problemData;
74
75
76      #region Algorithm Configuration
77      alg.Name = "Genetic Programming - Shape constrained Regression";
78      alg.Description = "A standard genetic programming algorithm to solve a shape constrained regression problem (Radiated gravitational wave power - Feynman instance)";
79      alg.Problem = problem;
80
81      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>
82        (alg, popSize: 500, elites: 1, maxGens: 300, mutationRate: 0.15, tournGroupSize: 3);
83
84      alg.Seed.Value = 0;
85      #endregion
86
87      alg.Engine = new ParallelEngine.ParallelEngine();
88      return alg;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.