Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
linear.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3o * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
6 *
7 * Copyright:
8 * Christian Schulte, 2002
9 * Vincent Barichard, 2012
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.org
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
37
38namespace Gecode {
39
40 void
42 const FloatVarArgs& x, FloatRelType frt, FloatVal c) {
43 using namespace Float;
45 Region re;
46 Linear::Term* t = re.alloc<Linear::Term>(x.size());
47 for (int i = x.size(); i--; ) {
48 t[i].a=1.0; t[i].x=x[i];
49 }
50 Linear::post(home,t,x.size(),frt,c);
51 }
52
53 void
55 const FloatVarArgs& x, FloatRelType frt, FloatVal c, Reify r) {
56 using namespace Float;
58 Region re;
59 Linear::Term* t = re.alloc<Linear::Term>(x.size());
60 for (int i = x.size(); i--; ) {
61 t[i].a=1.0; t[i].x=x[i];
62 }
63 Linear::post(home,t,x.size(),frt,c,r);
64 }
65
66 void
68 const FloatValArgs& a, const FloatVarArgs& x, FloatRelType frt,
69 FloatVal c) {
70 using namespace Float;
71 if (a.size() != x.size())
72 throw ArgumentSizeMismatch("Float::linear");
74 Region re;
75 Linear::Term* t = re.alloc<Linear::Term>(x.size());
76 for (int i = x.size(); i--; ) {
77 t[i].a=a[i]; t[i].x=x[i];
78 }
79 Linear::post(home,t,x.size(),frt,c);
80 }
81
82 void
84 const FloatValArgs& a, const FloatVarArgs& x, FloatRelType frt,
85 FloatVal c, Reify r) {
86 using namespace Float;
87 if (a.size() != x.size())
88 throw ArgumentSizeMismatch("Float::linear");
90 Region re;
91 Linear::Term* t = re.alloc<Linear::Term >(x.size());
92 for (int i = x.size(); i--; ) {
93 t[i].a=a[i]; t[i].x=x[i];
94 }
95 Linear::post(home,t,x.size(),frt,c,r);
96 }
97
98 void
100 const FloatVarArgs& x, FloatRelType frt, FloatVar y) {
101 using namespace Float;
103 Region re;
104 Linear::Term* t = re.alloc<Linear::Term>(x.size()+1);
105 for (int i = x.size(); i--; ) {
106 t[i].a=1.0; t[i].x=x[i];
107 }
109 estimate(t,x.size(),0.0,min,max);
110 FloatView v(y);
111 switch (frt) {
112 case FRT_EQ:
113 GECODE_ME_FAIL(v.gq(home,min)); GECODE_ME_FAIL(v.lq(home,max));
114 break;
115 case FRT_GQ: case FRT_GR:
116 GECODE_ME_FAIL(v.lq(home,max));
117 break;
118 case FRT_LQ: case FRT_LE:
119 GECODE_ME_FAIL(v.gq(home,min));
120 break;
121 default: ;
122 }
123 if (home.failed()) return;
124 t[x.size()].a=-1.0; t[x.size()].x=y;
125 Linear::post(home,t,x.size()+1,frt,0.0);
126 }
127
128 void
130 const FloatVarArgs& x, FloatRelType frt, FloatVar y, Reify r) {
131 using namespace Float;
133 Region re;
134 Linear::Term* t = re.alloc<Linear::Term>(x.size()+1);
135 for (int i = x.size(); i--; ) {
136 t[i].a=1.0; t[i].x=x[i];
137 }
138 t[x.size()].a=-1; t[x.size()].x=y;
139 Linear::post(home,t,x.size()+1,frt,0.0,r);
140 }
141
142 void
144 const FloatValArgs& a, const FloatVarArgs& x, FloatRelType frt,
145 FloatVar y) {
146 using namespace Float;
147 if (a.size() != x.size())
148 throw ArgumentSizeMismatch("Float::linear");
150 Region re;
151 Linear::Term* t = re.alloc<Linear::Term>(x.size()+1);
152 for (int i = x.size(); i--; ) {
153 t[i].a=a[i]; t[i].x=x[i];
154 }
156 estimate(t,x.size(),0.0,min,max);
157 FloatView v(y);
158 switch (frt) {
159 case FRT_EQ:
160 GECODE_ME_FAIL(v.gq(home,min)); GECODE_ME_FAIL(v.lq(home,max));
161 break;
162 case FRT_GQ: case FRT_GR:
163 GECODE_ME_FAIL(v.lq(home,max));
164 break;
165 case FRT_LQ: case FRT_LE:
166 GECODE_ME_FAIL(v.gq(home,min));
167 break;
168 default: ;
169 }
170 if (home.failed()) return;
171 t[x.size()].a=-1.0; t[x.size()].x=y;
172 Linear::post(home,t,x.size()+1,frt,0.0);
173 }
174
175 void
177 const FloatValArgs& a, const FloatVarArgs& x, FloatRelType frt,
178 FloatVar y, Reify r) {
179 using namespace Float;
180 if (a.size() != x.size())
181 throw ArgumentSizeMismatch("Float::linear");
183 Region re;
184 Linear::Term* t = re.alloc<Linear::Term>(x.size()+1);
185 for (int i = x.size(); i--; ) {
186 t[i].a=a[i]; t[i].x=x[i];
187 }
188 t[x.size()].a=-1.0; t[x.size()].x=y;
189 Linear::post(home,t,x.size()+1,frt,0.0,r);
190 }
191
192}
193
194// STATISTICS: float-post
Passing float arguments.
Definition float.hh:953
Float value type.
Definition float.hh:334
FloatNum size(void) const
Return size of float value (distance between maximum and minimum)
Definition val.hpp:78
Passing float variables.
Definition float.hh:982
Float variables.
Definition float.hh:870
Home class for posting propagators
Definition core.hpp:856
bool failed(void) const
Check whether corresponding space is failed.
Definition core.hpp:4055
Exception: Arguments are of different size
Definition exception.hpp:73
Class for describing linear term .
Definition linear.hh:1336
int a
Coefficient.
Definition linear.hh:1339
Handle to region.
Definition region.hpp:55
T * alloc(long unsigned int n)
Allocate block of n objects of type T from region.
Definition region.hpp:386
Reification specification.
Definition int.hh:891
void post(Home home, Term< IntView > *t, int n, IntRelType irt, int c, IntPropLevel=IPL_DEF)
Post propagator for linear constraint over integers.
Definition int-post.cpp:219
#define GECODE_POST
Check for failure in a constraint post function.
Definition macros.hpp:40
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition macros.hpp:77
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
double FloatNum
Floating point number base type.
Definition float.hh:106
FloatRelType
Relation types for floats.
Definition float.hh:1074
@ FRT_EQ
Equality ( )
Definition float.hh:1075
@ FRT_GR
Greater ( )
Definition float.hh:1080
@ FRT_LE
Less ( )
Definition float.hh:1078
@ FRT_LQ
Less or equal ( )
Definition float.hh:1077
@ FRT_GQ
Greater or equal ( )
Definition float.hh:1079
Floating point numbers.
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:773
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:773
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar x
Definition set.hh:773