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