[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 |
|
---|
| 55 | #region Equals, GetHashCode, == , !=
|
---|
| 56 | public bool Equals(Interval other) {
|
---|
| 57 | if (other == null)
|
---|
| 58 | return false;
|
---|
| 59 |
|
---|
[16327] | 60 | return this.UpperBound.IsAlmost(other.UpperBound) && this.LowerBound.IsAlmost(other.LowerBound);
|
---|
[16303] | 61 | }
|
---|
| 62 |
|
---|
| 63 | public override bool Equals(object obj) {
|
---|
| 64 | return Equals(obj as Interval);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public override int GetHashCode() {
|
---|
[16327] | 68 | return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
|
---|
[16303] | 69 | }
|
---|
| 70 |
|
---|
| 71 | public static bool operator ==(Interval interval1, Interval interval2) {
|
---|
| 72 | if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
|
---|
| 73 | return interval1.Equals(interval2);
|
---|
| 74 | }
|
---|
| 75 | public static bool operator !=(Interval interval1, Interval interval2) {
|
---|
| 76 | return !(interval1 == interval2);
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | #region operations
|
---|
| 81 |
|
---|
| 82 | // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
|
---|
| 83 | public static Interval Add(Interval a, Interval b) {
|
---|
[16327] | 84 | return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
|
---|
[16303] | 85 | }
|
---|
| 86 |
|
---|
| 87 | // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
|
---|
| 88 | public static Interval Subtract(Interval a, Interval b) {
|
---|
[16327] | 89 | return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
|
---|
[16303] | 90 | }
|
---|
| 91 |
|
---|
| 92 | // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
|
---|
| 93 | public static Interval Multiply(Interval a, Interval b) {
|
---|
[16327] | 94 | double v1 = a.LowerBound * b.LowerBound;
|
---|
| 95 | double v2 = a.LowerBound * b.UpperBound;
|
---|
| 96 | double v3 = a.UpperBound * b.LowerBound;
|
---|
| 97 | double v4 = a.UpperBound * b.UpperBound;
|
---|
[16303] | 98 |
|
---|
| 99 | double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
|
---|
| 100 | double max = Math.Max(Math.Min(v1, v2), Math.Max(v3, v4));
|
---|
| 101 | return new Interval(min, max);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[16327] | 104 | //mkommend: Division by intervals containing 0 is implemented as defined in
|
---|
[16303] | 105 | //http://en.wikipedia.org/wiki/Interval_arithmetic
|
---|
| 106 | public static Interval Divide(Interval a, Interval b) {
|
---|
| 107 | if (b.Contains(0.0)) {
|
---|
[16327] | 108 | if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
|
---|
| 109 | else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
|
---|
[16303] | 110 | else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
|
---|
| 111 | }
|
---|
[16327] | 112 | return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
|
---|
[16303] | 113 | }
|
---|
| 114 |
|
---|
| 115 | public static Interval Sine(Interval a) {
|
---|
[16327] | 116 | if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
|
---|
[16303] | 117 |
|
---|
| 118 | //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
|
---|
| 119 | double Pihalf = Math.PI / 2;
|
---|
| 120 | Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
|
---|
| 121 | //move to positive scale
|
---|
[16327] | 122 | if (scaled.LowerBound < 0) {
|
---|
| 123 | int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
|
---|
[16303] | 124 | scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[16327] | 127 | double scaledLowerBound = scaled.LowerBound % 4.0;
|
---|
| 128 | double scaledUpperBound = scaled.UpperBound % 4.0;
|
---|
[16303] | 129 | if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
|
---|
| 130 | List<double> sinValues = new List<double>();
|
---|
| 131 | sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
|
---|
| 132 | sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
|
---|
| 133 |
|
---|
| 134 | int startValue = (int)Math.Ceiling(scaledLowerBound);
|
---|
| 135 | while (startValue < scaledUpperBound) {
|
---|
| 136 | sinValues.Add(Math.Sin(startValue * Pihalf));
|
---|
| 137 | startValue += 1;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | return new Interval(sinValues.Min(), sinValues.Max());
|
---|
| 141 | }
|
---|
| 142 | public static Interval Cosine(Interval a) {
|
---|
| 143 | return Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
|
---|
| 144 | }
|
---|
| 145 | public static Interval Tangens(Interval a) {
|
---|
| 146 | return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | public static Interval Logarithm(Interval a) {
|
---|
[16327] | 150 | return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
|
---|
[16303] | 151 | }
|
---|
| 152 | public static Interval Exponential(Interval a) {
|
---|
[16327] | 153 | return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
|
---|
[16303] | 154 | }
|
---|
| 155 |
|
---|
| 156 | public static Interval Power(Interval a, Interval b) {
|
---|
[16327] | 157 | if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
|
---|
[16303] | 158 |
|
---|
[16327] | 159 | int bLower = (int)Math.Round(b.LowerBound);
|
---|
| 160 | int bUpper = (int)Math.Round(b.UpperBound);
|
---|
[16303] | 161 |
|
---|
| 162 | List<double> powerValues = new List<double>();
|
---|
[16327] | 163 | powerValues.Add(Math.Pow(a.UpperBound, bUpper));
|
---|
| 164 | powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
|
---|
| 165 | powerValues.Add(Math.Pow(a.UpperBound, bLower));
|
---|
| 166 | powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
|
---|
[16303] | 167 |
|
---|
[16327] | 168 | powerValues.Add(Math.Pow(a.LowerBound, bUpper));
|
---|
| 169 | powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
|
---|
| 170 | powerValues.Add(Math.Pow(a.LowerBound, bLower));
|
---|
| 171 | powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
|
---|
[16303] | 172 |
|
---|
| 173 | return new Interval(powerValues.Min(), powerValues.Max());
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[16323] | 176 | public static Interval Square(Interval a) {
|
---|
| 177 | return Power(a, new Interval(2, 2));
|
---|
| 178 | }
|
---|
[16303] | 179 |
|
---|
[16323] | 180 | public static Interval Cubic(Interval a) {
|
---|
| 181 | return Power(a, new Interval(3, 3));
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[16303] | 184 | public static Interval Root(Interval a, Interval b) {
|
---|
[16327] | 185 | int lower = (int)Math.Round(b.LowerBound);
|
---|
| 186 | int higher = (int)Math.Round(b.UpperBound);
|
---|
[16303] | 187 |
|
---|
[16327] | 188 | return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
|
---|
[16303] | 189 | }
|
---|
| 190 |
|
---|
[16323] | 191 | public static Interval SquareRoot(Interval a) {
|
---|
| 192 | return Root(a, new Interval(2, 2));
|
---|
| 193 | }
|
---|
[16303] | 194 |
|
---|
[16323] | 195 | public static Interval CubicRoot(Interval a) {
|
---|
| 196 | return Root(a, new Interval(3, 3));
|
---|
[16303] | 197 | }
|
---|
| 198 | #endregion
|
---|
| 199 | }
|
---|
| 200 | }
|
---|