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