Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971 Added serialization to Interval class

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