Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16327 was 16327, checked in by mkommend, 5 years ago

#2966: Changed bounds to auto property in Interval and corrected typo in comment.

File size: 7.5 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    #region Equals, GetHashCode, == , !=
56    public bool Equals(Interval other) {
57      if (other == null)
58        return false;
59
60      return this.UpperBound.IsAlmost(other.UpperBound) && this.LowerBound.IsAlmost(other.LowerBound);
61    }
62
63    public override bool Equals(object obj) {
64      return Equals(obj as Interval);
65    }
66
67    public override int GetHashCode() {
68      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
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) {
84      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
85    }
86
87    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
88    public static Interval Subtract(Interval a, Interval b) {
89      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
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) {
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;
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
104    //mkommend: Division by intervals containing 0 is implemented as defined in
105    //http://en.wikipedia.org/wiki/Interval_arithmetic
106    public static Interval Divide(Interval a, Interval b) {
107      if (b.Contains(0.0)) {
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));
110        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
111      }
112      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
113    }
114
115    public static Interval Sine(Interval a) {
116      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
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
122      if (scaled.LowerBound < 0) {
123        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
124        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
125      }
126
127      double scaledLowerBound = scaled.LowerBound % 4.0;
128      double scaledUpperBound = scaled.UpperBound % 4.0;
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) {
150      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
151    }
152    public static Interval Exponential(Interval a) {
153      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
154    }
155
156    public static Interval Power(Interval a, Interval b) {
157      if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
158
159      int bLower = (int)Math.Round(b.LowerBound);
160      int bUpper = (int)Math.Round(b.UpperBound);
161
162      List<double> powerValues = new List<double>();
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));
167
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));
172
173      return new Interval(powerValues.Min(), powerValues.Max());
174    }
175
176    public static Interval Square(Interval a) {
177      return Power(a, new Interval(2, 2));
178    }
179
180    public static Interval Cubic(Interval a) {
181      return Power(a, new Interval(3, 3));
182    }
183
184    public static Interval Root(Interval a, Interval b) {
185      int lower = (int)Math.Round(b.LowerBound);
186      int higher = (int)Math.Round(b.UpperBound);
187
188      return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
189    }
190
191    public static Interval SquareRoot(Interval a) {
192      return Root(a, new Interval(2, 2));
193    }
194
195    public static Interval CubicRoot(Interval a) {
196      return Root(a, new Interval(3, 3));
197    }
198    #endregion
199  }
200}
Note: See TracBrowser for help on using the repository browser.