[16323] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17210] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[16323] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[16303] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[17505] | 25 | using HEAL.Attic;
|
---|
[16303] | 26 | using HeuristicLab.Common;
|
---|
| 27 |
|
---|
[16326] | 28 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16640] | 29 | [StorableType("849e42d3-8934-419d-9aff-64ad81c06b67")]
|
---|
[16548] | 30 | public class Interval : IEquatable<Interval> {
|
---|
[16640] | 31 | [Storable]
|
---|
[16327] | 32 | public double LowerBound { get; private set; }
|
---|
[16640] | 33 | [Storable]
|
---|
[16327] | 34 | public double UpperBound { get; private set; }
|
---|
[16303] | 35 |
|
---|
[17902] | 36 | public double Width => UpperBound - LowerBound;
|
---|
| 37 |
|
---|
[16640] | 38 | [StorableConstructor]
|
---|
| 39 | protected Interval(StorableConstructorFlag _) { }
|
---|
[16548] | 40 |
|
---|
[17368] | 41 | /// <summary>
|
---|
| 42 | /// Creates an interval with given bounds, where lower bound must be smaller than
|
---|
| 43 | /// the upper bound. Floating point precision errors trough calculations are fixed by,
|
---|
| 44 | /// checking if the intervals are almost the same (E-12). If this is the case, the bounds
|
---|
| 45 | /// will be set to the bound closer to zero.
|
---|
| 46 | /// </summary>
|
---|
| 47 | /// <param name="lowerBound">Lower bound of the interval</param>
|
---|
| 48 | /// <param name="upperBound">Upper bound of the interval</param>
|
---|
[16303] | 49 | public Interval(double lowerBound, double upperBound) {
|
---|
[17368] | 50 | if (lowerBound.IsAlmost(upperBound)) {
|
---|
| 51 | //If the bounds go over zero
|
---|
| 52 | if (lowerBound <= 0 && upperBound >= 0) {
|
---|
| 53 | lowerBound = 0.0;
|
---|
| 54 | upperBound = 0.0;
|
---|
| 55 | //Interval is negative
|
---|
| 56 | } else if (upperBound < 0) {
|
---|
| 57 | lowerBound = upperBound;
|
---|
| 58 | //Interval is positive
|
---|
| 59 | } else {
|
---|
| 60 | upperBound = lowerBound;
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[17542] | 64 | if (lowerBound > upperBound)
|
---|
| 65 | throw new ArgumentException("lowerBound must be smaller than or equal to upperBound.");
|
---|
| 66 |
|
---|
[16327] | 67 | this.LowerBound = lowerBound;
|
---|
| 68 | this.UpperBound = upperBound;
|
---|
[16303] | 69 | }
|
---|
| 70 |
|
---|
[17902] | 71 | private Interval(double v) : this(v, v) { }
|
---|
| 72 |
|
---|
[16303] | 73 | public bool Contains(double value) {
|
---|
[16327] | 74 | return LowerBound <= value && value <= UpperBound;
|
---|
[16303] | 75 | }
|
---|
| 76 |
|
---|
[17370] | 77 | public bool Contains(Interval other) {
|
---|
| 78 | if (double.IsNegativeInfinity(LowerBound) && double.IsPositiveInfinity(UpperBound)) return true;
|
---|
| 79 | if (other.LowerBound >= LowerBound && other.UpperBound <= UpperBound) return true;
|
---|
[17963] | 80 |
|
---|
[17370] | 81 | return false;
|
---|
[16592] | 82 | }
|
---|
| 83 |
|
---|
[16303] | 84 | public override string ToString() {
|
---|
[16327] | 85 | return "Interval: [" + LowerBound + ", " + UpperBound + "]";
|
---|
[16303] | 86 | }
|
---|
| 87 |
|
---|
| 88 | public bool IsInfiniteOrUndefined {
|
---|
| 89 | get {
|
---|
| 90 | return double.IsInfinity(LowerBound) || double.IsInfinity(UpperBound) ||
|
---|
| 91 | double.IsNaN(LowerBound) || double.IsNaN(UpperBound);
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[17368] | 95 | /// <summary>
|
---|
| 96 | /// True if the interval is positive without zero
|
---|
| 97 | /// </summary>
|
---|
| 98 | public bool IsPositive {
|
---|
[17963] | 99 | get => LowerBound > 0.0;
|
---|
[17368] | 100 | }
|
---|
| 101 |
|
---|
| 102 | /// <summary>
|
---|
| 103 | /// True if the interval is negative without zero
|
---|
| 104 | /// </summary>
|
---|
| 105 | public bool IsNegative {
|
---|
| 106 | get => UpperBound < 0.0;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[16404] | 109 | public static Interval GetInterval(IEnumerable<double> values) {
|
---|
[16407] | 110 | if (values == null) throw new ArgumentNullException("values");
|
---|
| 111 | if (!values.Any()) throw new ArgumentException($"No values are present.");
|
---|
[16404] | 112 |
|
---|
| 113 | var min = double.MaxValue;
|
---|
| 114 | var max = double.MinValue;
|
---|
| 115 |
|
---|
| 116 | foreach (var value in values) {
|
---|
| 117 | //If an value is NaN return an interval [NaN, NaN]
|
---|
| 118 | if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN);
|
---|
| 119 |
|
---|
| 120 | if (value < min) min = value;
|
---|
| 121 | if (value > max) max = value;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return new Interval(min, max);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[16303] | 127 | #region Equals, GetHashCode, == , !=
|
---|
| 128 | public bool Equals(Interval other) {
|
---|
| 129 | if (other == null)
|
---|
| 130 | return false;
|
---|
| 131 |
|
---|
[17963] | 132 | return (UpperBound == other.UpperBound || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
|
---|
| 133 | && (LowerBound == other.LowerBound || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
|
---|
[16303] | 134 | }
|
---|
| 135 |
|
---|
| 136 | public override bool Equals(object obj) {
|
---|
| 137 | return Equals(obj as Interval);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | public override int GetHashCode() {
|
---|
[16327] | 141 | return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
|
---|
[16303] | 142 | }
|
---|
| 143 |
|
---|
| 144 | public static bool operator ==(Interval interval1, Interval interval2) {
|
---|
| 145 | if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
|
---|
| 146 | return interval1.Equals(interval2);
|
---|
| 147 | }
|
---|
| 148 | public static bool operator !=(Interval interval1, Interval interval2) {
|
---|
| 149 | return !(interval1 == interval2);
|
---|
| 150 | }
|
---|
| 151 | #endregion
|
---|
| 152 |
|
---|
| 153 | #region operations
|
---|
| 154 |
|
---|
| 155 | // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
|
---|
| 156 | public static Interval Add(Interval a, Interval b) {
|
---|
[16327] | 157 | return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
|
---|
[16303] | 158 | }
|
---|
| 159 |
|
---|
| 160 | // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
|
---|
| 161 | public static Interval Subtract(Interval a, Interval b) {
|
---|
[16327] | 162 | return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
|
---|
[16303] | 163 | }
|
---|
| 164 |
|
---|
| 165 | // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
|
---|
| 166 | public static Interval Multiply(Interval a, Interval b) {
|
---|
[16327] | 167 | double v1 = a.LowerBound * b.LowerBound;
|
---|
| 168 | double v2 = a.LowerBound * b.UpperBound;
|
---|
| 169 | double v3 = a.UpperBound * b.LowerBound;
|
---|
| 170 | double v4 = a.UpperBound * b.UpperBound;
|
---|
[16303] | 171 |
|
---|
| 172 | double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
|
---|
[16647] | 173 | double max = Math.Max(Math.Max(v1, v2), Math.Max(v3, v4));
|
---|
[16303] | 174 | return new Interval(min, max);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[17368] | 177 | //Division by intervals containing 0 is implemented as defined in
|
---|
[16303] | 178 | //http://en.wikipedia.org/wiki/Interval_arithmetic
|
---|
| 179 | public static Interval Divide(Interval a, Interval b) {
|
---|
| 180 | if (b.Contains(0.0)) {
|
---|
[16327] | 181 | if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
|
---|
| 182 | else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
|
---|
[16303] | 183 | else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
|
---|
| 184 | }
|
---|
[16327] | 185 | return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
|
---|
[16303] | 186 | }
|
---|
| 187 |
|
---|
| 188 | public static Interval Sine(Interval a) {
|
---|
[16327] | 189 | if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
|
---|
[16303] | 190 |
|
---|
| 191 | //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
|
---|
| 192 | double Pihalf = Math.PI / 2;
|
---|
| 193 | Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
|
---|
| 194 | //move to positive scale
|
---|
[16327] | 195 | if (scaled.LowerBound < 0) {
|
---|
| 196 | int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
|
---|
[16303] | 197 | scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[16327] | 200 | double scaledLowerBound = scaled.LowerBound % 4.0;
|
---|
| 201 | double scaledUpperBound = scaled.UpperBound % 4.0;
|
---|
[16303] | 202 | if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
|
---|
| 203 | List<double> sinValues = new List<double>();
|
---|
| 204 | sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
|
---|
| 205 | sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
|
---|
| 206 |
|
---|
| 207 | int startValue = (int)Math.Ceiling(scaledLowerBound);
|
---|
| 208 | while (startValue < scaledUpperBound) {
|
---|
| 209 | sinValues.Add(Math.Sin(startValue * Pihalf));
|
---|
| 210 | startValue += 1;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | return new Interval(sinValues.Min(), sinValues.Max());
|
---|
| 214 | }
|
---|
| 215 | public static Interval Cosine(Interval a) {
|
---|
[16775] | 216 | return Interval.Sine(Interval.Add(a, new Interval(Math.PI / 2, Math.PI / 2)));
|
---|
[16303] | 217 | }
|
---|
| 218 | public static Interval Tangens(Interval a) {
|
---|
| 219 | return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
|
---|
[17368] | 220 | }
|
---|
[16775] | 221 | public static Interval HyperbolicTangent(Interval a) {
|
---|
| 222 | return new Interval(Math.Tanh(a.LowerBound), Math.Tanh(a.UpperBound));
|
---|
[16303] | 223 | }
|
---|
| 224 |
|
---|
| 225 | public static Interval Logarithm(Interval a) {
|
---|
[16327] | 226 | return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
|
---|
[16303] | 227 | }
|
---|
| 228 | public static Interval Exponential(Interval a) {
|
---|
[16327] | 229 | return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
|
---|
[16303] | 230 | }
|
---|
| 231 |
|
---|
[16323] | 232 | public static Interval Square(Interval a) {
|
---|
[16640] | 233 | if (a.UpperBound <= 0) return new Interval(a.UpperBound * a.UpperBound, a.LowerBound * a.LowerBound); // interval is negative
|
---|
| 234 | else if (a.LowerBound >= 0) return new Interval(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound); // interval is positive
|
---|
[17368] | 235 | else return new Interval(0, Math.Max(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound)); // interval goes over zero
|
---|
[16323] | 236 | }
|
---|
[16303] | 237 |
|
---|
[16640] | 238 | public static Interval Cube(Interval a) {
|
---|
| 239 | return new Interval(Math.Pow(a.LowerBound, 3), Math.Pow(a.UpperBound, 3));
|
---|
[16323] | 240 | }
|
---|
| 241 |
|
---|
[17963] | 242 | public static Interval Power(Interval a, int b) {
|
---|
| 243 | if (b < 0) return Power(1.0 / a, -b); // a^(-b) = 1/(a^b)
|
---|
| 244 | if (b == 0 && (a.Contains(0.0) || a.IsInfiniteOrUndefined)) return new Interval(double.NaN, double.NaN); // 0^0, +/-inf^0 are undefined
|
---|
| 245 | if (b == 0) return new Interval(1.0, 1.0); // x^0 = 1
|
---|
| 246 | if (b == 1) return a;
|
---|
| 247 | if (b % 2 == 0) {
|
---|
| 248 | // even powers (see x²)
|
---|
| 249 | if (a.UpperBound <= 0) return new Interval(Math.Pow(a.UpperBound, b), Math.Pow(a.LowerBound, b)); // interval is negative
|
---|
| 250 | if (a.LowerBound >= 0) return new Interval(Math.Pow(a.LowerBound, b), Math.Pow(a.UpperBound, b)); // interval is positive
|
---|
| 251 | return new Interval(0, Math.Max(Math.Pow(a.LowerBound, b), Math.Pow(a.UpperBound, b))); // interval goes over zero
|
---|
| 252 | } else {
|
---|
| 253 | // odd powers (see x³)
|
---|
| 254 | return new Interval(Math.Pow(a.LowerBound, b), Math.Pow(a.UpperBound, b));
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[17562] | 258 | /// <summary>
|
---|
[17583] | 259 | /// The interval contains both possible results of the calculated square root +-sqrt(x). That results in a wider
|
---|
| 260 | /// interval, but it contains all possible solutions.
|
---|
[17562] | 261 | /// </summary>
|
---|
| 262 | /// <param name="a">Interval to build square root from.</param>
|
---|
| 263 | /// <returns></returns>
|
---|
[16323] | 264 | public static Interval SquareRoot(Interval a) {
|
---|
[16640] | 265 | if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN);
|
---|
[17583] | 266 | return new Interval(-Math.Sqrt(a.UpperBound), Math.Sqrt(a.UpperBound));
|
---|
[16323] | 267 | }
|
---|
[16303] | 268 |
|
---|
[16323] | 269 | public static Interval CubicRoot(Interval a) {
|
---|
[17300] | 270 | var lower = (a.LowerBound < 0) ? -Math.Pow(-a.LowerBound, 1d / 3d) : Math.Pow(a.LowerBound, 1d / 3d);
|
---|
| 271 | var upper = (a.UpperBound < 0) ? -Math.Pow(-a.UpperBound, 1d / 3d) : Math.Pow(a.UpperBound, 1d / 3d);
|
---|
| 272 |
|
---|
| 273 | return new Interval(lower, upper);
|
---|
[16303] | 274 | }
|
---|
[17313] | 275 |
|
---|
| 276 | public static Interval Absolute(Interval a) {
|
---|
| 277 | var absLower = Math.Abs(a.LowerBound);
|
---|
| 278 | var absUpper = Math.Abs(a.UpperBound);
|
---|
[17368] | 279 | var min = Math.Min(absLower, absUpper);
|
---|
| 280 | var max = Math.Max(absLower, absUpper);
|
---|
| 281 |
|
---|
| 282 | if (a.Contains(0.0)) {
|
---|
| 283 | min = 0.0;
|
---|
[17547] | 284 | }
|
---|
[17368] | 285 |
|
---|
| 286 | return new Interval(min, max);
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[17911] | 289 | public static Interval AnalyticQuotient(Interval a, Interval b) {
|
---|
[17368] | 290 | var dividend = a;
|
---|
| 291 | var divisor = Add(Square(b), new Interval(1.0, 1.0));
|
---|
| 292 | divisor = SquareRoot(divisor);
|
---|
| 293 |
|
---|
| 294 | var quotient = Divide(dividend, divisor);
|
---|
| 295 | return quotient;
|
---|
| 296 | }
|
---|
[16303] | 297 | #endregion
|
---|
[17902] | 298 |
|
---|
| 299 | #region arithmetic overloads
|
---|
| 300 | public static Interval operator +(Interval a, Interval b) => Add(a, b);
|
---|
| 301 | public static Interval operator +(Interval a, double b) => Add(a, new Interval(b));
|
---|
| 302 | public static Interval operator +(double a, Interval b) => Add(new Interval(a), b);
|
---|
| 303 | public static Interval operator -(Interval a, Interval b) => Subtract(a, b);
|
---|
| 304 | public static Interval operator -(Interval a, double b) => Subtract(a, new Interval(b));
|
---|
| 305 | public static Interval operator -(double a, Interval b) => Subtract(new Interval(a), b);
|
---|
| 306 | public static Interval operator -(Interval a) => Subtract(new Interval(0), a);
|
---|
| 307 | public static Interval operator *(Interval a, Interval b) => Multiply(a, b);
|
---|
| 308 | public static Interval operator *(Interval a, double b) => Multiply(a, new Interval(b));
|
---|
| 309 | public static Interval operator *(double a, Interval b) => Multiply(new Interval(a), b);
|
---|
| 310 | public static Interval operator /(Interval a, Interval b) => Divide(a, b);
|
---|
| 311 | public static Interval operator /(Interval a, double b) => Divide(a, new Interval(b));
|
---|
| 312 | public static Interval operator /(double a, Interval b) => Divide(new Interval(a), b);
|
---|
| 313 | public static Interval Exponential(double a) { return Exponential(new Interval(a)); }
|
---|
| 314 | public static Interval Logarithm(double a) { return Logarithm(new Interval(a)); }
|
---|
| 315 | public static Interval Sine(double a) { return Sine(new Interval(a)); }
|
---|
| 316 | public static Interval Cosine(double a) { return Cosine(new Interval(a)); }
|
---|
| 317 | public static Interval Tangens(double a) { return Tangens(new Interval(a)); }
|
---|
| 318 | public static Interval HyperbolicTangent(double a) { return HyperbolicTangent(new Interval(a)); }
|
---|
| 319 | public static Interval Square(double a) { return Square(new Interval(a)); }
|
---|
| 320 | public static Interval Cube(double a) { return Cube(new Interval(a)); }
|
---|
| 321 | public static Interval SquareRoot(double a) { return SquareRoot(new Interval(a)); }
|
---|
| 322 | public static Interval CubicRoot(double a) { return CubicRoot(new Interval(a)); }
|
---|
| 323 | public static Interval Absolute(double a) { return Absolute(new Interval(a)); }
|
---|
[17911] | 324 | public static Interval AnalyticQuotient(Interval a, double b) { return AnalyticQuotient(a, new Interval(b)); }
|
---|
| 325 | public static Interval AnalyticQuotient(double a, Interval b) { return AnalyticQuotient(new Interval(a), b); }
|
---|
| 326 | public static Interval AnalyticQuotient(double a, double b) { return AnalyticQuotient(new Interval(a), new Interval(b)); }
|
---|
[17902] | 327 | #endregion
|
---|
[16303] | 328 | }
|
---|
| 329 | }
|
---|