1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
33 | [StorableClass]
|
---|
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(bool deserializing) : base(deserializing) { }
|
---|
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 GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {
|
---|
72 | lock (syncRoot) {
|
---|
73 | EvaluatedSolutions++;
|
---|
74 | }
|
---|
75 | int instructionCount = 0;
|
---|
76 | var intervalBoundaries = DatasetUtil.GetVariableBoundaries(dataset, rows);
|
---|
77 | var instructions = PrepareInterpreterState(tree, intervalBoundaries);
|
---|
78 | var x = Evaluate(instructions, ref instructionCount);
|
---|
79 |
|
---|
80 | return x;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,
|
---|
84 | out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) {
|
---|
85 | lock (syncRoot) {
|
---|
86 | EvaluatedSolutions++;
|
---|
87 | }
|
---|
88 | int instructionCount = 0;
|
---|
89 | var intervalBoundaries = DatasetUtil.GetVariableBoundaries(dataset, rows);
|
---|
90 | intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
|
---|
91 | var instructions = PrepareInterpreterState(tree, intervalBoundaries);
|
---|
92 | var x = Evaluate(instructions, ref instructionCount, intervals);
|
---|
93 |
|
---|
94 | return x;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals) {
|
---|
98 | lock (syncRoot) {
|
---|
99 | EvaluatedSolutions++;
|
---|
100 | }
|
---|
101 | int instructionCount = 0;
|
---|
102 | var instructions = PrepareInterpreterState(tree, customIntervals);
|
---|
103 | var x = Evaluate(instructions, ref instructionCount);
|
---|
104 |
|
---|
105 | return x;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree,
|
---|
110 | Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
|
---|
111 | lock (syncRoot) {
|
---|
112 | EvaluatedSolutions++;
|
---|
113 | }
|
---|
114 | int instructionCount = 0;
|
---|
115 | intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
|
---|
116 | var instructions = PrepareInterpreterState(tree, customIntervals);
|
---|
117 | var x = Evaluate(instructions, ref instructionCount, intervals);
|
---|
118 |
|
---|
119 | return x;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals) {
|
---|
124 | Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
125 |
|
---|
126 | if (customIntervals == null)
|
---|
127 | throw new ArgumentException("No interval ranges are present!", nameof(customIntervals));
|
---|
128 |
|
---|
129 | foreach (var variable in tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName).Distinct()) {
|
---|
130 | if (!customIntervals.ContainsKey(variable)) throw new InvalidOperationException($"No ranges for variable {variable} is present");
|
---|
131 | }
|
---|
132 |
|
---|
133 | foreach (Instruction instr in code.Where(i => i.opCode == OpCodes.Variable)) {
|
---|
134 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
135 | instr.data = customIntervals[variableTreeNode.VariableName];
|
---|
136 | }
|
---|
137 | return code;
|
---|
138 | }
|
---|
139 |
|
---|
140 | private Interval Evaluate(Instruction[] instructions, ref int instructionCount, Dictionary<ISymbolicExpressionTreeNode,
|
---|
141 | Interval> intervals = null) {
|
---|
142 | Instruction currentInstr = instructions[instructionCount++];
|
---|
143 | Interval result = null;
|
---|
144 |
|
---|
145 | switch (currentInstr.opCode) {
|
---|
146 | //Elementary arithmetic rules
|
---|
147 | case OpCodes.Add: {
|
---|
148 | result = Evaluate(instructions, ref instructionCount, intervals);
|
---|
149 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
150 | result = Interval.Add(result, Evaluate(instructions, ref instructionCount, intervals));
|
---|
151 | }
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | case OpCodes.Sub: {
|
---|
155 | result = Evaluate(instructions, ref instructionCount, intervals);
|
---|
156 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
157 | result = Interval.Subtract(result, Evaluate(instructions, ref instructionCount, intervals));
|
---|
158 | }
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | case OpCodes.Mul: {
|
---|
162 | result = Evaluate(instructions, ref instructionCount, intervals);
|
---|
163 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
164 | result = Interval.Multiply(result, Evaluate(instructions, ref instructionCount, intervals));
|
---|
165 | }
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | case OpCodes.Div: {
|
---|
169 | result = Evaluate(instructions, ref instructionCount, intervals);
|
---|
170 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
171 | result = Interval.Divide(result, Evaluate(instructions, ref instructionCount, intervals));
|
---|
172 | }
|
---|
173 | break;
|
---|
174 | }
|
---|
175 | //Trigonometric functions
|
---|
176 | case OpCodes.Sin: {
|
---|
177 | result = Interval.Sine(Evaluate(instructions, ref instructionCount, intervals));
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | case OpCodes.Cos: {
|
---|
181 | result = Interval.Cosine(Evaluate(instructions, ref instructionCount, intervals));
|
---|
182 | break;
|
---|
183 | }
|
---|
184 | case OpCodes.Tan: {
|
---|
185 | result = Interval.Tangens(Evaluate(instructions, ref instructionCount, intervals));
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | //Exponential functions
|
---|
189 | case OpCodes.Log: {
|
---|
190 | result = Interval.Logarithm(Evaluate(instructions, ref instructionCount, intervals));
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | case OpCodes.Exp: {
|
---|
194 | result = Interval.Exponential(Evaluate(instructions, ref instructionCount, intervals));
|
---|
195 | break;
|
---|
196 | }
|
---|
197 | case OpCodes.Power: {
|
---|
198 | result = Evaluate(instructions, ref instructionCount, intervals);
|
---|
199 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
200 | result = Interval.Power(result, Evaluate(instructions, ref instructionCount, intervals));
|
---|
201 | }
|
---|
202 | break;
|
---|
203 | }
|
---|
204 | case OpCodes.Square: {
|
---|
205 | result = Interval.Square(Evaluate(instructions, ref instructionCount, intervals));
|
---|
206 | break;
|
---|
207 | }
|
---|
208 | case OpCodes.Root: {
|
---|
209 | result = Evaluate(instructions, ref instructionCount, intervals);
|
---|
210 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
211 | result = Interval.Root(result, Evaluate(instructions, ref instructionCount, intervals));
|
---|
212 | }
|
---|
213 | break;
|
---|
214 | }
|
---|
215 | case OpCodes.SquareRoot: {
|
---|
216 | result = Interval.SquareRoot(Evaluate(instructions, ref instructionCount, intervals));
|
---|
217 | break;
|
---|
218 | }
|
---|
219 | //Variables, Constants, ...
|
---|
220 | case OpCodes.Variable: {
|
---|
221 | intervals.Add(currentInstr.dynamicNode, (Interval)currentInstr.data);
|
---|
222 | var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
|
---|
223 | var variableWeight = variableTreeNode.Weight;
|
---|
224 |
|
---|
225 | return Interval.Multiply((Interval)currentInstr.data, new Interval(variableWeight, variableWeight));
|
---|
226 | }
|
---|
227 | case OpCodes.Constant: {
|
---|
228 | var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
|
---|
229 | var inter = new Interval(constTreeNode.Value, constTreeNode.Value);
|
---|
230 | intervals.Add(currentInstr.dynamicNode, inter);
|
---|
231 | return inter;
|
---|
232 | }
|
---|
233 | default:
|
---|
234 | throw new NotSupportedException("Tree contains an unknown symbol.");
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (intervals != null)
|
---|
238 | intervals.Add(currentInstr.dynamicNode, result);
|
---|
239 | return result;
|
---|
240 | }
|
---|
241 |
|
---|
242 | public static bool IsCompatible(ISymbolicExpressionTree tree) {
|
---|
243 | var containsUnknownSyumbol = (
|
---|
244 | from n in tree.Root.GetSubtree(0).IterateNodesPrefix()
|
---|
245 | where
|
---|
246 | !(n.Symbol is StartSymbol) &&
|
---|
247 | !(n.Symbol is Addition) &&
|
---|
248 | !(n.Symbol is Subtraction) &&
|
---|
249 | !(n.Symbol is Multiplication) &&
|
---|
250 | !(n.Symbol is Division) &&
|
---|
251 | !(n.Symbol is Sine) &&
|
---|
252 | !(n.Symbol is Cosine) &&
|
---|
253 | !(n.Symbol is Tangent) &&
|
---|
254 | !(n.Symbol is Logarithm) &&
|
---|
255 | !(n.Symbol is Exponential) &&
|
---|
256 | !(n.Symbol is Power) &&
|
---|
257 | !(n.Symbol is Square) &&
|
---|
258 | !(n.Symbol is Root) &&
|
---|
259 | !(n.Symbol is SquareRoot) &&
|
---|
260 | !(n.Symbol is Problems.DataAnalysis.Symbolic.Variable) &&
|
---|
261 | !(n.Symbol is Constant)
|
---|
262 | select n).Any();
|
---|
263 | return !containsUnknownSyumbol;
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|