1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HEAL.Attic;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
11 | [StorableType("598B5DCB-95AC-465A-920B-E1E6DACFFA4B")]
|
---|
12 | public class SubFunction : ParameterizedNamedItem {
|
---|
13 | #region Constants
|
---|
14 | private const string GrammarParameterName = "Grammar";
|
---|
15 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
16 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | #region Parameters
|
---|
20 | public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName];
|
---|
21 | public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName];
|
---|
22 | public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter => (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName];
|
---|
23 | #endregion
|
---|
24 |
|
---|
25 | #region Properties
|
---|
26 | public ISymbolicDataAnalysisGrammar Grammar {
|
---|
27 | get => GrammarParameter.Value;
|
---|
28 | set => GrammarParameter.Value = value;
|
---|
29 | }
|
---|
30 |
|
---|
31 | public int MaximumSymbolicExpressionTreeDepth {
|
---|
32 | get => MaximumSymbolicExpressionTreeDepthParameter.Value.Value;
|
---|
33 | set => MaximumSymbolicExpressionTreeDepthParameter.Value.Value = value;
|
---|
34 | }
|
---|
35 |
|
---|
36 | public int MaximumSymbolicExpressionTreeLength {
|
---|
37 | get => MaximumSymbolicExpressionTreeLengthParameter.Value.Value;
|
---|
38 | set => MaximumSymbolicExpressionTreeLengthParameter.Value.Value = value;
|
---|
39 | }
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | public IEnumerable<string> Arguments { get; set; } = Enumerable.Empty<string>();
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | #region Events
|
---|
46 | public event EventHandler Changed;
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region Constructors
|
---|
50 | public SubFunction() {
|
---|
51 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
52 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
53 | grammar.Symbols.First(s => s.Name == TypeCoherentExpressionGrammar.ExponentialFunctionsName).Enabled = false;
|
---|
54 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar));
|
---|
55 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, new IntValue(8)));
|
---|
56 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, new IntValue(20)));
|
---|
57 | RegisterEventHandlers();
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected SubFunction(SubFunction original, Cloner cloner) : base(original, cloner) {
|
---|
61 | Arguments = original.Arguments;
|
---|
62 | RegisterEventHandlers();
|
---|
63 | }
|
---|
64 |
|
---|
65 | [StorableConstructor]
|
---|
66 | protected SubFunction(StorableConstructorFlag _) : base(_) { }
|
---|
67 | public override IDeepCloneable Clone(Cloner cloner) =>
|
---|
68 | new SubFunction(this, cloner);
|
---|
69 |
|
---|
70 |
|
---|
71 | [StorableHook(HookType.AfterDeserialization)]
|
---|
72 | private void AfterDeserialization() {
|
---|
73 | RegisterEventHandlers();
|
---|
74 | }
|
---|
75 | #endregion
|
---|
76 |
|
---|
77 | #region Event Handling
|
---|
78 | private void RegisterEventHandlers() {
|
---|
79 | GrammarParameter.ValueChanged += (o, e) => {
|
---|
80 | if (Grammar is TypeCoherentExpressionGrammar tceg) tceg.ConfigureAsDefaultRegressionGrammar();
|
---|
81 | OnParameterValueChanged(o, e);
|
---|
82 | };
|
---|
83 | MaximumSymbolicExpressionTreeDepthParameter.Value.ValueChanged += OnParameterValueChanged;
|
---|
84 | MaximumSymbolicExpressionTreeLengthParameter.Value.ValueChanged += OnParameterValueChanged;
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void OnParameterValueChanged(object sender, EventArgs e) =>
|
---|
88 | Changed?.Invoke(this, EventArgs.Empty);
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 |
|
---|
92 | public override string ToString() => $"{Name}({string.Join(",", Arguments)})";
|
---|
93 |
|
---|
94 | public override int GetHashCode() => ToString().GetHashCode();
|
---|
95 |
|
---|
96 | public override bool Equals(object obj) => (obj is SubFunction other) && other.ToString() == ToString();
|
---|
97 |
|
---|
98 | public void SetupVariables(IEnumerable<string> inputVariables) {
|
---|
99 | var allowedInputVariables = new List<string>(inputVariables);
|
---|
100 |
|
---|
101 | foreach (var varSym in Grammar.Symbols.OfType<Variable>()) {
|
---|
102 | // set all variables
|
---|
103 | varSym.AllVariableNames = allowedInputVariables;
|
---|
104 |
|
---|
105 | // set all allowed variables
|
---|
106 | if (Arguments.Contains("_")) {
|
---|
107 | varSym.VariableNames = allowedInputVariables;
|
---|
108 | } else {
|
---|
109 | varSym.VariableNames = Arguments.ToList();
|
---|
110 | }
|
---|
111 |
|
---|
112 | varSym.Enabled = true;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|