Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3136_Structural_GP/HeuristicLab.Tests/HeuristicLab-3.3/Samples/StructureTemplateRegressionSampleTest.cs @ 18207

Last change on this file since 18207 was 18207, checked in by dpiringe, 8 months ago

#3136

  • updated test cases for StructureTemplateSymbolicRegressionProblem
  • updated optimizer template for StructureTemplateSymbolicRegressionProblem
  • updated project file for HeuristicLab.Problems.DataAnalysis.Symbolic.Regression (forgot to include last commit)
File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using HEAL.Attic;
8using HeuristicLab.Algorithms.GeneticAlgorithm;
9using HeuristicLab.Problems.Instances.DataAnalysis;
10using Microsoft.VisualStudio.TestTools.UnitTesting;
11using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
12using HeuristicLab.Problems.DataAnalysis.Symbolic;
13using HeuristicLab.Selection;
14using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
15using HeuristicLab.Optimization;
16
17namespace HeuristicLab.Tests {
18  [TestClass]
19  public class StructureTemplateRegressionSampleTest {
20    private const string SampleFileName = "GP_Structure_Template_Regression";
21    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
22
23    [TestMethod]
24    [TestCategory("Samples.Execute")]
25    [TestProperty("Time", "long")]
26    public void RunStructureTemplateRegressionSampleTest() {
27      var ga = CreateStructureTemplateRegressionSample();
28      ga.SetSeedRandomly.Value = false;
29      SamplesUtils.RunAlgorithm(ga);
30
31      if (Environment.Is64BitProcess) {
32        Assert.AreEqual(5.0286947997733353E-07, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
33        Assert.AreEqual(79.100605440090362, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
34        Assert.AreEqual(7905.465554758197, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
35        Assert.AreEqual(5050, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
36      } else {
37        Assert.AreEqual(5.45285034915826E-07, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
38        Assert.AreEqual(0.25447047591366467, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
39        Assert.AreEqual(23.211139545787372, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
40        Assert.AreEqual(5050, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
41      }
42    }
43
44    [TestMethod]
45    [TestCategory("Samples.Create")]
46    [TestProperty("Time", "medium")]
47    public void CreateStructureTemplateRegressionSampleTest() {
48      var ga = CreateStructureTemplateRegressionSample();
49      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
50      serializer.Serialize(ga, path);
51    }
52
53    public static GeneticAlgorithm CreateStructureTemplateRegressionSample() {
54      var alg = new GeneticAlgorithm();
55      var provider = new PhysicsInstanceProvider(seed: 0);
56      var descriptor = new SheetBendingProcess(seed: 0);
57      var problem = new StructureTemplateSymbolicRegressionProblem();
58      problem.Load(provider.LoadData(descriptor));
59      problem.StructureTemplate.Template =
60        "(" +
61          "(210000 / (210000 + h)) * ((sigma_y * t * t) / (wR * Rt * t)) + " +
62          "PlasticHardening(_) - Elasticity(_)" +
63        ")" +
64        " * C(_)";
65      foreach(var subFunction in problem.StructureTemplate.SubFunctions) {
66        subFunction.MaximumSymbolicExpressionTreeDepth = 8;
67        subFunction.MaximumSymbolicExpressionTreeLength = 20;
68        subFunction.Grammar = new ArithmeticExpressionGrammar();
69      }
70
71      #region Algorithm Configuration
72      alg.Name = "Genetic Programming - Structure Template";
73      alg.Description = "A standard genetic programming algorithm to solve a regression problem with a predefined structure template (Sheet Bending Process)";
74      alg.Problem = problem;
75
76      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiEncodingCrossover, MultiEncodingManipulator>
77        (alg, popSize: 100, elites: 1, maxGens: 50, mutationRate: 0.25, tournGroupSize: 3);
78
79      alg.Seed.Value = 0;
80      alg.Engine = new ParallelEngine.ParallelEngine();
81      #endregion
82
83      return alg;
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.