Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18140 was 18132, checked in by gkronber, 3 years ago

#3140: merged r18091:18131 from branch to trunk

File size: 5.3 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 = "GP_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.035536903914644882, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
50        Assert.AreEqual(26.707437555596698, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
51        Assert.AreEqual(3294.1754151628993, 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    [TestMethod]
62    [TestCategory("Samples.Create")]
63    [TestProperty("Time", "medium")]
64    public void CreateShapeConstrainedRegressionSampleTest() {
65      var ga = CreateShapeConstrainedRegressionSample();
66      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
67      serializer.Serialize(ga, path);
68    }
69
70    public static GeneticAlgorithm CreateShapeConstrainedRegressionSample() {
71      var alg = new GeneticAlgorithm();
72      var provider = new FeynmanSmallInstanceProvider(0);
73      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();
74      var problem = new ShapeConstrainedRegressionSingleObjectiveProblem();
75      problem.Load(provider.LoadData(instance));
76      var problemData = (IShapeConstrainedRegressionProblemData)problem.ProblemData;
77      problemData.ShapeConstraints.Add(new ShapeConstraint(new Interval(double.NegativeInfinity, 0), 1.0));
78      problemData.ShapeConstraints.Add(new ShapeConstraint("G", 1, new Interval(double.NegativeInfinity, 0), 1.0));
79      problemData.ShapeConstraints.Add(new ShapeConstraint("c", 1, new Interval(0, double.PositiveInfinity), 1.0));
80      problemData.ShapeConstraints.Add(new ShapeConstraint("m1", 1, new Interval(double.NegativeInfinity, 0), 1.0));
81      problemData.ShapeConstraints.Add(new ShapeConstraint("m2", 1, new Interval(double.NegativeInfinity, 0), 1.0));
82      problemData.ShapeConstraints.Add(new ShapeConstraint("r", 1, new Interval(0, double.PositiveInfinity), 1.0));
83
84      problemData.VariableRanges.SetInterval("G", new Interval(1, 2));
85      problemData.VariableRanges.SetInterval("c", new Interval(1, 2));
86      problemData.VariableRanges.SetInterval("m1", new Interval(1, 5));
87      problemData.VariableRanges.SetInterval("m2", new Interval(1, 5));
88      problemData.VariableRanges.SetInterval("r", new Interval(1, 2));
89      problem.ProblemData = problemData;
90
91
92      #region Algorithm Configuration
93      alg.Name = "Genetic Programming - Shape-constrained Regression";
94      alg.Description = "A standard genetic programming algorithm to solve a shape constrained regression problem (Radiated gravitational wave power - Feynman instance)";
95      alg.Problem = problem;
96
97      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>
98        (alg, popSize: 500, elites: 1, maxGens: 300, mutationRate: 0.15, tournGroupSize: 3);
99
100      alg.Seed.Value = 0;
101      #endregion
102
103      alg.Engine = new ParallelEngine.ParallelEngine();
104
105      return alg;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.