1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
28 | public enum OpCode : byte {
|
---|
29 | Add = 1,
|
---|
30 | Sub = 2,
|
---|
31 | Mul = 3,
|
---|
32 | Div = 4,
|
---|
33 | Sin = 5,
|
---|
34 | Cos = 6,
|
---|
35 | Tan = 7,
|
---|
36 | Log = 8,
|
---|
37 | Exp = 9,
|
---|
38 | IfThenElse = 10,
|
---|
39 | GT = 11,
|
---|
40 | LT = 12,
|
---|
41 | AND = 13,
|
---|
42 | OR = 14,
|
---|
43 | NOT = 15,
|
---|
44 | Average = 16,
|
---|
45 | Call = 17,
|
---|
46 | Variable = 18,
|
---|
47 | LagVariable = 19,
|
---|
48 | Constant = 20,
|
---|
49 | Arg = 21,
|
---|
50 | Power = 22,
|
---|
51 | Root = 23,
|
---|
52 | TimeLag = 24,
|
---|
53 | Integral = 25,
|
---|
54 | Derivative = 26,
|
---|
55 | VariableCondition = 27,
|
---|
56 | Square = 28,
|
---|
57 | SquareRoot = 29,
|
---|
58 | Gamma = 30,
|
---|
59 | Psi = 31,
|
---|
60 | Dawson = 32,
|
---|
61 | ExponentialIntegralEi = 33,
|
---|
62 | CosineIntegral = 34,
|
---|
63 | SineIntegral = 35,
|
---|
64 | HyperbolicCosineIntegral = 36,
|
---|
65 | HyperbolicSineIntegral = 37,
|
---|
66 | FresnelCosineIntegral = 38,
|
---|
67 | FresnelSineIntegral = 39,
|
---|
68 | AiryA = 40,
|
---|
69 | AiryB = 41,
|
---|
70 | Norm = 42,
|
---|
71 | Erf = 43,
|
---|
72 | Bessel = 44,
|
---|
73 | XOR = 45,
|
---|
74 | FactorVariable = 46,
|
---|
75 | BinaryFactorVariable = 47,
|
---|
76 | Absolute = 48,
|
---|
77 | AnalyticQuotient = 49,
|
---|
78 | Cube = 50,
|
---|
79 | CubeRoot = 51,
|
---|
80 | Tanh = 52,
|
---|
81 | VectorVariable = 53,
|
---|
82 | VectorAdd = 54,
|
---|
83 | VectorSub = 55,
|
---|
84 | VectorMul = 56,
|
---|
85 | VectorDiv = 57,
|
---|
86 | VectorSum = 58,
|
---|
87 | VectorAvg = 59
|
---|
88 | };
|
---|
89 | public static class OpCodes {
|
---|
90 | // constants for API compatibility only
|
---|
91 | public const byte Add = (byte)OpCode.Add;
|
---|
92 | public const byte Sub = (byte)OpCode.Sub;
|
---|
93 | public const byte Mul = (byte)OpCode.Mul;
|
---|
94 | public const byte Div = (byte)OpCode.Div;
|
---|
95 | public const byte Sin = (byte)OpCode.Sin;
|
---|
96 | public const byte Cos = (byte)OpCode.Cos;
|
---|
97 | public const byte Tan = (byte)OpCode.Tan;
|
---|
98 | public const byte Log = (byte)OpCode.Log;
|
---|
99 | public const byte Exp = (byte)OpCode.Exp;
|
---|
100 | public const byte IfThenElse = (byte)OpCode.IfThenElse;
|
---|
101 | public const byte GT = (byte)OpCode.GT;
|
---|
102 | public const byte LT = (byte)OpCode.LT;
|
---|
103 | public const byte AND = (byte)OpCode.AND;
|
---|
104 | public const byte OR = (byte)OpCode.OR;
|
---|
105 | public const byte NOT = (byte)OpCode.NOT;
|
---|
106 | public const byte Average = (byte)OpCode.Average;
|
---|
107 | public const byte Call = (byte)OpCode.Call;
|
---|
108 | public const byte Variable = (byte)OpCode.Variable;
|
---|
109 | public const byte LagVariable = (byte)OpCode.LagVariable;
|
---|
110 | public const byte Constant = (byte)OpCode.Constant;
|
---|
111 | public const byte Arg = (byte)OpCode.Arg;
|
---|
112 | public const byte Power = (byte)OpCode.Power;
|
---|
113 | public const byte Root = (byte)OpCode.Root;
|
---|
114 | public const byte TimeLag = (byte)OpCode.TimeLag;
|
---|
115 | public const byte Integral = (byte)OpCode.Integral;
|
---|
116 | public const byte Derivative = (byte)OpCode.Derivative;
|
---|
117 | public const byte VariableCondition = (byte)OpCode.VariableCondition;
|
---|
118 | public const byte Square = (byte)OpCode.Square;
|
---|
119 | public const byte SquareRoot = (byte)OpCode.SquareRoot;
|
---|
120 | public const byte Gamma = (byte)OpCode.Gamma;
|
---|
121 | public const byte Psi = (byte)OpCode.Psi;
|
---|
122 | public const byte Dawson = (byte)OpCode.Dawson;
|
---|
123 | public const byte ExponentialIntegralEi = (byte)OpCode.ExponentialIntegralEi;
|
---|
124 | public const byte CosineIntegral = (byte)OpCode.CosineIntegral;
|
---|
125 | public const byte SineIntegral = (byte)OpCode.SineIntegral;
|
---|
126 | public const byte HyperbolicCosineIntegral = (byte)OpCode.HyperbolicCosineIntegral;
|
---|
127 | public const byte HyperbolicSineIntegral = (byte)OpCode.HyperbolicSineIntegral;
|
---|
128 | public const byte FresnelCosineIntegral = (byte)OpCode.FresnelCosineIntegral;
|
---|
129 | public const byte FresnelSineIntegral = (byte)OpCode.FresnelSineIntegral;
|
---|
130 | public const byte AiryA = (byte)OpCode.AiryA;
|
---|
131 | public const byte AiryB = (byte)OpCode.AiryB;
|
---|
132 | public const byte Norm = (byte)OpCode.Norm;
|
---|
133 | public const byte Erf = (byte)OpCode.Erf;
|
---|
134 | public const byte Bessel = (byte)OpCode.Bessel;
|
---|
135 | public const byte XOR = (byte)OpCode.XOR;
|
---|
136 | public const byte FactorVariable = (byte)OpCode.FactorVariable;
|
---|
137 | public const byte BinaryFactorVariable = (byte)OpCode.BinaryFactorVariable;
|
---|
138 | public const byte Absolute = (byte)OpCode.Absolute;
|
---|
139 | public const byte AnalyticQuotient = (byte)OpCode.AnalyticQuotient;
|
---|
140 | public const byte Cube = (byte)OpCode.Cube;
|
---|
141 | public const byte CubeRoot = (byte)OpCode.CubeRoot;
|
---|
142 | public const byte Tanh = (byte)OpCode.Tanh;
|
---|
143 | public const byte VectorVariable = (byte)OpCode.VectorVariable;
|
---|
144 | public const byte VectorAdd = (byte)OpCode.VectorAdd;
|
---|
145 | public const byte VectorSub = (byte)OpCode.VectorSub;
|
---|
146 | public const byte VectorMul = (byte)OpCode.VectorMul;
|
---|
147 | public const byte VectorDiv = (byte)OpCode.VectorDiv;
|
---|
148 | public const byte VectorSum = (byte)OpCode.VectorSum;
|
---|
149 | public const byte VectorMean = (byte)OpCode.VectorAvg;
|
---|
150 |
|
---|
151 |
|
---|
152 | private static Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
|
---|
153 | { typeof(Addition), OpCodes.Add },
|
---|
154 | { typeof(Subtraction), OpCodes.Sub },
|
---|
155 | { typeof(Multiplication), OpCodes.Mul },
|
---|
156 | { typeof(Division), OpCodes.Div },
|
---|
157 | { typeof(Sine), OpCodes.Sin },
|
---|
158 | { typeof(Cosine), OpCodes.Cos },
|
---|
159 | { typeof(Tangent), OpCodes.Tan },
|
---|
160 | { typeof (HyperbolicTangent), OpCodes.Tanh},
|
---|
161 | { typeof(Logarithm), OpCodes.Log },
|
---|
162 | { typeof(Exponential), OpCodes.Exp },
|
---|
163 | { typeof(IfThenElse), OpCodes.IfThenElse },
|
---|
164 | { typeof(GreaterThan), OpCodes.GT },
|
---|
165 | { typeof(LessThan), OpCodes.LT },
|
---|
166 | { typeof(And), OpCodes.AND },
|
---|
167 | { typeof(Or), OpCodes.OR },
|
---|
168 | { typeof(Not), OpCodes.NOT},
|
---|
169 | { typeof(Xor),OpCodes.XOR},
|
---|
170 | { typeof(Average), OpCodes.Average},
|
---|
171 | { typeof(InvokeFunction), OpCodes.Call },
|
---|
172 | { typeof(Variable), OpCodes.Variable },
|
---|
173 | { typeof(LaggedVariable), OpCodes.LagVariable },
|
---|
174 | { typeof(AutoregressiveTargetVariable),OpCodes.LagVariable},
|
---|
175 | { typeof(Constant), OpCodes.Constant },
|
---|
176 | { typeof(Argument), OpCodes.Arg },
|
---|
177 | { typeof(Power),OpCodes.Power},
|
---|
178 | { typeof(Root),OpCodes.Root},
|
---|
179 | { typeof(TimeLag), OpCodes.TimeLag},
|
---|
180 | { typeof(Integral), OpCodes.Integral},
|
---|
181 | { typeof(Derivative), OpCodes.Derivative},
|
---|
182 | { typeof(VariableCondition),OpCodes.VariableCondition},
|
---|
183 | { typeof(Square),OpCodes.Square},
|
---|
184 | { typeof(SquareRoot),OpCodes.SquareRoot},
|
---|
185 | { typeof(Gamma), OpCodes.Gamma },
|
---|
186 | { typeof(Psi), OpCodes.Psi },
|
---|
187 | { typeof(Dawson), OpCodes.Dawson},
|
---|
188 | { typeof(ExponentialIntegralEi), OpCodes.ExponentialIntegralEi },
|
---|
189 | { typeof(CosineIntegral), OpCodes.CosineIntegral },
|
---|
190 | { typeof(SineIntegral), OpCodes.SineIntegral },
|
---|
191 | { typeof(HyperbolicCosineIntegral), OpCodes.HyperbolicCosineIntegral },
|
---|
192 | { typeof(HyperbolicSineIntegral), OpCodes.HyperbolicSineIntegral },
|
---|
193 | { typeof(FresnelCosineIntegral), OpCodes.FresnelCosineIntegral },
|
---|
194 | { typeof(FresnelSineIntegral), OpCodes.FresnelSineIntegral },
|
---|
195 | { typeof(AiryA), OpCodes.AiryA },
|
---|
196 | { typeof(AiryB), OpCodes.AiryB },
|
---|
197 | { typeof(Norm), OpCodes.Norm},
|
---|
198 | { typeof(Erf), OpCodes.Erf},
|
---|
199 | { typeof(Bessel), OpCodes.Bessel},
|
---|
200 | { typeof(FactorVariable), OpCodes.FactorVariable },
|
---|
201 | { typeof(BinaryFactorVariable), OpCodes.BinaryFactorVariable },
|
---|
202 | { typeof(Absolute), OpCodes.Absolute },
|
---|
203 | { typeof(AnalyticQuotient), OpCodes.AnalyticQuotient },
|
---|
204 | { typeof(Cube), OpCodes.Cube },
|
---|
205 | { typeof(CubeRoot), OpCodes.CubeRoot },
|
---|
206 | { typeof(VectorVariable), OpCodes.VectorVariable },
|
---|
207 | { typeof(VectorAddition), OpCodes.VectorAdd },
|
---|
208 | { typeof(VectorSubtraction), OpCodes.VectorSub },
|
---|
209 | { typeof(VectorMultiplication), OpCodes.VectorMul },
|
---|
210 | { typeof(VectorDivision), OpCodes.VectorDiv },
|
---|
211 | { typeof(VectorSum), OpCodes.VectorSum },
|
---|
212 | { typeof(VectorMean), OpCodes.VectorMean }
|
---|
213 | };
|
---|
214 |
|
---|
215 | public static byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
|
---|
216 | if (symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out byte opCode)) return opCode;
|
---|
217 | else throw new NotSupportedException("Symbol: " + treeNode.Symbol);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|