Generated on for Gecode by doxygen 1.17.0
action.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.dev>
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.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 <cfloat>
35
36namespace Gecode {
37
42 class Action : public SharedHandle {
43 protected:
44 template<class View, bool p, bool f>
45 class Recorder;
48 public:
52 int n;
54 double invd;
56 double* a;
58 template<class View>
59 Storage(Home home, ViewArray<View>& x, double d,
62 void update(int i);
65 ~Storage(void);
66 };
67
68 Storage& object(void) const;
70 void object(Storage& o);
72 void update(int i);
74 void acquire(void);
76 void release(void);
77 public:
79
80
87 Action(void);
90 Action(const Action& a);
99 template<class View>
100 Action(Home home, ViewArray<View>& x, double d, bool p, bool f,
107 template<class View>
108 void init(Home home, ViewArray<View>& x, double d, bool p, bool f,
113
116 ~Action(void);
117
119
120
121 double operator [](int i) const;
123 int size(void) const;
125
127
128
130 void decay(Space& home, double d);
133 double decay(const Space& home) const;
135 };
136
138 template<class View, bool p, bool f>
139 class Action::Recorder : public NaryPropagator<View,PC_GEN_NONE> {
140 protected:
143 class Idx : public Advisor {
144 protected:
146 unsigned int _info;
147 public:
149 Idx(Space& home, Propagator& r, Council<Idx>& c, int i);
151 Idx(Space& home, Idx& a);
153 void propagate(void);
155 void fail(void);
157 bool propagated(void) const;
159 bool failed(void) const;
161 void clear(void);
163 int idx(void) const;
164 };
165
171 public:
175 virtual Propagator* copy(Space& home);
177 virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
179 virtual void reschedule(Space& home);
181 virtual ExecStatus advise(Space& home, Advisor& a, const Delta& d);
183 virtual void advise(Space& home, Advisor& a);
185 virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
187 virtual size_t dispose(Space& home);
189 static ExecStatus post(Home home, ViewArray<View>& x, Action& a);
190 };
191
196 template<class Char, class Traits>
197 std::basic_ostream<Char,Traits>&
198 operator <<(std::basic_ostream<Char,Traits>& os,
199 const Action& a);
200
201
202 /*
203 * Advisor for action recorder
204 *
205 */
206 template<class View, bool p, bool f>
207 forceinline
209 Council<Idx>& c, int i)
210 : Advisor(home,r,c), _info(static_cast<unsigned int>(i) << 2U) {}
211 template<class View, bool p, bool f>
212 forceinline
216 template<class View, bool p, bool f>
217 forceinline void
221 template<class View, bool p, bool f>
222 forceinline void
226 template<class View, bool p, bool f>
227 forceinline bool
229 return (_info & 1U) != 0;
230 }
231 template<class View, bool p, bool f>
232 forceinline bool
234 return (_info & 2U) != 0;
235 }
236 template<class View, bool p, bool f>
237 forceinline void
241 template<class View, bool p, bool f>
242 forceinline int
244 return static_cast<int>(_info >> 2);
245 }
246
247
248 /*
249 * Posting of action recorder propagator
250 *
251 */
252 template<class View, bool p, bool f>
253 forceinline
255 Action& a0)
256 : NaryPropagator<View,PC_GEN_NONE>(home,x), a(a0), c(home) {
257 home.notice(*this,AP_DISPOSE);
258 for (int i=0; i<x.size(); i++)
259 if (!x[i].assigned())
260 x[i].subscribe(home,*new (home) Idx(home,*this,c,i), true);
261 }
262
263 template<class View, bool p, bool f>
264 forceinline ExecStatus
266 (void) new (home) Recorder<View,p,f>(home,x,a);
267 return ES_OK;
268 }
269
270
271 /*
272 * Action value storage
273 *
274 */
275
276 template<class View>
277 forceinline
279 typename
281 : n(x.size()), invd(1.0 / d), a(heap.alloc<double>(x.size())) {
282 if (bm)
283 for (int i=0; i<n; i++) {
284 typename View::VarType xi(x[i].varimp());
285 a[i] = bm(home,xi,i);
286 }
287 else
288 for (int i=0; i<n; i++)
289 a[i] = 1.0;
290 }
291 forceinline void
293 /*
294 * The trick to inverse decay is from: An Extensible SAT-solver,
295 * Niklas Eén, Niklas Sörensson, SAT 2003.
296 */
297 assert((i >= 0) && (i < n));
298 a[i] = invd * (a[i] + 1.0);
300 for (int j=0; j<n; j++)
302 }
303
304
305 /*
306 * Action
307 *
308 */
309
310 forceinline Action::Storage&
311 Action::object(void) const {
312 return static_cast<Action::Storage&>(*SharedHandle::object());
313 }
314
315 forceinline void
319
320 forceinline void
322 object().update(i);
323 }
324 forceinline double
325 Action::operator [](int i) const {
326 assert((i >= 0) && (i < object().n));
327 return object().a[i];
328 }
329 forceinline int
330 Action::size(void) const {
331 return object().n;
332 }
333 forceinline void
335 object().m.acquire();
336 }
337 forceinline void
339 object().m.release();
340 }
341
342
343 forceinline
345
346 template<class View>
347 forceinline
349 bool p, bool f,
351 assert(!*this);
352 if (!p && !f)
353 return;
354 object(*new Storage(home,x,d,bm));
355 if (p && f)
356 (void) Recorder<View,true,true>::post(home,x,*this);
357 else if (p && !f)
358 (void) Recorder<View,true,false>::post(home,x,*this);
359 else if (!p && f)
360 (void) Recorder<View,false,true>::post(home,x,*this);
361 }
362 template<class View>
363 forceinline void
365 bool p, bool f,
367 assert(!*this);
368 if (!p && !f)
369 return;
370 object(*new Storage(home,x,d,bm));
371 if (p && f)
372 (void) Recorder<View,true,true>::post(home,x,*this);
373 else if (p && !f)
374 (void) Recorder<View,true,false>::post(home,x,*this);
375 else if (!p && f)
376 (void) Recorder<View,false,true>::post(home,x,*this);
377 }
378
379 template<class Char, class Traits>
380 std::basic_ostream<Char,Traits>&
381 operator <<(std::basic_ostream<Char,Traits>& os,
382 const Action& a) {
383 std::basic_ostringstream<Char,Traits> s;
384 s.copyfmt(os); s.width(0);
385 s << '{';
386 if (a.size() > 0) {
387 s << a[0];
388 for (int i=1; i<a.size(); i++)
389 s << ", " << a[i];
390 }
391 s << '}';
392 return os << s.str();
393 }
394
395
396 /*
397 * Propagation for action recorder
398 *
399 */
400 template<class View, bool p, bool f>
401 forceinline
403 : NaryPropagator<View,PC_GEN_NONE>(home,r), a(r.a) {
404 c.update(home, r.c);
405 }
406
407 template<class View, bool p, bool f>
410 return new (home) Recorder<View,p,f>(home, *this);
411 }
412
413 template<class View, bool p, bool f>
414 inline size_t
416 // Delete access to action information
417 home.ignore(*this,AP_DISPOSE);
418 a.~Action();
419 // Cancel remaining advisors
420 for (Advisors<Idx> as(c); as(); ++as)
421 x[as.advisor().idx()].cancel(home,as.advisor(),true);
422 c.dispose(home);
424 return sizeof(*this);
425 }
426
427 template<class View, bool p, bool f>
430 return PropCost::record();
431 }
432
433 template<class View, bool p, bool f>
434 void
436 View::schedule(home,*this,ME_GEN_ASSIGNED);
437 }
438
439 template<class View, bool p, bool f>
442 Idx& a = static_cast<Idx&>(_a);
443 if (p) {
444 a.propagate();
445 return ES_NOFIX;
446 } else {
447 return x[a.idx()].assigned() ? home.ES_FIX_DISPOSE(c,a) : ES_FIX;
448 }
449 }
450
451 template<class View, bool p, bool f>
452 void
454 Idx& a = static_cast<Idx&>(_a);
455 if (f)
456 a.fail();
457 }
458
459 template<class View, bool p, bool f>
462 // Lock action information
463 a.acquire();
464 for (Advisors<Idx> as(c); as(); ++as) {
465 int i = as.advisor().idx();
466 if (p && as.advisor().propagated()) {
467 as.advisor().clear();
468 a.update(i);
469 if (x[i].assigned())
470 as.advisor().dispose(home,c);
471 }
472 if (f && as.advisor().failed()) {
473 as.advisor().clear();
474 a.update(i);
475 if (x[i].assigned())
476 as.advisor().dispose(home,c);
477 }
478 }
479 a.release();
480 return c.empty() ? home.ES_SUBSUMED(*this) : ES_FIX;
481 }
482
483
484}
485
486// STATISTICS: kernel-branch
Advisor with index and change information.
Definition action.hpp:143
void propagate(void)
Mark index as propagated.
Definition action.hpp:218
void clear(void)
Clear any information.
Definition action.hpp:238
bool failed(void) const
Whether index has been failed.
Definition action.hpp:233
unsigned int _info
Index and mark/failed information.
Definition action.hpp:146
Idx(Space &home, Propagator &r, Council< Idx > &c, int i)
Constructor for creation.
Definition action.hpp:208
void fail(void)
Mark index as failed.
Definition action.hpp:223
int idx(void) const
Get index of view.
Definition action.hpp:243
bool propagated(void) const
Whether index has been propagated.
Definition action.hpp:228
Propagator for recording action information.
Definition action.hpp:139
Council< Idx > c
The advisor council.
Definition action.hpp:168
Recorder(Space &home, Recorder< View, p, f > &r)
Constructor for cloning r.
Definition action.hpp:402
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition action.hpp:415
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition action.hpp:409
static ExecStatus post(Home home, ViewArray< View > &x, Action &a)
Post action recorder propagator.
Definition action.hpp:265
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition action.hpp:441
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition action.hpp:461
Action a
Access to action information.
Definition action.hpp:166
virtual void reschedule(Space &home)
Schedule function.
Definition action.hpp:435
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (record so that propagator runs last).
Definition action.hpp:429
Object for storing action values.
Definition action.hpp:47
void update(int i)
Update action value at position i.
Definition action.hpp:292
double invd
Inverse decay factor.
Definition action.hpp:54
double * a
Action values (more follow).
Definition action.hpp:56
static Support::Mutex m
Mutex to synchronize globally shared access.
Definition action.hpp:50
~Storage(void)
Delete object.
Storage(Home home, ViewArray< View > &x, double d, typename BranchTraits< typename View::VarType >::Merit bm)
Initialize action values.
Definition action.hpp:278
int n
Number of action values.
Definition action.hpp:52
Class for action management.
Definition action.hpp:42
Storage & object(void) const
Return object of correct type.
Definition action.hpp:311
void init(Home home, ViewArray< View > &x, double d, bool p, bool f, typename BranchTraits< typename View::VarType >::Merit bm)
Initialize for views x and decay factor d and action as defined by bm.
Definition action.hpp:364
void update(int i)
Update action value at position i.
Definition action.hpp:321
Action & operator=(const Action &a)
Assignment operator.
Action(const Action &a)
Copy constructor.
int size(void) const
Return number of action values.
Definition action.hpp:330
double operator[](int i) const
Return action value at position i.
Definition action.hpp:325
void acquire(void)
Acquire mutex.
Definition action.hpp:334
void release(void)
Release mutex.
Definition action.hpp:338
static const Action def
Default (empty) action information.
Definition action.hpp:111
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, const Action &a)
Print action values enclosed in curly brackets.
Definition action.hpp:381
Action(void)
Construct as not yet initialized.
Definition action.hpp:344
double decay(const Space &home) const
Return decay factor.
~Action(void)
Destructor.
void decay(Space &home, double d)
Set decay factor to d.
Advisor(Space &home, Propagator &p, Council< A > &c)
Constructor for creation.
Definition core.hpp:3952
friend class Council
Definition core.hpp:1303
Class to iterate over advisors of a council.
Definition core.hpp:1275
Traits for branching.
Definition traits.hpp:55
Generic domain change information to be supplied to advisors.
Definition core.hpp:209
Home class for posting propagators
Definition core.hpp:863
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:3301
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition pattern.hpp:513
NaryPropagator(Space &home, NaryPropagator &p)
Propagation cost.
Definition core.hpp:493
static PropCost record(void)
For recording information (no propagation allowed).
Definition core.hpp:4941
Base-class for propagators.
Definition core.hpp:1073
friend class Space
Definition core.hpp:1075
friend class Advisor
Definition core.hpp:1077
friend class Council
Definition core.hpp:1078
ModEventDelta med
A set of modification events (used during propagation).
Definition core.hpp:1084
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3607
SharedHandle(void)
Create shared handle with no object pointing to.
SharedHandle::Object * object(void) const
Access to the shared object.
Computation spaces.
Definition core.hpp:1775
A mutex for mutual exclausion among several threads.
Definition thread.hpp:78
void release(void)
Release the mutex.
Definition thread.hpp:94
void acquire(void)
Acquire the mutex and possibly block.
Definition thread.hpp:70
View arrays.
Definition array.hpp:255
Heap heap
The single global heap.
ExecStatus ES_FIX_DISPOSE(Council< A > &c, A &a)
Advisor a must be disposed
Definition core.hpp:4006
ExecStatus ES_SUBSUMED(Propagator &p)
Propagator p is subsumed
Definition core.hpp:3672
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4213
int ModEventDelta
Modification event deltas.
Definition core.hpp:94
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:569
#define GECODE_KERNEL_EXPORT
Definition kernel.hh:70
const double rescale_limit
Rescale action and afc values when larger than this.
Definition kernel.hh:101
const double rescale
Rescale factor for action and afc values.
Definition kernel.hh:99
Gecode toplevel namespace
Archive & operator<<(Archive &e, FloatNumBranch nl)
Definition val-sel.hpp:39
ExecStatus
Definition core.hpp:479
@ ES_OK
Execution is okay.
Definition core.hpp:483
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:484
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:482
const ModEvent ME_GEN_ASSIGNED
Generic modification event: variable is assigned a value.
Definition core.hpp:74
const PropCond PC_GEN_NONE
Propagation condition to be ignored (convenience).
Definition core.hpp:79
#define GECODE_VTABLE_EXPORT
Definition support.hh:76