Generated on for Gecode by doxygen 1.17.0
lastval.hh
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Jip J. Dekker <jip.dekker@monash.edu>
5 *
6 * Copyright:
7 * Jip J. Dekker, 2023
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#ifndef __FLATZINC_LASTVAL_HH__
35#define __FLATZINC_LASTVAL_HH__
36
37#include <gecode/flatzinc.hh>
38#include <gecode/int.hh>
40
41#ifdef GECODE_HAS_SET_VARS
42#include <gecode/set.hh>
43#include <gecode/set/element.hh>
44#endif
45#ifdef GECODE_HAS_FLOAT_VARS
46#include <gecode/float.hh>
47#endif
48
49using namespace Gecode::Int;
50
51#ifdef GECODE_HAS_FLOAT_VARS
52namespace Gecode { namespace Int {
53
55 template<>
60
61}}
62#endif
63
64namespace Gecode { namespace FlatZinc {
65
66 template<class View, class SetLastVal>
67 class LastVal : public Propagator {
68 protected:
71
73 LastVal(Space& home, LastVal& p) : Propagator(home,p) {
74 x.update(home, p.x);
75 };
76
78 x.subscribe(home, *this, PC_GEN_ASSIGNED);
79 for (int i = 0; i < x.size(); ++i) {
80 SetLastVal::init_last_val(home, x[i].idx, x[i].view);
81 }
82 }
83 public:
86 int n = x.size();
87 for (int i = n; i--; ) {
88 if (x[i].view.assigned()) {
89 SetLastVal::set_last_val(home, x[i].idx, x[i].view);
90 x[i] = x[--n];
91 }
92 }
93 x.size(n);
94 }
95
97 virtual Actor* copy(Space& home){ return new (home) LastVal(home,*this); }
98 virtual void reschedule(Space &home) {
99 x.reschedule(home, *this, PC_GEN_ASSIGNED);
100 }
101
102 virtual PropCost cost(const Space& /* home */, const ModEventDelta& /* med */) const { return PropCost::record(); }
104 virtual ExecStatus propagate(Space& home, const ModEventDelta& /* med */) {
105 record_assigned(static_cast<FlatZincSpace&>(home), x);
106 if (x.size() == 0) {
107 return home.ES_SUBSUMED(*this);
108 } else {
109 return ES_FIX;
110 }
111 }
112
115 IdxViewArray<View> ix(home, x);
116 record_assigned(home, ix);
117 if (ix.size() > 0) {
118 (void) new (home) LastVal(home, ix);
119 }
120 return ES_OK;
121 }
122 };
123
125 public:
126 static void init_last_val(FlatZincSpace& home, size_t idx, IntView& var) {
127 home.restart_data().last_val_int[idx] = var.min();
128 }
129 static void set_last_val(FlatZincSpace& home, size_t idx, IntView& var) {
130 home.restart_data().last_val_int[idx] = var.val();
131 }
132 };
134
136 public:
137 static void init_last_val(FlatZincSpace& home, size_t idx, BoolView& var) {
138 home.restart_data().last_val_bool[idx] = var.min();
139 }
140 static void set_last_val(FlatZincSpace& home, size_t idx, BoolView& var) {
141 home.restart_data().last_val_bool[idx] = var.val();
142 }
143 };
145
146#ifdef GECODE_HAS_FLOAT_VARS
148 public:
149 static void init_last_val(FlatZincSpace& home, size_t idx, Float::FloatView& var) {
150 home.restart_data().last_val_float[idx] = var.min();
151 }
152 static void set_last_val(FlatZincSpace& home, size_t idx, Float::FloatView& var) {
153 home.restart_data().last_val_float[idx] = var.val();
154 }
155 };
157#endif
158
159#ifdef GECODE_HAS_SET_VARS
161 public:
162 static void init_last_val(FlatZincSpace& home, size_t idx, Set::SetView& var) {
164 home.restart_data().last_val_set[idx] = IntSet(lb);
165 }
166 static void set_last_val(FlatZincSpace& home, size_t idx, Set::SetView& var) {
168 home.restart_data().last_val_set[idx] = IntSet(lb);
169 }
170 };
172#endif
173
174}}
175
176#endif //__FLATZINC_LASTVAL_HH__
177
178// STATISTICS: flatzinc-branch
A space that can be initialized with a FlatZinc model.
Definition flatzinc.hh:424
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition lastval.hh:97
virtual PropCost cost(const Space &, const ModEventDelta &) const
Cost function.
Definition lastval.hh:102
LastVal(FlatZincSpace &home, IdxViewArray< View > x0)
Constructor for posting.
Definition lastval.hh:77
virtual ExecStatus propagate(Space &home, const ModEventDelta &)
Perform propagation.
Definition lastval.hh:104
virtual void reschedule(Space &home)
Schedule function.
Definition lastval.hh:98
static ExecStatus post(FlatZincSpace &home, const typename ViewToVarArg< View >::argtype &x)
Post propagator.
Definition lastval.hh:114
LastVal(Space &home, LastVal &p)
Constructor for cloning p.
Definition lastval.hh:73
static void record_assigned(FlatZincSpace &home, IdxViewArray< View > &x)
Record (and forget) the values of any assigned views in x in the right position in last_val.
Definition lastval.hh:85
static void set_last_val(FlatZincSpace &home, size_t idx, BoolView &var)
Definition lastval.hh:140
static void init_last_val(FlatZincSpace &home, size_t idx, BoolView &var)
Definition lastval.hh:137
static void set_last_val(FlatZincSpace &home, size_t idx, Float::FloatView &var)
Definition lastval.hh:152
static void init_last_val(FlatZincSpace &home, size_t idx, Float::FloatView &var)
Definition lastval.hh:149
static void set_last_val(FlatZincSpace &home, size_t idx, IntView &var)
Definition lastval.hh:129
static void init_last_val(FlatZincSpace &home, size_t idx, IntView &var)
Definition lastval.hh:126
static void set_last_val(FlatZincSpace &home, size_t idx, Set::SetView &var)
Definition lastval.hh:166
static void init_last_val(FlatZincSpace &home, size_t idx, Set::SetView &var)
Definition lastval.hh:162
Passing float variables.
Definition float.hh:982
Float view for float variables.
Definition view.hpp:52
FloatNum min(void) const
Return minimum of domain.
Definition float.hpp:60
FloatVal val(void) const
Return assigned value.
Definition float.hpp:72
Integer sets.
Definition int.hh:178
Boolean view for Boolean variables.
Definition view.hpp:1378
int min(void) const
Return minimum of domain.
Definition bool.hpp:62
int val(void) const
Return assigned value (only if assigned).
Definition bool.hpp:74
An array of IdxView pairs.
Definition idx-view.hh:67
int size(void) const
Return the current size.
Definition idx-view.hpp:105
Integer view for integer variables.
Definition view.hpp:129
int min(void) const
Return minimum of domain.
Definition int.hpp:58
int val(void) const
Return assigned value (only if assigned).
Definition int.hpp:70
Class to map VarArg type to view.
Definition idx-view.hh:60
Propagation cost.
Definition core.hpp:493
static PropCost record(void)
For recording information (no propagation allowed).
Definition core.hpp:4941
friend class Space
Definition core.hpp:1075
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3607
Range iterator for the greatest lower bound.
Definition var-imp.hpp:359
Set view for set variables
Definition view.hpp:56
ExecStatus ES_SUBSUMED(Propagator &p)
Propagator p is subsumed
Definition core.hpp:3672
int ModEventDelta
Modification event deltas.
Definition core.hpp:94
Interpreter for the FlatZinc language.
LastVal< Float::FloatView, SetFloatLastVal > LastValFloat
Definition lastval.hh:156
LastVal< IntView, SetIntLastVal > LastValInt
Definition lastval.hh:133
LastVal< BoolView, SetBoolLastVal > LastValBool
Definition lastval.hh:144
LastVal< Set::SetView, SetIntSetLastVal > LastValSet
Definition lastval.hh:171
Finite domain integers.
Definition lastval.hh:52
Gecode toplevel namespace
const PropCond PC_GEN_ASSIGNED
Propagation condition for an assigned variable.
Definition core.hpp:81
ExecStatus
Definition core.hpp:479
@ ES_OK
Execution is okay.
Definition core.hpp:483
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:484