1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HEAL.Attic;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
9 | [StorableType("5A2300A0-D0FC-4F2D-B910-F86384FE9052")]
|
---|
10 | [Item("IntervalArithmeticGrammar", "Represents a grammar for interval arithmetic")]
|
---|
11 | public class IntervalArithmeticGrammar : DataAnalysisGrammar, ISymbolicDataAnalysisGrammar {
|
---|
12 | public IntervalArithmeticGrammar() : base(ItemAttribute.GetName(typeof(IntervalArithmeticGrammar)),
|
---|
13 | ItemAttribute.GetDescription(typeof(IntervalArithmeticGrammar))) {
|
---|
14 | Initialize();
|
---|
15 | }
|
---|
16 |
|
---|
17 | [StorableConstructor]
|
---|
18 | public IntervalArithmeticGrammar(StorableConstructorFlag _) : base(_) { }
|
---|
19 |
|
---|
20 | protected IntervalArithmeticGrammar(IntervalArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
21 | public IntervalArithmeticGrammar(string name, string description) : base(name, description) { }
|
---|
22 |
|
---|
23 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
24 | return new IntervalArithmeticGrammar(this, cloner);
|
---|
25 | }
|
---|
26 |
|
---|
27 | private void Initialize() {
|
---|
28 | #region Symbols
|
---|
29 |
|
---|
30 | var add = new Addition();
|
---|
31 | var sub = new Subtraction();
|
---|
32 | var mul = new Multiplication();
|
---|
33 | var div = new Division();
|
---|
34 | var sin = new Sine();
|
---|
35 | var cos = new Cosine();
|
---|
36 | var tan = new Tangent();
|
---|
37 | var log = new Logarithm();
|
---|
38 | var pow = new Power();
|
---|
39 | var square = new Square();
|
---|
40 | var root = new Root();
|
---|
41 | var sqrt = new SquareRoot();
|
---|
42 | var exp = new Exponential();
|
---|
43 | var tanh = new HyperbolicTangent();
|
---|
44 | var constant = new Constant();
|
---|
45 | constant.MinValue = -20;
|
---|
46 | constant.MaxValue = 20;
|
---|
47 | var variableSymbol = new Variable();
|
---|
48 |
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | //Special symbols
|
---|
52 | var offset = new Addition {Name = "Offset"};
|
---|
53 | var scaling = new Multiplication {Name = "Scaling"};
|
---|
54 | //all other symbols
|
---|
55 | var allSymbols = new List<Symbol> {
|
---|
56 | add, sub, mul, div, constant, variableSymbol, sin, cos, tan, log, pow, square, root, sqrt, exp,
|
---|
57 | tanh
|
---|
58 | };
|
---|
59 |
|
---|
60 | var arithmeticSymbols = new List<Symbol> {add, sub, mul, div};
|
---|
61 | var trigonometricSymbols = new List<Symbol> {sin, cos, tan, tanh};
|
---|
62 | var exponentialAndLogarithmicSymbols = new List<Symbol> {exp, log};
|
---|
63 | var powerSymbols = new List<Symbol> {square, pow, sqrt, root};
|
---|
64 | var realValueSymbols = new List<Symbol>
|
---|
65 | {add, sub, mul, div, sin, cos, tan, tanh, exp, log, variableSymbol, constant};
|
---|
66 |
|
---|
67 |
|
---|
68 | //Add special symbols
|
---|
69 | AddSymbol(offset);
|
---|
70 | AddSymbol(scaling);
|
---|
71 | //Add all other symbols
|
---|
72 | foreach (var symb in allSymbols) AddSymbol(symb);
|
---|
73 |
|
---|
74 | #region define subtree count for special symbols
|
---|
75 |
|
---|
76 | foreach (var symb in arithmeticSymbols) SetSubtreeCount(symb, 2, 2);
|
---|
77 |
|
---|
78 | foreach (var symb in trigonometricSymbols) SetSubtreeCount(symb, 1, 1);
|
---|
79 |
|
---|
80 | foreach (var symb in exponentialAndLogarithmicSymbols) SetSubtreeCount(symb, 1, 1);
|
---|
81 |
|
---|
82 | SetSubtreeCount(offset, 2, 2);
|
---|
83 | SetSubtreeCount(scaling, 2, 2);
|
---|
84 | SetSubtreeCount(pow, 2, 2);
|
---|
85 | SetSubtreeCount(root, 2, 2);
|
---|
86 | SetSubtreeCount(square, 1, 1);
|
---|
87 | SetSubtreeCount(sqrt, 1, 1);
|
---|
88 |
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 | #region child symbols config
|
---|
92 |
|
---|
93 | AddAllowedChildSymbol(StartSymbol, offset);
|
---|
94 |
|
---|
95 | //Define childs for offset
|
---|
96 | AddAllowedChildSymbol(offset, scaling, 0);
|
---|
97 | AddAllowedChildSymbol(offset, constant, 1);
|
---|
98 |
|
---|
99 | //Define childs for scaling
|
---|
100 | foreach (var symb in allSymbols) AddAllowedChildSymbol(scaling, symb, 0);
|
---|
101 | AddAllowedChildSymbol(scaling, constant, 1);
|
---|
102 |
|
---|
103 | //Define childs for realvalue symbols
|
---|
104 | foreach (var symb in realValueSymbols) {
|
---|
105 | foreach (var c in realValueSymbols) AddAllowedChildSymbol(symb, c);
|
---|
106 | }
|
---|
107 |
|
---|
108 | //define childs for power symbols
|
---|
109 | foreach (var symb in powerSymbols) {
|
---|
110 | AddAllowedChildSymbol(symb, variableSymbol, 0);
|
---|
111 | AddAllowedChildSymbol(symb, constant, 1);
|
---|
112 | }
|
---|
113 |
|
---|
114 | //define child for sqrt and square
|
---|
115 | foreach (var c in realValueSymbols) {
|
---|
116 | AddAllowedChildSymbol(square, c, 0);
|
---|
117 | AddAllowedChildSymbol(sqrt, c, 0);
|
---|
118 | }
|
---|
119 | #endregion
|
---|
120 |
|
---|
121 | Symbols.First(s => s is Power).Enabled = false;
|
---|
122 | Symbols.First(s => s is Root).Enabled = false;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | } |
---|