Generated on for Gecode by doxygen 1.17.0
optimize.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main author:
4 * Christian Schulte <schulte@gecode.dev>
5 *
6 * Copyright:
7 * Christian Schulte, 2008
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <gecode/minimodel.hh>
35
36#include <cmath>
37
38namespace Gecode {
39
40 void
42 const IntMinimizeSpace* best =
43 dynamic_cast<const IntMinimizeSpace*>(&_best);
44 if (best == nullptr)
45 throw DynamicCastFailed("IntMinimizeSpace::constrain");
46 rel(*this, cost(), IRT_LE, best->cost().val());
47 }
48
49
50 void
52 const IntMaximizeSpace* best =
53 dynamic_cast<const IntMaximizeSpace*>(&_best);
54 if (best == nullptr)
55 throw DynamicCastFailed("IntMaximizeSpace::constrain");
56 rel(*this, cost(), IRT_GR, best->cost().val());
57 }
58
59
60 void
62 const IntLexMinimizeSpace* best =
63 dynamic_cast<const IntLexMinimizeSpace*>(&_best);
64 if (best == nullptr)
65 throw DynamicCastFailed("IntLexMinimizeSpace::constrain");
66 IntVarArgs cx(cost()), bx(best->cost());
67 IntArgs bn(bx.size());
68 for (int i=bn.size(); i--; )
69 bn[i] = bx[i].val();
70 rel(*this, cx, IRT_LE, bn);
71 }
72
73 void
75 const IntLexMaximizeSpace* best =
76 dynamic_cast<const IntLexMaximizeSpace*>(&_best);
77 if (best == nullptr)
78 throw DynamicCastFailed("IntLexMaximizeSpace::constrain");
79 IntVarArgs cx(cost()), bx(best->cost());
80 IntArgs bn(bx.size());
81 for (int i=bn.size(); i--; )
82 bn[i] = bx[i].val();
83 rel(*this, cx, IRT_GR, bn);
84 }
85
86#ifdef GECODE_HAS_FLOAT_VARS
87
88 void
90 const FloatMinimizeSpace* best =
91 dynamic_cast<const FloatMinimizeSpace*>(&_best);
92 if (best == nullptr)
93 throw DynamicCastFailed("FloatMinimizeSpace::constrain");
94 // An assigned cost is below the bound of the best solution minus the
95 // step exactly when its upper bound is below the lower bound of that
96 // difference. Posting this strict bound prunes the incumbent value
97 // before the cost is assigned.
98 FloatNum n = (FloatVal(best->cost().max())-step).min();
99 if (n <= Float::Limits::min)
100 fail();
101 else
102 rel(*this, cost(), FRT_LQ, std::nextafter(n, Float::Limits::min));
103 }
104
105
106 void
108 const FloatMaximizeSpace* best =
109 dynamic_cast<const FloatMaximizeSpace*>(&_best);
110 if (best == nullptr)
111 throw DynamicCastFailed("FloatMaximizeSpace::constrain");
112 FloatNum n = (FloatVal(best->cost().min())+step).max();
113 if (n >= Float::Limits::max)
114 fail();
115 else
116 rel(*this, cost(), FRT_GQ, std::nextafter(n, Float::Limits::max));
117 }
118
119#endif
120
121}
122
123// STATISTICS: minimodel-search
124
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1606
Exception: dynamic cast failed
Definition exception.hpp:69
virtual void constrain(const Space &best)
Member function constraining according to cost.
Definition optimize.cpp:107
FloatNum step
Step by which a next solution has to have lower cost.
virtual FloatVar cost(void) const =0
Return variable with current cost.
FloatMaximizeSpace(FloatNum s=0.0)
Constructor with step s.
Definition optimize.hpp:80
virtual FloatVar cost(void) const =0
Return variable with current cost.
virtual void constrain(const Space &best)
Member function constraining according to cost.
Definition optimize.cpp:89
FloatMinimizeSpace(FloatNum s=0.0)
Constructor with step s.
Definition optimize.hpp:71
FloatNum step
Step by which a next solution has to have lower cost.
Float value type.
Definition float.hh:329
FloatNum min(void) const
Return minimum of domain.
Definition float.hpp:59
FloatNum max(void) const
Return maximum of domain.
Definition float.hpp:67
Passing integer arguments.
Definition int.hh:653
virtual IntVarArgs cost(void) const =0
Return variables with current costs.
IntLexMaximizeSpace(void)
Default constructor.
Definition optimize.hpp:61
virtual void constrain(const Space &best)
Member function constraining according to increasing costs.
Definition optimize.cpp:74
virtual IntVarArgs cost(void) const =0
Return variables with current costs.
virtual void constrain(const Space &best)
Member function constraining according to decreasing costs.
Definition optimize.cpp:61
IntLexMinimizeSpace(void)
Default constructor.
Definition optimize.hpp:53
virtual IntVar cost(void) const =0
Return variable with current cost.
IntMaximizeSpace(void)
Default constructor.
Definition optimize.hpp:45
virtual void constrain(const Space &best)
Member function constraining according to increasing cost.
Definition optimize.cpp:51
virtual IntVar cost(void) const =0
Return variable with current cost.
IntMinimizeSpace(void)
Default constructor.
Definition optimize.hpp:37
virtual void constrain(const Space &best)
Member function constraining according to decreasing cost.
Definition optimize.cpp:41
Passing integer variables.
Definition int.hh:681
int val(void) const
Return assigned value.
Definition int.hpp:56
void fail(void)
Fail space.
Definition core.hpp:4275
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
double FloatNum
Floating point number base type.
Definition float.hh:106
@ FRT_LQ
Less or equal ( ).
Definition float.hh:1073
@ FRT_GQ
Greater or equal ( ).
Definition float.hh:1075
@ IRT_LE
Less ( ).
Definition int.hh:1229
@ IRT_GR
Greater ( ).
Definition int.hh:1231
Space(void)
Default constructor.
Definition core.cpp:121
const FloatNum max
Largest allowed float value.
Definition float.hh:839
const FloatNum min
Smallest allowed float value.
Definition float.hh:841
Gecode toplevel namespace
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .