[16323] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 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;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[17097] | 26 | using HEAL.Attic;
|
---|
[16303] | 27 |
|
---|
[16326] | 28 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[17097] | 29 | [StorableType("849e42d3-8934-419d-9aff-64ad81c06b67")]
|
---|
[16303] | 30 | public class Interval : IEquatable<Interval> {
|
---|
[17097] | 31 | [Storable]
|
---|
[16327] | 32 | public double LowerBound { get; private set; }
|
---|
[17097] | 33 | [Storable]
|
---|
[16327] | 34 | public double UpperBound { get; private set; }
|
---|
[16303] | 35 |
|
---|
[17097] | 36 | [StorableConstructor]
|
---|
| 37 | protected Interval(StorableConstructorFlag _) { }
|
---|
| 38 |
|
---|
[17495] | 39 | /// <summary>
|
---|
| 40 | /// Creates an interval with given bounds, where lower bound must be smaller than
|
---|
| 41 | /// the upper bound. Floating point precision errors trough calculations are fixed by,
|
---|
| 42 | /// checking if the intervals are almost the same (E-12). If this is the case, the bounds
|
---|
| 43 | /// will be set to the bound closer to zero.
|
---|
| 44 | /// </summary>
|
---|
| 45 | /// <param name="lowerBound">Lower bound of the interval</param>
|
---|
| 46 | /// <param name="upperBound">Upper bound of the interval</param>
|
---|
[16303] | 47 | public Interval(double lowerBound, double upperBound) {
|
---|
[17495] | 48 | if (lowerBound.IsAlmost(upperBound)) {
|
---|
| 49 | //If the bounds go over zero
|
---|
| 50 | if (lowerBound <= 0 && upperBound >= 0) {
|
---|
| 51 | lowerBound = 0.0;
|
---|
| 52 | upperBound = 0.0;
|
---|
| 53 | //Interval is negative
|
---|
| 54 | } else if (upperBound < 0) {
|
---|
| 55 | lowerBound = upperBound;
|
---|
| 56 | //Interval is positive
|
---|
| 57 | } else {
|
---|
| 58 | upperBound = lowerBound;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[16303] | 62 | if (lowerBound > upperBound)
|
---|
| 63 | throw new ArgumentException("LowerBound must be smaller than UpperBound.");
|
---|
| 64 |
|
---|
[16327] | 65 | this.LowerBound = lowerBound;
|
---|
| 66 | this.UpperBound = upperBound;
|
---|
[16303] | 67 | }
|
---|
| 68 |
|
---|
| 69 | public bool Contains(double value) {
|
---|
[16327] | 70 | return LowerBound <= value && value <= UpperBound;
|
---|
[16303] | 71 | }
|
---|
| 72 |
|
---|
| 73 | public override string ToString() {
|
---|
[16327] | 74 | return "Interval: [" + LowerBound + ", " + UpperBound + "]";
|
---|
[16303] | 75 | }
|
---|
| 76 |
|
---|
| 77 | public bool IsInfiniteOrUndefined {
|
---|
| 78 | get {
|
---|
| 79 | return double.IsInfinity(LowerBound) || double.IsInfinity(UpperBound) ||
|
---|
| 80 | double.IsNaN(LowerBound) || double.IsNaN(UpperBound);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[17495] | 84 | /// <summary>
|
---|
| 85 | /// True if the interval is positive without zero
|
---|
| 86 | /// </summary>
|
---|
| 87 | public bool IsPositive {
|
---|
| 88 | get => LowerBound > 0.0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /// <summary>
|
---|
| 92 | /// True if the interval is negative without zero
|
---|
| 93 | /// </summary>
|
---|
| 94 | public bool IsNegative {
|
---|
| 95 | get => UpperBound < 0.0;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[16404] | 98 | public static Interval GetInterval(IEnumerable<double> values) {
|
---|
[16407] | 99 | if (values == null) throw new ArgumentNullException("values");
|
---|
| 100 | if (!values.Any()) throw new ArgumentException($"No values are present.");
|
---|
[16404] | 101 |
|
---|
| 102 | var min = double.MaxValue;
|
---|
| 103 | var max = double.MinValue;
|
---|
| 104 |
|
---|
| 105 | foreach (var value in values) {
|
---|
| 106 | //If an value is NaN return an interval [NaN, NaN]
|
---|
| 107 | if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN);
|
---|
| 108 |
|
---|
| 109 | if (value < min) min = value;
|
---|
| 110 | if (value > max) max = value;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | return new Interval(min, max);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[16303] | 116 | #region Equals, GetHashCode, == , !=
|
---|
| 117 | public bool Equals(Interval other) {
|
---|
| 118 | if (other == null)
|
---|
| 119 | return false;
|
---|
| 120 |
|
---|
[16407] | 121 | return (UpperBound.IsAlmost(other.UpperBound) || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
|
---|
| 122 | && (LowerBound.IsAlmost(other.LowerBound) || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
|
---|
[16303] | 123 | }
|
---|
| 124 |
|
---|
| 125 | public override bool Equals(object obj) {
|
---|
| 126 | return Equals(obj as Interval);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public override int GetHashCode() {
|
---|
[16327] | 130 | return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
|
---|
[16303] | 131 | }
|
---|
| 132 |
|
---|
| 133 | public static bool operator ==(Interval interval1, Interval interval2) {
|
---|
| 134 | if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
|
---|
| 135 | return interval1.Equals(interval2);
|
---|
| 136 | }
|
---|
| 137 | public static bool operator !=(Interval interval1, Interval interval2) {
|
---|
| 138 | return !(interval1 == interval2);
|
---|
| 139 | }
|
---|
| 140 | #endregion
|
---|
| 141 |
|
---|
| 142 | #region operations
|
---|
| 143 |
|
---|
| 144 | // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
|
---|
| 145 | public static Interval Add(Interval a, Interval b) {
|
---|
[16327] | 146 | return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
|
---|
[16303] | 147 | }
|
---|
| 148 |
|
---|
| 149 | // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
|
---|
| 150 | public static Interval Subtract(Interval a, Interval b) {
|
---|
[16327] | 151 | return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
|
---|
[16303] | 152 | }
|
---|
| 153 |
|
---|
| 154 | // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
|
---|
| 155 | public static Interval Multiply(Interval a, Interval b) {
|
---|
[16327] | 156 | double v1 = a.LowerBound * b.LowerBound;
|
---|
| 157 | double v2 = a.LowerBound * b.UpperBound;
|
---|
| 158 | double v3 = a.UpperBound * b.LowerBound;
|
---|
| 159 | double v4 = a.UpperBound * b.UpperBound;
|
---|
[16303] | 160 |
|
---|
| 161 | double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
|
---|
[17100] | 162 | double max = Math.Max(Math.Max(v1, v2), Math.Max(v3, v4));
|
---|
[16303] | 163 | return new Interval(min, max);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[17495] | 166 | //Division by intervals containing 0 is implemented as defined in
|
---|
[16303] | 167 | //http://en.wikipedia.org/wiki/Interval_arithmetic
|
---|
| 168 | public static Interval Divide(Interval a, Interval b) {
|
---|
| 169 | if (b.Contains(0.0)) {
|
---|
[16327] | 170 | if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
|
---|
| 171 | else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
|
---|
[16303] | 172 | else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
|
---|
| 173 | }
|
---|
[16327] | 174 | return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
|
---|
[16303] | 175 | }
|
---|
| 176 |
|
---|
| 177 | public static Interval Sine(Interval a) {
|
---|
[16327] | 178 | if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
|
---|
[16303] | 179 |
|
---|
| 180 | //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
|
---|
| 181 | double Pihalf = Math.PI / 2;
|
---|
| 182 | Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
|
---|
| 183 | //move to positive scale
|
---|
[16327] | 184 | if (scaled.LowerBound < 0) {
|
---|
| 185 | int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
|
---|
[16303] | 186 | scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[16327] | 189 | double scaledLowerBound = scaled.LowerBound % 4.0;
|
---|
| 190 | double scaledUpperBound = scaled.UpperBound % 4.0;
|
---|
[16303] | 191 | if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
|
---|
| 192 | List<double> sinValues = new List<double>();
|
---|
| 193 | sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
|
---|
| 194 | sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
|
---|
| 195 |
|
---|
| 196 | int startValue = (int)Math.Ceiling(scaledLowerBound);
|
---|
| 197 | while (startValue < scaledUpperBound) {
|
---|
| 198 | sinValues.Add(Math.Sin(startValue * Pihalf));
|
---|
| 199 | startValue += 1;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | return new Interval(sinValues.Min(), sinValues.Max());
|
---|
| 203 | }
|
---|
| 204 | public static Interval Cosine(Interval a) {
|
---|
[17100] | 205 | return Interval.Sine(Interval.Add(a, new Interval(Math.PI / 2, Math.PI / 2)));
|
---|
[16303] | 206 | }
|
---|
| 207 | public static Interval Tangens(Interval a) {
|
---|
| 208 | return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
|
---|
[17495] | 209 | }
|
---|
[17100] | 210 | public static Interval HyperbolicTangent(Interval a) {
|
---|
| 211 | return new Interval(Math.Tanh(a.LowerBound), Math.Tanh(a.UpperBound));
|
---|
[16303] | 212 | }
|
---|
| 213 |
|
---|
| 214 | public static Interval Logarithm(Interval a) {
|
---|
[16327] | 215 | return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
|
---|
[16303] | 216 | }
|
---|
| 217 | public static Interval Exponential(Interval a) {
|
---|
[16327] | 218 | return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
|
---|
[16303] | 219 | }
|
---|
| 220 |
|
---|
| 221 | public static Interval Power(Interval a, Interval b) {
|
---|
[16327] | 222 | if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
|
---|
[16303] | 223 |
|
---|
[16327] | 224 | int bLower = (int)Math.Round(b.LowerBound);
|
---|
| 225 | int bUpper = (int)Math.Round(b.UpperBound);
|
---|
[16303] | 226 |
|
---|
| 227 | List<double> powerValues = new List<double>();
|
---|
[16327] | 228 | powerValues.Add(Math.Pow(a.UpperBound, bUpper));
|
---|
| 229 | powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
|
---|
| 230 | powerValues.Add(Math.Pow(a.UpperBound, bLower));
|
---|
| 231 | powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
|
---|
[16303] | 232 |
|
---|
[16327] | 233 | powerValues.Add(Math.Pow(a.LowerBound, bUpper));
|
---|
| 234 | powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
|
---|
| 235 | powerValues.Add(Math.Pow(a.LowerBound, bLower));
|
---|
| 236 | powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
|
---|
[16303] | 237 |
|
---|
| 238 | return new Interval(powerValues.Min(), powerValues.Max());
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[16323] | 241 | public static Interval Square(Interval a) {
|
---|
[17100] | 242 | if (a.UpperBound <= 0) return new Interval(a.UpperBound * a.UpperBound, a.LowerBound * a.LowerBound); // interval is negative
|
---|
| 243 | else if (a.LowerBound >= 0) return new Interval(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound); // interval is positive
|
---|
[17495] | 244 | else return new Interval(0, Math.Max(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound)); // interval goes over zero
|
---|
[16323] | 245 | }
|
---|
[16303] | 246 |
|
---|
[17100] | 247 | public static Interval Cube(Interval a) {
|
---|
| 248 | return new Interval(Math.Pow(a.LowerBound, 3), Math.Pow(a.UpperBound, 3));
|
---|
[16323] | 249 | }
|
---|
| 250 |
|
---|
[16303] | 251 | public static Interval Root(Interval a, Interval b) {
|
---|
[16327] | 252 | int lower = (int)Math.Round(b.LowerBound);
|
---|
| 253 | int higher = (int)Math.Round(b.UpperBound);
|
---|
[16303] | 254 |
|
---|
[16327] | 255 | return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
|
---|
[16303] | 256 | }
|
---|
| 257 |
|
---|
[16323] | 258 | public static Interval SquareRoot(Interval a) {
|
---|
[17100] | 259 | if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN);
|
---|
| 260 | return new Interval(Math.Sqrt(a.LowerBound), Math.Sqrt(a.UpperBound));
|
---|
[16323] | 261 | }
|
---|
[16303] | 262 |
|
---|
[16323] | 263 | public static Interval CubicRoot(Interval a) {
|
---|
[17495] | 264 | var lower = (a.LowerBound < 0) ? -Math.Pow(-a.LowerBound, 1d / 3d) : Math.Pow(a.LowerBound, 1d / 3d);
|
---|
| 265 | var upper = (a.UpperBound < 0) ? -Math.Pow(-a.UpperBound, 1d / 3d) : Math.Pow(a.UpperBound, 1d / 3d);
|
---|
| 266 |
|
---|
| 267 | return new Interval(lower, upper);
|
---|
[16303] | 268 | }
|
---|
[17495] | 269 |
|
---|
| 270 | public static Interval Absolute(Interval a) {
|
---|
| 271 | var absLower = Math.Abs(a.LowerBound);
|
---|
| 272 | var absUpper = Math.Abs(a.UpperBound);
|
---|
| 273 | var min = Math.Min(absLower, absUpper);
|
---|
| 274 | var max = Math.Max(absLower, absUpper);
|
---|
| 275 |
|
---|
| 276 | if (a.Contains(0.0)) {
|
---|
| 277 | min = 0.0;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | return new Interval(min, max);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | public static Interval AnalyticalQuotient(Interval a, Interval b) {
|
---|
| 284 | var dividend = a;
|
---|
| 285 | var divisor = Add(Square(b), new Interval(1.0, 1.0));
|
---|
| 286 | divisor = SquareRoot(divisor);
|
---|
| 287 |
|
---|
| 288 | var quotient = Divide(dividend, divisor);
|
---|
| 289 | return quotient;
|
---|
| 290 | }
|
---|
[16303] | 291 | #endregion
|
---|
| 292 | }
|
---|
| 293 | }
|
---|