Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs @ 18164

Last change on this file since 18164 was 18164, checked in by dpiringe, 2 years ago

#3136

  • fixed a bug in StructureTemplateView -> only nodes of type SubFunctionTreeNode are selectable
  • added a way to keep old sub functions after parsing a new expression
    • overwrote some basic object methods for SubFunction to keep it simple
    • only old sub functions, which match the name and signature of the new ones, are saved; examples:
      • old: f(x), new: f(x) -> keep old
      • old: f(x1), new: f(x1, x2) -> use new
      • old: f1(x), new f2(x) -> use new
File size: 3.6 KB
RevLine 
[18063]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HEAL.Attic;
8using HeuristicLab.Common;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
10using HeuristicLab.Parameters;
11using HeuristicLab.Data;
12
13namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
14  [StorableType("598B5DCB-95AC-465A-920B-E1E6DACFFA4B")]
15  public class SubFunction : ParameterizedNamedItem {
16    #region Constants
17    private const string GrammarParameterName = "Grammar";
18    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
19    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
20    #endregion
21
22    #region Parameters
23    public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName];
24    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName];
25    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName];
26    #endregion
27
28    #region Properties
29    public ISymbolicDataAnalysisGrammar Grammar {
30      get => GrammarParameter.Value;
31      set => GrammarParameter.Value = value;
32    }
33
34    public int MaximumSymbolicExpressionTreeDepth {
35      get => MaximumSymbolicExpressionTreeDepthParameter.Value.Value;
36      set => MaximumSymbolicExpressionTreeDepthParameter.Value.Value = value;
37    }
38
39    public int MaximumSymbolicExpressionTreeLength {
40      get => MaximumSymbolicExpressionTreeLengthParameter.Value.Value;
41      set => MaximumSymbolicExpressionTreeLengthParameter.Value.Value = value;
42    }
[18065]43
[18069]44    [Storable]
[18068]45    public IEnumerable<string> Arguments { get; set; }
46    #endregion
[18065]47
[18068]48    #region Events
49    public event EventHandler Changed;
[18063]50    #endregion
51
52    #region Constructors
53    public SubFunction() {
[18071]54      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, new ArithmeticExpressionGrammar()));
55      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, new IntValue(8)));
56      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, new IntValue(20)));
[18068]57
58      GrammarParameter.ValueChanged += OnParameterValueChanged;
59      MaximumSymbolicExpressionTreeDepthParameter.Value.ValueChanged += OnParameterValueChanged;
60      MaximumSymbolicExpressionTreeLengthParameter.Value.ValueChanged += OnParameterValueChanged;
[18063]61    }
62
[18068]63    private void OnParameterValueChanged(object sender, EventArgs e) => Changed?.Invoke(this, EventArgs.Empty);
64
[18072]65    protected SubFunction(SubFunction original, Cloner cloner) : base(original, cloner) {
[18069]66      Arguments = original.Arguments;
67    }
[18063]68
69    [StorableConstructor]
70    protected SubFunction(StorableConstructorFlag _) : base(_) {}
71    #endregion
72
73    #region Cloning
74    public override IDeepCloneable Clone(Cloner cloner) =>
75      new SubFunction(this, cloner);
76    #endregion
[18164]77
78    public override string ToString() => $"{Name}({string.Join(",", Arguments)})";
79
80    public override int GetHashCode() => ToString().GetHashCode();
81
82    public override bool Equals(object obj) => (obj is SubFunction other) && other.ToString() == ToString();
[18063]83  }
84}
Note: See TracBrowser for help on using the repository browser.