Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs @ 18151

Last change on this file since 18151 was 18151, checked in by dpiringe, 3 years ago

#3136

  • fixed eventhandler reregister after deserialisazion/cloning
  • added a test case for StructuredSymbolicRegressionSingleObjectiveProblem
  • changed the usage of a Dictionary to List
File size: 13.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HEAL.Attic;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
9using HeuristicLab.Optimization;
10using HeuristicLab.Parameters;
11using HeuristicLab.PluginInfrastructure;
12using HeuristicLab.Problems.Instances;
13using HeuristicLab.Problems.Instances.DataAnalysis;
14
15namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
16  [StorableType("7464E84B-65CC-440A-91F0-9FA920D730F9")]
17  [Item(Name = "Structured Symbolic Regression Single Objective Problem (single-objective)", Description = "A problem with a structural definition and unfixed subfunctions.")]
18  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 150)]
19  public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData> {
20
21    #region Constants
22    private const string TreeEvaluatorParameterName = "TreeEvaluator";
23    private const string ProblemDataParameterName = "ProblemData";
24    private const string StructureTemplateParameterName = "Structure Template";
25    private const string InterpreterParameterName = "Interpreter";
26    private const string EstimationLimitsParameterName = "EstimationLimits";
27    private const string BestTrainingSolutionParameterName = "Best Training Solution";
28
29    private const string SymbolicExpressionTreeName = "SymbolicExpressionTree";
30    private const string VariableName = "Variable";
31
32    private const string StructureTemplateDescriptionText =
33      "Enter your expression as string in infix format into the empty input field.\n" +
34      "By checking the \"Apply Linear Scaling\" checkbox you can add the relevant scaling terms to your expression.\n" +
35      "After entering the expression click parse to build the tree.\n" +
36      "To edit the defined sub-functions, click on the corresponding-colored node in the tree view.\n" +
37      "Check the info box besides the input field for more information.";
38    #endregion
39
40    #region Parameters
41    public IConstrainedValueParameter<SymbolicRegressionSingleObjectiveEvaluator> TreeEvaluatorParameter => (IConstrainedValueParameter<SymbolicRegressionSingleObjectiveEvaluator>)Parameters[TreeEvaluatorParameterName];
42    public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
43    public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
44    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter => (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName];
45    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter => (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName];
46    public IResultParameter<ISymbolicRegressionSolution> BestTrainingSolutionParameter => (IResultParameter<ISymbolicRegressionSolution>)Parameters[BestTrainingSolutionParameterName];
47    #endregion
48
49    #region Properties
50
51    public IRegressionProblemData ProblemData {
52      get => ProblemDataParameter.Value;
53      set {
54        ProblemDataParameter.Value = value;
55        ProblemDataChanged?.Invoke(this, EventArgs.Empty);
56      }
57    }
58
59    public StructureTemplate StructureTemplate => StructureTemplateParameter.Value;
60
61    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter => InterpreterParameter.Value;
62
63    IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
64    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
65
66    public DoubleLimit EstimationLimits => EstimationLimitsParameter.Value;
67
68    public override bool Maximization => false;
69    #endregion
70
71    #region EventHandlers
72    public event EventHandler ProblemDataChanged;
73    #endregion
74
75    #region Constructors & Cloning
76    public StructuredSymbolicRegressionSingleObjectiveProblem() {
77      var provider = new PhysicsInstanceProvider();
78      var descriptor = new SheetBendingProcess();
79      var problemData = provider.LoadData(descriptor);
80      var shapeConstraintProblemData = new ShapeConstrainedRegressionProblemData(problemData);
81
82      var targetInterval = shapeConstraintProblemData.VariableRanges.GetInterval(shapeConstraintProblemData.TargetVariable);
83      var estimationWidth = targetInterval.Width * 10;
84
85      var structureTemplate = new StructureTemplate();
86
87      var evaluators = new ItemSet<SymbolicRegressionSingleObjectiveEvaluator>(
88        ApplicationManager.Manager.GetInstances<SymbolicRegressionSingleObjectiveEvaluator>()
89        .Where(x => x.Maximization == Maximization));
90
91      Parameters.Add(new ConstrainedValueParameter<SymbolicRegressionSingleObjectiveEvaluator>(
92        TreeEvaluatorParameterName,
93        evaluators,
94        evaluators.First()));
95
96      Parameters.Add(new ValueParameter<IRegressionProblemData>(
97        ProblemDataParameterName,
98        shapeConstraintProblemData));
99
100      Parameters.Add(new FixedValueParameter<StructureTemplate>(
101        StructureTemplateParameterName,
102        StructureTemplateDescriptionText,
103        structureTemplate));
104
105      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(
106        InterpreterParameterName,
107        new SymbolicDataAnalysisExpressionTreeInterpreter()) { Hidden = true });
108
109      Parameters.Add(new FixedValueParameter<DoubleLimit>(
110        EstimationLimitsParameterName,
111        new DoubleLimit(targetInterval.LowerBound - estimationWidth, targetInterval.UpperBound + estimationWidth)) { Hidden = true });
112
113      Parameters.Add(new ResultParameter<ISymbolicRegressionSolution>(BestTrainingSolutionParameterName, "") { Hidden = true });
114
115      this.EvaluatorParameter.Hidden = true;
116
117      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
118      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
119      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
120
121      RegisterEventHandlers();
122      StructureTemplate.Template =
123        "(" +
124          "(210000 / (210000 + h)) * ((sigma_y * t * t) / (wR * Rt * t)) + " +
125          "PlasticHardening(_) - Elasticity(_)" +
126        ")" +
127        " * C(_)";
128    }
129
130    public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original,
131      Cloner cloner) : base(original, cloner) {
132      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
133      RegisterEventHandlers();
134    }
135
136    [StorableConstructor]
137    protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
138
139
140    [StorableHook(HookType.AfterDeserialization)]
141    private void AfterDeserialization() {
142      RegisterEventHandlers();
143    }
144
145    #endregion
146
147    #region Cloning
148    public override IDeepCloneable Clone(Cloner cloner) =>
149      new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
150    #endregion
151
152    private void ProblemDataParameterValueChanged(object sender, EventArgs e) {
153      StructureTemplate.Reset();
154      // InfoBox for Reset?
155    }
156
157    private void RegisterEventHandlers() {
158      if (StructureTemplate != null) {
159        StructureTemplate.Changed += OnTemplateChanged;
160      }
161
162      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
163    }
164
165    private void OnTemplateChanged(object sender, EventArgs args) {
166      SetupStructureTemplate();
167    }
168
169    private void SetupStructureTemplate() {
170      foreach (var e in Encoding.Encodings.ToArray())
171        Encoding.Remove(e);
172
173      foreach (var f in StructureTemplate.SubFunctions) {
174        SetupVariables(f);
175        if (!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice
176          Encoding.Add(new SymbolicExpressionTreeEncoding(
177            f.Name,
178            f.Grammar,
179            f.MaximumSymbolicExpressionTreeLength,
180            f.MaximumSymbolicExpressionTreeDepth));
181      }
182    }
183
184    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
185      base.Analyze(individuals, qualities, results, random);
186
187      var best = GetBestIndividual(individuals, qualities).Item1;
188
189      if (!results.ContainsKey(BestTrainingSolutionParameter.ActualName)) {
190        results.Add(new Result(BestTrainingSolutionParameter.ActualName, typeof(SymbolicRegressionSolution)));
191      }
192
193      var tree = (ISymbolicExpressionTree)best[SymbolicExpressionTreeName];
194
195      var model = new SymbolicRegressionModel(ProblemData.TargetVariable, tree, Interpreter);
196      var solution = model.CreateRegressionSolution(ProblemData);
197
198      results[BestTrainingSolutionParameter.ActualName].Value = solution;
199    }
200
201
202    public override double Evaluate(Individual individual, IRandom random) {
203      var tree = BuildTree(individual);
204
205      if (StructureTemplate.ApplyLinearScaling)
206        AdjustLinearScalingParams(ProblemData, tree, Interpreter);
207
208      individual[SymbolicExpressionTreeName] = tree;
209
210      // dpiringe: needed when Maximization = true
211      if (TreeEvaluatorParameter.Value is SymbolicRegressionParameterOptimizationEvaluator constantOptEvaluator) {
212        constantOptEvaluator.RandomParameter.Value = random;
213        constantOptEvaluator.RelativeNumberOfEvaluatedSamplesParameter.Value =
214          (PercentValue)constantOptEvaluator.ParameterOptimizationRowsPercentage.Clone();
215      }
216
217      return TreeEvaluatorParameter.Value.Evaluate(
218        tree, ProblemData,
219        ProblemData.TrainingIndices,
220        Interpreter,
221        StructureTemplate.ApplyLinearScaling,
222        EstimationLimits.Lower,
223        EstimationLimits.Upper);
224    }
225
226    private static void AdjustLinearScalingParams(IRegressionProblemData problemData, ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
227      var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0);
228      var scalingNode = offsetNode.Subtrees.Where(x => !(x is NumberTreeNode)).First();
229
230      var offsetNumberNode = (NumberTreeNode)offsetNode.Subtrees.Where(x => x is NumberTreeNode).First();
231      var scalingNumberNode = (NumberTreeNode)scalingNode.Subtrees.Where(x => x is NumberTreeNode).First();
232
233      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
234      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
235
236      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error);
237      if (error == OnlineCalculatorError.None) {
238        offsetNumberNode.Value = a;
239        scalingNumberNode.Value = b;
240      }
241    }
242
243    private ISymbolicExpressionTree BuildTree(Individual individual) {
244      if (StructureTemplate.Tree == null)
245        throw new ArgumentException("No structure template defined!");
246
247      var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
248
249      // build main tree
250      foreach (var subFunctionTreeNode in templateTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
251        var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
252
253        // add new tree
254        var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
255                                          .GetSubtree(0); // Offset
256        subTree = (ISymbolicExpressionTreeNode)subTree.Clone();
257        subFunctionTreeNode.AddSubtree(subTree);
258
259      }
260      return templateTree;
261    }
262
263    private void SetupVariables(SubFunction subFunction) {
264      var varSym = (Variable)subFunction.Grammar.GetSymbol(VariableName);
265      if (varSym == null) {
266        varSym = new Variable();
267        subFunction.Grammar.AddSymbol(varSym);
268      }
269
270      var allVariables = ProblemData.InputVariables.Select(x => x.Value);
271      var allInputs = allVariables.Where(x => x != ProblemData.TargetVariable);
272
273      // set all variables
274      varSym.AllVariableNames = allVariables;
275
276      // set all allowed variables
277      if (subFunction.Arguments.Contains("_")) {
278        varSym.VariableNames = allInputs;
279      } else {
280        var vars = new List<string>();
281        var exceptions = new List<Exception>();
282        foreach (var arg in subFunction.Arguments) {
283          if (allInputs.Contains(arg))
284            vars.Add(arg);
285          else
286            exceptions.Add(new ArgumentException($"The argument '{arg}' for sub-function '{subFunction.Name}' is not a valid variable."));
287        }
288        if (exceptions.Any())
289          throw new AggregateException(exceptions);
290        varSym.VariableNames = vars;
291      }
292
293      varSym.Enabled = true;
294    }
295
296    public void Load(IRegressionProblemData data) {
297      ProblemData = data;
298      StructureTemplate.Template = "f(_)";
299    }
300  }
301}
Note: See TracBrowser for help on using the repository browser.