Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3073: refactoring ShapeConstrainedRegressionProblem as discussed with MKo and CHa

File size: 3.6 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
21using System.IO;
22using System.Linq;
23using HEAL.Attic;
24using HeuristicLab.Algorithms.GeneticAlgorithm;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
27using HeuristicLab.Problems.Instances.DataAnalysis;
28using HeuristicLab.Selection;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace HeuristicLab.Tests {
32  [TestClass]
33  public class ShapeConstrainedRegressionSampleTest {
34    private const string SampleFileName = "GA_Shape_Constrained_Regression";
35    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
36
37    [TestMethod]
38    [TestCategory("Samples.Create")]
39    [TestProperty("Time", "medium")]
40    public void CreateShapeConstrainedRegressionSampleTest() {
41      var ga = CreateShapeConstrainedRegressionSample();
42      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
43      serializer.Serialize(ga, path);
44    }
45    [TestMethod]
46    [TestCategory("Samples.Execute")]
47    [TestProperty("Time", "long")]
48    public void RunShapeConstrainedRegressionSampleTest() {
49      var ga = CreateShapeConstrainedRegressionSample();
50      ga.SetSeedRandomly.Value = false;
51      SamplesUtils.RunAlgorithm(ga);
52
53      Assert.AreEqual(0.0780908301423225, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
54      Assert.AreEqual(0.381314019205066, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
55      Assert.AreEqual(1.0000000000000002, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
56      Assert.AreEqual(150200, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
57    }
58
59    public static GeneticAlgorithm CreateShapeConstrainedRegressionSample() {
60      var alg = new GeneticAlgorithm();
61      var provider = new FeynmanSmallInstanceProvider(0);
62      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();
63      var problem = new ShapeConstrainedRegressionSingleObjectiveProblem();
64      problem.Load(provider.LoadData(instance));
65
66      #region Algorithm Configuration
67      alg.Name = "Genetic Programming - Shape constrained Regression";
68      alg.Description = "A standard genetic programming algorithm to solve a shape constrained regression problem (Radiated gravitational wave power - Feynman instance)";
69      alg.Problem = problem;
70
71      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>
72        (alg, popSize: 500, elites: 1, maxGens: 300, mutationRate: 0.15, tournGroupSize: 3);
73
74      alg.Seed.Value = 0;
75      #endregion
76
77      alg.Engine = new ParallelEngine.ParallelEngine();
78      return alg;
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.