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