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 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, IDataset dataset, IEnumerable<int> rows, Dictionary<string, Interval> customIntervals = null) {
|
---|
45 | Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
46 | int necessaryArgStackSize = 0;
|
---|
47 |
|
---|
48 | foreach(Instruction instr in code) {
|
---|
49 | if (instr.opCode == OpCodes.Variable) {
|
---|
50 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
51 | IList<double> values = new List<double>();
|
---|
52 |
|
---|
53 | if (customIntervals != null && customIntervals.ContainsKey(variableTreeNode.VariableName)) {
|
---|
54 | instr.data = customIntervals[variableTreeNode.VariableName];
|
---|
55 | } else {
|
---|
56 | foreach (var rowEnum in rows) {
|
---|
57 | values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
|
---|
58 | }
|
---|
59 | instr.data = new Interval(values.Min(), values.Max());
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | return new InterpreterState(code, necessaryArgStackSize);
|
---|
65 | }
|
---|
66 |
|
---|
67 | //private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, Dictionary<string, Interval> customIntervals = null) {
|
---|
68 | // Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
69 | // int necessaryArgStackSize = 0;
|
---|
70 | // foreach (Instruction instr in code) {
|
---|
71 | // if (instr.opCode == OpCodes.Variable) {
|
---|
72 | // var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
73 | // IList<double> values = new List<double>();
|
---|
74 |
|
---|
75 | // if (customIntervals != null && customIntervals.ContainsKey(variableTreeNode.VariableName)) {
|
---|
76 | // instr.data = customIntervals[variableTreeNode.VariableName];
|
---|
77 | // } else {
|
---|
78 | // foreach (var rowEnum in rows) {
|
---|
79 | // values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]);
|
---|
80 | // }
|
---|
81 | // instr.data = new Interval(values.Min(), values.Max());
|
---|
82 | // }
|
---|
83 | // //instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
84 | // } else if (instr.opCode == OpCodes.FactorVariable) {
|
---|
85 | // var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode;
|
---|
86 | // instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
|
---|
87 | // } else if (instr.opCode == OpCodes.BinaryFactorVariable) {
|
---|
88 | // var factorTreeNode = instr.dynamicNode as BinaryFactorVariableTreeNode;
|
---|
89 | // instr.data = dataset.GetReadOnlyStringValues(factorTreeNode.VariableName);
|
---|
90 | // } else if (instr.opCode == OpCodes.LagVariable) {
|
---|
91 | // var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
|
---|
92 | // instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
|
---|
93 | // } else if (instr.opCode == OpCodes.VariableCondition) {
|
---|
94 | // var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
|
---|
95 | // instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
|
---|
96 | // } else if (instr.opCode == OpCodes.Call) {
|
---|
97 | // necessaryArgStackSize += instr.nArguments + 1;
|
---|
98 | // }
|
---|
99 | // }
|
---|
100 | // return new InterpreterState(code, necessaryArgStackSize);
|
---|
101 | //}
|
---|
102 |
|
---|
103 | private Interval Evaluate(InterpreterState state, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
|
---|
104 | Instruction currentInstr = state.NextInstruction();
|
---|
105 | switch (currentInstr.opCode) {
|
---|
106 | //Elementary arithmetic rules
|
---|
107 | case OpCodes.Add: {
|
---|
108 | var s = Evaluate(state, intervals);
|
---|
109 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
110 | s = Interval.Add(s, Evaluate(state, intervals));
|
---|
111 | }
|
---|
112 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
113 | return s;
|
---|
114 | }
|
---|
115 | case OpCodes.Sub: {
|
---|
116 | var s = Evaluate(state, intervals);
|
---|
117 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
118 | s = Interval.Subtract(s, Evaluate(state, intervals));
|
---|
119 | }
|
---|
120 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
121 | return s;
|
---|
122 | }
|
---|
123 | case OpCodes.Mul: {
|
---|
124 | var s = Evaluate(state, intervals);
|
---|
125 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
126 | s = Interval.Multiply(s, Evaluate(state, intervals));
|
---|
127 | }
|
---|
128 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
129 | return s;
|
---|
130 | }
|
---|
131 | case OpCodes.Div: {
|
---|
132 | var s = Evaluate(state, intervals);
|
---|
133 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
134 | s = Interval.Divide(s, Evaluate(state, intervals));
|
---|
135 | }
|
---|
136 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
137 | return s;
|
---|
138 | }
|
---|
139 | //Trigonometric functions
|
---|
140 | case OpCodes.Sin: {
|
---|
141 | var s = Interval.Sine(Evaluate(state, intervals));
|
---|
142 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
143 | return s;
|
---|
144 | }
|
---|
145 | case OpCodes.Cos: {
|
---|
146 | var s = Interval.Cosine(Evaluate(state, intervals));
|
---|
147 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
148 | return s;
|
---|
149 | }
|
---|
150 | case OpCodes.Tan: {
|
---|
151 | var s = Interval.Tangens(Evaluate(state, intervals));
|
---|
152 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
153 | return s;
|
---|
154 | }
|
---|
155 | //Exponential functions
|
---|
156 | case OpCodes.Log: {
|
---|
157 | var s = Interval.Logarithm(Evaluate(state, intervals));
|
---|
158 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
159 | return s;
|
---|
160 | }
|
---|
161 | case OpCodes.Exp: {
|
---|
162 | var s = Interval.Exponential(Evaluate(state, intervals));
|
---|
163 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
164 | return s;
|
---|
165 | }
|
---|
166 | case OpCodes.Power: {
|
---|
167 | var s = Evaluate(state, intervals);
|
---|
168 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
169 | s = Interval.Power(s, Evaluate(state, intervals));
|
---|
170 | }
|
---|
171 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
172 | return s;
|
---|
173 | }
|
---|
174 | case OpCodes.Root: {
|
---|
175 | var s = Evaluate(state, intervals);
|
---|
176 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
177 | s = Interval.Root(s, Evaluate(state, intervals));
|
---|
178 | }
|
---|
179 | intervals.Add(currentInstr.dynamicNode, s);
|
---|
180 | return s;
|
---|
181 | }
|
---|
182 | //Variables, Constants, ...
|
---|
183 | case OpCodes.Variable: {
|
---|
184 | intervals.Add(currentInstr.dynamicNode, (Interval)currentInstr.data);
|
---|
185 | return (Interval)currentInstr.data;
|
---|
186 | }
|
---|
187 | case OpCodes.Constant: {
|
---|
188 | var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
|
---|
189 | var inter = new Interval(constTreeNode.Value, constTreeNode.Value);
|
---|
190 | intervals.Add(currentInstr.dynamicNode, inter);
|
---|
191 | return inter;
|
---|
192 | }
|
---|
193 | default:
|
---|
194 | throw new NotSupportedException();
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void InitializeState() {
|
---|
199 | EvaluatedSolutions = 0;
|
---|
200 | }
|
---|
201 |
|
---|
202 | //public InterpreterState GetInstructions(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, Dictionary<string, Interval> customIntervals = null) {
|
---|
203 | // var state = PrepareInterpreterState(tree, dataset, rows, customIntervals);
|
---|
204 | // var x = Evaluate(dataset, state);
|
---|
205 | // state.Reset();
|
---|
206 |
|
---|
207 | // return state;
|
---|
208 | //}
|
---|
209 |
|
---|
210 | // public Interval GetSymbolicExressionTreeInterval(tree,variableIntervals)
|
---|
211 | //public Interval GetSymbolicExressionTreeInterval(tree,Dataset,intervals = null)
|
---|
212 | public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows,
|
---|
213 | out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {
|
---|
214 | intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
|
---|
215 | var state = PrepareInterpreterState(tree, dataset, rows);
|
---|
216 | var x = Evaluate(state, intervals);
|
---|
217 |
|
---|
218 | return x;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | }
|
---|