Changeset 16364 for branches/2966_interval_calculation
- Timestamp:
- 12/11/18 14:54:35 (6 years ago)
- Location:
- branches/2966_interval_calculation
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs
r16331 r16364 78 78 ResetInstrucitonCount(); 79 79 } 80 var instructions = PrepareInterpreterState(tree, rows, dataset); 80 var intervalBoundaries = DatasetUtil.GetVariableBoundaries(dataset, rows); 81 var instructions = PrepareInterpreterState(tree, intervalBoundaries); 81 82 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 }103 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();104 var instructions = PrepareInterpreterState(tree, rows, dataset, customIntervals);105 var x = Evaluate(instructions, intervals);106 83 107 84 return x; … … 110 87 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, 111 88 out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) { 112 lock(syncRoot) { 113 EvaluatedSolutions++; 114 ResetInstrucitonCount(); 115 } 89 lock (syncRoot) { 90 EvaluatedSolutions++; 91 ResetInstrucitonCount(); 92 } 93 var intervalBoundaries = DatasetUtil.GetVariableBoundaries(dataset, rows); 116 94 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 117 var instructions = PrepareInterpreterState(tree, rows, dataset);95 var instructions = PrepareInterpreterState(tree, intervalBoundaries); 118 96 var x = Evaluate(instructions, intervals); 119 97 … … 121 99 } 122 100 123 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals, 124 out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) { 101 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals) { 102 lock (syncRoot) { 103 EvaluatedSolutions++; 104 ResetInstrucitonCount(); 105 } 106 var instructions = PrepareInterpreterState(tree, customIntervals); 107 var x = Evaluate(instructions); 108 109 return x; 110 } 111 112 113 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, 114 Dictionary<string, Interval> customIntervals, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) { 125 115 lock (syncRoot) { 126 116 EvaluatedSolutions++; … … 128 118 } 129 119 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 130 var instructions = PrepareInterpreterState(tree, rows, null,customIntervals);120 var instructions = PrepareInterpreterState(tree, customIntervals); 131 121 var x = Evaluate(instructions, intervals); 132 122 … … 134 124 } 135 125 136 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, 137 IEnumerable<int> rows = null, IDataset dataset = null, Dictionary<string, Interval> customIntervals = null) {126 127 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervals) { 138 128 Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode); 139 129 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); 145 146 foreach (Instruction instr in code) { 147 if (instr.opCode == OpCodes.Variable) { 148 var variableTreeNode = (VariableTreeNode)instr.dynamicNode; 149 IList<double> values = new List<double>(); 150 151 if (customIntervals != null) { 152 if (customIntervals.ContainsKey(variableTreeNode.VariableName)) { 153 instr.data = customIntervals[variableTreeNode.VariableName]; 154 } 155 } else { 156 foreach (var rowEnum in rows) { 157 values.Add(dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName)[rowEnum]); 158 } 159 instr.data = new Interval(values.Min(), values.Max()); 160 } 161 } 130 if (customIntervals == null) 131 throw new ArgumentException("No interval ranges are present!", nameof(customIntervals)); 132 133 foreach (var variable in tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName).Distinct()) { 134 if (!customIntervals.ContainsKey(variable)) throw new InvalidOperationException($"No ranges for variable {variable} is present"); 135 } 136 137 foreach (Instruction instr in code.Where(i => i.opCode == OpCodes.Variable)) { 138 var variableTreeNode = (VariableTreeNode)instr.dynamicNode; 139 instr.data = customIntervals[variableTreeNode.VariableName]; 162 140 } 163 141 return code; 164 142 } 165 143 166 private Interval Evaluate(Instruction[] instructions, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals = null) { 144 private Interval Evaluate(Instruction[] instructions, Dictionary<ISymbolicExpressionTreeNode, 145 Interval> intervals = null) { 167 146 Instruction currentInstr = instructions[InstructionCount++]; 168 147 Interval intermediate = null; -
branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/DatasetUtil.cs
r16363 r16364 93 93 } 94 94 95 public static Dictionary<string, Interval> GetVariableBoundaries(IDataset dataset ) {95 public static Dictionary<string, Interval> GetVariableBoundaries(IDataset dataset, IEnumerable<int> rows = null) { 96 96 Dictionary<string, Interval> variableBoundaries = new Dictionary<string, Interval>(); 97 97 … … 100 100 var max = double.MinValue; 101 101 102 foreach (var val in dataset.GetDoubleValues(variable)) { 103 if (val < min) min = val; 104 if (val > max) max = val; 102 if (rows != null) { 103 foreach (int row in rows) { 104 var val = dataset.GetDoubleValue(variable, row); 105 if (val < min) min = val; 106 if (val > max) max = val; 107 } 108 } else { 109 foreach (var val in dataset.GetDoubleValues(variable)) { 110 if (val < min) min = val; 111 if (val > max) max = val; 112 } 105 113 } 106 114 variableBoundaries.Add(variable, new Interval(min, max));
Note: See TracChangeset
for help on using the changeset viewer.