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

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

#3136

  • added a hidden interpreter parameter for StructuredSymbolicRegressionSingleObjectiveProblem
  • fixed a bug which crashed the application by changing ProblemData with different variables
  • fixed a bug which crashed the application by running the problem with an empty StructureTemplate
  • added a better output of exceptions of type AggregateException
  • added and resize event handler to repaint nodes of type SubFunctionTreeNode
  • code cleanup
File size: 9.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Optimization;
8using HEAL.Attic;
9using HeuristicLab.Common;
10using HeuristicLab.Problems.Instances;
11using HeuristicLab.Parameters;
12using HeuristicLab.Data;
13using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
14using HeuristicLab.PluginInfrastructure;
15
16namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
17  [StorableType("7464E84B-65CC-440A-91F0-9FA920D730F9")]
18  [Item(Name = "Structured Symbolic Regression Single Objective Problem (single-objective)", Description = "A problem with a structural definition and unfixed subfunctions.")]
19  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 150)]
20  public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<IRegressionProblemData> {
21
22    #region Constants
23    private const string ProblemDataParameterName = "ProblemData";
24    private const string StructureTemplateParameterName = "Structure Template";
25    private const string InterpreterParameterName = "Interpreter";
26
27    private const string StructureTemplateDescriptionText =
28      "Enter your expression as string in infix format into the empty input field.\n" +
29      "By checking the \"Apply Linear Scaling\" checkbox you can add the relevant scaling terms to your expression.\n" +
30      "After entering the expression click parse to build the tree.\n" +
31      "To edit the defined sub-functions, click on the coressponding colored node in the tree view.";
32    #endregion
33
34    #region Parameters
35    public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
36    public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
37    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter => (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName];
38    #endregion
39
40    #region Properties
41    public IRegressionProblemData ProblemData {
42      get => ProblemDataParameter.Value;
43      set {
44        ProblemDataParameter.Value = value;
45        ProblemDataChanged?.Invoke(this, EventArgs.Empty);
46      }
47    }
48
49    public StructureTemplate StructureTemplate => StructureTemplateParameter.Value;
50
51    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter => InterpreterParameter.Value;
52
53    IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
54    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
55
56    public override bool Maximization => true;
57    #endregion
58
59    #region EventHandlers
60    public event EventHandler ProblemDataChanged;
61    #endregion
62
63    #region Constructors & Cloning
64    public StructuredSymbolicRegressionSingleObjectiveProblem() {
65      var problemData = new ShapeConstrainedRegressionProblemData();
66
67      var structureTemplate = new StructureTemplate();
68      structureTemplate.Changed += OnTemplateChanged;
69
70      Parameters.Add(new ValueParameter<IRegressionProblemData>(
71        ProblemDataParameterName,
72        problemData));
73
74      Parameters.Add(new FixedValueParameter<StructureTemplate>(
75        StructureTemplateParameterName,
76        StructureTemplateDescriptionText,
77        structureTemplate));
78
79      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(
80        InterpreterParameterName,
81        new SymbolicDataAnalysisExpressionTreeInterpreter())
82        { Hidden = true });
83
84      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
85    }
86
87    public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original,
88      Cloner cloner) : base(original, cloner){ }
89
90    [StorableConstructor]
91    protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
92    #endregion
93
94    #region Cloning
95    public override IDeepCloneable Clone(Cloner cloner) =>
96      new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
97    #endregion
98
99    private void ProblemDataParameterValueChanged(object sender, EventArgs e) {
100      StructureTemplate.Reset();
101      // InfoBox for Reset?
102    }
103
104    private void OnTemplateChanged(object sender, EventArgs args) {
105      SetupStructureTemplate();
106    }
107
108    private void SetupStructureTemplate() {
109      foreach (var e in Encoding.Encodings.ToArray())
110        Encoding.Remove(e);
111
112      foreach (var f in StructureTemplate.SubFunctions.Values) {
113        SetupVariables(f);
114        if (!Encoding.Encodings.Any(x => x.Name == f.Name)) // to prevent the same encoding twice
115          Encoding.Add(new SymbolicExpressionTreeEncoding(
116            f.Name,
117            f.Grammar,
118            f.MaximumSymbolicExpressionTreeLength,
119            f.MaximumSymbolicExpressionTreeDepth));
120      }
121    }
122
123    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
124      base.Analyze(individuals, qualities, results, random);
125
126      int bestIdx = 0;
127      double bestQuality = Maximization ? double.MinValue : double.MaxValue;
128      for(int idx = 0; idx < qualities.Length; ++idx) {
129        if((Maximization && qualities[idx] > bestQuality) ||
130          (!Maximization && qualities[idx] < bestQuality)) {
131          bestQuality = qualities[idx];
132          bestIdx = idx;
133        }
134      }
135
136      if (results.TryGetValue("Best Tree", out IResult result)) {
137        var tree = BuildTree(individuals[bestIdx]);
138        if (StructureTemplate.ApplyLinearScaling)
139          AdjustLinearScalingParams(tree, Interpreter);
140        result.Value = tree;
141      }
142      else {
143        var tree = BuildTree(individuals[bestIdx]);
144        if (StructureTemplate.ApplyLinearScaling)
145          AdjustLinearScalingParams(tree, Interpreter);
146        results.Add(new Result("Best Tree", tree));
147      }
148    }
149
150    public override double Evaluate(Individual individual, IRandom random) {
151      var tree = BuildTree(individual);
152
153      if (StructureTemplate.ApplyLinearScaling)
154        AdjustLinearScalingParams(tree, Interpreter);
155      var estimationInterval = ProblemData.VariableRanges.GetInterval(ProblemData.TargetVariable);
156      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
157        Interpreter, tree,
158        estimationInterval.LowerBound, estimationInterval.UpperBound,
159        ProblemData, ProblemData.TrainingIndices, false);
160     
161      return quality;
162    }
163
164    private void AdjustLinearScalingParams(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
165      var offsetNode = tree.Root.GetSubtree(0).GetSubtree(0);
166      var scalingNode = offsetNode.Subtrees.Where(x => !(x is ConstantTreeNode)).First();
167
168      var offsetConstantNode = (ConstantTreeNode)offsetNode.Subtrees.Where(x => x is ConstantTreeNode).First();
169      var scalingConstantNode = (ConstantTreeNode)scalingNode.Subtrees.Where(x => x is ConstantTreeNode).First();
170
171      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, ProblemData.TrainingIndices);
172      var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices);
173
174      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out double a, out double b, out OnlineCalculatorError error);
175      if(error == OnlineCalculatorError.None) {
176        offsetConstantNode.Value = a;
177        scalingConstantNode.Value = b;
178      }
179    }
180
181    private ISymbolicExpressionTree BuildTree(Individual individual) {
182      if (StructureTemplate.Tree == null)
183        throw new ArgumentException("No structure template defined!");
184
185      var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
186
187      // build main tree
188      foreach (var n in templateTree.IterateNodesPrefix()) {
189        if (n.Symbol is SubFunctionSymbol) {
190          var subFunctionTreeNode = n as SubFunctionTreeNode;
191          var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
192
193          // add new tree
194          var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
195                                            .GetSubtree(0); // Offset
196          subFunctionTreeNode.AddSubtree(subTree);
197        }
198      }
199      return templateTree;
200    }
201
202    private void SetupVariables(SubFunction subFunction) {
203      var varSym = (Variable)subFunction.Grammar.GetSymbol("Variable");
204      if (varSym == null) {
205        varSym = new Variable();
206        subFunction.Grammar.AddSymbol(varSym);
207      }
208
209      var allVariables = ProblemData.InputVariables.Select(x => x.Value);
210      var allInputs = allVariables.Where(x => x != ProblemData.TargetVariable);
211
212      // set all variables
213      varSym.AllVariableNames = allVariables;
214
215      // set all allowed variables
216      if (subFunction.Arguments.Contains("_")) {
217        varSym.VariableNames = allInputs;
218      } else {
219        var vars = new List<string>();
220        var exceptions = new List<Exception>();
221        foreach (var arg in subFunction.Arguments) {
222          if (allInputs.Contains(arg))
223            vars.Add(arg);
224          else
225            exceptions.Add(new ArgumentException($"The argument '{arg}' for sub-function '{subFunction.Name}' is not a valid variable."));
226        }
227        if (exceptions.Any())
228          throw new AggregateException(exceptions);
229        varSym.VariableNames = vars;
230      }
231
232      varSym.Enabled = true;
233    }
234
235    public void Load(IRegressionProblemData data) => ProblemData = data;
236  }
237}
Note: See TracBrowser for help on using the repository browser.