Changeset 16330
 Timestamp:
 11/27/18 10:20:59 (14 months ago)
 Location:
 branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
 Files:

 1 edited
 1 moved
Legend:
 Unmodified
 Added
 Removed

branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic3.4.csproj
r16323 r16330 199 199 <Compile Include="Interfaces\IVariableSymbol.cs" /> 200 200 <Compile Include="Interpreter\SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs" /> 201 <Compile Include="Interpreter\Interval nterpreter.cs" />201 <Compile Include="Interpreter\IntervalInterpreter.cs" /> 202 202 <Compile Include="SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs" /> 203 203 <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" /> 
branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs
r16329 r16330 1 using System; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 20022018 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; 2 23 using System.Collections.Generic; 3 24 using System.Linq; … … 12 33 namespace HeuristicLab.Algorithms.DataAnalysis.Symbolic { 13 34 [StorableClass] 14 [Item(" SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.")]35 [Item("IntervalInterpreter", "Intperter for calculation of intervals of symbolic models.")] 15 36 public sealed class IntervalInterpreter : ParameterizedNamedItem, IStatefulItem { 16 37 17 38 private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions"; 39 private static int InstructionCount = 0; 40 18 41 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter { 19 42 get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } … … 30 53 31 54 public IntervalInterpreter() 32 : base(" SymbolicDataAnalysisIntervalArithmeticInterpreter", "Interpreter for interval arithmetic within symbolic regression.") { }55 : base("IntervalInterpreter", "Intperter for calculation of intervals of symbolic models.") { } 33 56 34 57 public override IDeepCloneable Clone(Cloner cloner) { 35 58 return new IntervalInterpreter(this, cloner); 36 59 } 60 61 private readonly object syncRoot = new object(); 37 62 38 63 #region IStatefulItem Members … … 43 68 #endregion 44 69 45 46 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IEnumerable<int> rows, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) { 70 private static void ResetInstrucitonCount() { 71 InstructionCount = 0; 72 } 73 74 75 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) { 76 lock (syncRoot) { 77 EvaluatedSolutions++; 78 ResetInstrucitonCount(); 79 } 80 var instructions = PrepareInterpreterState(tree, rows, dataset); 81 var x = Evaluate(instructions); 82 83 return x; 84 } 85 86 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals, IEnumerable<int> rows = null) { 87 lock (syncRoot) { 88 EvaluatedSolutions++; 89 ResetInstrucitonCount(); 90 } 91 var instructions = PrepareInterpreterState(tree, rows, null, customIntervals); 92 var x = Evaluate(instructions); 93 94 return x; 95 } 96 97 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, 98 Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) { 99 lock (syncRoot) { 100 EvaluatedSolutions++; 101 ResetInstrucitonCount(); 102 } 47 103 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 48 var state = PrepareInterpreterState(tree, rows); 49 var x = Evaluate(state, intervals); 50 51 return x; 52 } 53 54 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, 55 Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) { 104 var instructions = PrepareInterpreterState(tree, rows, dataset, customIntervals); 105 var x = Evaluate(instructions, intervals); 106 107 return x; 108 } 109 110 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, 111 out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) { 112 lock(syncRoot) { 113 EvaluatedSolutions++; 114 ResetInstrucitonCount(); 115 } 56 116 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 57 var state = PrepareInterpreterState(tree, rows, dataset, customIntervals); 58 var x = Evaluate(state, intervals); 59 60 return x; 61 } 62 63 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, 64 out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) { 117 var instructions = PrepareInterpreterState(tree, rows, dataset); 118 var x = Evaluate(instructions, intervals); 119 120 return x; 121 } 122 123 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals, 124 out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) { 125 lock (syncRoot) { 126 EvaluatedSolutions++; 127 ResetInstrucitonCount(); 128 } 65 129 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 66 var state = PrepareInterpreterState(tree, rows, dataset); 67 var x = Evaluate(state, intervals); 68 69 return x; 70 } 71 72 private readonly object syncRoot = new object(); 73 74 private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, 75 IEnumerable<int> rows, IDataset dataset = null, Dictionary<string, Interval> customIntervals = null) { 130 var instructions = PrepareInterpreterState(tree, rows, null, customIntervals); 131 var x = Evaluate(instructions, intervals); 132 133 return x; 134 } 135 136 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, 137 IEnumerable<int> rows = null, IDataset dataset = null, Dictionary<string, Interval> customIntervals = null) { 76 138 Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode); 77 int necessaryArgStackSize = 0; 139 140 if (dataset == null && customIntervals == null) 141 throw new Exception("No dataset or ranges for intervals are given!"); 142 143 if(rows == null) 144 rows = Enumerable.Range(0, dataset.Rows); 78 145 79 146 foreach (Instruction instr in code) { … … 82 149 IList<double> values = new List<double>(); 83 150 84 if ( dataset != null &&customIntervals != null) {151 if (customIntervals != null) { 85 152 if (customIntervals.ContainsKey(variableTreeNode.VariableName)) { 86 153 instr.data = customIntervals[variableTreeNode.VariableName]; 87 154 } 88 else { 89 foreach (var rowEnum in rows) { 90 values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]); 91 } 92 instr.data = new Interval(values.Min(), values.Max()); 93 } 94 } 95 else if (dataset != null) { 155 } else { 96 156 foreach (var rowEnum in rows) { 97 157 values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]); … … 99 159 instr.data = new Interval(values.Min(), values.Max()); 100 160 } 101 else if (customIntervals != null) {102 if (customIntervals.ContainsKey(variableTreeNode.VariableName)) {103 instr.data = customIntervals[variableTreeNode.VariableName];104 }105 }106 else {107 throw new Exception("No valid input for variables!");108 }109 161 } 110 162 } 111 112 return new InterpreterState(code, necessaryArgStackSize); 113 } 114 115 private Interval Evaluate(InterpreterState state, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) { 116 Instruction currentInstr = state.NextInstruction(); 163 return code; 164 } 165 166 private Interval Evaluate(Instruction[] instructions, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals = null) { 167 Instruction currentInstr = instructions[InstructionCount++]; 117 168 switch (currentInstr.opCode) { 118 169 //Elementary arithmetic rules 119 170 case OpCodes.Add: { 120 var s = Evaluate( state, intervals);121 for (int i = 1; i < currentInstr.nArguments; i++) { 122 s = Interval.Add(s, Evaluate( state, intervals));171 var s = Evaluate(instructions, intervals); 172 for (int i = 1; i < currentInstr.nArguments; i++) { 173 s = Interval.Add(s, Evaluate(instructions, intervals)); 123 174 } 124 175 intervals.Add(currentInstr.dynamicNode, s); … … 126 177 } 127 178 case OpCodes.Sub: { 128 var s = Evaluate( state, intervals);129 for (int i = 1; i < currentInstr.nArguments; i++) { 130 s = Interval.Subtract(s, Evaluate( state, intervals));179 var s = Evaluate(instructions, intervals); 180 for (int i = 1; i < currentInstr.nArguments; i++) { 181 s = Interval.Subtract(s, Evaluate(instructions, intervals)); 131 182 } 132 183 intervals.Add(currentInstr.dynamicNode, s); … … 134 185 } 135 186 case OpCodes.Mul: { 136 var s = Evaluate( state, intervals);137 for (int i = 1; i < currentInstr.nArguments; i++) { 138 s = Interval.Multiply(s, Evaluate( state, intervals));187 var s = Evaluate(instructions, intervals); 188 for (int i = 1; i < currentInstr.nArguments; i++) { 189 s = Interval.Multiply(s, Evaluate(instructions, intervals)); 139 190 } 140 191 intervals.Add(currentInstr.dynamicNode, s); … … 142 193 } 143 194 case OpCodes.Div: { 144 var s = Evaluate( state, intervals);145 for (int i = 1; i < currentInstr.nArguments; i++) { 146 s = Interval.Divide(s, Evaluate( state, intervals));195 var s = Evaluate(instructions, intervals); 196 for (int i = 1; i < currentInstr.nArguments; i++) { 197 s = Interval.Divide(s, Evaluate(instructions, intervals)); 147 198 } 148 199 intervals.Add(currentInstr.dynamicNode, s); … … 151 202 //Trigonometric functions 152 203 case OpCodes.Sin: { 153 var s = Interval.Sine(Evaluate( state, intervals));204 var s = Interval.Sine(Evaluate(instructions, intervals)); 154 205 intervals.Add(currentInstr.dynamicNode, s); 155 206 return s; 156 207 } 157 208 case OpCodes.Cos: { 158 var s = Interval.Cosine(Evaluate( state, intervals));209 var s = Interval.Cosine(Evaluate(instructions, intervals)); 159 210 intervals.Add(currentInstr.dynamicNode, s); 160 211 return s; 161 212 } 162 213 case OpCodes.Tan: { 163 var s = Interval.Tangens(Evaluate( state, intervals));214 var s = Interval.Tangens(Evaluate(instructions, intervals)); 164 215 intervals.Add(currentInstr.dynamicNode, s); 165 216 return s; … … 167 218 //Exponential functions 168 219 case OpCodes.Log: { 169 var s = Interval.Logarithm(Evaluate( state, intervals));220 var s = Interval.Logarithm(Evaluate(instructions, intervals)); 170 221 intervals.Add(currentInstr.dynamicNode, s); 171 222 return s; 172 223 } 173 224 case OpCodes.Exp: { 174 var s = Interval.Exponential(Evaluate( state, intervals));225 var s = Interval.Exponential(Evaluate(instructions, intervals)); 175 226 intervals.Add(currentInstr.dynamicNode, s); 176 227 return s; 177 228 } 178 229 case OpCodes.Power: { 179 var s = Evaluate( state, intervals);180 for (int i = 1; i < currentInstr.nArguments; i++) { 181 s = Interval.Power(s, Evaluate( state, intervals));230 var s = Evaluate(instructions, intervals); 231 for (int i = 1; i < currentInstr.nArguments; i++) { 232 s = Interval.Power(s, Evaluate(instructions, intervals)); 182 233 } 183 234 intervals.Add(currentInstr.dynamicNode, s); … … 185 236 } 186 237 case OpCodes.Square: { 187 var s = Interval.Square(Evaluate( state, intervals));238 var s = Interval.Square(Evaluate(instructions, intervals)); 188 239 intervals.Add(currentInstr.dynamicNode, s); 189 240 return s; 190 241 } 191 242 case OpCodes.Root: { 192 var s = Evaluate( state, intervals);193 for (int i = 1; i < currentInstr.nArguments; i++) { 194 s = Interval.Root(s, Evaluate( state, intervals));243 var s = Evaluate(instructions, intervals); 244 for (int i = 1; i < currentInstr.nArguments; i++) { 245 s = Interval.Root(s, Evaluate(instructions, intervals)); 195 246 } 196 247 intervals.Add(currentInstr.dynamicNode, s); … … 198 249 } 199 250 case OpCodes.SquareRoot: { 200 var s = Interval.SquareRoot(Evaluate( state, intervals));251 var s = Interval.SquareRoot(Evaluate(instructions, intervals)); 201 252 intervals.Add(currentInstr.dynamicNode, s); 202 253 return s; … … 214 265 } 215 266 default: 216 throw new NotSupportedException(); 217 } 267 throw new NotSupportedException("Tree contains an unknown symbol."); 268 } 269 } 270 271 public static bool IsCompatible(ISymbolicExpressionTree tree) { 272 var containsUnknownSyumbol = ( 273 from n in tree.Root.GetSubtree(0).IterateNodesPrefix() 274 where 275 !(n.Symbol is StartSymbol) && 276 !(n.Symbol is Addition) && 277 !(n.Symbol is Subtraction) && 278 !(n.Symbol is Multiplication) && 279 !(n.Symbol is Division) && 280 !(n.Symbol is Sine) && 281 !(n.Symbol is Cosine) && 282 !(n.Symbol is Tangent) && 283 !(n.Symbol is Logarithm) && 284 !(n.Symbol is Exponential) && 285 !(n.Symbol is Power) && 286 !(n.Symbol is Square) && 287 !(n.Symbol is Root) && 288 !(n.Symbol is SquareRoot) && 289 !(n.Symbol is Problems.DataAnalysis.Symbolic.Variable) && 290 !(n.Symbol is Constant) 291 select n).Any(); 292 return !containsUnknownSyumbol; 218 293 } 219 294 }
Note: See TracChangeset
for help on using the changeset viewer.