Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs @ 16548

Last change on this file since 16548 was 16548, checked in by chaider, 5 years ago

#2971

  • Removed Storable from Intervals
  • Changed copy constructor from NamedIntervals class
File size: 8.3 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;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  public class Interval : IEquatable<Interval> {
31    public double LowerBound { get; private set; }
32    public double UpperBound { get; private set; }
33
34
35    public Interval(double lowerBound, double upperBound) {
36      if (lowerBound > upperBound)
37        throw new ArgumentException("LowerBound must be smaller than UpperBound.");
38
39      this.LowerBound = lowerBound;
40      this.UpperBound = upperBound;
41    }
42
43    public bool Contains(double value) {
44      return LowerBound <= value && value <= UpperBound;
45    }
46
47    public override string ToString() {
48      return "Interval: [" + LowerBound + ", " + UpperBound + "]";
49    }
50
51    public bool IsInfiniteOrUndefined {
52      get {
53        return double.IsInfinity(LowerBound) || double.IsInfinity(UpperBound) ||
54                double.IsNaN(LowerBound) || double.IsNaN(UpperBound);
55      }
56    }
57
58    public static Interval GetInterval(IEnumerable<double> values) {
59      if (values == null) throw new ArgumentNullException("values");
60      if (!values.Any()) throw new ArgumentException($"No values are present.");
61
62      var min = double.MaxValue;
63      var max = double.MinValue;
64
65      foreach (var value in values) {
66        //If an value is NaN return an interval [NaN, NaN]
67        if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN);
68
69        if (value < min) min = value;
70        if (value > max) max = value;
71      }
72
73      return new Interval(min, max);
74    }
75
76    #region Equals, GetHashCode, == , !=
77    public bool Equals(Interval other) {
78      if (other == null)
79        return false;
80
81      return (UpperBound.IsAlmost(other.UpperBound) || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
82        && (LowerBound.IsAlmost(other.LowerBound) || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
83    }
84
85    public override bool Equals(object obj) {
86      return Equals(obj as Interval);
87    }
88
89    public override int GetHashCode() {
90      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
91    }
92
93    public static bool operator ==(Interval interval1, Interval interval2) {
94      if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
95      return interval1.Equals(interval2);
96    }
97    public static bool operator !=(Interval interval1, Interval interval2) {
98      return !(interval1 == interval2);
99    }
100    #endregion
101
102    #region operations
103
104    // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
105    public static Interval Add(Interval a, Interval b) {
106      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
107    }
108
109    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
110    public static Interval Subtract(Interval a, Interval b) {
111      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
112    }
113
114    // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
115    public static Interval Multiply(Interval a, Interval b) {
116      double v1 = a.LowerBound * b.LowerBound;
117      double v2 = a.LowerBound * b.UpperBound;
118      double v3 = a.UpperBound * b.LowerBound;
119      double v4 = a.UpperBound * b.UpperBound;
120
121      double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
122      double max = Math.Max(Math.Min(v1, v2), Math.Max(v3, v4));
123      return new Interval(min, max);
124    }
125
126    //mkommend: Division by intervals containing 0 is implemented as defined in
127    //http://en.wikipedia.org/wiki/Interval_arithmetic
128    public static Interval Divide(Interval a, Interval b) {
129      if (b.Contains(0.0)) {
130        if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
131        else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
132        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
133      }
134      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
135    }
136
137    public static Interval Sine(Interval a) {
138      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
139
140      //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
141      double Pihalf = Math.PI / 2;
142      Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
143      //move to positive scale
144      if (scaled.LowerBound < 0) {
145        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
146        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
147      }
148
149      double scaledLowerBound = scaled.LowerBound % 4.0;
150      double scaledUpperBound = scaled.UpperBound % 4.0;
151      if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
152      List<double> sinValues = new List<double>();
153      sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
154      sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
155
156      int startValue = (int)Math.Ceiling(scaledLowerBound);
157      while (startValue < scaledUpperBound) {
158        sinValues.Add(Math.Sin(startValue * Pihalf));
159        startValue += 1;
160      }
161
162      return new Interval(sinValues.Min(), sinValues.Max());
163    }
164    public static Interval Cosine(Interval a) {
165      return Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
166    }
167    public static Interval Tangens(Interval a) {
168      return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
169    }
170
171    public static Interval Logarithm(Interval a) {
172      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
173    }
174    public static Interval Exponential(Interval a) {
175      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
176    }
177
178    public static Interval Power(Interval a, Interval b) {
179      if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
180
181      int bLower = (int)Math.Round(b.LowerBound);
182      int bUpper = (int)Math.Round(b.UpperBound);
183
184      List<double> powerValues = new List<double>();
185      powerValues.Add(Math.Pow(a.UpperBound, bUpper));
186      powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
187      powerValues.Add(Math.Pow(a.UpperBound, bLower));
188      powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
189
190      powerValues.Add(Math.Pow(a.LowerBound, bUpper));
191      powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
192      powerValues.Add(Math.Pow(a.LowerBound, bLower));
193      powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
194
195      return new Interval(powerValues.Min(), powerValues.Max());
196    }
197
198    public static Interval Square(Interval a) {
199      return Power(a, new Interval(2, 2));
200    }
201
202    public static Interval Cubic(Interval a) {
203      return Power(a, new Interval(3, 3));
204    }
205
206    public static Interval Root(Interval a, Interval b) {
207      int lower = (int)Math.Round(b.LowerBound);
208      int higher = (int)Math.Round(b.UpperBound);
209
210      return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
211    }
212
213    public static Interval SquareRoot(Interval a) {
214      return Root(a, new Interval(2, 2));
215    }
216
217    public static Interval CubicRoot(Interval a) {
218      return Root(a, new Interval(3, 3));
219    }
220    #endregion
221  }
222}
Note: See TracBrowser for help on using the repository browser.