1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.IntervalArithmetic;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 | using HeuristicLab.Problems.DataAnalysis;
|
---|
11 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.Interpreter {
|
---|
14 | [StorableClass]
|
---|
15 | [Item("SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.")]
|
---|
16 | public class IntervalInterpreter : ParameterizedNamedItem{
|
---|
17 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
18 | public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
19 | get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
20 | }
|
---|
21 |
|
---|
22 | public int EvaluatedSolutions {
|
---|
23 | get { return EvaluatedSolutionsParameter.Value.Value; }
|
---|
24 | set { EvaluatedSolutionsParameter.Value.Value = value; }
|
---|
25 | }
|
---|
26 |
|
---|
27 | public void ClearState() { }
|
---|
28 |
|
---|
29 | protected IntervalInterpreter(bool deserializing) : base(deserializing) { }
|
---|
30 |
|
---|
31 | protected IntervalInterpreter(IntervalInterpreter original,
|
---|
32 | Cloner cloner)
|
---|
33 | : base(original, cloner) { }
|
---|
34 |
|
---|
35 | public IntervalInterpreter()
|
---|
36 | : base("SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.") { }
|
---|
37 |
|
---|
38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
39 | return new IntervalInterpreter(this, cloner);
|
---|
40 | }
|
---|
41 |
|
---|
42 | private readonly object syncRoot = new object();
|
---|
43 |
|
---|
44 | private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree,
|
---|
45 | IEnumerable<int> rows, IDataset dataset = null, Dictionary<string, Interval> customIntervals = null) {
|
---|
46 | Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
47 | int necessaryArgStackSize = 0;
|
---|
48 |
|
---|
49 | foreach(Instruction instr in code) {
|
---|
50 | if (instr.opCode == OpCodes.Variable) {
|
---|
51 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
52 | IList<double> values = new List<double>();
|
---|
53 |
|
---|
54 | if (dataset != null && customIntervals != null) {
|
---|
55 | if (customIntervals.ContainsKey(variableTreeNode.VariableName)) {
|
---|
56 | instr.data = customIntervals[variableTreeNode.VariableName];
|
---|
57 | } else {
|
---|
58 | foreach (var rowEnum in rows) {
|
---|
59 | values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
|
---|
60 | }
|
---|
61 | instr.data = new Interval(values.Min(), values.Max());
|
---|
62 | }
|
---|
63 | } else if (dataset != null) {
|
---|
64 | foreach (var rowEnum in rows) {
|
---|
65 | values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
|
---|
66 | }
|
---|
67 | instr.data = new Interval(values.Min(), values.Max());
|
---|
68 | } else if (customIntervals != null) {
|
---|
69 | if (customIntervals.ContainsKey(variableTreeNode.VariableName)) {
|
---|
70 | instr.data = customIntervals[variableTreeNode.VariableName];
|
---|
71 | }
|
---|
72 | } else {
|
---|
73 | throw new Exception("No valid input for variables!");
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | return new InterpreterState(code, necessaryArgStackSize);
|
---|
79 | }
|
---|
80 |
|
---|
81 | private Interval Evaluate(InterpreterState state, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
|
---|
82 | Instruction currentInstr = state.NextInstruction();
|
---|
83 | switch (currentInstr.opCode) {
|
---|
84 | //Elementary arithmetic rules
|
---|
85 | case OpCodes.Add: {
|
---|
86 | var s = Evaluate(state, intervals);
|
---|
87 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
88 | s = Interval.Add(s, Evaluate(state, intervals));
|
---|
89 | }
|
---|
90 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
91 | return s;
|
---|
92 | }
|
---|
93 | case OpCodes.Sub: {
|
---|
94 | var s = Evaluate(state, intervals);
|
---|
95 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
96 | s = Interval.Subtract(s, Evaluate(state, intervals));
|
---|
97 | }
|
---|
98 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
99 | return s;
|
---|
100 | }
|
---|
101 | case OpCodes.Mul: {
|
---|
102 | var s = Evaluate(state, intervals);
|
---|
103 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
104 | s = Interval.Multiply(s, Evaluate(state, intervals));
|
---|
105 | }
|
---|
106 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
107 | return s;
|
---|
108 | }
|
---|
109 | case OpCodes.Div: {
|
---|
110 | var s = Evaluate(state, intervals);
|
---|
111 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
112 | s = Interval.Divide(s, Evaluate(state, intervals));
|
---|
113 | }
|
---|
114 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
115 | return s;
|
---|
116 | }
|
---|
117 | //Trigonometric functions
|
---|
118 | case OpCodes.Sin: {
|
---|
119 | var s = Interval.Sine(Evaluate(state, intervals));
|
---|
120 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
121 | return s;
|
---|
122 | }
|
---|
123 | case OpCodes.Cos: {
|
---|
124 | var s = Interval.Cosine(Evaluate(state, intervals));
|
---|
125 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
126 | return s;
|
---|
127 | }
|
---|
128 | case OpCodes.Tan: {
|
---|
129 | var s = Interval.Tangens(Evaluate(state, intervals));
|
---|
130 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
131 | return s;
|
---|
132 | }
|
---|
133 | //Exponential functions
|
---|
134 | case OpCodes.Log: {
|
---|
135 | var s = Interval.Logarithm(Evaluate(state, intervals));
|
---|
136 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
137 | return s;
|
---|
138 | }
|
---|
139 | case OpCodes.Exp: {
|
---|
140 | var s = Interval.Exponential(Evaluate(state, intervals));
|
---|
141 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
142 | return s;
|
---|
143 | }
|
---|
144 | case OpCodes.Power: {
|
---|
145 | var s = Evaluate(state, intervals);
|
---|
146 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
147 | s = Interval.Power(s, Evaluate(state, intervals));
|
---|
148 | }
|
---|
149 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
150 | return s;
|
---|
151 | }
|
---|
152 | case OpCodes.Square: {
|
---|
153 | var s = Interval.Square(Evaluate(state, intervals));
|
---|
154 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
155 | return s;
|
---|
156 | }
|
---|
157 | case OpCodes.Root: {
|
---|
158 | var s = Evaluate(state, intervals);
|
---|
159 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
160 | s = Interval.Root(s, Evaluate(state, intervals));
|
---|
161 | }
|
---|
162 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
163 | return s;
|
---|
164 | }
|
---|
165 | case OpCodes.SquareRoot: {
|
---|
166 | var s = Interval.SquareRoot(Evaluate(state, intervals));
|
---|
167 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
168 | return s;
|
---|
169 | }
|
---|
170 | //Variables, Constants, ...
|
---|
171 | case OpCodes.Variable: {
|
---|
172 | intervals.Add(currentInstr.dynamicNode, (Interval)currentInstr.data);
|
---|
173 | return (Interval)currentInstr.data;
|
---|
174 | }
|
---|
175 | case OpCodes.Constant: {
|
---|
176 | var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
|
---|
177 | var inter = new Interval(constTreeNode.Value, constTreeNode.Value);
|
---|
178 | intervals.Add(currentInstr.dynamicNode, inter);
|
---|
179 | return inter;
|
---|
180 | }
|
---|
181 | default:
|
---|
182 | throw new NotSupportedException();
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void InitializeState() {
|
---|
187 | EvaluatedSolutions = 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IEnumerable<int> rows, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
|
---|
191 | intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
|
---|
192 | var state = PrepareInterpreterState(tree, rows);
|
---|
193 | var x = Evaluate(state, intervals);
|
---|
194 |
|
---|
195 | return x;
|
---|
196 | }
|
---|
197 |
|
---|
198 | public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
|
---|
199 | Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
|
---|
200 | intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
|
---|
201 | var state = PrepareInterpreterState(tree, rows, dataset, customIntervals);
|
---|
202 | var x = Evaluate(state, intervals);
|
---|
203 |
|
---|
204 | return x;
|
---|
205 | }
|
---|
206 |
|
---|
207 | public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
|
---|
208 | out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
|
---|
209 | intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
|
---|
210 | var state = PrepareInterpreterState(tree, rows, dataset);
|
---|
211 | var x = Evaluate(state, intervals);
|
---|
212 |
|
---|
213 | return x;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|