Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/18 17:06:50 (6 years ago)
Author:
mkommend
Message:

#2966: Changed bounds to auto property in Interval and corrected typo in comment.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2966_interval_calculation/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs

    r16326 r16327  
    2727namespace HeuristicLab.Problems.DataAnalysis {
    2828  public class Interval : IEquatable<Interval> {
    29     private double lowerBound;
    30     public double LowerBound {
    31       get { return lowerBound; }
    32       private set { lowerBound = value; }
    33     }
    34     private double upperBound;
    35     public double UpperBound {
    36       get { return upperBound; }
    37       private set { lowerBound = value; }
    38     }
     29    public double LowerBound { get; private set; }
     30    public double UpperBound { get; private set; }
    3931
    4032    public Interval(double lowerBound, double upperBound) {
     
    4234        throw new ArgumentException("LowerBound must be smaller than UpperBound.");
    4335
    44       this.lowerBound = lowerBound;
    45       this.upperBound = upperBound;
     36      this.LowerBound = lowerBound;
     37      this.UpperBound = upperBound;
    4638    }
    4739
    4840    public bool Contains(double value) {
    49       return lowerBound <= value && value <= upperBound;
     41      return LowerBound <= value && value <= UpperBound;
    5042    }
    5143
    5244    public override string ToString() {
    53       return "Interval: [" + lowerBound + ", " + upperBound + "]";
     45      return "Interval: [" + LowerBound + ", " + UpperBound + "]";
    5446    }
    5547
     
    6658        return false;
    6759
    68       return this.upperBound.IsAlmost(other.upperBound) && this.lowerBound.IsAlmost(other.lowerBound);
     60      return this.UpperBound.IsAlmost(other.UpperBound) && this.LowerBound.IsAlmost(other.LowerBound);
    6961    }
    7062
     
    7466
    7567    public override int GetHashCode() {
    76       return lowerBound.GetHashCode() ^ upperBound.GetHashCode();
     68      return LowerBound.GetHashCode() ^ UpperBound.GetHashCode();
    7769    }
    7870
     
    9082    // [x1,x2] + [y1,y2] = [x1 + y1,x2 + y2]
    9183    public static Interval Add(Interval a, Interval b) {
    92       return new Interval(a.lowerBound + b.lowerBound, a.upperBound + b.upperBound);
     84      return new Interval(a.LowerBound + b.LowerBound, a.UpperBound + b.UpperBound);
    9385    }
    9486
    9587    // [x1,x2] − [y1,y2] = [x1 − y2,x2 − y1]
    9688    public static Interval Subtract(Interval a, Interval b) {
    97       return new Interval(a.lowerBound - b.upperBound, a.upperBound - b.lowerBound);
     89      return new Interval(a.LowerBound - b.UpperBound, a.UpperBound - b.LowerBound);
    9890    }
    9991
    10092    // [x1,x2] * [y1,y2] = [min(x1*y1,x1*y2,x2*y1,x2*y2),max(x1*y1,x1*y2,x2*y1,x2*y2)]
    10193    public static Interval Multiply(Interval a, Interval b) {
    102       double v1 = a.lowerBound * b.lowerBound;
    103       double v2 = a.lowerBound * b.upperBound;
    104       double v3 = a.upperBound * b.lowerBound;
    105       double v4 = a.upperBound * b.upperBound;
     94      double v1 = a.LowerBound * b.LowerBound;
     95      double v2 = a.LowerBound * b.UpperBound;
     96      double v3 = a.UpperBound * b.LowerBound;
     97      double v4 = a.UpperBound * b.UpperBound;
    10698
    10799      double min = Math.Min(Math.Min(v1, v2), Math.Min(v3, v4));
     
    110102    }
    111103
    112     //mkommend: Divisiont by intervals containing 0 is implemented as defined in
     104    //mkommend: Division by intervals containing 0 is implemented as defined in
    113105    //http://en.wikipedia.org/wiki/Interval_arithmetic
    114106    public static Interval Divide(Interval a, Interval b) {
    115107      if (b.Contains(0.0)) {
    116         if (b.lowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.upperBound, double.PositiveInfinity));
    117         else if (b.upperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.lowerBound));
     108        if (b.LowerBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, double.PositiveInfinity));
     109        else if (b.UpperBound.IsAlmost(0.0)) return Interval.Multiply(a, new Interval(double.NegativeInfinity, 1.0 / b.LowerBound));
    118110        else return new Interval(double.NegativeInfinity, double.PositiveInfinity);
    119111      }
    120       return Interval.Multiply(a, new Interval(1.0 / b.upperBound, 1.0 / b.lowerBound));
     112      return Interval.Multiply(a, new Interval(1.0 / b.UpperBound, 1.0 / b.LowerBound));
    121113    }
    122114
    123115    public static Interval Sine(Interval a) {
    124       if (Math.Abs(a.upperBound - a.lowerBound) >= Math.PI * 2) return new Interval(-1, 1);
     116      if (Math.Abs(a.UpperBound - a.LowerBound) >= Math.PI * 2) return new Interval(-1, 1);
    125117
    126118      //divide the interval by PI/2 so that the optima lie at x element of N (0,1,2,3,4,...)
     
    128120      Interval scaled = Interval.Divide(a, new Interval(Pihalf, Pihalf));
    129121      //move to positive scale
    130       if (scaled.lowerBound < 0) {
    131         int periodsToMove = Math.Abs((int)scaled.lowerBound / 4) + 1;
     122      if (scaled.LowerBound < 0) {
     123        int periodsToMove = Math.Abs((int)scaled.LowerBound / 4) + 1;
    132124        scaled = Interval.Add(scaled, new Interval(periodsToMove * 4, periodsToMove * 4));
    133125      }
    134126
    135       double scaledLowerBound = scaled.lowerBound % 4.0;
    136       double scaledUpperBound = scaled.upperBound % 4.0;
     127      double scaledLowerBound = scaled.LowerBound % 4.0;
     128      double scaledUpperBound = scaled.UpperBound % 4.0;
    137129      if (scaledUpperBound < scaledLowerBound) scaledUpperBound += 4.0;
    138130      List<double> sinValues = new List<double>();
     
    156148
    157149    public static Interval Logarithm(Interval a) {
    158       return new Interval(Math.Log(a.lowerBound), Math.Log(a.upperBound));
     150      return new Interval(Math.Log(a.LowerBound), Math.Log(a.UpperBound));
    159151    }
    160152    public static Interval Exponential(Interval a) {
    161       return new Interval(Math.Exp(a.lowerBound), Math.Exp(a.upperBound));
     153      return new Interval(Math.Exp(a.LowerBound), Math.Exp(a.UpperBound));
    162154    }
    163155
    164156    public static Interval Power(Interval a, Interval b) {
    165       if (a.Contains(0.0) && b.lowerBound < 0) return new Interval(double.NaN, double.NaN);
    166 
    167       int bLower = (int)Math.Round(b.lowerBound);
    168       int bUpper = (int)Math.Round(b.upperBound);
     157      if (a.Contains(0.0) && b.LowerBound < 0) return new Interval(double.NaN, double.NaN);
     158
     159      int bLower = (int)Math.Round(b.LowerBound);
     160      int bUpper = (int)Math.Round(b.UpperBound);
    169161
    170162      List<double> powerValues = new List<double>();
    171       powerValues.Add(Math.Pow(a.upperBound, bUpper));
    172       powerValues.Add(Math.Pow(a.upperBound, bUpper - 1));
    173       powerValues.Add(Math.Pow(a.upperBound, bLower));
    174       powerValues.Add(Math.Pow(a.upperBound, bLower + 1));
    175 
    176       powerValues.Add(Math.Pow(a.lowerBound, bUpper));
    177       powerValues.Add(Math.Pow(a.lowerBound, bUpper - 1));
    178       powerValues.Add(Math.Pow(a.lowerBound, bLower));
    179       powerValues.Add(Math.Pow(a.lowerBound, bLower + 1));
     163      powerValues.Add(Math.Pow(a.UpperBound, bUpper));
     164      powerValues.Add(Math.Pow(a.UpperBound, bUpper - 1));
     165      powerValues.Add(Math.Pow(a.UpperBound, bLower));
     166      powerValues.Add(Math.Pow(a.UpperBound, bLower + 1));
     167
     168      powerValues.Add(Math.Pow(a.LowerBound, bUpper));
     169      powerValues.Add(Math.Pow(a.LowerBound, bUpper - 1));
     170      powerValues.Add(Math.Pow(a.LowerBound, bLower));
     171      powerValues.Add(Math.Pow(a.LowerBound, bLower + 1));
    180172
    181173      return new Interval(powerValues.Min(), powerValues.Max());
     
    191183
    192184    public static Interval Root(Interval a, Interval b) {
    193       int lower = (int)Math.Round(b.lowerBound);
    194       int higher = (int)Math.Round(b.upperBound);
    195 
    196       return new Interval(Math.Pow(a.lowerBound, 1.0 / higher), Math.Pow(a.upperBound, 1.0 / lower));
     185      int lower = (int)Math.Round(b.LowerBound);
     186      int higher = (int)Math.Round(b.UpperBound);
     187
     188      return new Interval(Math.Pow(a.LowerBound, 1.0 / higher), Math.Pow(a.UpperBound, 1.0 / lower));
    197189    }
    198190
Note: See TracChangeset for help on using the changeset viewer.