Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3136

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