[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;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
[16628] | 26 | using HEAL.Attic;
|
---|
[16303] | 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 |
|
---|
[16640] | 36 | [StorableConstructor]
|
---|
| 37 | protected Interval(StorableConstructorFlag _) { }
|
---|
[16548] | 38 |
|
---|
[16303] | 39 | public Interval(double lowerBound, double upperBound) {
|
---|
[16882] | 40 | //TODO Handle floating point issues where lowerbound is bigger than upperbound in the nth decimal place
|
---|
[16303] | 41 | if (lowerBound > upperBound)
|
---|
[16633] | 42 | throw new ArgumentException("lowerBound must be smaller than or equal to upperBound.");
|
---|
[16303] | 43 |
|
---|
[16327] | 44 | this.LowerBound = lowerBound;
|
---|
| 45 | this.UpperBound = upperBound;
|
---|
[16303] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public bool Contains(double value) {
|
---|
[16327] | 49 | return LowerBound <= value && value <= UpperBound;
|
---|
[16303] | 50 | }
|
---|
| 51 |
|
---|
[16592] | 52 | public bool Contains(Interval other, bool lowerBoundInclusive = true, bool upperBoundInclusive = false) {
|
---|
| 53 | if (double.IsNegativeInfinity(this.LowerBound) && double.IsPositiveInfinity(this.UpperBound))
|
---|
| 54 | return true;
|
---|
| 55 | //Left-unbounded and right-bounded:
|
---|
| 56 | if (double.IsNegativeInfinity(this.LowerBound)) {
|
---|
| 57 | if (upperBoundInclusive)
|
---|
| 58 | return other.LowerBound <= this.UpperBound && other.UpperBound <= this.UpperBound;
|
---|
| 59 | return other.LowerBound < this.UpperBound && other.UpperBound < this.UpperBound;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | //Left-bounded and right-unbounded:
|
---|
| 63 | if (double.IsPositiveInfinity(this.UpperBound)) {
|
---|
| 64 | if (lowerBoundInclusive)
|
---|
| 65 | return other.LowerBound >= this.LowerBound && other.UpperBound >= this.LowerBound;
|
---|
| 66 | return other.LowerBound > this.LowerBound && other.UpperBound > this.LowerBound;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | //Proper and bounded:
|
---|
| 70 | //Closed:
|
---|
| 71 | if (lowerBoundInclusive && upperBoundInclusive) {
|
---|
| 72 | return this.LowerBound <= other.LowerBound && other.UpperBound <= this.UpperBound;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | //Open:
|
---|
| 76 | if (!lowerBoundInclusive && !upperBoundInclusive) {
|
---|
| 77 | return this.LowerBound < other.LowerBound && other.UpperBound < this.UpperBound;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | //Left-closed, right-open:
|
---|
| 81 | if (lowerBoundInclusive) {
|
---|
| 82 | return this.LowerBound <= other.LowerBound && other.UpperBound < this.UpperBound;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | //Left-open, right-closed:
|
---|
| 86 | return this.LowerBound < other.LowerBound && other.UpperBound <= this.UpperBound;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[16303] | 89 | public override string ToString() {
|
---|
[16327] | 90 | return "Interval: [" + LowerBound + ", " + UpperBound + "]";
|
---|
[16303] | 91 | }
|
---|
| 92 |
|
---|
| 93 | public bool IsInfiniteOrUndefined {
|
---|
| 94 | get {
|
---|
| 95 | return double.IsInfinity(LowerBound) || double.IsInfinity(UpperBound) ||
|
---|
| 96 | double.IsNaN(LowerBound) || double.IsNaN(UpperBound);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[16404] | 100 | public static Interval GetInterval(IEnumerable<double> values) {
|
---|
[16407] | 101 | if (values == null) throw new ArgumentNullException("values");
|
---|
| 102 | if (!values.Any()) throw new ArgumentException($"No values are present.");
|
---|
[16404] | 103 |
|
---|
| 104 | var min = double.MaxValue;
|
---|
| 105 | var max = double.MinValue;
|
---|
| 106 |
|
---|
| 107 | foreach (var value in values) {
|
---|
| 108 | //If an value is NaN return an interval [NaN, NaN]
|
---|
| 109 | if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN);
|
---|
| 110 |
|
---|
| 111 | if (value < min) min = value;
|
---|
| 112 | if (value > max) max = value;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | return new Interval(min, max);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[16303] | 118 | #region Equals, GetHashCode, == , !=
|
---|
| 119 | public bool Equals(Interval other) {
|
---|
| 120 | if (other == null)
|
---|
| 121 | return false;
|
---|
| 122 |
|
---|
[16407] | 123 | return (UpperBound.IsAlmost(other.UpperBound) || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
|
---|
| 124 | && (LowerBound.IsAlmost(other.LowerBound) || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
|
---|
[16303] | 125 | }
|
---|
| 126 |
|
---|
| 127 | public override bool Equals(object obj) {
|
---|
| 128 | return Equals(obj as Interval);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public override int GetHashCode() {
|
---|
[16327] | 132 | return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
|
---|
[16303] | 133 | }
|
---|
| 134 |
|
---|
| 135 | public static bool operator ==(Interval interval1, Interval interval2) {
|
---|
| 136 | if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
|
---|
| 137 | return interval1.Equals(interval2);
|
---|
| 138 | }
|
---|
| 139 | public static bool operator !=(Interval interval1, Interval interval2) {
|
---|
| 140 | return !(interval1 == interval2);
|
---|
| 141 | }
|
---|
| 142 | #endregion
|
---|
| 143 |
|
---|
| 144 | #region operations
|
---|
| 145 |
|
---|
| 146 | // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
|
---|
| 147 | public static Interval Add(Interval a, Interval b) {
|
---|
[16327] | 148 | return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
|
---|
[16303] | 149 | }
|
---|
| 150 |
|
---|
| 151 | // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
|
---|
| 152 | public static Interval Subtract(Interval a, Interval b) {
|
---|
[16327] | 153 | return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
|
---|
[16303] | 154 | }
|
---|
| 155 |
|
---|
| 156 | // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
|
---|
| 157 | public static Interval Multiply(Interval a, Interval b) {
|
---|
[16327] | 158 | double v1 = a.LowerBound * b.LowerBound;
|
---|
| 159 | double v2 = a.LowerBound * b.UpperBound;
|
---|
| 160 | double v3 = a.UpperBound * b.LowerBound;
|
---|
| 161 | double v4 = a.UpperBound * b.UpperBound;
|
---|
[16303] | 162 |
|
---|
| 163 | double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
|
---|
[16647] | 164 | double max = Math.Max(Math.Max(v1, v2), Math.Max(v3, v4));
|
---|
[16303] | 165 | return new Interval(min, max);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[16327] | 168 | //mkommend: Division by intervals containing 0 is implemented as defined in
|
---|
[16303] | 169 | //http://en.wikipedia.org/wiki/Interval_arithmetic
|
---|
| 170 | public static Interval Divide(Interval a, Interval b) {
|
---|
| 171 | if (b.Contains(0.0)) {
|
---|
[16327] | 172 | if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
|
---|
| 173 | else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
|
---|
[16303] | 174 | else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
|
---|
| 175 | }
|
---|
[16327] | 176 | return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
|
---|
[16303] | 177 | }
|
---|
| 178 |
|
---|
| 179 | public static Interval Sine(Interval a) {
|
---|
[16327] | 180 | if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
|
---|
[16303] | 181 |
|
---|
| 182 | //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
|
---|
| 183 | double Pihalf = Math.PI / 2;
|
---|
| 184 | Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
|
---|
| 185 | //move to positive scale
|
---|
[16327] | 186 | if (scaled.LowerBound < 0) {
|
---|
| 187 | int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
|
---|
[16303] | 188 | scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[16327] | 191 | double scaledLowerBound = scaled.LowerBound % 4.0;
|
---|
| 192 | double scaledUpperBound = scaled.UpperBound % 4.0;
|
---|
[16303] | 193 | if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
|
---|
| 194 | List<double> sinValues = new List<double>();
|
---|
| 195 | sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
|
---|
| 196 | sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
|
---|
| 197 |
|
---|
| 198 | int startValue = (int)Math.Ceiling(scaledLowerBound);
|
---|
| 199 | while (startValue < scaledUpperBound) {
|
---|
| 200 | sinValues.Add(Math.Sin(startValue * Pihalf));
|
---|
| 201 | startValue += 1;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | return new Interval(sinValues.Min(), sinValues.Max());
|
---|
| 205 | }
|
---|
| 206 | public static Interval Cosine(Interval a) {
|
---|
[16775] | 207 | return Interval.Sine(Interval.Add(a, new Interval(Math.PI / 2, Math.PI / 2)));
|
---|
[16303] | 208 | }
|
---|
| 209 | public static Interval Tangens(Interval a) {
|
---|
| 210 | return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
|
---|
[16775] | 211 | }
|
---|
| 212 | public static Interval HyperbolicTangent(Interval a) {
|
---|
| 213 | return new Interval(Math.Tanh(a.LowerBound), Math.Tanh(a.UpperBound));
|
---|
[16303] | 214 | }
|
---|
| 215 |
|
---|
| 216 | public static Interval Logarithm(Interval a) {
|
---|
[16327] | 217 | return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
|
---|
[16303] | 218 | }
|
---|
| 219 | public static Interval Exponential(Interval a) {
|
---|
[16327] | 220 | return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
|
---|
[16303] | 221 | }
|
---|
| 222 |
|
---|
| 223 | public static Interval Power(Interval a, Interval b) {
|
---|
[16327] | 224 | if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
|
---|
[16303] | 225 |
|
---|
[16327] | 226 | int bLower = (int)Math.Round(b.LowerBound);
|
---|
| 227 | int bUpper = (int)Math.Round(b.UpperBound);
|
---|
[16303] | 228 |
|
---|
| 229 | List<double> powerValues = new List<double>();
|
---|
[16327] | 230 | powerValues.Add(Math.Pow(a.UpperBound, bUpper));
|
---|
| 231 | powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
|
---|
| 232 | powerValues.Add(Math.Pow(a.UpperBound, bLower));
|
---|
| 233 | powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
|
---|
[16303] | 234 |
|
---|
[16327] | 235 | powerValues.Add(Math.Pow(a.LowerBound, bUpper));
|
---|
| 236 | powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
|
---|
| 237 | powerValues.Add(Math.Pow(a.LowerBound, bLower));
|
---|
| 238 | powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
|
---|
[16303] | 239 |
|
---|
| 240 | return new Interval(powerValues.Min(), powerValues.Max());
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[16323] | 243 | public static Interval Square(Interval a) {
|
---|
[16640] | 244 | if (a.UpperBound <= 0) return new Interval(a.UpperBound * a.UpperBound, a.LowerBound * a.LowerBound); // interval is negative
|
---|
| 245 | else if (a.LowerBound >= 0) return new Interval(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound); // interval is positive
|
---|
| 246 | else return new Interval(0, Math.Max(a.LowerBound*a.LowerBound, a.UpperBound*a.UpperBound)); // interval goes over zero
|
---|
[16323] | 247 | }
|
---|
[16303] | 248 |
|
---|
[16640] | 249 | public static Interval Cube(Interval a) {
|
---|
| 250 | return new Interval(Math.Pow(a.LowerBound, 3), Math.Pow(a.UpperBound, 3));
|
---|
[16323] | 251 | }
|
---|
| 252 |
|
---|
[16303] | 253 | public static Interval Root(Interval a, Interval b) {
|
---|
[16327] | 254 | int lower = (int)Math.Round(b.LowerBound);
|
---|
| 255 | int higher = (int)Math.Round(b.UpperBound);
|
---|
[16303] | 256 |
|
---|
[16327] | 257 | return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
|
---|
[16303] | 258 | }
|
---|
| 259 |
|
---|
[16323] | 260 | public static Interval SquareRoot(Interval a) {
|
---|
[16640] | 261 | if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN);
|
---|
| 262 | return new Interval(Math.Sqrt(a.LowerBound), Math.Sqrt(a.UpperBound));
|
---|
[16323] | 263 | }
|
---|
[16303] | 264 |
|
---|
[16323] | 265 | public static Interval CubicRoot(Interval a) {
|
---|
[16640] | 266 | if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN);
|
---|
| 267 | return new Interval(Math.Pow(a.LowerBound, 1.0/3), Math.Pow(a.UpperBound, 1.0/3));
|
---|
[16303] | 268 | }
|
---|
| 269 | #endregion
|
---|
| 270 | }
|
---|
| 271 | }
|
---|