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.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HEAL.Attic;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
33 | [StorableType("DE6C1E1E-D7C1-4070-847E-63B68562B10C")]
|
---|
34 | [Item("IntervalInterpreter", "Intperter for calculation of intervals of symbolic models.")]
|
---|
35 | public sealed class IntervalInterpreter : ParameterizedNamedItem, IStatefulItem {
|
---|
36 |
|
---|
37 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
38 |
|
---|
39 | public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
40 | get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public int EvaluatedSolutions {
|
---|
44 | get { return EvaluatedSolutionsParameter.Value.Value; }
|
---|
45 | set { EvaluatedSolutionsParameter.Value.Value = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | [StorableConstructor]
|
---|
49 | private IntervalInterpreter(StorableConstructorFlag _) : base(_) { }
|
---|
50 | private IntervalInterpreter(IntervalInterpreter original, Cloner cloner)
|
---|
51 | : base(original, cloner) { }
|
---|
52 |
|
---|
53 | public IntervalInterpreter()
|
---|
54 | : base("IntervalInterpreter", "Intperter for calculation of intervals of symbolic models.") {
|
---|
55 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new IntervalInterpreter(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 | private readonly object syncRoot = new object();
|
---|
63 |
|
---|
64 | #region IStatefulItem Members
|
---|
65 | public void InitializeState() {
|
---|
66 | EvaluatedSolutions = 0;
|
---|
67 | }
|
---|
68 | public void ClearState() { }
|
---|
69 | #endregion
|
---|
70 |
|
---|
71 | public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {
|
---|
72 | var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows);
|
---|
73 | return GetSymbolicExpressionTreeInterval(tree, variableRanges);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,
|
---|
77 | out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) {
|
---|
78 | var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows);
|
---|
79 | return GetSymbolicExpressionTreeIntervals(tree, variableRanges, out nodeIntervals);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) {
|
---|
83 | lock (syncRoot) {
|
---|
84 | EvaluatedSolutions++;
|
---|
85 | }
|
---|
86 | int instructionCount = 0;
|
---|
87 | var instructions = PrepareInterpreterState(tree, variableRanges);
|
---|
88 | var outputInterval = Evaluate(instructions, ref instructionCount);
|
---|
89 |
|
---|
90 | // because of numerical errors the bounds might be incorrect
|
---|
91 | if (outputInterval.LowerBound <= outputInterval.UpperBound)
|
---|
92 | return outputInterval;
|
---|
93 | else
|
---|
94 | return new Interval(outputInterval.UpperBound, outputInterval.LowerBound);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree,
|
---|
99 | IDictionary<string, Interval> variableRanges, out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) {
|
---|
100 | lock (syncRoot) {
|
---|
101 | EvaluatedSolutions++;
|
---|
102 | }
|
---|
103 | int instructionCount = 0;
|
---|
104 | var intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
|
---|
105 | var instructions = PrepareInterpreterState(tree, variableRanges);
|
---|
106 | var outputInterval = Evaluate(instructions, ref instructionCount, intervals);
|
---|
107 |
|
---|
108 | // fix incorrect intervals if necessary (could occur because of numerical errors)
|
---|
109 | nodeIntervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
|
---|
110 | foreach (var kvp in intervals) {
|
---|
111 | var interval = kvp.Value;
|
---|
112 | if (interval.IsInfiniteOrUndefined || interval.LowerBound <= interval.UpperBound)
|
---|
113 | nodeIntervals.Add(kvp.Key, interval);
|
---|
114 | else
|
---|
115 | nodeIntervals.Add(kvp.Key, new Interval(interval.UpperBound, interval.LowerBound));
|
---|
116 | }
|
---|
117 |
|
---|
118 | // because of numerical errors the bounds might be incorrect
|
---|
119 | if (outputInterval.IsInfiniteOrUndefined || outputInterval.LowerBound <= outputInterval.UpperBound)
|
---|
120 | return outputInterval;
|
---|
121 | else
|
---|
122 | return new Interval(outputInterval.UpperBound, outputInterval.LowerBound);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) {
|
---|
127 | if (variableRanges == null)
|
---|
128 | throw new ArgumentNullException("No variablew ranges are present!", nameof(variableRanges));
|
---|
129 |
|
---|
130 | //Check if all variables used in the tree are present in the dataset
|
---|
131 | foreach (var variable in tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName).Distinct()) {
|
---|
132 | if (!variableRanges.ContainsKey(variable)) throw new InvalidOperationException($"No ranges for variable {variable} is present");
|
---|
133 | }
|
---|
134 |
|
---|
135 | Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
136 | foreach (Instruction instr in code.Where(i => i.opCode == OpCodes.Variable)) {
|
---|
137 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
138 | instr.data = variableRanges[variableTreeNode.VariableName];
|
---|
139 | }
|
---|
140 | return code;
|
---|
141 | }
|
---|
142 |
|
---|
143 | private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) {
|
---|
144 | Instruction currentInstr = instructions[instructionCounter];
|
---|
145 | //Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side
|
---|
146 | //Update instructionCounter, whenever Evaluate is called
|
---|
147 | instructionCounter++;
|
---|
148 | Interval result = null;
|
---|
149 |
|
---|
150 | switch (currentInstr.opCode) {
|
---|
151 | //Variables, Constants, ...
|
---|
152 | case OpCodes.Variable: {
|
---|
153 | var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
|
---|
154 | var weightInterval = new Interval(variableTreeNode.Weight, variableTreeNode.Weight);
|
---|
155 | var variableInterval = (Interval)currentInstr.data;
|
---|
156 |
|
---|
157 | result = Interval.Multiply(variableInterval, weightInterval);
|
---|
158 | break;
|
---|
159 | }
|
---|
160 | case OpCodes.Constant: {
|
---|
161 | var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
|
---|
162 | result = new Interval(constTreeNode.Value, constTreeNode.Value);
|
---|
163 | break;
|
---|
164 | }
|
---|
165 | //Elementary arithmetic rules
|
---|
166 | case OpCodes.Add: {
|
---|
167 | result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
168 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
169 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
170 | result = Interval.Add(result, argumentInterval);
|
---|
171 | }
|
---|
172 | break;
|
---|
173 | }
|
---|
174 | case OpCodes.Sub: {
|
---|
175 | result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
176 | if (currentInstr.nArguments == 1)
|
---|
177 | result = Interval.Multiply(new Interval(-1, -1), result);
|
---|
178 |
|
---|
179 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
180 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
181 | result = Interval.Subtract(result, argumentInterval);
|
---|
182 | }
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | case OpCodes.Mul: {
|
---|
186 | result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
187 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
188 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
189 | result = Interval.Multiply(result, argumentInterval);
|
---|
190 | }
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | case OpCodes.Div: {
|
---|
194 | result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
195 | if (currentInstr.nArguments == 1)
|
---|
196 | result = Interval.Divide(new Interval(1, 1), result);
|
---|
197 |
|
---|
198 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
199 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
200 | result = Interval.Divide(result, argumentInterval);
|
---|
201 | }
|
---|
202 | break;
|
---|
203 | }
|
---|
204 | //Trigonometric functions
|
---|
205 | case OpCodes.Sin: {
|
---|
206 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
207 | result = Interval.Sine(argumentInterval);
|
---|
208 | break;
|
---|
209 | }
|
---|
210 | case OpCodes.Cos: {
|
---|
211 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
212 | result = Interval.Cosine(argumentInterval);
|
---|
213 | break;
|
---|
214 | }
|
---|
215 | case OpCodes.Tan: {
|
---|
216 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
217 | result = Interval.Tangens(argumentInterval);
|
---|
218 | break;
|
---|
219 | }
|
---|
220 | case OpCodes.Tanh: {
|
---|
221 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
222 | result = Interval.HyperbolicTangent(argumentInterval);
|
---|
223 | break;
|
---|
224 | }
|
---|
225 | //Exponential functions
|
---|
226 | case OpCodes.Log: {
|
---|
227 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
228 | result = Interval.Logarithm(argumentInterval);
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | case OpCodes.Exp: {
|
---|
232 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
233 | result = Interval.Exponential(argumentInterval);
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | case OpCodes.Power: {
|
---|
237 | result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
238 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
239 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
240 | result = Interval.Power(result, argumentInterval);
|
---|
241 | }
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | case OpCodes.Square: {
|
---|
245 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
246 | result = Interval.Square(argumentInterval);
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | case OpCodes.Cube: {
|
---|
250 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
251 | result = Interval.Cube(argumentInterval);
|
---|
252 | break;
|
---|
253 | }
|
---|
254 | case OpCodes.Root: {
|
---|
255 | result = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
256 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
257 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
258 | result = Interval.Root(result, argumentInterval);
|
---|
259 | }
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | case OpCodes.SquareRoot: {
|
---|
263 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
264 | result = Interval.SquareRoot(argumentInterval);
|
---|
265 | break;
|
---|
266 | }
|
---|
267 | case OpCodes.CubeRoot: {
|
---|
268 | var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
|
---|
269 | result = Interval.CubicRoot(argumentInterval);
|
---|
270 | break;
|
---|
271 | }
|
---|
272 | default:
|
---|
273 | throw new NotSupportedException($"The tree contains the unknown symbol {currentInstr.dynamicNode.Symbol}");
|
---|
274 | }
|
---|
275 |
|
---|
276 | if (nodeIntervals != null)
|
---|
277 | nodeIntervals.Add(currentInstr.dynamicNode, result);
|
---|
278 |
|
---|
279 | return result;
|
---|
280 | }
|
---|
281 |
|
---|
282 | public static bool IsCompatible(ISymbolicExpressionTree tree) {
|
---|
283 | var containsUnknownSyumbol = (
|
---|
284 | from n in tree.Root.GetSubtree(0).IterateNodesPrefix()
|
---|
285 | where
|
---|
286 | !(n.Symbol is StartSymbol) &&
|
---|
287 | !(n.Symbol is Addition) &&
|
---|
288 | !(n.Symbol is Subtraction) &&
|
---|
289 | !(n.Symbol is Multiplication) &&
|
---|
290 | !(n.Symbol is Division) &&
|
---|
291 | !(n.Symbol is Sine) &&
|
---|
292 | !(n.Symbol is Cosine) &&
|
---|
293 | !(n.Symbol is Tangent) &&
|
---|
294 | !(n.Symbol is Logarithm) &&
|
---|
295 | !(n.Symbol is Exponential) &&
|
---|
296 | !(n.Symbol is Power) &&
|
---|
297 | !(n.Symbol is Square) &&
|
---|
298 | !(n.Symbol is Root) &&
|
---|
299 | !(n.Symbol is SquareRoot) &&
|
---|
300 | !(n.Symbol is Problems.DataAnalysis.Symbolic.Variable) &&
|
---|
301 | !(n.Symbol is Constant)
|
---|
302 | select n).Any();
|
---|
303 | return !containsUnknownSyumbol;
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|