Changeset 16404 for branches/2966_interval_calculation
- Timestamp:
- 12/19/18 14:03:10 (6 years ago)
- Location:
- branches/2966_interval_calculation
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs
r16403 r16404 69 69 #endregion 70 70 71 public Interval GetSymbolicExressionTreeInterval s(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {71 public Interval GetSymbolicExressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) { 72 72 var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows); 73 return GetSymbolicExressionTreeInterval s(tree, variableRanges);73 return GetSymbolicExressionTreeInterval(tree, variableRanges); 74 74 } 75 75 76 76 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, 77 out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) {77 out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) { 78 78 var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows); 79 return GetSymbolicExressionTreeIntervals(tree, variableRanges, out intervals);80 } 81 82 public Interval GetSymbolicExressionTreeInterval s(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervalsForVariables) {79 return GetSymbolicExressionTreeIntervals(tree, variableRanges, out nodeIntervals); 80 } 81 82 public Interval GetSymbolicExressionTreeInterval(ISymbolicExpressionTree tree, Dictionary<string, Interval> variableRanges) { 83 83 lock (syncRoot) { 84 84 EvaluatedSolutions++; 85 85 } 86 86 int instructionCount = 0; 87 var instructions = PrepareInterpreterState(tree, customIntervalsForVariables);87 var instructions = PrepareInterpreterState(tree, variableRanges); 88 88 var outputInterval = Evaluate(instructions, ref instructionCount); 89 89 … … 93 93 94 94 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, 95 Dictionary<string, Interval> customIntervalsForVariables, out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals) {95 Dictionary<string, Interval> variableRanges, out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) { 96 96 lock (syncRoot) { 97 97 EvaluatedSolutions++; 98 98 } 99 99 int instructionCount = 0; 100 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();101 var instructions = PrepareInterpreterState(tree, customIntervalsForVariables);100 var intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 101 var instructions = PrepareInterpreterState(tree, variableRanges); 102 102 var outputInterval = Evaluate(instructions, ref instructionCount, intervals); 103 103 104 nodeIntervals = intervals; 105 104 106 return outputInterval; 105 107 } 106 108 107 109 108 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> customIntervalsForVariables) { 110 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> variableRanges) { 111 if (variableRanges == null) 112 throw new ArgumentNullException("No variablew ranges are present!", nameof(variableRanges)); 113 114 //Check if all variables used in the tree are present in the dataset 115 foreach (var variable in tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName).Distinct()) { 116 if (!variableRanges.ContainsKey(variable)) throw new InvalidOperationException($"No ranges for variable {variable} is present"); 117 } 118 109 119 Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode); 110 111 if (customIntervalsForVariables == null)112 throw new ArgumentException("No interval ranges are present!", nameof(customIntervalsForVariables));113 114 foreach (var variable in tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName).Distinct()) {115 if (!customIntervalsForVariables.ContainsKey(variable)) throw new InvalidOperationException($"No ranges for variable {variable} is present");116 }117 118 120 foreach (Instruction instr in code.Where(i => i.opCode == OpCodes.Variable)) { 119 121 var variableTreeNode = (VariableTreeNode)instr.dynamicNode; 120 instr.data = customIntervalsForVariables[variableTreeNode.VariableName];122 instr.data = variableRanges[variableTreeNode.VariableName]; 121 123 } 122 124 return code; 123 125 } 124 126 125 private Interval Evaluate(Instruction[] instructions, ref int instructionCount, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals = null) { 126 Instruction currentInstr = instructions[instructionCount++]; 127 private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) { 128 Instruction currentInstr = instructions[instructionCounter]; 129 //Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side 130 //Update instructionCounter, whenever Evaluate is called 131 instructionCounter++; 127 132 Interval result = null; 128 133 … … 131 136 case OpCodes.Variable: { 132 137 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 133 var variableWeight = variableTreeNode.Weight;134 var vari bleWeightInterval = new Interval(variableWeight, variableWeight);135 136 result = Interval.Multiply( (Interval)currentInstr.data, varibleWeightInterval);138 var weightInterval = new Interval(variableTreeNode.Weight, variableTreeNode.Weight); 139 var variableInterval = (Interval)currentInstr.data; 140 141 result = Interval.Multiply(variableInterval, weightInterval); 137 142 break; 138 143 } … … 144 149 //Elementary arithmetic rules 145 150 case OpCodes.Add: { 146 result = Evaluate(instructions, ref instructionCount , intervals);147 for (int i = 1; i < currentInstr.nArguments; i++) { 148 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);151 result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 152 for (int i = 1; i < currentInstr.nArguments; i++) { 153 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 149 154 result = Interval.Add(result, argumentInterval); 150 155 } … … 152 157 } 153 158 case OpCodes.Sub: { 154 result = Evaluate(instructions, ref instructionCount , intervals);159 result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 155 160 if (currentInstr.nArguments == 1) 156 161 result = Interval.Multiply(new Interval(-1, -1), result); 157 158 for (int i = 1; i < currentInstr.nArguments; i++) { 159 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);162 163 for (int i = 1; i < currentInstr.nArguments; i++) { 164 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 160 165 result = Interval.Subtract(result, argumentInterval); 161 166 } … … 163 168 } 164 169 case OpCodes.Mul: { 165 result = Evaluate(instructions, ref instructionCount , intervals);166 for (int i = 1; i < currentInstr.nArguments; i++) { 167 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);170 result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 171 for (int i = 1; i < currentInstr.nArguments; i++) { 172 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 168 173 result = Interval.Multiply(result, argumentInterval); 169 174 } … … 171 176 } 172 177 case OpCodes.Div: { 173 result = Evaluate(instructions, ref instructionCount , intervals);174 if (currentInstr.nArguments == 1)175 result = Interval.Divide(new Interval(1, 1),result);176 177 for (int i = 1; i < currentInstr.nArguments; i++) { 178 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);178 result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 179 if (currentInstr.nArguments == 1) 180 result = Interval.Divide(new Interval(1, 1), result); 181 182 for (int i = 1; i < currentInstr.nArguments; i++) { 183 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 179 184 result = Interval.Divide(result, argumentInterval); 180 185 } … … 183 188 //Trigonometric functions 184 189 case OpCodes.Sin: { 185 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);190 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 186 191 result = Interval.Sine(argumentInterval); 187 192 break; 188 193 } 189 194 case OpCodes.Cos: { 190 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);195 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 191 196 result = Interval.Cosine(argumentInterval); 192 197 break; 193 198 } 194 199 case OpCodes.Tan: { 195 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);200 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 196 201 result = Interval.Tangens(argumentInterval); 197 202 break; … … 199 204 //Exponential functions 200 205 case OpCodes.Log: { 201 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);206 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 202 207 result = Interval.Logarithm(argumentInterval); 203 208 break; 204 209 } 205 210 case OpCodes.Exp: { 206 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);211 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 207 212 result = Interval.Exponential(argumentInterval); 208 213 break; 209 214 } 210 215 case OpCodes.Power: { 211 result = Evaluate(instructions, ref instructionCount , intervals);212 for (int i = 1; i < currentInstr.nArguments; i++) { 213 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);216 result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 217 for (int i = 1; i < currentInstr.nArguments; i++) { 218 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 214 219 result = Interval.Power(result, argumentInterval); 215 220 } … … 217 222 } 218 223 case OpCodes.Square: { 219 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);224 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 220 225 result = Interval.Square(argumentInterval); 221 226 break; 222 227 } 223 228 case OpCodes.Root: { 224 result = Evaluate(instructions, ref instructionCount , intervals);225 for (int i = 1; i < currentInstr.nArguments; i++) { 226 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);229 result = Evaluate(instructions, ref instructionCounter, nodeIntervals); 230 for (int i = 1; i < currentInstr.nArguments; i++) { 231 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 227 232 result = Interval.Root(result, argumentInterval); 228 233 } … … 230 235 } 231 236 case OpCodes.SquareRoot: { 232 var argumentInterval = Evaluate(instructions, ref instructionCount , intervals);237 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 233 238 result = Interval.SquareRoot(argumentInterval); 234 239 break; … … 238 243 } 239 244 240 if ( intervals != null)241 intervals.Add(currentInstr.dynamicNode, result);245 if (nodeIntervals != null) 246 nodeIntervals.Add(currentInstr.dynamicNode, result); 242 247 243 248 return result; -
branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/DatasetUtil.cs
r16383 r16404 97 97 98 98 foreach (var variable in dataset.VariableNames) { 99 var min = double.MaxValue;100 var max = double.MinValue;101 99 IEnumerable<double> values = null; 102 100 … … 104 102 else values = dataset.GetDoubleValues(variable, rows); 105 103 106 foreach (var val in values) { 107 if (val < min) min = val; 108 if (val > max) max = val; 109 } 110 111 variableRanges.Add(variable, new Interval(min, max)); 104 var range = Interval.GetInterval(values); 105 variableRanges.Add(variable, range); 112 106 } 113 107 -
branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs
r16327 r16404 51 51 double.IsNaN(LowerBound) || double.IsNaN(UpperBound); 52 52 } 53 } 54 55 public static Interval GetInterval(IEnumerable<double> values) { 56 if (values == null) throw new ArgumentNullException("The given value set is not defined."); 57 if (!values.Any()) throw new ArgumentException($"No valid values are present."); 58 59 var min = double.MaxValue; 60 var max = double.MinValue; 61 62 foreach (var value in values) { 63 //If an value is NaN return an interval [NaN, NaN] 64 if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN); 65 66 if (value < min) min = value; 67 if (value > max) max = value; 68 } 69 70 return new Interval(min, max); 53 71 } 54 72
Note: See TracChangeset
for help on using the changeset viewer.