Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs @ 16565

Last change on this file since 16565 was 16565, checked in by gkronber, 5 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HEAL.Attic;
27
28namespace HeuristicLab.Problems.DataAnalysis {
29  [StorableType("849e42d3-8934-419d-9aff-64ad81c06b67")]
30  public class Interval : IEquatable<Interval> {
31    [Storable]
32    public double LowerBound { get; private set; }
33    [Storable]
34    public double UpperBound { get; private set; }
35
36    public Interval(double lowerBound, double upperBound) {
37      if (lowerBound > upperBound)
38        throw new ArgumentException("LowerBound must be smaller than UpperBound.");
39
40      this.LowerBound = lowerBound;
41      this.UpperBound = upperBound;
42    }
43
44    public bool Contains(double value) {
45      return LowerBound <= value && value <= UpperBound;
46    }
47
48    public override string ToString() {
49      return "Interval: [" + LowerBound + ", " + UpperBound + "]";
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
59    public static Interval GetInterval(IEnumerable<double> values) {
60      if (values == null) throw new ArgumentNullException("values");
61      if (!values.Any()) throw new ArgumentException($"No values are present.");
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
77    #region Equals, GetHashCode, == , !=
78    public bool Equals(Interval other) {
79      if (other == null)
80        return false;
81
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)));
84    }
85
86    public override bool Equals(object obj) {
87      return Equals(obj as Interval);
88    }
89
90    public override int GetHashCode() {
91      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
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) {
107      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
108    }
109
110    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
111    public static Interval Subtract(Interval a, Interval b) {
112      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
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) {
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;
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
127    //mkommend: Division by intervals containing 0 is implemented as defined in
128    //http://en.wikipedia.org/wiki/Interval_arithmetic
129    public static Interval Divide(Interval a, Interval b) {
130      if (b.Contains(0.0)) {
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));
133        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
134      }
135      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
136    }
137
138    public static Interval Sine(Interval a) {
139      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
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
145      if (scaled.LowerBound < 0) {
146        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
147        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
148      }
149
150      double scaledLowerBound = scaled.LowerBound % 4.0;
151      double scaledUpperBound = scaled.UpperBound % 4.0;
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) {
173      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
174    }
175    public static Interval Exponential(Interval a) {
176      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
177    }
178
179    public static Interval Power(Interval a, Interval b) {
180      if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
181
182      int bLower = (int)Math.Round(b.LowerBound);
183      int bUpper = (int)Math.Round(b.UpperBound);
184
185      List<double> powerValues = new List<double>();
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));
190
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));
195
196      return new Interval(powerValues.Min(), powerValues.Max());
197    }
198
199    public static Interval Square(Interval a) {
200      return Power(a, new Interval(2, 2));
201    }
202
203    public static Interval Cubic(Interval a) {
204      return Power(a, new Interval(3, 3));
205    }
206
207    public static Interval Root(Interval a, Interval b) {
208      int lower = (int)Math.Round(b.LowerBound);
209      int higher = (int)Math.Round(b.UpperBound);
210
211      return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
212    }
213
214    public static Interval SquareRoot(Interval a) {
215      return Root(a, new Interval(2, 2));
216    }
217
218    public static Interval CubicRoot(Interval a) {
219      return Root(a, new Interval(3, 3));
220    }
221    #endregion
222  }
223}
Note: See TracBrowser for help on using the repository browser.