Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
view-val.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2012
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
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
34namespace Gecode {
35
45 template<class Val>
47 private:
49 const Val _val;
50 public:
52 PosValChoice(const Brancher& b, unsigned int a, const Pos& p, const Val& n);
53 const Val& val(void) const;
55 virtual void archive(Archive& e) const;
56 };
57
58
60 template<class View, class Val, PropCond pc>
61 class ViewValNGL : public NGL {
62 protected:
64 View x;
66 Val n;
67 public:
69 ViewValNGL(Space& home, View x, Val n);
73 virtual void subscribe(Space& home, Propagator& p);
75 virtual void cancel(Space& home, Propagator& p);
77 virtual void reschedule(Space& home, Propagator& p);
79 virtual size_t dispose(Space& home);
80 };
81
89 template<class View, int n, class Val, unsigned int a,
90 class Filter, class Print>
91 class ViewValBrancher : public ViewBrancher<View,Filter,n> {
92 protected:
93 using ViewBrancher<View,Filter,n>::vs;
94 using ViewBrancher<View,Filter,n>::x;
95 using ViewBrancher<View,Filter,n>::f;
97 typedef typename View::VarType Var;
101 Print p;
111 public:
113 virtual const Choice* choice(Space& home);
115 virtual const Choice* choice(const Space& home, Archive& e);
117 virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
119 virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
127 virtual void print(const Space& home, const Choice& c, unsigned int b,
128 std::ostream& o) const;
130 virtual Actor* copy(Space& home);
132 virtual size_t dispose(Space& home);
134 static void post(Home home,
140 };
141
143 template<class View, int n, class Val, unsigned int a>
144 void
147 ViewSel<View>* vs[n],
152
153 /*
154 * %Choice with position and value
155 *
156 */
157 template<class Val>
160 const Pos& p, const Val& n)
161 : PosChoice(b,a,p), _val(n) {}
162
163 template<class Val>
164 forceinline const Val&
166 return _val;
167 }
168
169 template<class Val>
170 forceinline void
173 e << _val;
174 }
175
176
177 /*
178 * View-value no-good literal
179 *
180 */
181 template<class View, class Val, PropCond pc>
184 : NGL(home), x(x0), n(n0) {}
185
186 template<class View, class Val, PropCond pc>
189 : NGL(home,ngl), n(ngl.n) {
190 x.update(home,ngl.x);
191 }
192
193 template<class View, class Val, PropCond pc>
194 void
196 x.subscribe(home,p,pc);
197 }
198
199 template<class View, class Val, PropCond pc>
200 void
202 x.cancel(home,p,pc);
203 }
204
205 template<class View, class Val, PropCond pc>
206 void
208 x.reschedule(home,p,pc);
209 }
210
211 template<class View, class Val, PropCond pc>
212 size_t
214 (void) NGL::dispose(home);
215 return sizeof(*this);
216 }
217
218
219
220 /*
221 * Generic brancher based on variable/value selection
222 *
223 */
224 template<class View, int n, class Val, unsigned int a,
225 class Filter, class Print>
230 ViewSel<View>* vs[n],
234 : ViewBrancher<View,Filter,n>(home,x,vs,bf), vsc(vsc0), p(vvp) {
235 if (vsc->notice() || f.notice() || p.notice())
236 home.notice(*this,AP_DISPOSE,true);
237 }
238
239 template<class View, int n, class Val, unsigned int a,
240 class Filter, class Print>
241 forceinline void
250
251 template<class View, int n, class Val, unsigned int a,
252 class Filter, class Print>
259
260 template<class View, int n, class Val, unsigned int a,
261 class Filter, class Print>
262 Actor*
267
268 template<class View, int n, class Val, unsigned int a,
269 class Filter, class Print>
270 const Choice*
276
277 template<class View, int n, class Val, unsigned int a,
278 class Filter, class Print>
279 const Choice*
281 Archive& e) {
282 (void) home;
283 int p; e >> p;
284 Val v; e >> v;
285 return new PosValChoice<Val>(*this,a,p,v);
286 }
287
288 template<class View, int n, class Val, unsigned int a,
289 class Filter, class Print>
292 ::commit(Space& home, const Choice& c, unsigned int b) {
293 const PosValChoice<Val>& pvc
294 = static_cast<const PosValChoice<Val>&>(c);
295 return me_failed(vsc->commit(home,b,
297 pvc.pos().pos,
298 pvc.val()))
299 ? ES_FAILED : ES_OK;
300 }
301
302 template<class View, int n, class Val, unsigned int a,
303 class Filter, class Print>
304 NGL*
306 ::ngl(Space& home, const Choice& c, unsigned int b) const {
307 const PosValChoice<Val>& pvc
308 = static_cast<const PosValChoice<Val>&>(c);
309 return vsc->ngl(home,b,
311 }
312
313 template<class View, int n, class Val, unsigned int a,
314 class Filter, class Print>
315 void
317 ::print(const Space& home, const Choice& c, unsigned int b,
318 std::ostream& o) const {
319 const PosValChoice<Val>& pvc
320 = static_cast<const PosValChoice<Val>&>(c);
321 View xi = ViewBrancher<View,Filter,n>::view(pvc.pos());
322 if (p)
323 p(home,*this,b,xi,pvc.pos().pos,pvc.val(),o);
324 else
325 vsc->print(home,b,xi,pvc.pos().pos,pvc.val(),o);
326 }
327
328 template<class View, int n, class Val, unsigned int a,
329 class Filter, class Print>
330 forceinline size_t
332 if (vsc->notice() || f.notice() || p.notice())
333 home.ignore(*this,AP_DISPOSE,true);
334 vsc->dispose(home);
337 }
338
339 template<class View, int n, class Val, unsigned int a>
340 forceinline void
343 ViewSel<View>* vs[n],
347 if (bf) {
348 if (vvp) {
349 ViewValBrancher<View,n,Val,a,
351 ::post(home,x,vs,vsc,bf,vvp);
352 } else {
353 ViewValBrancher<View,n,Val,a,
355 ::post(home,x,vs,vsc,bf,vvp);
356 }
357 } else {
358 if (vvp)
359 ViewValBrancher<View,n,Val,a,
361 ::post(home,x,vs,vsc,bf,vvp);
362 else
363 ViewValBrancher<View,n,Val,a,
365 ::post(home,x,vs,vsc,bf,vvp);
366 }
367 }
368
369}
370
371// STATISTICS: kernel-branch
struct Gecode::@603::NNF::@65::@66 b
For binary nodes (and, or, eqv)
int p
Number of positive literals for node type.
int n
Number of negative literals for node type.
struct Gecode::@603::NNF::@65::@67 a
For atomic nodes.
Base-class for both propagators and branchers.
Definition core.hpp:628
Archive representation
Definition archive.hpp:42
Class storing a branch filter function.
Definition filter.hpp:45
Class withot a branch filter function.
Definition filter.hpp:68
Class without print function.
Definition print.hpp:73
Class storing a print function.
Definition print.hpp:47
Base-class for branchers.
Definition core.hpp:1442
Choice for performing commit
Definition core.hpp:1412
Home class for posting propagators
Definition core.hpp:856
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:3219
No-good literal recorded during search.
Definition core.hpp:1340
virtual size_t dispose(Space &home)
Dispose.
Definition core.hpp:3821
Choices storing position
Definition view.hpp:55
virtual void archive(Archive &e) const
Archive into e.
Definition view.hpp:130
const Pos & pos(void) const
Return position in array.
Definition view.hpp:126
Choice storing position and value
Definition view-val.hpp:46
const Val & val(void) const
Definition view-val.hpp:165
virtual void archive(Archive &e) const
Archive into e.
Definition view-val.hpp:171
PosValChoice(const Brancher &b, unsigned int a, const Pos &p, const Val &n)
Initialize choice for brancher b, number of alternatives a, position p, and value n.
Definition view-val.hpp:159
Position information.
Definition view.hpp:44
const int pos
Position of view.
Definition view.hpp:47
Base-class for propagators.
Definition core.hpp:1064
Computation spaces.
Definition core.hpp:1742
Base class for value selection and commit.
virtual bool notice(void) const =0
Whether dispose must always be called (that is, notice is needed)
View arrays.
Definition array.hpp:253
Generic brancher by view selection.
Definition view.hpp:78
Pos pos(Space &home)
Return position information.
Definition view.hpp:173
Filter f
Filter function.
Definition view.hpp:89
ViewArray< View > x
Views to branch on.
Definition view.hpp:83
View view(const Pos &p) const
Return view according to position information p.
Definition view.hpp:213
ViewSel< View > * vs[n]
View selection objects.
Definition view.hpp:87
Abstract class for view selection.
Definition view-sel.hpp:44
Generic brancher by view and value selection.
Definition view-val.hpp:91
virtual void print(const Space &home, const Choice &c, unsigned int b, std::ostream &o) const
Print branch for choice c and alternative b.
Definition view-val.hpp:317
virtual Actor * copy(Space &home)
Perform cloning.
Definition view-val.hpp:263
View::VarType Var
The corresponding variable.
Definition view-val.hpp:97
virtual NGL * ngl(Space &home, const Choice &c, unsigned int b) const
Create no-good literal for choice c and alternative b.
Definition view-val.hpp:306
static void post(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, BranchFilter< Var > bf, VarValPrint< Var, Val > vvp)
Brancher post function.
Definition view-val.hpp:243
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
Definition view-val.hpp:292
virtual const Choice * choice(Space &home)
Return choice.
Definition view-val.hpp:271
ViewValBrancher(Space &home, ViewValBrancher &b)
Constructor for cloning b.
Definition view-val.hpp:255
ValSelCommitBase< View, Val > * vsc
Value selection and commit object.
Definition view-val.hpp:99
Print p
Print function.
Definition view-val.hpp:101
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition view-val.hpp:331
View-value no-good literal.
Definition view-val.hpp:61
View x
The stored view.
Definition view-val.hpp:64
virtual void reschedule(Space &home, Propagator &p)
Schedule propagator p.
Definition view-val.hpp:207
Val n
The stored value.
Definition view-val.hpp:66
virtual void cancel(Space &home, Propagator &p)
Cancel subscription for no-good literal.
Definition view-val.hpp:201
ViewValNGL(Space &home, ViewValNGL &ngl)
Constructor for cloning ngl.
Definition view-val.hpp:188
ViewValNGL(Space &home, View x, Val n)
Initialize for propagator p with view x and value n.
Definition view-val.hpp:183
virtual size_t dispose(Space &home)
Dispose.
Definition view-val.hpp:213
virtual void subscribe(Space &home, Propagator &p)
Create subscription for no-good literal.
Definition view-val.hpp:195
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4074
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition modevent.hpp:54
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
Gecode toplevel namespace
std::function< void(const Space &home, const Brancher &b, unsigned int a, Var x, int i, const Val &m, std::ostream &o)> VarValPrint
Function type for printing variable and value selection.
Definition print.hpp:40
void postviewvalbrancher(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, BranchFilter< typename View::VarType > bf, VarValPrint< typename View::VarType, Val > vvp)
Post view value brancher.
Definition view-val.hpp:341
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
Post propagator for SetVar x
Definition set.hh:767
std::function< bool(const Space &home, Var x, int i)> BranchFilter
Function type for branch filter functions.
Definition filter.hpp:40
#define forceinline
Definition config.hpp:187
#define GECODE_VTABLE_EXPORT
Definition support.hh:72