Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18156 was 18156, checked in by dpiringe, 10 months ago

#3136

  • adapted the unit test RunStructureTemplateRegressionSampleTest to match the results
  • added the sample to the optimizer start page
File size: 4.1 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(1.2142319244409949E-06, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
33        Assert.AreEqual(120.13505532841884, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
34        Assert.AreEqual(8322.6086777405562, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"), 1E-8);
35        Assert.AreEqual(5050, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
36      } else {
37        Assert.AreEqual(1.2142319244408615E-06, SamplesUtils.GetDoubleResult(ga, "BestQuality"), 1E-8);
38        Assert.AreEqual(120.135055328418, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"), 1E-8);
39        Assert.AreEqual(8322.6086777405562, 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 StructuredSymbolicRegressionSingleObjectiveProblem();
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      var evaluator = problem.TreeEvaluatorParameter.ValidValues
71        .OfType<NMSESingleObjectiveConstraintsEvaluator>()
72        .First();
73      evaluator.OptimizeParameters = true;
74      problem.TreeEvaluatorParameter.Value = evaluator;
75
76      #region Algorithm Configuration
77      alg.Name = "Genetic Programming - Structure Template";
78      alg.Description = "A standard genetic programming algorithm to solve a regression problem with a predefined structure template (Sheet Bending Process)";
79      alg.Problem = problem;
80
81      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, MultiEncodingCrossover, MultiEncodingManipulator>
82        (alg, popSize: 100, elites: 1, maxGens: 50, mutationRate: 0.25, tournGroupSize: 3);
83
84      alg.Seed.Value = 0;
85      alg.Engine = new ParallelEngine.ParallelEngine();
86      #endregion
87
88      return alg;
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.