[18151] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Threading.Tasks;
|
---|
| 7 | using HEAL.Attic;
|
---|
| 8 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
| 9 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
| 10 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 11 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
| 12 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 13 | using HeuristicLab.Selection;
|
---|
| 14 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 15 | using HeuristicLab.Optimization;
|
---|
| 16 |
|
---|
| 17 | namespace 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) {
|
---|
[18207] | 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);
|
---|
[18151] | 35 | Assert.AreEqual(5050, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
|
---|
| 36 | } else {
|
---|
[18207] | 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);
|
---|
[18151] | 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);
|
---|
[18207] | 57 | var problem = new StructureTemplateSymbolicRegressionProblem();
|
---|
[18151] | 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 | }
|
---|