[16323] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16453] | 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;
|
---|
[16559] | 26 | using HEAL.Attic;
|
---|
[16303] | 27 |
|
---|
[16326] | 28 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16476] | 29 | [StorableType("849e42d3-8934-419d-9aff-64ad81c06b67")]
|
---|
[16303] | 30 | public class Interval : IEquatable<Interval> {
|
---|
[16476] | 31 | [Storable]
|
---|
[16327] | 32 | public double LowerBound { get; private set; }
|
---|
[16476] | 33 | [Storable]
|
---|
[16327] | 34 | public double UpperBound { get; private set; }
|
---|
[16303] | 35 |
|
---|
| 36 | public Interval(double lowerBound, double upperBound) {
|
---|
| 37 | if (lowerBound > upperBound)
|
---|
| 38 | throw new ArgumentException("LowerBound must be smaller than UpperBound.");
|
---|
| 39 |
|
---|
[16327] | 40 | this.LowerBound = lowerBound;
|
---|
| 41 | this.UpperBound = upperBound;
|
---|
[16303] | 42 | }
|
---|
| 43 |
|
---|
| 44 | public bool Contains(double value) {
|
---|
[16327] | 45 | return LowerBound <= value && value <= UpperBound;
|
---|
[16303] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public override string ToString() {
|
---|
[16327] | 49 | return "Interval: [" + LowerBound + ", " + UpperBound + "]";
|
---|
[16303] | 50 | }
|
---|
| 51 |
|
---|
| 52 | public bool IsInfiniteOrUndefined {
|
---|
| 53 | get {
|
---|
| 54 | return double.IsInfinity(LowerBound) || double.IsInfinity(UpperBound) ||
|
---|
| 55 | double.IsNaN(LowerBound) || double.IsNaN(UpperBound);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[16404] | 59 | public static Interval GetInterval(IEnumerable<double> values) {
|
---|
[16407] | 60 | if (values == null) throw new ArgumentNullException("values");
|
---|
| 61 | if (!values.Any()) throw new ArgumentException($"No values are present.");
|
---|
[16404] | 62 |
|
---|
| 63 | var min = double.MaxValue;
|
---|
| 64 | var max = double.MinValue;
|
---|
| 65 |
|
---|
| 66 | foreach (var value in values) {
|
---|
| 67 | //If an value is NaN return an interval [NaN, NaN]
|
---|
| 68 | if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN);
|
---|
| 69 |
|
---|
| 70 | if (value < min) min = value;
|
---|
| 71 | if (value > max) max = value;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | return new Interval(min, max);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[16303] | 77 | #region Equals, GetHashCode, == , !=
|
---|
| 78 | public bool Equals(Interval other) {
|
---|
| 79 | if (other == null)
|
---|
| 80 | return false;
|
---|
| 81 |
|
---|
[16407] | 82 | return (UpperBound.IsAlmost(other.UpperBound) || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
|
---|
| 83 | && (LowerBound.IsAlmost(other.LowerBound) || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
|
---|
[16303] | 84 | }
|
---|
| 85 |
|
---|
| 86 | public override bool Equals(object obj) {
|
---|
| 87 | return Equals(obj as Interval);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public override int GetHashCode() {
|
---|
[16327] | 91 | return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
|
---|
[16303] | 92 | }
|
---|
| 93 |
|
---|
| 94 | public static bool operator ==(Interval interval1, Interval interval2) {
|
---|
| 95 | if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
|
---|
| 96 | return interval1.Equals(interval2);
|
---|
| 97 | }
|
---|
| 98 | public static bool operator !=(Interval interval1, Interval interval2) {
|
---|
| 99 | return !(interval1 == interval2);
|
---|
| 100 | }
|
---|
| 101 | #endregion
|
---|
| 102 |
|
---|
| 103 | #region operations
|
---|
| 104 |
|
---|
| 105 | // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
|
---|
| 106 | public static Interval Add(Interval a, Interval b) {
|
---|
[16327] | 107 | return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
|
---|
[16303] | 108 | }
|
---|
| 109 |
|
---|
| 110 | // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
|
---|
| 111 | public static Interval Subtract(Interval a, Interval b) {
|
---|
[16327] | 112 | return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
|
---|
[16303] | 113 | }
|
---|
| 114 |
|
---|
| 115 | // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
|
---|
| 116 | public static Interval Multiply(Interval a, Interval b) {
|
---|
[16327] | 117 | double v1 = a.LowerBound * b.LowerBound;
|
---|
| 118 | double v2 = a.LowerBound * b.UpperBound;
|
---|
| 119 | double v3 = a.UpperBound * b.LowerBound;
|
---|
| 120 | double v4 = a.UpperBound * b.UpperBound;
|
---|
[16303] | 121 |
|
---|
| 122 | double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
|
---|
| 123 | double max = Math.Max(Math.Min(v1, v2), Math.Max(v3, v4));
|
---|
| 124 | return new Interval(min, max);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[16327] | 127 | //mkommend: Division by intervals containing 0 is implemented as defined in
|
---|
[16303] | 128 | //http://en.wikipedia.org/wiki/Interval_arithmetic
|
---|
| 129 | public static Interval Divide(Interval a, Interval b) {
|
---|
| 130 | if (b.Contains(0.0)) {
|
---|
[16327] | 131 | if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
|
---|
| 132 | else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
|
---|
[16303] | 133 | else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
|
---|
| 134 | }
|
---|
[16327] | 135 | return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
|
---|
[16303] | 136 | }
|
---|
| 137 |
|
---|
| 138 | public static Interval Sine(Interval a) {
|
---|
[16327] | 139 | if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
|
---|
[16303] | 140 |
|
---|
| 141 | //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
|
---|
| 142 | double Pihalf = Math.PI / 2;
|
---|
| 143 | Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
|
---|
| 144 | //move to positive scale
|
---|
[16327] | 145 | if (scaled.LowerBound < 0) {
|
---|
| 146 | int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
|
---|
[16303] | 147 | scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[16327] | 150 | double scaledLowerBound = scaled.LowerBound % 4.0;
|
---|
| 151 | double scaledUpperBound = scaled.UpperBound % 4.0;
|
---|
[16303] | 152 | if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
|
---|
| 153 | List<double> sinValues = new List<double>();
|
---|
| 154 | sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
|
---|
| 155 | sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
|
---|
| 156 |
|
---|
| 157 | int startValue = (int)Math.Ceiling(scaledLowerBound);
|
---|
| 158 | while (startValue < scaledUpperBound) {
|
---|
| 159 | sinValues.Add(Math.Sin(startValue * Pihalf));
|
---|
| 160 | startValue += 1;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | return new Interval(sinValues.Min(), sinValues.Max());
|
---|
| 164 | }
|
---|
| 165 | public static Interval Cosine(Interval a) {
|
---|
| 166 | return Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
|
---|
| 167 | }
|
---|
| 168 | public static Interval Tangens(Interval a) {
|
---|
| 169 | return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | public static Interval Logarithm(Interval a) {
|
---|
[16327] | 173 | return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
|
---|
[16303] | 174 | }
|
---|
| 175 | public static Interval Exponential(Interval a) {
|
---|
[16327] | 176 | return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
|
---|
[16303] | 177 | }
|
---|
| 178 |
|
---|
| 179 | public static Interval Power(Interval a, Interval b) {
|
---|
[16327] | 180 | if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
|
---|
[16303] | 181 |
|
---|
[16327] | 182 | int bLower = (int)Math.Round(b.LowerBound);
|
---|
| 183 | int bUpper = (int)Math.Round(b.UpperBound);
|
---|
[16303] | 184 |
|
---|
| 185 | List<double> powerValues = new List<double>();
|
---|
[16327] | 186 | powerValues.Add(Math.Pow(a.UpperBound, bUpper));
|
---|
| 187 | powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
|
---|
| 188 | powerValues.Add(Math.Pow(a.UpperBound, bLower));
|
---|
| 189 | powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
|
---|
[16303] | 190 |
|
---|
[16327] | 191 | powerValues.Add(Math.Pow(a.LowerBound, bUpper));
|
---|
| 192 | powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
|
---|
| 193 | powerValues.Add(Math.Pow(a.LowerBound, bLower));
|
---|
| 194 | powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
|
---|
[16303] | 195 |
|
---|
| 196 | return new Interval(powerValues.Min(), powerValues.Max());
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[16323] | 199 | public static Interval Square(Interval a) {
|
---|
| 200 | return Power(a, new Interval(2, 2));
|
---|
| 201 | }
|
---|
[16303] | 202 |
|
---|
[16323] | 203 | public static Interval Cubic(Interval a) {
|
---|
| 204 | return Power(a, new Interval(3, 3));
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[16303] | 207 | public static Interval Root(Interval a, Interval b) {
|
---|
[16327] | 208 | int lower = (int)Math.Round(b.LowerBound);
|
---|
| 209 | int higher = (int)Math.Round(b.UpperBound);
|
---|
[16303] | 210 |
|
---|
[16327] | 211 | return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
|
---|
[16303] | 212 | }
|
---|
| 213 |
|
---|
[16323] | 214 | public static Interval SquareRoot(Interval a) {
|
---|
| 215 | return Root(a, new Interval(2, 2));
|
---|
| 216 | }
|
---|
[16303] | 217 |
|
---|
[16323] | 218 | public static Interval CubicRoot(Interval a) {
|
---|
| 219 | return Root(a, new Interval(3, 3));
|
---|
[16303] | 220 | }
|
---|
| 221 | #endregion
|
---|
| 222 | }
|
---|
| 223 | }
|
---|