Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17542 was 17542, checked in by chaider, 4 years ago

#2971 fixed bounds check in interval cstr

  • Property svn:mergeinfo set to (toggle deleted branches)
    /trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/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
    /stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs10032-10033,​11170,​11173,​12008-12010,​12751-12752,​13316-13317,​13319,​15587
    /trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/Interval.cs16357-17303
File size: 11.3 KB
RevLine 
[16323]1#region License Information
2/* HeuristicLab
[17210]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[16323]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;
[16303]23using System.Collections.Generic;
24using System.Linq;
[17505]25using HEAL.Attic;
[16303]26using HeuristicLab.Common;
27
[16326]28namespace HeuristicLab.Problems.DataAnalysis {
[16640]29  [StorableType("849e42d3-8934-419d-9aff-64ad81c06b67")]
[16548]30  public class Interval : IEquatable<Interval> {
[16640]31    [Storable]
[16327]32    public double LowerBound { get; private set; }
[16640]33    [Storable]
[16327]34    public double UpperBound { get; private set; }
[16303]35
[16640]36    [StorableConstructor]
37    protected Interval(StorableConstructorFlag _) { }
[16548]38
[17368]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>
[16303]47    public Interval(double lowerBound, double upperBound) {
[17368]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
[17542]62      if (lowerBound > upperBound)
63        throw new ArgumentException("lowerBound must be smaller than or equal to upperBound.");
64
[16327]65      this.LowerBound = lowerBound;
66      this.UpperBound = upperBound;
[16303]67    }
68
69    public bool Contains(double value) {
[16327]70      return LowerBound <= value && value <= UpperBound;
[16303]71    }
72
[17370]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;
[16592]78    }
79
[17542]80
81
[16303]82    public override string ToString() {
[16327]83      return "Interval: [" + LowerBound + ", " + UpperBound + "]";
[16303]84    }
85
86    public bool IsInfiniteOrUndefined {
87      get {
88        return double.IsInfinity(LowerBound) || double.IsInfinity(UpperBound) ||
89                double.IsNaN(LowerBound) || double.IsNaN(UpperBound);
90      }
91    }
92
[17368]93    /// <summary>
94    /// True if the interval is positive without zero
95    /// </summary>
96    public bool IsPositive {
97      get => LowerBound > 0.0;
98    }
99
100    /// <summary>
101    /// True if the interval is negative without zero
102    /// </summary>
103    public bool IsNegative {
104      get => UpperBound < 0.0;
105    }
106
[16404]107    public static Interval GetInterval(IEnumerable<double> values) {
[16407]108      if (values == null) throw new ArgumentNullException("values");
109      if (!values.Any()) throw new ArgumentException($"No values are present.");
[16404]110
111      var min = double.MaxValue;
112      var max = double.MinValue;
113
114      foreach (var value in values) {
115        //If an value is NaN return an interval [NaN, NaN]
116        if (double.IsNaN(value)) return new Interval(double.NaN, double.NaN);
117
118        if (value < min) min = value;
119        if (value > max) max = value;
120      }
121
122      return new Interval(min, max);
123    }
124
[16303]125    #region Equals, GetHashCode, == , !=
126    public bool Equals(Interval other) {
127      if (other == null)
128        return false;
129
[16407]130      return (UpperBound.IsAlmost(other.UpperBound) || (double.IsNaN(UpperBound) && double.IsNaN(other.UpperBound)))
131        && (LowerBound.IsAlmost(other.LowerBound) || (double.IsNaN(LowerBound) && double.IsNaN(other.LowerBound)));
[16303]132    }
133
134    public override bool Equals(object obj) {
135      return Equals(obj as Interval);
136    }
137
138    public override int GetHashCode() {
[16327]139      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
[16303]140    }
141
142    public static bool operator ==(Interval interval1, Interval interval2) {
143      if (ReferenceEquals(interval1, null)) return ReferenceEquals(interval2, null);
144      return interval1.Equals(interval2);
145    }
146    public static bool operator !=(Interval interval1, Interval interval2) {
147      return !(interval1 == interval2);
148    }
149    #endregion
150
151    #region operations
152
153    // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
154    public static Interval Add(Interval a, Interval b) {
[16327]155      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
[16303]156    }
157
158    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
159    public static Interval Subtract(Interval a, Interval b) {
[16327]160      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
[16303]161    }
162
163    // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
164    public static Interval Multiply(Interval a, Interval b) {
[16327]165      double v1 = a.LowerBound * b.LowerBound;
166      double v2 = a.LowerBound * b.UpperBound;
167      double v3 = a.UpperBound * b.LowerBound;
168      double v4 = a.UpperBound * b.UpperBound;
[16303]169
170      double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
[16647]171      double max = Math.Max(Math.Max(v1, v2), Math.Max(v3, v4));
[16303]172      return new Interval(min, max);
173    }
174
[17368]175    //Division by intervals containing 0 is implemented as defined in
[16303]176    //http://en.wikipedia.org/wiki/Interval_arithmetic
177    public static Interval Divide(Interval a, Interval b) {
178      if (b.Contains(0.0)) {
[16327]179        if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
180        else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
[16303]181        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
182      }
[16327]183      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
[16303]184    }
185
186    public static Interval Sine(Interval a) {
[16327]187      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
[16303]188
189      //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
190      double Pihalf = Math.PI / 2;
191      Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
192      //move to positive scale
[16327]193      if (scaled.LowerBound < 0) {
194        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
[16303]195        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
196      }
197
[16327]198      double scaledLowerBound = scaled.LowerBound % 4.0;
199      double scaledUpperBound = scaled.UpperBound % 4.0;
[16303]200      if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
201      List<double> sinValues = new List<double>();
202      sinValues.Add(Math.Sin(scaledLowerBound * Pihalf));
203      sinValues.Add(Math.Sin(scaledUpperBound * Pihalf));
204
205      int startValue = (int)Math.Ceiling(scaledLowerBound);
206      while (startValue < scaledUpperBound) {
207        sinValues.Add(Math.Sin(startValue * Pihalf));
208        startValue += 1;
209      }
210
211      return new Interval(sinValues.Min(), sinValues.Max());
212    }
213    public static Interval Cosine(Interval a) {
[16775]214      return Interval.Sine(Interval.Add(a, new Interval(Math.PI / 2, Math.PI / 2)));
[16303]215    }
216    public static Interval Tangens(Interval a) {
217      return Interval.Divide(Interval.Sine(a), Interval.Cosine(a));
[17368]218    }
[16775]219    public static Interval HyperbolicTangent(Interval a) {
220      return new Interval(Math.Tanh(a.LowerBound), Math.Tanh(a.UpperBound));
[16303]221    }
222
223    public static Interval Logarithm(Interval a) {
[16327]224      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
[16303]225    }
226    public static Interval Exponential(Interval a) {
[16327]227      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
[16303]228    }
229
230    public static Interval Power(Interval a, Interval b) {
[16327]231      if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
[16303]232
[16327]233      int bLower = (int)Math.Round(b.LowerBound);
234      int bUpper = (int)Math.Round(b.UpperBound);
[16303]235
236      List<double> powerValues = new List<double>();
[16327]237      powerValues.Add(Math.Pow(a.UpperBound, bUpper));
238      powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
239      powerValues.Add(Math.Pow(a.UpperBound, bLower));
240      powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
[16303]241
[16327]242      powerValues.Add(Math.Pow(a.LowerBound, bUpper));
243      powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
244      powerValues.Add(Math.Pow(a.LowerBound, bLower));
245      powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
[16303]246
247      return new Interval(powerValues.Min(), powerValues.Max());
248    }
249
[16323]250    public static Interval Square(Interval a) {
[16640]251      if (a.UpperBound <= 0) return new Interval(a.UpperBound * a.UpperBound, a.LowerBound * a.LowerBound);     // interval is negative
252      else if (a.LowerBound >= 0) return new Interval(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound); // interval is positive
[17368]253      else return new Interval(0, Math.Max(a.LowerBound * a.LowerBound, a.UpperBound * a.UpperBound)); // interval goes over zero
[16323]254    }
[16303]255
[16640]256    public static Interval Cube(Interval a) {
257      return new Interval(Math.Pow(a.LowerBound, 3), Math.Pow(a.UpperBound, 3));
[16323]258    }
259
[16303]260    public static Interval Root(Interval a, Interval b) {
[16327]261      int lower = (int)Math.Round(b.LowerBound);
262      int higher = (int)Math.Round(b.UpperBound);
[16303]263
[16327]264      return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
[16303]265    }
266
[16323]267    public static Interval SquareRoot(Interval a) {
[16640]268      if (a.LowerBound < 0) return new Interval(double.NaN, double.NaN);
269      return new Interval(Math.Sqrt(a.LowerBound), Math.Sqrt(a.UpperBound));
[16323]270    }
[16303]271
[16323]272    public static Interval CubicRoot(Interval a) {
[17300]273      var lower = (a.LowerBound < 0) ? -Math.Pow(-a.LowerBound, 1d / 3d) : Math.Pow(a.LowerBound, 1d / 3d);
274      var upper = (a.UpperBound < 0) ? -Math.Pow(-a.UpperBound, 1d / 3d) : Math.Pow(a.UpperBound, 1d / 3d);
275
276      return new Interval(lower, upper);
[16303]277    }
[17313]278
279    public static Interval Absolute(Interval a) {
280      var absLower = Math.Abs(a.LowerBound);
281      var absUpper = Math.Abs(a.UpperBound);
[17368]282      var min = Math.Min(absLower, absUpper);
283      var max = Math.Max(absLower, absUpper);
284
285      if (a.Contains(0.0)) {
286        min = 0.0;
[17313]287    }
[17368]288
289      return new Interval(min, max);
290    }
291
292    public static Interval AnalyticalQuotient(Interval a, Interval b) {
293      var dividend = a;
294      var divisor = Add(Square(b), new Interval(1.0, 1.0));
295      divisor = SquareRoot(divisor);
296
297      var quotient = Divide(dividend, divisor);
298      return quotient;
299    }
[16303]300    #endregion
301  }
302}
Note: See TracBrowser for help on using the repository browser.