source: branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs @ 16404

Last change on this file since 16404 was 16404, checked in by chaider, 16 months ago

#2966

  • Variable name changes
  • Changed GetVaribleRanges in DatasetUtil
  • Added method to get interval ranges from a double enumeration in Interval class
  • Added some comments
File size: 8.1 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("The given value set is not defined.");
57      if (!values.Any()) throw new ArgumentException($"No valid 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 this.UpperBound.IsAlmost(other.UpperBound) && this.LowerBound.IsAlmost(other.LowerBound);
79    }
80
81    public override bool Equals(object obj) {
82      return Equals(obj as Interval);
83    }
84
85    public override int GetHashCode() {
86      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
87    }
88
89    public static bool operator ==(Interval interval1, Interval interval2) {
90      if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
91      return interval1.Equals(interval2);
92    }
93    public static bool operator !=(Interval interval1, Interval interval2) {
94      return !(interval1 == interval2);
95    }
96    #endregion
97
98    #region operations
99
100    // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
101    public static Interval Add(Interval a, Interval b) {
102      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
103    }
104
105    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
106    public static Interval Subtract(Interval a, Interval b) {
107      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
108    }
109
110    // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
111    public static Interval Multiply(Interval a, Interval b) {
112      double v1 = a.LowerBound * b.LowerBound;
113      double v2 = a.LowerBound * b.UpperBound;
114      double v3 = a.UpperBound * b.LowerBound;
115      double v4 = a.UpperBound * b.UpperBound;
116
117      double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
118      double max = Math.Max(Math.Min(v1, v2), Math.Max(v3, v4));
119      return new Interval(min, max);
120    }
121
122    //mkommend: Division by intervals containing 0 is implemented as defined in
123    //http://en.wikipedia.org/wiki/Interval_arithmetic
124    public static Interval Divide(Interval a, Interval b) {
125      if (b.Contains(0.0)) {
126        if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
127        else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
128        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
129      }
130      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
131    }
132
133    public static Interval Sine(Interval a) {
134      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
135
136      //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
137      double Pihalf = Math.PI / 2;
138      Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
139      //move to positive scale
140      if (scaled.LowerBound < 0) {
141        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
142        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
143      }
144
145      double scaledLowerBound = scaled.LowerBound % 4.0;
146      double scaledUpperBound = scaled.UpperBound % 4.0;
147      if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
148      List<double> sinValues = new List<double>();
149      sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
150      sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
151
152      int startValue = (int)Math.Ceiling(scaledLowerBound);
153      while (startValue < scaledUpperBound) {
154        sinValues.Add(Math.Sin(startValue * Pihalf));
155        startValue += 1;
156      }
157
158      return new Interval(sinValues.Min(), sinValues.Max());
159    }
160    public static Interval Cosine(Interval a) {
161      return Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
162    }
163    public static Interval Tangens(Interval a) {
164      return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
165    }
166
167    public static Interval Logarithm(Interval a) {
168      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
169    }
170    public static Interval Exponential(Interval a) {
171      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
172    }
173
174    public static Interval Power(Interval a, Interval b) {
175      if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
176
177      int bLower = (int)Math.Round(b.LowerBound);
178      int bUpper = (int)Math.Round(b.UpperBound);
179
180      List<double> powerValues = new List<double>();
181      powerValues.Add(Math.Pow(a.UpperBound, bUpper));
182      powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
183      powerValues.Add(Math.Pow(a.UpperBound, bLower));
184      powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
185
186      powerValues.Add(Math.Pow(a.LowerBound, bUpper));
187      powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
188      powerValues.Add(Math.Pow(a.LowerBound, bLower));
189      powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
190
191      return new Interval(powerValues.Min(), powerValues.Max());
192    }
193
194    public static Interval Square(Interval a) {
195      return Power(a, new Interval(2, 2));
196    }
197
198    public static Interval Cubic(Interval a) {
199      return Power(a, new Interval(3, 3));
200    }
201
202    public static Interval Root(Interval a, Interval b) {
203      int lower = (int)Math.Round(b.LowerBound);
204      int higher = (int)Math.Round(b.UpperBound);
205
206      return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
207    }
208
209    public static Interval SquareRoot(Interval a) {
210      return Root(a, new Interval(2, 2));
211    }
212
213    public static Interval CubicRoot(Interval a) {
214      return Root(a, new Interval(3, 3));
215    }
216    #endregion
217  }
218}
Note: See TracBrowser for help on using the repository browser.