Changeset 16383
- Timestamp:
- 12/14/18 14:17:31 (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/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r16330 r16383 348 348 <ItemGroup> 349 349 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj"> 350 <Project>{ DF87C13E-A889-46FF-8153-66DCAA8C5674}</Project>350 <Project>{df87c13e-a889-46ff-8153-66dcaa8c5674}</Project> 351 351 <Name>HeuristicLab.Problems.DataAnalysis-3.4</Name> 352 <Private>False</Private>353 352 </ProjectReference> 354 353 </ItemGroup> -
branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs
r16380 r16383 70 70 71 71 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) { 72 lock (syncRoot) { 73 EvaluatedSolutions++; 74 } 75 int instructionCount = 0; 76 var intervalBoundaries = DatasetUtil.GetVariableBoundaries(dataset, rows); 77 var instructions = PrepareInterpreterState(tree, intervalBoundaries); 78 var x = Evaluate(instructions, ref instructionCount); 79 80 return x; 72 var intervalBoundaries = DatasetUtil.GetVariableRanges(dataset, rows); 73 return GetSymbolicExressionTreeIntervals(tree, intervalBoundaries); 81 74 } 82 75 83 76 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, 84 77 out Dictionary<ISymbolicExpressionTreeNode, Interval> intervals, IEnumerable<int> rows = null) { 85 lock (syncRoot) { 86 EvaluatedSolutions++; 87 } 88 int instructionCount = 0; 89 var intervalBoundaries = DatasetUtil.GetVariableBoundaries(dataset, rows); 90 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 91 var instructions = PrepareInterpreterState(tree, intervalBoundaries); 92 var x = Evaluate(instructions, ref instructionCount, intervals); 93 94 return x; 78 var intervalBoundaries = DatasetUtil.GetVariableRanges(dataset, rows); 79 return GetSymbolicExressionTreeIntervals(tree, intervalBoundaries, out intervals); 95 80 } 96 81 … … 101 86 int instructionCount = 0; 102 87 var instructions = PrepareInterpreterState(tree, customIntervals); 103 var x= Evaluate(instructions, ref instructionCount);104 105 return x;88 var outputInterval = Evaluate(instructions, ref instructionCount); 89 90 return outputInterval; 106 91 } 107 92 … … 115 100 intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 116 101 var instructions = PrepareInterpreterState(tree, customIntervals); 117 var x= Evaluate(instructions, ref instructionCount, intervals);118 119 return x;102 var outputInterval = Evaluate(instructions, ref instructionCount, intervals); 103 104 return outputInterval; 120 105 } 121 106 … … 138 123 } 139 124 140 private Interval Evaluate(Instruction[] instructions, ref int instructionCount, Dictionary<ISymbolicExpressionTreeNode, 141 Interval> intervals = null) { 125 private Interval Evaluate(Instruction[] instructions, ref int instructionCount, Dictionary<ISymbolicExpressionTreeNode, Interval> intervals = null) { 142 126 Instruction currentInstr = instructions[instructionCount++]; 143 127 Interval result = null; 144 128 145 129 switch (currentInstr.opCode) { 146 //Elementary arithmetic rules147 case OpCodes.Add: {148 result = Evaluate(instructions, ref instructionCount, intervals);149 for (int i = 1; i < currentInstr.nArguments; i++) {150 result = Interval.Add(result, Evaluate(instructions, ref instructionCount, intervals));151 }152 break;153 }154 case OpCodes.Sub: {155 result = Evaluate(instructions, ref instructionCount, intervals);156 for (int i = 1; i < currentInstr.nArguments; i++) {157 result = Interval.Subtract(result, Evaluate(instructions, ref instructionCount, intervals));158 }159 break;160 }161 case OpCodes.Mul: {162 result = Evaluate(instructions, ref instructionCount, intervals);163 for (int i = 1; i < currentInstr.nArguments; i++) {164 result = Interval.Multiply(result, Evaluate(instructions, ref instructionCount, intervals));165 }166 break;167 }168 case OpCodes.Div: {169 result = Evaluate(instructions, ref instructionCount, intervals);170 for (int i = 1; i < currentInstr.nArguments; i++) {171 result = Interval.Divide(result, Evaluate(instructions, ref instructionCount, intervals));172 }173 break;174 }175 //Trigonometric functions176 case OpCodes.Sin: {177 result = Interval.Sine(Evaluate(instructions, ref instructionCount, intervals));178 break;179 }180 case OpCodes.Cos: {181 result = Interval.Cosine(Evaluate(instructions, ref instructionCount, intervals));182 break;183 }184 case OpCodes.Tan: {185 result = Interval.Tangens(Evaluate(instructions, ref instructionCount, intervals));186 break;187 }188 //Exponential functions189 case OpCodes.Log: {190 result = Interval.Logarithm(Evaluate(instructions, ref instructionCount, intervals));191 break;192 }193 case OpCodes.Exp: {194 result = Interval.Exponential(Evaluate(instructions, ref instructionCount, intervals));195 break;196 }197 case OpCodes.Power: {198 result = Evaluate(instructions, ref instructionCount, intervals);199 for (int i = 1; i < currentInstr.nArguments; i++) {200 result = Interval.Power(result, Evaluate(instructions, ref instructionCount, intervals));201 }202 break;203 }204 case OpCodes.Square: {205 result = Interval.Square(Evaluate(instructions, ref instructionCount, intervals));206 break;207 }208 case OpCodes.Root: {209 result = Evaluate(instructions, ref instructionCount, intervals);210 for (int i = 1; i < currentInstr.nArguments; i++) {211 result = Interval.Root(result, Evaluate(instructions, ref instructionCount, intervals));212 }213 break;214 }215 case OpCodes.SquareRoot: {216 result = Interval.SquareRoot(Evaluate(instructions, ref instructionCount, intervals));217 break;218 }219 130 //Variables, Constants, ... 220 131 case OpCodes.Variable: { … … 222 133 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 223 134 var variableWeight = variableTreeNode.Weight; 224 135 225 136 return Interval.Multiply((Interval)currentInstr.data, new Interval(variableWeight, variableWeight)); 226 137 } … … 231 142 return inter; 232 143 } 144 //Elementary arithmetic rules 145 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); 149 result = Interval.Add(result, argumentInterval); 150 } 151 break; 152 } 153 case OpCodes.Sub: { 154 result = Evaluate(instructions, ref instructionCount, intervals); 155 if (currentInstr.nArguments == 1) 156 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); 160 result = Interval.Subtract(result, argumentInterval); 161 } 162 break; 163 } 164 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); 168 result = Interval.Multiply(result, argumentInterval); 169 } 170 break; 171 } 172 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); 179 result = Interval.Divide(result, argumentInterval); 180 } 181 break; 182 } 183 //Trigonometric functions 184 case OpCodes.Sin: { 185 var argumentInterval = Evaluate(instructions, ref instructionCount, intervals); 186 result = Interval.Sine(argumentInterval); 187 break; 188 } 189 case OpCodes.Cos: { 190 var argumentInterval = Evaluate(instructions, ref instructionCount, intervals); 191 result = Interval.Cosine(argumentInterval); 192 break; 193 } 194 case OpCodes.Tan: { 195 var argumentInterval = Evaluate(instructions, ref instructionCount, intervals); 196 result = Interval.Tangens(argumentInterval); 197 break; 198 } 199 //Exponential functions 200 case OpCodes.Log: { 201 var argumentInterval = Evaluate(instructions, ref instructionCount, intervals); 202 result = Interval.Logarithm(argumentInterval); 203 break; 204 } 205 case OpCodes.Exp: { 206 var argumentInterval = Evaluate(instructions, ref instructionCount, intervals); 207 result = Interval.Exponential(argumentInterval); 208 break; 209 } 210 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); 214 result = Interval.Power(result, argumentInterval); 215 } 216 break; 217 } 218 case OpCodes.Square: { 219 var argumentInterval = Evaluate(instructions, ref instructionCount, intervals); 220 result = Interval.Square(argumentInterval); 221 break; 222 } 223 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); 227 result = Interval.Root(result, argumentInterval); 228 } 229 break; 230 } 231 case OpCodes.SquareRoot: { 232 var argumentInterval = Evaluate(instructions, ref instructionCount, intervals); 233 result = Interval.SquareRoot(argumentInterval); 234 break; 235 } 233 236 default: 234 throw new NotSupportedException( "Tree contains an unknown symbol.");237 throw new NotSupportedException($"The tree contains the unknown symbol {currentInstr.dynamicNode.Symbol}"); 235 238 } 236 239 237 240 if (intervals != null) 238 241 intervals.Add(currentInstr.dynamicNode, result); 242 239 243 return result; 240 244 } -
branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/DatasetUtil.cs
r16364 r16383 93 93 } 94 94 95 public static Dictionary<string, Interval> GetVariable Boundaries(IDataset dataset, IEnumerable<int> rows = null) {96 Dictionary<string, Interval> variable Boundaries = new Dictionary<string, Interval>();95 public static Dictionary<string, Interval> GetVariableRanges(IDataset dataset, IEnumerable<int> rows = null) { 96 Dictionary<string, Interval> variableRanges = new Dictionary<string, Interval>(); 97 97 98 98 foreach (var variable in dataset.VariableNames) { 99 99 var min = double.MaxValue; 100 100 var max = double.MinValue; 101 IEnumerable<double> values = null; 101 102 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 } 103 if (rows == null) values = dataset.GetDoubleValues(variable); 104 else values = dataset.GetDoubleValues(variable, rows); 105 106 foreach (var val in values) { 107 if (val < min) min = val; 108 if (val > max) max = val; 113 109 } 114 variableBoundaries.Add(variable, new Interval(min, max)); 110 111 variableRanges.Add(variable, new Interval(min, max)); 115 112 } 116 113 117 return variable Boundaries;114 return variableRanges; 118 115 } 119 116
Note: See TracChangeset
for help on using the changeset viewer.