Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2971

  • Added ContainsInterval method in Interval
  • Some renaming
  • Added ConstraintAnalyzer to SymbolicRegressionProblem
  • Added Counter for constraint violations in analyzer
File size: 9.9 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 bool Contains(Interval other, bool lowerBoundInclusive = true, bool upperBoundInclusive = false) {
48      if (double.IsNegativeInfinity(this.LowerBound) && double.IsPositiveInfinity(this.UpperBound))
49        return true;
50      //Left-unbounded and right-bounded:
51      if (double.IsNegativeInfinity(this.LowerBound)) {
52        if (upperBoundInclusive)
53          return other.LowerBound <= this.UpperBound && other.UpperBound <= this.UpperBound;
54        return other.LowerBound < this.UpperBound && other.UpperBound < this.UpperBound;
55      }
56
57      //Left-bounded and right-unbounded:
58      if (double.IsPositiveInfinity(this.UpperBound)) {
59        if (lowerBoundInclusive)
60          return other.LowerBound >= this.LowerBound && other.UpperBound >= this.LowerBound;
61        return other.LowerBound > this.LowerBound && other.UpperBound > this.LowerBound;
62      }
63
64      //Proper and bounded:
65      //Closed:
66      if (lowerBoundInclusive && upperBoundInclusive) {
67        return this.LowerBound <= other.LowerBound && other.UpperBound <= this.UpperBound;
68      }
69
70      //Open:
71      if (!lowerBoundInclusive && !upperBoundInclusive) {
72        return this.LowerBound < other.LowerBound && other.UpperBound < this.UpperBound;
73      }
74
75      //Left-closed, right-open:
76      if (lowerBoundInclusive) {
77        return this.LowerBound <= other.LowerBound && other.UpperBound < this.UpperBound;
78      }
79
80      //Left-open, right-closed:
81      return this.LowerBound < other.LowerBound && other.UpperBound <= this.UpperBound;
82    }
83
84    public override string ToString() {
85      return "Interval: [" + LowerBound + ", " + UpperBound + "]";
86    }
87
88    public bool IsInfiniteOrUndefined {
89      get {
90        return double.IsInfinity(LowerBound) || double.IsInfinity(UpperBound) ||
91                double.IsNaN(LowerBound) || double.IsNaN(UpperBound);
92      }
93    }
94
95    public static Interval GetInterval(IEnumerable<double> values) {
96      if (values == null) throw new ArgumentNullException("values");
97      if (!values.Any()) throw new ArgumentException($"No values are present.");
98
99      var min = double.MaxValue;
100      var max = double.MinValue;
101
102      foreach (var value in values) {
103        //If an value is NaN return an interval [NaN, NaN]
104        if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN);
105
106        if (value < min) min = value;
107        if (value > max) max = value;
108      }
109
110      return new Interval(min, max);
111    }
112
113    #region Equals, GetHashCode, == , !=
114    public bool Equals(Interval other) {
115      if (other == null)
116        return false;
117
118      return (UpperBound.IsAlmost(other.UpperBound) || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
119        && (LowerBound.IsAlmost(other.LowerBound) || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
120    }
121
122    public override bool Equals(object obj) {
123      return Equals(obj as Interval);
124    }
125
126    public override int GetHashCode() {
127      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
128    }
129
130    public static bool operator ==(Interval interval1, Interval interval2) {
131      if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
132      return interval1.Equals(interval2);
133    }
134    public static bool operator !=(Interval interval1, Interval interval2) {
135      return !(interval1 == interval2);
136    }
137    #endregion
138
139    #region operations
140
141    // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
142    public static Interval Add(Interval a, Interval b) {
143      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
144    }
145
146    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
147    public static Interval Subtract(Interval a, Interval b) {
148      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
149    }
150
151    // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
152    public static Interval Multiply(Interval a, Interval b) {
153      double v1 = a.LowerBound * b.LowerBound;
154      double v2 = a.LowerBound * b.UpperBound;
155      double v3 = a.UpperBound * b.LowerBound;
156      double v4 = a.UpperBound * b.UpperBound;
157
158      double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
159      double max = Math.Max(Math.Min(v1, v2), Math.Max(v3, v4));
160      return new Interval(min, max);
161    }
162
163    //mkommend: Division by intervals containing 0 is implemented as defined in
164    //http://en.wikipedia.org/wiki/Interval_arithmetic
165    public static Interval Divide(Interval a, Interval b) {
166      if (b.Contains(0.0)) {
167        if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
168        else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
169        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
170      }
171      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
172    }
173
174    public static Interval Sine(Interval a) {
175      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
176
177      //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
178      double Pihalf = Math.PI / 2;
179      Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
180      //move to positive scale
181      if (scaled.LowerBound < 0) {
182        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
183        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
184      }
185
186      double scaledLowerBound = scaled.LowerBound % 4.0;
187      double scaledUpperBound = scaled.UpperBound % 4.0;
188      if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
189      List<double> sinValues = new List<double>();
190      sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
191      sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
192
193      int startValue = (int)Math.Ceiling(scaledLowerBound);
194      while (startValue < scaledUpperBound) {
195        sinValues.Add(Math.Sin(startValue * Pihalf));
196        startValue += 1;
197      }
198
199      return new Interval(sinValues.Min(), sinValues.Max());
200    }
201    public static Interval Cosine(Interval a) {
202      return Interval.Sine(Interval.Subtract(a, new Interval(Math.PI / 2, Math.PI / 2)));
203    }
204    public static Interval Tangens(Interval a) {
205      return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
206    }
207
208    public static Interval Logarithm(Interval a) {
209      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
210    }
211    public static Interval Exponential(Interval a) {
212      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
213    }
214
215    public static Interval Power(Interval a, Interval b) {
216      if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
217
218      int bLower = (int)Math.Round(b.LowerBound);
219      int bUpper = (int)Math.Round(b.UpperBound);
220
221      List<double> powerValues = new List<double>();
222      powerValues.Add(Math.Pow(a.UpperBound, bUpper));
223      powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
224      powerValues.Add(Math.Pow(a.UpperBound, bLower));
225      powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
226
227      powerValues.Add(Math.Pow(a.LowerBound, bUpper));
228      powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
229      powerValues.Add(Math.Pow(a.LowerBound, bLower));
230      powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
231
232      return new Interval(powerValues.Min(), powerValues.Max());
233    }
234
235    public static Interval Square(Interval a) {
236      return Power(a, new Interval(2, 2));
237    }
238
239    public static Interval Cubic(Interval a) {
240      return Power(a, new Interval(3, 3));
241    }
242
243    public static Interval Root(Interval a, Interval b) {
244      int lower = (int)Math.Round(b.LowerBound);
245      int higher = (int)Math.Round(b.UpperBound);
246
247      return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
248    }
249
250    public static Interval SquareRoot(Interval a) {
251      return Root(a, new Interval(2, 2));
252    }
253
254    public static Interval CubicRoot(Interval a) {
255      return Root(a, new Interval(3, 3));
256    }
257    #endregion
258  }
259}
Note: See TracBrowser for help on using the repository browser.