Scippy

SCIP

Solving Constraint Integer Programs

intervalarithext.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2014 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file intervalarithext.h
17  * @brief C++ extensions to interval arithmetics for provable bounds
18  * @author Stefan Vigerske
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #ifndef __SCIP_INTERVALARITHEXT_HPP__
24 #define __SCIP_INTERVALARITHEXT_HPP__
25 
26 #include "scip/intervalarith.h"
27 #include "scip/pub_message.h"
28 
29 /* In some cases, a user may need the SCIPInterval class to be defined in some namespace.
30  * To allow this, the symbol SCIPInterval_NAMESPACE should be defined to the name of that namespace
31  * before inclusion of this header file
32  * It is in the users responsibility to implement the corresponding symbols from nlpi/intervalarith.c.
33  */
34 #ifdef SCIPInterval_NAMESPACE
35 namespace SCIPInterval_NAMESPACE {
36 #endif
37 
38 /** an interval that extends the SCIP_INTERVAL struct
39  * by various methods to allow calculating with intervals as with ordinary numbers */
40 class SCIPInterval : public SCIP_INTERVAL
41 {
42 public:
43  /** value to use for infinity
44  * currently a global variable, thus use with care!
45  */
46  static SCIP_Real infinity;
47 
48  /** default constructor -> gives entire interval */
49  SCIPInterval()
50  {
51  SCIPintervalSetBounds(this, -infinity, infinity);
52  }
53 
54  /* constructor for an SCIP_INTERVAL struct */
55  SCIPInterval(
56  const SCIP_INTERVAL& x /**< interval to copy */
57  )
58  {
59  SCIPintervalSetBounds(this, x.inf, x.sup);
60  }
61 
62  /** constructor for an interval giving pointwise bounds */
63  SCIPInterval(
64  SCIP_Real infinum, /**< lower bound of interval */
65  SCIP_Real supremum /**< upper bound of interval */
66  )
67  {
68  SCIPintervalSetBounds(this, infinum, supremum);
69  }
70 
71  /** constructor for a singleton */
72  SCIPInterval(
73  SCIP_Real number /**< number to be represented by interval */
74  )
75  {
76  SCIPintervalSet(this, number);
77  }
78 
79  /** sets interval bounds */
80  void setBounds(
81  SCIP_Real newinf, /**< lower bound of interval */
82  SCIP_Real newsup /**< upper bound of interval */
83  )
84  {
85  SCIPintervalSetBounds(this, newinf, newsup);
86  }
87 
88  /** returns whether this interval is equal to another one */
89  bool operator==(
90  const SCIP_INTERVAL& y /**< interval to compare with */
91  ) const
92  {
93  if( SCIPintervalIsEmpty(*this) && !SCIPintervalIsEmpty(y) )
94  return false;
95  if( this->inf <= -infinity && y.inf > -infinity )
96  return false;
97  if( this->sup >= infinity && y.sup < infinity )
98  return false;
99  return (this->inf == y.inf) && (this->sup == y.sup);
100  }
101 
102  /** returns whether this interval is not equal to another one */
103  bool operator!=(
104  const SCIP_INTERVAL& y /**< interval to compare with */
105  ) const
106  {
107  return !operator==(y);
108  }
109 
110  /** returns whether this interval is equal to a given number */
111  bool operator==(
112  const SCIP_Real& y /**< number to compare with */
113  ) const
114  {
115  return ( (inf == y) && (sup == y) ) ||
116  ( sup <= -infinity && y <= -infinity ) ||
117  ( inf >= infinity && y >= infinity );
118  }
119 
120  /** adds another interval to this one */
121  SCIPInterval& operator+=(
122  const SCIPInterval& y /**< interval to add in */
123  )
124  {
125  SCIPintervalAdd(SCIPInterval::infinity, this, *this, y);
126  return *this;
127  }
128 
129  /** subtracts an interval from this one */
130  SCIPInterval& operator-=(
131  const SCIPInterval& y /**< interval to substract */
132  )
133  {
134  SCIPintervalSub(SCIPInterval::infinity, this, *this, y);
135  return *this;
136  }
137 
138  /** multiplies an interval with this one */
139  SCIPInterval& operator*=(
140  const SCIPInterval& y /**< interval to multiply with */
141  )
142  {
143  SCIPintervalMul(SCIPInterval::infinity, this, *this, y);
144  return *this;
145  }
146 
147  /** divides this interval by another one */
148  SCIPInterval& operator/=(
149  const SCIPInterval& y /**< interval to divide by */
150  )
151  {
152  SCIPintervalDiv(SCIPInterval::infinity, this, *this, y);
153  return *this;
154  }
155 
156  /** assigns a number to this interval */
157  SCIPInterval& operator=(
158  const SCIP_Real& y /**< new value for both interval bounds */
159  )
160  {
161  SCIPintervalSet(this, y);
162  return *this;
163  }
164 
165  /** assign an interval to this interval */
166  SCIPInterval& operator=(
167  const SCIP_INTERVAL& y /**< new value for this interval */
168  )
169  {
170  SCIPintervalSetBounds(this, y.inf, y.sup);
171  return *this;
172  }
173 
174 };
175 
176 /** addition of two intervals */
177 inline
178 SCIPInterval operator+(
179  const SCIPInterval& x, /**< first operand */
180  const SCIPInterval& y /**< second operand */
181  )
182 {
183  SCIPInterval resultant;
184 
185  SCIPintervalAdd(SCIPInterval::infinity, &resultant, x, y);
186 
187  return resultant;
188 }
189 
190 /** substraction for two intervals */
191 inline
192 SCIPInterval operator-(
193  const SCIPInterval& x, /**< first operand */
194  const SCIPInterval& y /**< second operand */
195  )
196 {
197  SCIPInterval resultant;
198 
199  SCIPintervalSub(SCIPInterval::infinity, &resultant, x, y);
200 
201  return resultant;
202 }
203 
204 /** negation of an interval */
205 inline
206 SCIPInterval operator-(
207  const SCIPInterval& y /**< operand */
208  )
209 {
210  SCIPInterval resultant;
211 
212  SCIPintervalSetBounds(&resultant, -y.sup, -y.inf);
213 
214  return resultant;
215 }
216 
217 /** multiplication of two intervals */
218 inline
219 SCIPInterval operator*(
220  const SCIPInterval& x, /**< first operand */
221  const SCIPInterval& y /**< second operand */
222  )
223 {
224  SCIPInterval resultant;
225 
226  SCIPintervalMul(SCIPInterval::infinity, &resultant, x, y);
227 
228  return resultant;
229 }
230 
231 /** division for two intervals */
232 inline
233 SCIPInterval operator/(
234  const SCIPInterval& x, /**< first operand */
235  const SCIPInterval& y /**< second operand */
236  )
237 {
238  SCIPInterval resultant;
239 
240  SCIPintervalDiv(SCIPInterval::infinity, &resultant, x, y);
241 
242  return resultant;
243 }
244 
245 /** cosinus of an interval */
246 inline
247 SCIPInterval cos(
248  const SCIPInterval& x /**< operand */
249  )
250 {
251  /* @todo implement cosinus for intervals */
252  SCIPerrorMessage("Cosinus of interval not implemented. Returning trivial interval [-1,1].\n");
253 
254  return SCIPInterval(-1.0, 1.0);
255 }
256 
257 /** exponential of an interval */
258 inline
259 SCIPInterval exp(
260  const SCIPInterval& x /**< operand */
261  )
262 {
263  SCIPInterval resultant;
264 
265  SCIPintervalExp(SCIPInterval::infinity, &resultant, x);
266 
267  return resultant;
268 }
269 
270 /** natural logarithm of an interval */
271 inline
272 SCIPInterval log(
273  const SCIPInterval& x /**< operand */
274  )
275 {
276  SCIPInterval resultant;
277 
278  SCIPintervalLog(SCIPInterval::infinity, &resultant, x);
279 
280  return resultant;
281 }
282 
283 /** power of an interval to another interval */
284 inline
285 SCIPInterval pow(
286  const SCIPInterval& x, /**< first operand */
287  const SCIPInterval& y /**< second operand */
288  )
289 {
290  SCIPInterval resultant;
291 
292  SCIPintervalPower(SCIPInterval::infinity, &resultant, x, y);
293 
294  return resultant;
295 }
296 
297 /** power of an interval to a scalar */
298 inline
299 SCIPInterval pow(
300  const SCIPInterval& x, /**< first operand */
301  const SCIP_Real& y /**< exponent */
302  )
303 {
304  SCIPInterval resultant;
305 
306  SCIPintervalPowerScalar(SCIPInterval::infinity, &resultant, x, y);
307 
308  return resultant;
309 }
310 
311 /** signpower of an interval to a scalar */
312 inline
313 SCIPInterval signpow(
314  const SCIPInterval& x, /**< first operand */
315  const SCIP_Real p /**< exponent */
316  )
317 {
318  SCIPInterval resultant;
319 
320  SCIPintervalSignPowerScalar(SCIPInterval::infinity, &resultant, x, p);
321 
322  return resultant;
323 }
324 
325 /** sinus of an interval */
326 inline
327 SCIPInterval sin(
328  const SCIPInterval& x /**< operand */
329  )
330 {
331  /* @todo implement sinus for intervals */
332  SCIPerrorMessage("Sinus of interval not implemented. Returning trivial interval [-1,1].\n");
333 
334  return SCIPInterval(-1.0, 1.0);
335 }
336 
337 /** square an interval */
338 inline
339 SCIPInterval square(
340  const SCIPInterval& x /**< operand */
341  )
342 {
343  SCIPInterval resultant;
344 
345  SCIPintervalSquare(SCIPInterval::infinity, &resultant, x);
346 
347  return resultant;
348 }
349 
350 /** square root of an interval */
351 inline
352 SCIPInterval sqrt(
353  const SCIPInterval& x /**< operand */
354  )
355 {
356  SCIPInterval resultant;
357 
358  SCIPintervalSquareRoot(SCIPInterval::infinity, &resultant, x);
359 
360  return resultant;
361 }
362 
363 /** absolute value of an interval */
364 inline
365 SCIPInterval abs(
366  const SCIPInterval& x /**< operand */
367  )
368 {
369  SCIPInterval resultant;
370 
371  SCIPintervalAbs(&resultant, x);
372 
373  return resultant;
374 }
375 
376 /** sign of an interval */
377 inline
378 SCIPInterval sign(
379  const SCIPInterval& x /**< operand */
380  )
381 {
382  SCIPInterval resultant;
383 
384  SCIPintervalSign(&resultant, x);
385 
386  return resultant;
387 }
388 
389 /** macro to easy definition of so far unimplemented interval functions */
390 #define SCIP_INTERVALARITH_UNDEFFUNC(function) \
391 inline \
392 SCIPInterval function( \
393  const SCIPInterval& x /**< operand */ \
394  ) \
395 { \
396  SCIPerrorMessage("Error: " #function " not implemented for intervals.\n"); \
397  return SCIPInterval(); \
398 }
399 
408 #undef SCIP_INTERVALARITH_UNDEFFUNC
409 
410 #ifdef SCIPInterval_NAMESPACE
411 } /* namespace SCIPInterval_NAMESPACE */
412 #endif
413 
414 #endif
415