Changeset 16327 for branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs
 Timestamp:
 11/23/18 17:06:50 (2 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs
r16326 r16327 27 27 namespace HeuristicLab.Problems.DataAnalysis { 28 28 public class Interval : IEquatable<Interval> { 29 private double lowerBound; 30 public double LowerBound { 31 get { return lowerBound; } 32 private set { lowerBound = value; } 33 } 34 private double upperBound; 35 public double UpperBound { 36 get { return upperBound; } 37 private set { lowerBound = value; } 38 } 29 public double LowerBound { get; private set; } 30 public double UpperBound { get; private set; } 39 31 40 32 public Interval(double lowerBound, double upperBound) { … … 42 34 throw new ArgumentException("LowerBound must be smaller than UpperBound."); 43 35 44 this. lowerBound = lowerBound;45 this. upperBound = upperBound;36 this.LowerBound = lowerBound; 37 this.UpperBound = upperBound; 46 38 } 47 39 48 40 public bool Contains(double value) { 49 return lowerBound <= value && value <= upperBound;41 return LowerBound <= value && value <= UpperBound; 50 42 } 51 43 52 44 public override string ToString() { 53 return "Interval: [" + lowerBound + ", " + upperBound + "]";45 return "Interval: [" + LowerBound + ", " + UpperBound + "]"; 54 46 } 55 47 … … 66 58 return false; 67 59 68 return this. upperBound.IsAlmost(other.upperBound) && this.lowerBound.IsAlmost(other.lowerBound);60 return this.UpperBound.IsAlmost(other.UpperBound) && this.LowerBound.IsAlmost(other.LowerBound); 69 61 } 70 62 … … 74 66 75 67 public override int GetHashCode() { 76 return lowerBound.GetHashCode() ^ upperBound.GetHashCode();68 return LowerBound.GetHashCode() ^ UpperBound.GetHashCode(); 77 69 } 78 70 … … 90 82 // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2] 91 83 public static Interval Add(Interval a, Interval b) { 92 return new Interval(a. lowerBound + b.lowerBound, a.upperBound + b.upperBound);84 return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound); 93 85 } 94 86 95 87 // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1] 96 88 public static Interval Subtract(Interval a, Interval b) { 97 return new Interval(a. lowerBound  b.upperBound, a.upperBound  b.lowerBound);89 return new Interval(a.LowerBound  b.UpperBound, a.UpperBound  b.LowerBound); 98 90 } 99 91 100 92 // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)] 101 93 public static Interval Multiply(Interval a, Interval b) { 102 double v1 = a. lowerBound * b.lowerBound;103 double v2 = a. lowerBound * b.upperBound;104 double v3 = a. upperBound * b.lowerBound;105 double v4 = a. upperBound * b.upperBound;94 double v1 = a.LowerBound * b.LowerBound; 95 double v2 = a.LowerBound * b.UpperBound; 96 double v3 = a.UpperBound * b.LowerBound; 97 double v4 = a.UpperBound * b.UpperBound; 106 98 107 99 double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4)); … … 110 102 } 111 103 112 //mkommend: Division tby intervals containing 0 is implemented as defined in104 //mkommend: Division by intervals containing 0 is implemented as defined in 113 105 //http://en.wikipedia.org/wiki/Interval_arithmetic 114 106 public static Interval Divide(Interval a, Interval b) { 115 107 if (b.Contains(0.0)) { 116 if (b. lowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.upperBound, double.PositiveInfinity));117 else if (b. upperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.lowerBound));108 if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity)); 109 else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound)); 118 110 else return new Interval(double.NegativeInfinity, double.PositiveInfinity); 119 111 } 120 return Interval.Multiply(a, new Interval(1.0 / b. upperBound, 1.0 / b.lowerBound));112 return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound)); 121 113 } 122 114 123 115 public static Interval Sine(Interval a) { 124 if (Math.Abs(a. upperBound  a.lowerBound) >= Math.PI * 2) return new Interval(1, 1);116 if (Math.Abs(a.UpperBound  a.LowerBound) >= Math.PI * 2) return new Interval(1, 1); 125 117 126 118 //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...) … … 128 120 Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf)); 129 121 //move to positive scale 130 if (scaled. lowerBound < 0) {131 int periodsToMove = Math.Abs((int)scaled. lowerBound / 4) + 1;122 if (scaled.LowerBound < 0) { 123 int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1; 132 124 scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4)); 133 125 } 134 126 135 double scaledLowerBound = scaled. lowerBound % 4.0;136 double scaledUpperBound = scaled. upperBound % 4.0;127 double scaledLowerBound = scaled.LowerBound % 4.0; 128 double scaledUpperBound = scaled.UpperBound % 4.0; 137 129 if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0; 138 130 List<double> sinValues = new List<double>(); … … 156 148 157 149 public static Interval Logarithm(Interval a) { 158 return new Interval(Math.Log(a. lowerBound), Math.Log(a.upperBound));150 return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound)); 159 151 } 160 152 public static Interval Exponential(Interval a) { 161 return new Interval(Math.Exp(a. lowerBound), Math.Exp(a.upperBound));153 return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound)); 162 154 } 163 155 164 156 public static Interval Power(Interval a, Interval b) { 165 if (a.Contains(0.0) && b. lowerBound < 0) return new Interval(double.NaN, double.NaN);166 167 int bLower = (int)Math.Round(b. lowerBound);168 int bUpper = (int)Math.Round(b. upperBound);157 if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN); 158 159 int bLower = (int)Math.Round(b.LowerBound); 160 int bUpper = (int)Math.Round(b.UpperBound); 169 161 170 162 List<double> powerValues = new List<double>(); 171 powerValues.Add(Math.Pow(a. upperBound, bUpper));172 powerValues.Add(Math.Pow(a. upperBound, bUpper  1));173 powerValues.Add(Math.Pow(a. upperBound, bLower));174 powerValues.Add(Math.Pow(a. upperBound, bLower + 1));175 176 powerValues.Add(Math.Pow(a. lowerBound, bUpper));177 powerValues.Add(Math.Pow(a. lowerBound, bUpper  1));178 powerValues.Add(Math.Pow(a. lowerBound, bLower));179 powerValues.Add(Math.Pow(a. lowerBound, bLower + 1));163 powerValues.Add(Math.Pow(a.UpperBound, bUpper)); 164 powerValues.Add(Math.Pow(a.UpperBound, bUpper  1)); 165 powerValues.Add(Math.Pow(a.UpperBound, bLower)); 166 powerValues.Add(Math.Pow(a.UpperBound, bLower + 1)); 167 168 powerValues.Add(Math.Pow(a.LowerBound, bUpper)); 169 powerValues.Add(Math.Pow(a.LowerBound, bUpper  1)); 170 powerValues.Add(Math.Pow(a.LowerBound, bLower)); 171 powerValues.Add(Math.Pow(a.LowerBound, bLower + 1)); 180 172 181 173 return new Interval(powerValues.Min(), powerValues.Max()); … … 191 183 192 184 public static Interval Root(Interval a, Interval b) { 193 int lower = (int)Math.Round(b. lowerBound);194 int higher = (int)Math.Round(b. upperBound);195 196 return new Interval(Math.Pow(a. lowerBound, 1.0 / higher), Math.Pow(a.upperBound, 1.0 / lower));185 int lower = (int)Math.Round(b.LowerBound); 186 int higher = (int)Math.Round(b.UpperBound); 187 188 return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower)); 197 189 } 198 190
Note: See TracChangeset
for help on using the changeset viewer.