Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs @ 17054

Last change on this file since 17054 was 16436, checked in by mkommend, 6 years ago

#2966: Merged 16407 into stable.

File size: 8.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Problems.DataAnalysis {
28  public class Interval : IEquatable<Interval> {
29    public double LowerBound { get; private set; }
30    public double UpperBound { get; private set; }
31
32    public Interval(double lowerBound, double upperBound) {
33      if (lowerBound > upperBound)
34        throw new ArgumentException("LowerBound must be smaller than UpperBound.");
35
36      this.LowerBound = lowerBound;
37      this.UpperBound = upperBound;
38    }
39
40    public bool Contains(double value) {
41      return LowerBound <= value && value <= UpperBound;
42    }
43
44    public override string ToString() {
45      return "Interval: [" + LowerBound + ", " + UpperBound + "]";
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    public static Interval GetInterval(IEnumerable<double> values) {
56      if (values == null) throw new ArgumentNullException("values");
57      if (!values.Any()) throw new ArgumentException($"No values are present.");
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
73    #region Equals, GetHashCode, == , !=
74    public bool Equals(Interval other) {
75      if (other == null)
76        return false;
77
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)));
80    }
81
82    public override bool Equals(object obj) {
83      return Equals(obj as Interval);
84    }
85
86    public override int GetHashCode() {
87      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
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) {
103      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
104    }
105
106    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
107    public static Interval Subtract(Interval a, Interval b) {
108      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
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) {
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;
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
123    //mkommend: Division by intervals containing 0 is implemented as defined in
124    //http://en.wikipedia.org/wiki/Interval_arithmetic
125    public static Interval Divide(Interval a, Interval b) {
126      if (b.Contains(0.0)) {
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));
129        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
130      }
131      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
132    }
133
134    public static Interval Sine(Interval a) {
135      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
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
141      if (scaled.LowerBound < 0) {
142        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
143        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
144      }
145
146      double scaledLowerBound = scaled.LowerBound % 4.0;
147      double scaledUpperBound = scaled.UpperBound % 4.0;
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) {
169      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
170    }
171    public static Interval Exponential(Interval a) {
172      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
173    }
174
175    public static Interval Power(Interval a, Interval b) {
176      if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
177
178      int bLower = (int)Math.Round(b.LowerBound);
179      int bUpper = (int)Math.Round(b.UpperBound);
180
181      List<double> powerValues = new List<double>();
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));
186
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));
191
192      return new Interval(powerValues.Min(), powerValues.Max());
193    }
194
195    public static Interval Square(Interval a) {
196      return Power(a, new Interval(2, 2));
197    }
198
199    public static Interval Cubic(Interval a) {
200      return Power(a, new Interval(3, 3));
201    }
202
203    public static Interval Root(Interval a, Interval b) {
204      int lower = (int)Math.Round(b.LowerBound);
205      int higher = (int)Math.Round(b.UpperBound);
206
207      return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
208    }
209
210    public static Interval SquareRoot(Interval a) {
211      return Root(a, new Interval(2, 2));
212    }
213
214    public static Interval CubicRoot(Interval a) {
215      return Root(a, new Interval(3, 3));
216    }
217    #endregion
218  }
219}
Note: See TracBrowser for help on using the repository browser.