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