1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #endregion
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
31 | [StorableType("5A2300A0-D0FC-4F2D-B910-F86384FE9052")]
|
---|
32 | [Item("LinearScalingGrammar", "Represents a grammar which includes linear scaling parts implicitly.")]
|
---|
33 | public class LinearScalingGrammar : DataAnalysisGrammar, ISymbolicDataAnalysisGrammar {
|
---|
34 | public LinearScalingGrammar() : base(ItemAttribute.GetName(typeof(LinearScalingGrammar)),
|
---|
35 | ItemAttribute.GetDescription(typeof(LinearScalingGrammar))) {
|
---|
36 | Initialize();
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | public LinearScalingGrammar(StorableConstructorFlag _) : base(_) { }
|
---|
41 |
|
---|
42 | protected LinearScalingGrammar(LinearScalingGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
43 | public LinearScalingGrammar(string name, string description) : base(name, description) { }
|
---|
44 |
|
---|
45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
46 | return new LinearScalingGrammar(this, cloner);
|
---|
47 | }
|
---|
48 |
|
---|
49 | private void Initialize() {
|
---|
50 | #region Symbols
|
---|
51 |
|
---|
52 | var add = new Addition();
|
---|
53 | var sub = new Subtraction();
|
---|
54 | var mul = new Multiplication();
|
---|
55 | var div = new Division();
|
---|
56 | var sin = new Sine();
|
---|
57 | var cos = new Cosine();
|
---|
58 | var tan = new Tangent();
|
---|
59 | var tanh = new HyperbolicTangent();
|
---|
60 | var log = new Logarithm();
|
---|
61 | var exp = new Exponential();
|
---|
62 | var square = new Square();
|
---|
63 | var sqrt = new SquareRoot();
|
---|
64 | var cube = new Cube();
|
---|
65 | var cbrt = new CubeRoot();
|
---|
66 | var abs = new Absolute();
|
---|
67 | var aq = new AnalyticQuotient();
|
---|
68 | var constant = new Constant();
|
---|
69 | constant.MinValue = -20;
|
---|
70 | constant.MaxValue = 20;
|
---|
71 | var variableSymbol = new Variable();
|
---|
72 |
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | //Special symbols
|
---|
76 | var offset = new Addition { Name = "Offset" };
|
---|
77 | var scaling = new Multiplication { Name = "Scaling" };
|
---|
78 | //all other symbols
|
---|
79 | var allSymbols = new List<Symbol> {
|
---|
80 | add, sub, mul, div, constant, variableSymbol, sin, cos, tan, log, square, sqrt, cube, cbrt, exp,
|
---|
81 | tanh, aq, abs
|
---|
82 | };
|
---|
83 |
|
---|
84 | var bivariateFuncs = new List<Symbol> { add, sub, mul, div, aq };
|
---|
85 | var univariateFuncs = new List<Symbol> { sin, cos, tan, tanh, exp, log, abs, square, cube, sqrt, cbrt };
|
---|
86 | var realValueSymbols = new List<Symbol> {
|
---|
87 | add, sub, mul, div, sin, cos, tan, tanh, exp, log, aq, abs, square, cube, sqrt, cbrt,
|
---|
88 | variableSymbol, constant,
|
---|
89 | };
|
---|
90 |
|
---|
91 |
|
---|
92 | //Add special symbols
|
---|
93 | AddSymbol(offset);
|
---|
94 | AddSymbol(scaling);
|
---|
95 | //Add all other symbols
|
---|
96 | foreach (var symb in allSymbols) AddSymbol(symb);
|
---|
97 |
|
---|
98 | #region define subtree count for special symbols
|
---|
99 |
|
---|
100 | foreach (var symb in bivariateFuncs) SetSubtreeCount(symb, 2, 2);
|
---|
101 |
|
---|
102 | foreach (var symb in univariateFuncs) SetSubtreeCount(symb, 1, 1);
|
---|
103 |
|
---|
104 | SetSubtreeCount(offset, 2, 2);
|
---|
105 | SetSubtreeCount(scaling, 2, 2);
|
---|
106 |
|
---|
107 | #endregion
|
---|
108 |
|
---|
109 | #region child symbols config
|
---|
110 |
|
---|
111 | AddAllowedChildSymbol(StartSymbol, offset);
|
---|
112 |
|
---|
113 | //Define childs for offset
|
---|
114 | AddAllowedChildSymbol(offset, scaling, 0);
|
---|
115 | AddAllowedChildSymbol(offset, constant, 1);
|
---|
116 |
|
---|
117 | //Define childs for scaling
|
---|
118 | foreach (var symb in allSymbols) AddAllowedChildSymbol(scaling, symb, 0);
|
---|
119 | AddAllowedChildSymbol(scaling, constant, 1);
|
---|
120 |
|
---|
121 | //Define childs for realvalue symbols
|
---|
122 | foreach (var symb in realValueSymbols) {
|
---|
123 | foreach (var c in realValueSymbols) AddAllowedChildSymbol(symb, c);
|
---|
124 | }
|
---|
125 |
|
---|
126 | #endregion
|
---|
127 |
|
---|
128 | Symbols.First(s => s is Cube).Enabled = false;
|
---|
129 | Symbols.First(s => s is CubeRoot).Enabled = false;
|
---|
130 | Symbols.First(s => s is Absolute).Enabled = false;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | } |
---|