Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2956_apriori_knowledge/HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration/3.4/Interpreter/SymbolicDataAnalysisIntervalArithmeticInterpreter.cs @ 16773

Last change on this file since 16773 was 16313, checked in by chaider, 6 years ago

#2956:

  • Updated value visualization of simplifier tooltip
  • Added VariableRanges field to dataset
  • Added overloads for GetSymbolicExressionTreeIntervals
File size: 11.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.IntervalArithmetic;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10using HeuristicLab.Problems.DataAnalysis;
11using HeuristicLab.Problems.DataAnalysis.Symbolic;
12
13namespace HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.Interpreter {
14  [StorableClass]
15  [Item("SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.")]
16  public class SymbolicDataAnalysisIntervalArithmeticInterpreter : 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 SymbolicDataAnalysisIntervalArithmeticInterpreter(bool deserializing) : base(deserializing) { }
30
31    protected SymbolicDataAnalysisIntervalArithmeticInterpreter(SymbolicDataAnalysisIntervalArithmeticInterpreter original,
32        Cloner cloner)
33        : base(original, cloner) { }
34
35    public SymbolicDataAnalysisIntervalArithmeticInterpreter()
36        : base("SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.") { }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new SymbolicDataAnalysisIntervalArithmeticInterpreter(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 static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, Dictionary<string, Interval> customIntervals = null) {
82    //  Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
83    //  int necessaryArgStackSize = 0;
84    //  foreach (Instruction instr in code) {
85    //    if (instr.opCode == OpCodes.Variable) {
86    //      var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
87    //      IList<double> values = new List<double>();
88
89    //      if (customIntervals != null && customIntervals.ContainsKey(variableTreeNode.VariableName)) {
90    //        instr.data = customIntervals[variableTreeNode.VariableName];
91    //      } else {
92    //        foreach (var rowEnum in rows) {
93    //          values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
94    //        }
95    //        instr.data = new Interval(values.Min(), values.Max());
96    //      }
97    //      //instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
98    //    } else if (instr.opCode == OpCodes.FactorVariable) {
99    //      var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode;
100    //      instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
101    //    } else if (instr.opCode == OpCodes.BinaryFactorVariable) {
102    //      var factorTreeNode = instr.dynamicNode as BinaryFactorVariableTreeNode;
103    //      instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
104    //    } else if (instr.opCode == OpCodes.LagVariable) {
105    //      var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
106    //      instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
107    //    } else if (instr.opCode == OpCodes.VariableCondition) {
108    //      var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
109    //      instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
110    //    } else if (instr.opCode == OpCodes.Call) {
111    //      necessaryArgStackSize += instr.nArguments + 1;
112    //    }
113    //  }
114    //  return new InterpreterState(code, necessaryArgStackSize);
115    //}
116
117    private Interval Evaluate(InterpreterState state, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
118      Instruction currentInstr = state.NextInstruction();
119      switch (currentInstr.opCode) {
120        //Elementary arithmetic rules
121        case OpCodes.Add: {
122            var s = Evaluate(state, intervals);
123            for (int i = 1; i < currentInstr.nArguments; i++) {
124              s = Interval.Add(s, Evaluate(state, intervals));
125            }
126            intervals.Add(currentInstr.dynamicNode, s);
127            return s;
128          }
129        case OpCodes.Sub: {
130            var s = Evaluate(state, intervals);
131            for (int i = 1; i < currentInstr.nArguments; i++) {
132              s = Interval.Subtract(s, Evaluate(state, intervals));
133            }
134            intervals.Add(currentInstr.dynamicNode, s);
135            return s;
136          }
137        case OpCodes.Mul: {
138            var s = Evaluate(state, intervals);
139            for (int i = 1; i < currentInstr.nArguments; i++) {
140              s = Interval.Multiply(s, Evaluate(state, intervals));
141            }
142            intervals.Add(currentInstr.dynamicNode, s);
143            return s;
144          }
145        case OpCodes.Div: {
146            var s = Evaluate(state, intervals);
147            for (int i = 1; i < currentInstr.nArguments; i++) {
148              s = Interval.Divide(s, Evaluate(state, intervals));
149            }
150            intervals.Add(currentInstr.dynamicNode, s);
151            return s;
152          }
153        //Trigonometric functions
154        case OpCodes.Sin: {
155            var s = Interval.Sine(Evaluate(state, intervals));
156            intervals.Add(currentInstr.dynamicNode, s);
157            return s;
158          }
159        case OpCodes.Cos: {
160            var s = Interval.Cosine(Evaluate(state, intervals));
161            intervals.Add(currentInstr.dynamicNode, s);
162            return s;
163          }
164        case OpCodes.Tan: {
165            var s = Interval.Tangens(Evaluate(state, intervals));
166            intervals.Add(currentInstr.dynamicNode, s);
167            return s;
168          }
169        //Exponential functions
170        case OpCodes.Log: {
171            var s = Interval.Logarithm(Evaluate(state, intervals));
172            intervals.Add(currentInstr.dynamicNode, s);
173            return s;
174          }
175        case OpCodes.Exp: {
176            var s = Interval.Exponential(Evaluate(state, intervals));
177            intervals.Add(currentInstr.dynamicNode, s);
178            return s;
179          }
180        case OpCodes.Power: {
181            var s = Evaluate(state, intervals);
182            for (int i = 1; i < currentInstr.nArguments; i++) {
183              s = Interval.Power(s, Evaluate(state, intervals));
184            }
185            intervals.Add(currentInstr.dynamicNode, s);
186            return s;
187          }
188        case OpCodes.Root: {
189            var s = Evaluate(state, intervals);
190            for (int i = 1; i < currentInstr.nArguments; i++) {
191              s = Interval.Root(s, Evaluate(state, intervals));
192            }
193            intervals.Add(currentInstr.dynamicNode, s);
194            return s;
195          }
196        //Variables, Constants, ...
197        case OpCodes.Variable: {
198            intervals.Add(currentInstr.dynamicNode, (Interval)currentInstr.data);
199            return (Interval)currentInstr.data;
200          }
201        case OpCodes.Constant: {
202            var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
203            var inter = new Interval(constTreeNode.Value, constTreeNode.Value);
204            intervals.Add(currentInstr.dynamicNode, inter);
205            return inter;
206          }
207        default:
208          throw new NotSupportedException();
209      }
210    }
211
212    public void InitializeState() {
213      EvaluatedSolutions = 0;
214    }
215
216    //public InterpreterState GetInstructions(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, Dictionary<string, Interval> customIntervals = null) {
217    //  var state = PrepareInterpreterState(tree, dataset, rows, customIntervals);
218    //  var x = Evaluate(dataset, state);
219    //  state.Reset();
220
221    //  return state;
222    //}
223
224    // public Interval GetSymbolicExressionTreeInterval(tree,variableIntervals)
225    //public Interval GetSymbolicExressionTreeInterval(tree,Dataset,intervals = null)
226
227    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IEnumerable<int> rows, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
228      intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
229      var state = PrepareInterpreterState(tree, rows);
230      var x = Evaluate(state, intervals);
231
232      return x;
233    }
234
235    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
236      Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
237      intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
238      var state = PrepareInterpreterState(tree, rows, dataset, customIntervals);
239      var x = Evaluate(state, intervals);
240
241      return x;
242    }
243
244    public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
245      out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
246      intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
247      var state = PrepareInterpreterState(tree, rows, dataset);
248      var x = Evaluate(state, intervals);
249     
250      return x;
251    }
252  }
253}
Note: See TracBrowser for help on using the repository browser.