Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs @ 17945

Last change on this file since 17945 was 17803, checked in by mkommend, 4 years ago

#2971: Merged r17579, r17580, r17583, r17584, r17754 into stable.

  • Property svn:mergeinfo set to (toggle deleted branches)
    /branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.csmergedeligible
    /stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.csmergedeligible
    /trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.csmergedeligible
    /branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs16683-17036
    /branches/2520_PersistenceReintegration/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs16451-16564
    /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs15761-16116
    /branches/2892_LR-prediction-intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs15743-16388
    /branches/2904_CalculateImpacts/3.4/Implementation/Interval/Interval.cs15808-16421
    /branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs15943-16355
    /branches/2947_ConfigurableIndexedDataTable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs16148-16526
    /branches/2965_CancelablePersistence/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs16321-16439
    /branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs16320-16406
    /branches/Async/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs13329-15286
    /branches/Classification-Extensions/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs11606-11761
    /branches/ClassificationModelComparison/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs9073-13099
    /branches/CloningRefactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs4656-4721
    /branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs5471-5808
    /branches/DataAnalysis SolutionEnsembles/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs5815-6180
    /branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs4220,​4226,​4236-4238,​4389,​4458-4459,​4462,​4464
    /branches/DataAnalysisCSVImport/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs8713-8875
    /branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs10085-11101
    /branches/DatasetFeatureCorrelation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs8035-8538
    /branches/GP.Grammar.Editor/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs6284-6795
    /branches/GP.Symbols (TimeLag, Diff, Integral)/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs5060
    /branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs11570-12508
    /branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs11130-12721
    /branches/HeuristicLab.RegressionSolutionGradientView/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs13819-14091
    /branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs7098-8789
    /branches/LogResidualEvaluator/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs10202-10483
    /branches/NET40/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs5138-5162
    /branches/ParallelEngine/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs5175-5192
    /branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs7570-7810
    /branches/QAPAlgorithms/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs6350-6627
    /branches/Restructure trunk solution/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs6828
    /branches/SensitivityEvaluator/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs12212-16784
    /branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs15318-15370
    /branches/SpectralKernelForGaussianProcesses/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs10204-10479
    /branches/Trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs6829-6865
    /branches/histogram/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs5959-6341
    /branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs14232-14825
    /trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs17301-17367
    /trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs9498,​9552,​9762,​9973-9975,​9994,​10406,​10480,​10484,​10486,​10540-10541,​10543,​10545,​11031,​11114,​11116,​11156,​11330,​11332,​11417,​11422,​11623,​11631,​11634,​11762-11764,​11766,​12067,​12485,​12492,​12504,​12506,​12509-12512,​12524,​12578,​12581,​12612,​12622,​12641,​12649,​12664,​12722,​12770,​12772,​12790-12792,​12796,​12798,​12801,​12811-12812,​12816-12817,​12836-12837,​12851,​12907,​12971,​13001,​13027,​13038,​13040,​13100-13104,​13154,​13268,​13395,​13406,​13419,​13427-13430,​13434,​13440-13442,​13445-13447,​13450,​13474,​13501,​13503,​13511,​13513,​13525-13526,​13529,​13534-13535,​13539-13540,​13550,​13552,​13584-13585,​13593,​13645,​13648,​13650-13652,​13654,​13657-13659,​13661-13662,​13666,​13669,​13682-13684,​13690-13693,​13697-13698,​13700-13702,​13704-13705,​13708-13709,​13711,​13715,​13724,​13746,​13760-13761,​13766,​13785-13786,​13801,​13826,​13901,​13921-13922,​13925,​13938,​13941-13942,​13985-13987,​13992-13993,​14000-14001,​14015-14016,​14095-14096,​14098-14099,​14107,​14118-14119,​14131,​14135,​14140,​14142,​14157-14158,​14160,​14226,​14228-14230,​14234-14236,​14244-14247,​14250,​14255-14258,​14260,​14267,​14271-14272,​14282,​14284-14298,​14300,​14307,​14314-14316,​14319,​14322,​14332,​14343-14350,​14358,​14367-14368,​14372,​14376,​14378,​14381-14382,​14384,​14388,​14390-14391,​14393-14394,​14396,​14400,​14405,​14407-14408,​14418,​14422-14423,​14425,​14434,​14463-14465,​14468-14469,​14479,​14483,​14486,​14507,​14517,​14523,​14527,​14529,​14531-14533,​14553,​14623,​14630,​14781,​14789-14791,​14805,​14826-14827,​14829-14832,​14839-14840,​14843,​14845-14847,​14851-14854,​14857,​14864-14865,​14871,​14889-14890,​14899,​14904,​14918,​14938,​14940,​14943-14946,​14948-14951,​15002,​15013,​15015-15016,​15023-15024,​15026,​15046,​15052-15054,​15058,​15077,​15085,​15088,​15094,​15103-15106,​15111-15113,​15122-15124,​15129,​15139,​15160,​15163,​15165,​15184-15185,​15187,​15194,​15287,​15371-15372,​15390,​15396,​15400,​15402
File size: 10.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Problems.DataAnalysis {
29  [StorableType("849e42d3-8934-419d-9aff-64ad81c06b67")]
30  public class Interval : IEquatable<Interval> {
31    [Storable]
32    public double LowerBound { get; private set; }
33    [Storable]
34    public double UpperBound { get; private set; }
35
36    [StorableConstructor]
37    protected Interval(StorableConstructorFlag _) { }
38
39    /// <summary>
40    /// Creates an interval with given bounds, where lower bound must be smaller than
41    /// the upper bound. Floating point precision errors trough calculations are fixed by,
42    /// checking if the intervals are almost the same (E-12). If this is the case, the bounds
43    /// will be set to the bound closer to zero.
44    /// </summary>
45    /// <param name="lowerBound">Lower bound of the interval</param>
46    /// <param name="upperBound">Upper bound of the interval</param>
47    public Interval(double lowerBound, double upperBound) {
48      if (lowerBound.IsAlmost(upperBound)) {
49        //If the bounds go over zero
50        if (lowerBound <= 0 && upperBound >= 0) {
51          lowerBound = 0.0;
52          upperBound = 0.0;
53          //Interval is negative
54        } else if (upperBound < 0) {
55          lowerBound = upperBound;
56          //Interval is positive
57        } else {
58          upperBound = lowerBound;
59        }
60      }
61
62      if (lowerBound > upperBound)
63        throw new ArgumentException("lowerBound must be smaller than or equal to upperBound.");
64
65      this.LowerBound = lowerBound;
66      this.UpperBound = upperBound;
67    }
68
69    public bool Contains(double value) {
70      return LowerBound <= value && value <= UpperBound;
71    }
72
73    public bool Contains(Interval other) {
74      if (double.IsNegativeInfinity(LowerBound) && double.IsPositiveInfinity(UpperBound)) return true;
75      if (other.LowerBound >= LowerBound && other.UpperBound <= UpperBound) return true;
76 
77      return false;
78    }
79
80    public override string ToString() {
81      return "Interval: [" + LowerBound + ", " + UpperBound + "]";
82    }
83
84    public bool IsInfiniteOrUndefined {
85      get {
86        return double.IsInfinity(LowerBound) || double.IsInfinity(UpperBound) ||
87                double.IsNaN(LowerBound) || double.IsNaN(UpperBound);
88      }
89    }
90
91    /// <summary>
92    /// True if the interval is positive without zero
93    /// </summary>
94    public bool IsPositive {
95      get => LowerBound > 0.0;
96    }
97
98    /// <summary>
99    /// True if the interval is negative without zero
100    /// </summary>
101    public bool IsNegative {
102      get => UpperBound < 0.0;
103    }
104
105    public static Interval GetInterval(IEnumerable<double> values) {
106      if (values == null) throw new ArgumentNullException("values");
107      if (!values.Any()) throw new ArgumentException($"No values are present.");
108
109      var min = double.MaxValue;
110      var max = double.MinValue;
111
112      foreach (var value in values) {
113        //If an value is NaN return an interval [NaN, NaN]
114        if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN);
115
116        if (value < min) min = value;
117        if (value > max) max = value;
118      }
119
120      return new Interval(min, max);
121    }
122
123    #region Equals, GetHashCode, == , !=
124    public bool Equals(Interval other) {
125      if (other == null)
126        return false;
127
128      return (UpperBound.IsAlmost(other.UpperBound) || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
129        && (LowerBound.IsAlmost(other.LowerBound) || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
130    }
131
132    public override bool Equals(object obj) {
133      return Equals(obj as Interval);
134    }
135
136    public override int GetHashCode() {
137      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
138    }
139
140    public static bool operator ==(Interval interval1, Interval interval2) {
141      if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
142      return interval1.Equals(interval2);
143    }
144    public static bool operator !=(Interval interval1, Interval interval2) {
145      return !(interval1 == interval2);
146    }
147    #endregion
148
149    #region operations
150
151    // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
152    public static Interval Add(Interval a, Interval b) {
153      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
154    }
155
156    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
157    public static Interval Subtract(Interval a, Interval b) {
158      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
159    }
160
161    // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
162    public static Interval Multiply(Interval a, Interval b) {
163      double v1 = a.LowerBound * b.LowerBound;
164      double v2 = a.LowerBound * b.UpperBound;
165      double v3 = a.UpperBound * b.LowerBound;
166      double v4 = a.UpperBound * b.UpperBound;
167
168      double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
169      double max = Math.Max(Math.Max(v1, v2), Math.Max(v3, v4));
170      return new Interval(min, max);
171    }
172
173    //Division by intervals containing 0 is implemented as defined in
174    //http://en.wikipedia.org/wiki/Interval_arithmetic
175    public static Interval Divide(Interval a, Interval b) {
176      if (b.Contains(0.0)) {
177        if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
178        else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
179        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
180      }
181      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
182    }
183
184    public static Interval Sine(Interval a) {
185      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
186
187      //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
188      double Pihalf = Math.PI / 2;
189      Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
190      //move to positive scale
191      if (scaled.LowerBound < 0) {
192        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
193        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
194      }
195
196      double scaledLowerBound = scaled.LowerBound % 4.0;
197      double scaledUpperBound = scaled.UpperBound % 4.0;
198      if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
199      List<double> sinValues = new List<double>();
200      sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
201      sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
202
203      int startValue = (int)Math.Ceiling(scaledLowerBound);
204      while (startValue < scaledUpperBound) {
205        sinValues.Add(Math.Sin(startValue * Pihalf));
206        startValue += 1;
207      }
208
209      return new Interval(sinValues.Min(), sinValues.Max());
210    }
211    public static Interval Cosine(Interval a) {
212      return Interval.Sine(Interval.Add(a, new Interval(Math.PI / 2, Math.PI / 2)));
213    }
214    public static Interval Tangens(Interval a) {
215      return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
216    }
217    public static Interval HyperbolicTangent(Interval a) {
218      return new Interval(Math.Tanh(a.LowerBound), Math.Tanh(a.UpperBound));
219    }
220
221    public static Interval Logarithm(Interval a) {
222      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
223    }
224    public static Interval Exponential(Interval a) {
225      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
226    }
227
228    public static Interval Square(Interval a) {
229      if (a.UpperBound <= 0) return new Interval(a.UpperBound * a.UpperBound, a.LowerBound * a.LowerBound);     // interval is negative
230      else if (a.LowerBound >= 0) return new Interval(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound); // interval is positive
231      else return new Interval(0, Math.Max(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound)); // interval goes over zero
232    }
233
234    public static Interval Cube(Interval a) {
235      return new Interval(Math.Pow(a.LowerBound, 3), Math.Pow(a.UpperBound, 3));
236    }
237
238    /// <summary>
239    /// The interval contains both possible results of the calculated square root +-sqrt(x). That results in a wider
240    /// interval, but it contains all possible solutions.
241    /// </summary>
242    /// <param name="a">Interval to build square root from.</param>
243    /// <returns></returns>
244    public static Interval SquareRoot(Interval a) {
245      if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN);
246      return new Interval(-Math.Sqrt(a.UpperBound), Math.Sqrt(a.UpperBound));
247    }
248
249    public static Interval CubicRoot(Interval a) {
250      var lower = (a.LowerBound < 0) ? -Math.Pow(-a.LowerBound, 1d / 3d) : Math.Pow(a.LowerBound, 1d / 3d);
251      var upper = (a.UpperBound < 0) ? -Math.Pow(-a.UpperBound, 1d / 3d) : Math.Pow(a.UpperBound, 1d / 3d);
252
253      return new Interval(lower, upper);
254    }
255
256    public static Interval Absolute(Interval a) {
257      var absLower = Math.Abs(a.LowerBound);
258      var absUpper = Math.Abs(a.UpperBound);
259      var min = Math.Min(absLower, absUpper);
260      var max = Math.Max(absLower, absUpper);
261
262      if (a.Contains(0.0)) {
263        min = 0.0;
264      }
265
266      return new Interval(min, max);
267    }
268
269    public static Interval AnalyticalQuotient(Interval a, Interval b) {
270      var dividend = a;
271      var divisor = Add(Square(b), new Interval(1.0, 1.0));
272      divisor = SquareRoot(divisor);
273
274      var quotient = Divide(dividend, divisor);
275      return quotient;
276    }
277    #endregion
278  }
279}
Note: See TracBrowser for help on using the repository browser.