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

Last change on this file since 18062 was 18062, checked in by dpiringe, 9 months ago

#3136

  • added a new Symbol SubFunctionSymbol for sub functions
  • modified InfixExpressionParser to support SubFunctionSymbol (parsing of variableNames still in work)
  • modified StructuredSymbolicRegressionSingleObjectiveProblem to extract sub functions and add them to MultiEncoding
File size: 4.7 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;
14
15namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.SingleObjective {
16  [StorableType("7464E84B-65CC-440A-91F0-9FA920D730F9")]
17  [Item(Name = "StructuredSymbolicRegressionSingleObjectiveProblem", Description = "A problem with a structural definition and unfixed subfunctions.")]
18  public class StructuredSymbolicRegressionSingleObjectiveProblem : SingleObjectiveBasicProblem<MultiEncoding>, IRegressionProblem, IProblemInstanceConsumer<RegressionProblemData> {
19
20    #region Constants
21    private const string ProblemDataParameterName = "ProblemData";
22    private const string StructureDefinitionParameterName = "Structure Definition";
23    private const string GrammarParameterName = "Grammar";
24
25    #endregion
26
27    #region Parameter
28    public IValueParameter<IRegressionProblemData> ProblemDataParameter => (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName];
29    public IFixedValueParameter<StringValue> StructureDefinitionParameter => (IFixedValueParameter<StringValue>)Parameters[StructureDefinitionParameterName];
30    public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName]; // könnte auch weg?
31    #endregion
32
33    #region Properties
34    public IRegressionProblemData ProblemData {
35      get => ProblemDataParameter.Value;
36      set {
37        ProblemDataParameter.Value = value;
38        ProblemDataChanged?.Invoke(this, EventArgs.Empty);
39      }
40    }
41
42    public string StructureDefinition {
43      get => StructureDefinitionParameter.Value.Value;
44      set => StructureDefinitionParameter.Value.Value = value;
45    }
46
47    public ISymbolicDataAnalysisGrammar Grammar {
48      get => GrammarParameter.Value;
49      set => GrammarParameter.Value = value;
50    }
51
52    IParameter IDataAnalysisProblem.ProblemDataParameter => ProblemDataParameter;
53    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
54
55    public override bool Maximization => false;
56    #endregion
57
58    #region EventHandlers
59    public event EventHandler ProblemDataChanged;
60    #endregion
61
62    #region Constructors & Cloning
63    public StructuredSymbolicRegressionSingleObjectiveProblem() {
64      var problemData = new ShapeConstrainedRegressionProblemData();
65      var grammar = new LinearScalingGrammar();
66      var varSym = (Variable)grammar.GetSymbol("Variable");
67      varSym.AllVariableNames = problemData.InputVariables.Select(x => x.Value);
68      varSym.VariableNames = problemData.InputVariables.Select(x => x.Value);
69      varSym.Enabled = true;
70
71      Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, problemData));
72      Parameters.Add(new FixedValueParameter<StringValue>(StructureDefinitionParameterName, new StringValue("e^f(x)/F(y)")));
73      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar));
74      var parser = new InfixExpressionParser();
75      var tree = parser.Parse(StructureDefinition);
76     
77      GetSubFunctions(tree);
78    }
79
80    public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, Cloner cloner) {
81    }
82
83    [StorableConstructor]
84    protected StructuredSymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) {
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) =>
88      new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
89    #endregion
90
91    public void GetSubFunctions(ISymbolicExpressionTree tree) {
92      int count = 1;
93      foreach(var node in tree.IterateNodesPrefix())
94        if(node.Symbol is SubFunctionSymbol)
95          Encoding.Add(new SymbolicExpressionTreeEncoding($"f{count++}", Grammar/*new LinearScalingGrammar()*/, 25, 8));
96    }
97
98    public override double Evaluate(Individual individual, IRandom random) {
99      foreach(var kvp in individual.Values) {
100        if(kvp.Value is SymbolicExpressionTree tree) {
101          foreach(var n in tree.IterateNodesPrefix()) {
102            if(n.Symbol is Variable v) {
103              var t = v.VariableNames;
104            }
105          }
106          Console.WriteLine(tree);
107        }
108      }
109      return 0.0;
110    }
111
112    public void Load(RegressionProblemData data) {
113      ProblemData = data;
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.