Changeset 17100
- Timestamp:
- 07/07/19 23:47:53 (5 years ago)
- Location:
- stable
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk merged: 16629,16631,16646,16740,16743,16757-16758,16769,16822
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis merged: 16631,16646,16757-16758
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 16629,16740,16758,16822
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs
r17097 r17100 69 69 #endregion 70 70 71 public Interval GetSymbolicEx ressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {71 public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) { 72 72 var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows); 73 return GetSymbolicEx ressionTreeInterval(tree, variableRanges);74 } 75 76 public Interval GetSymbolicEx ressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,77 out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) {73 return GetSymbolicExpressionTreeInterval(tree, variableRanges); 74 } 75 76 public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset, 77 out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) { 78 78 var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows); 79 return GetSymbolicEx ressionTreeIntervals(tree, variableRanges, out nodeIntervals);80 } 81 82 public Interval GetSymbolicEx ressionTreeInterval(ISymbolicExpressionTree tree,Dictionary<string, Interval> variableRanges) {79 return GetSymbolicExpressionTreeIntervals(tree, variableRanges, out nodeIntervals); 80 } 81 82 public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) { 83 83 lock (syncRoot) { 84 84 EvaluatedSolutions++; … … 88 88 var outputInterval = Evaluate(instructions, ref instructionCount); 89 89 90 return outputInterval; 91 } 92 93 94 public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, 95 Dictionary<string, Interval> variableRanges, out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) { 90 // because of numerical errors the bounds might be incorrect 91 if (outputInterval.LowerBound <= outputInterval.UpperBound) 92 return outputInterval; 93 else 94 return new Interval(outputInterval.UpperBound, outputInterval.LowerBound); 95 } 96 97 98 public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree, 99 IDictionary<string, Interval> variableRanges, out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) { 96 100 lock (syncRoot) { 97 101 EvaluatedSolutions++; … … 102 106 var outputInterval = Evaluate(instructions, ref instructionCount, intervals); 103 107 104 nodeIntervals = intervals; 105 106 return outputInterval; 107 } 108 109 110 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> variableRanges) { 108 // fix incorrect intervals if necessary (could occur because of numerical errors) 109 nodeIntervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>(); 110 foreach (var kvp in intervals) { 111 var interval = kvp.Value; 112 if (interval.IsInfiniteOrUndefined || interval.LowerBound <= interval.UpperBound) 113 nodeIntervals.Add(kvp.Key, interval); 114 else 115 nodeIntervals.Add(kvp.Key, new Interval(interval.UpperBound, interval.LowerBound)); 116 } 117 118 // because of numerical errors the bounds might be incorrect 119 if (outputInterval.IsInfiniteOrUndefined || outputInterval.LowerBound <= outputInterval.UpperBound) 120 return outputInterval; 121 else 122 return new Interval(outputInterval.UpperBound, outputInterval.LowerBound); 123 } 124 125 126 private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) { 111 127 if (variableRanges == null) 112 128 throw new ArgumentNullException("No variablew ranges are present!", nameof(variableRanges)); … … 125 141 } 126 142 127 private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) {143 private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) { 128 144 Instruction currentInstr = instructions[instructionCounter]; 129 145 //Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side … … 200 216 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 201 217 result = Interval.Tangens(argumentInterval); 218 break; 219 } 220 case OpCodes.Tanh: { 221 var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals); 222 result = Interval.HyperbolicTangent(argumentInterval); 202 223 break; 203 224 } -
stable/HeuristicLab.Problems.DataAnalysis/3.4
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis/3.4 merged: 16631,16646,16757-16758
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs
r17097 r17100 124 124 125 125 double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4)); 126 double max = Math.Max(Math.M in(v1, v2), Math.Max(v3, v4));126 double max = Math.Max(Math.Max(v1, v2), Math.Max(v3, v4)); 127 127 return new Interval(min, max); 128 128 } … … 167 167 } 168 168 public static Interval Cosine(Interval a) { 169 return Interval.Sine(Interval. Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));169 return Interval.Sine(Interval.Add(a, new Interval(Math.PI / 2, Math.PI / 2))); 170 170 } 171 171 public static Interval Tangens(Interval a) { 172 172 return Interval.Divide(Interval.Sine(a), Interval.Cosine(a)); 173 } 174 public static Interval HyperbolicTangent(Interval a) { 175 return new Interval(Math.Tanh(a.LowerBound), Math.Tanh(a.UpperBound)); 173 176 } 174 177 … … 201 204 202 205 public static Interval Square(Interval a) { 203 return Power(a, new Interval(2, 2)); 204 } 205 206 public static Interval Cubic(Interval a) { 207 return Power(a, new Interval(3, 3)); 206 if (a.UpperBound <= 0) return new Interval(a.UpperBound * a.UpperBound, a.LowerBound * a.LowerBound); // interval is negative 207 else if (a.LowerBound >= 0) return new Interval(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound); // interval is positive 208 else return new Interval(0, Math.Max(a.LowerBound*a.LowerBound, a.UpperBound*a.UpperBound)); // interval goes over zero 209 } 210 211 public static Interval Cube(Interval a) { 212 return new Interval(Math.Pow(a.LowerBound, 3), Math.Pow(a.UpperBound, 3)); 208 213 } 209 214 … … 216 221 217 222 public static Interval SquareRoot(Interval a) { 218 return Root(a, new Interval(2, 2)); 223 if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN); 224 return new Interval(Math.Sqrt(a.LowerBound), Math.Sqrt(a.UpperBound)); 219 225 } 220 226 221 227 public static Interval CubicRoot(Interval a) { 222 return Root(a, new Interval(3, 3)); 228 if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN); 229 return new Interval(Math.Pow(a.LowerBound, 1.0/3), Math.Pow(a.UpperBound, 1.0/3)); 223 230 } 224 231 #endregion -
stable/HeuristicLab.Tests
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Tests merged: 16631,16646,16743,16757-16758,16769
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/IntervalTest.cs
r16436 r17100 1 1 using System; 2 using HeuristicLab.Problems.DataAnalysis;3 2 using Microsoft.VisualStudio.TestTools.UnitTesting; 4 3 … … 52 51 //([-1, 1] * [0, 3]) * [-2, 2] = [-6, 6] 53 52 Assert.AreEqual<Interval>(Interval.Multiply(Interval.Multiply(a, c), b), new Interval(-6, 6)); 53 54 // [-2, 0] * [-2, 0] = [0, 4] 55 Assert.AreEqual<Interval>(new Interval(0, 4), Interval.Multiply(new Interval(-2, 0), new Interval(-2, 0))); 54 56 } 55 57 … … 124 126 Assert.AreEqual<Interval>(new Interval(1, Math.Exp(1)), Interval.Exponential(new Interval(0, 1))); 125 127 } 128 129 130 [TestMethod] 131 [TestCategory("Problems.DataAnalysis")] 132 [TestProperty("Time", "short")] 133 public void TestIntervalSqrOperator() { 134 Assert.AreEqual<Interval>(new Interval(1, 4), Interval.Square(new Interval(1, 2))); 135 Assert.AreEqual<Interval>(new Interval(1, 4), Interval.Square(new Interval(-2, -1))); 136 Assert.AreEqual<Interval>(new Interval(0, 4), Interval.Square(new Interval(-2, 2))); 137 } 138 139 [TestMethod] 140 [TestCategory("Problems.DataAnalysis")] 141 [TestProperty("Time", "short")] 142 public void TestIntervalSqrtOperator() { 143 Assert.AreEqual<Interval>(new Interval(1, 2), Interval.SquareRoot(new Interval(1, 4))); 144 Assert.AreEqual<Interval>(new Interval(double.NaN, double.NaN), Interval.SquareRoot(new Interval(-4, -1))); 145 } 146 147 [TestMethod] 148 [TestCategory("Problems.DataAnalysis")] 149 [TestProperty("Time", "short")] 150 public void TestIntervalCubeOperator() { 151 Assert.AreEqual<Interval>(new Interval(1, 8), Interval.Cube(new Interval(1, 2))); 152 Assert.AreEqual<Interval>(new Interval(-8, -1), Interval.Cube(new Interval(-2, -1))); 153 Assert.AreEqual<Interval>(new Interval(-8, 8), Interval.Cube(new Interval(-2, 2))); 154 } 155 156 [TestMethod] 157 [TestCategory("Problems.DataAnalysis")] 158 [TestProperty("Time", "short")] 159 public void TestIntervalCbrtOperator() { 160 Assert.AreEqual<Interval>(new Interval(1, 2), Interval.CubicRoot(new Interval(1, 8))); 161 Assert.AreEqual<Interval>(new Interval(double.NaN, double.NaN), Interval.CubicRoot(new Interval(-8, -1))); 162 } 126 163 } 127 164 } -
stable/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/IntervalInterpreterTest.cs
r16438 r17100 34 34 } 35 35 36 private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null ) {36 private void EvaluateTest(string expression, Interval expectedResult, Dictionary<string, Interval> variableRanges = null, double lowerDelta =0, double upperDelta = 0) { 37 37 var parser = new InfixExpressionParser(); 38 38 var tree = parser.Parse(expression); … … 40 40 Interval result; 41 41 if (variableRanges == null) 42 result = interpreter.GetSymbolicEx ressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices);42 result = interpreter.GetSymbolicExpressionTreeInterval(tree, problemData.Dataset, problemData.AllIndices); 43 43 else 44 result = interpreter.GetSymbolicEx ressionTreeInterval(tree, variableRanges);44 result = interpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges); 45 45 46 Assert.AreEqual(expectedResult, result); 46 Assert.AreEqual(expectedResult.LowerBound, result.LowerBound, lowerDelta); 47 Assert.AreEqual(expectedResult.UpperBound, result.UpperBound, upperDelta); 47 48 } 48 49 … … 79 80 EvaluateTest("sin(x1+x2)", new Interval(-1, 1), variableRanges); 80 81 EvaluateTest("sin(1+2)", new Interval(Math.Sin(3), Math.Sin(3))); 82 83 var localVarRanges = new Dictionary<string, Interval>(); 84 localVarRanges.Add("x1", new Interval(-1, 1)); 85 localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0)); 86 localVarRanges.Add("x3", new Interval(0, Math.PI / 2)); 87 localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI)); 88 localVarRanges.Add("x5", new Interval(Math.PI/4, Math.PI*3.0/4)); 89 90 EvaluateTest("sin(x1)", new Interval(Math.Sin(-1), Math.Sin(1)), localVarRanges, 1E-8, 1E-8); 91 EvaluateTest("sin(x2)", new Interval(-1, 0), localVarRanges, 1E-8, 1E-8); 92 EvaluateTest("sin(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8); 93 EvaluateTest("sin(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8); 94 EvaluateTest("sin(x5)", new Interval(Math.Sin(Math.PI/4), 1), localVarRanges, 1E-8, 1E-8); 81 95 } 82 96 … … 87 101 EvaluateTest("cos(x1+x2)", new Interval(-1, 1)); 88 102 EvaluateTest("cos(x1+x2)", new Interval(-1, 1), variableRanges); 89 EvaluateTest("cos(1+2)", new Interval(Math.Sin(3 - Math.PI / 2), Math.Sin(3 - Math.PI / 2))); 103 EvaluateTest("cos(1+2)", new Interval(Math.Sin(3 + Math.PI / 2), Math.Sin(3 + Math.PI / 2))); 104 105 var localVarRanges = new Dictionary<string, Interval>(); 106 localVarRanges.Add("x1", new Interval(-1, 1)); 107 localVarRanges.Add("x2", new Interval(-(Math.PI / 2), 0)); 108 localVarRanges.Add("x3", new Interval(0, Math.PI / 2)); 109 localVarRanges.Add("x4", new Interval(-Math.PI, Math.PI)); 110 localVarRanges.Add("x5", new Interval(Math.PI / 4, Math.PI * 3.0 / 4)); 111 112 EvaluateTest("cos(x1)", new Interval(Math.Cos(-1), 1), localVarRanges, 1E-8, 1E-8); 113 EvaluateTest("cos(x2)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8); 114 EvaluateTest("cos(x3)", new Interval(0, 1), localVarRanges, 1E-8, 1E-8); 115 EvaluateTest("cos(x4)", new Interval(-1, 1), localVarRanges, 1E-8, 1E-8); 116 EvaluateTest("cos(x5)", new Interval(Math.Cos(Math.PI *3.0/ 4), Math.Cos(Math.PI/ 4)), localVarRanges, 1E-8, 1E-8); 117 90 118 } 119 120 [TestMethod] 121 [TestCategory("Problems.DataAnalysis.Symbolic")] 122 [TestProperty("Time", "short")] 123 public void TestIntervalInterpreterTan() { 124 // critical values: 125 // lim tan(x) = -inf for x => -pi/2 126 // lim tan(x) = +inf for x => pi/2 127 var variableRanges = new Dictionary<string, Interval>(); 128 variableRanges.Add("x1", new Interval(-1, 1)); 129 variableRanges.Add("x2", new Interval(-(Math.PI / 2), 0)); 130 variableRanges.Add("x3", new Interval(0, Math.PI / 2)); 131 variableRanges.Add("x4", new Interval(-Math.PI, Math.PI)); 132 133 EvaluateTest("tan(x1)", new Interval(Math.Tan(-1), Math.Tan(1)), variableRanges, 1E-8, 1E-8); 134 EvaluateTest("tan(x2)", new Interval(double.NegativeInfinity, 0), variableRanges, 0, 1E-8); 135 EvaluateTest("tan(x3)", new Interval(0, 8.16588936419192E+15), variableRanges, 0, 1E6); // actually upper bound should be infinity. 136 EvaluateTest("tan(x4)", new Interval(double.NegativeInfinity, double.PositiveInfinity), variableRanges); 137 } 138 139 [TestMethod] 140 [TestCategory("Problems.DataAnalysis.Symbolic")] 141 [TestProperty("Time", "short")] 142 public void TestIntervalInterpreterTanh() { 143 // critical values: 144 // lim tanh(x) = -1 for x => -inf 145 // lim tanh(x) = 1 for x => inf 146 var variableRanges = new Dictionary<string, Interval>(); 147 variableRanges.Add("x1", new Interval(-1, 1)); 148 variableRanges.Add("x2", new Interval(double.NegativeInfinity, 0)); 149 variableRanges.Add("x3", new Interval(0, double.PositiveInfinity)); 150 151 EvaluateTest("tanh(x1)", new Interval(Math.Tanh(-1), Math.Tanh(1)), variableRanges); 152 EvaluateTest("tanh(x2)", new Interval(-1, 0), variableRanges); 153 EvaluateTest("tanh(x3)", new Interval(0, 1), variableRanges); 154 } 155 91 156 92 157 [TestMethod]
Note: See TracChangeset
for help on using the changeset viewer.