Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
nq.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, 2004
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
34#include <algorithm>
35
36namespace Gecode { namespace Int { namespace Rel {
37
38 /*
39 * Disequality
40 *
41 */
42 template<class V0, class V1>
44 Nq<V0,V1>::Nq(Home home, V0 x0, V1 x1)
45 : MixBinaryPropagator<V0,PC_INT_VAL,V1,PC_INT_VAL>(home,x0,x1) {}
46
47 template<class V0, class V1>
49 Nq<V0,V1>::post(Home home, V0 x0, V1 x1){
50 if (x0.assigned()) {
51 GECODE_ME_CHECK(x1.nq(home,x0.val()));
52 } else if (x1.assigned()) {
53 GECODE_ME_CHECK(x0.nq(home,x1.val()));
54 } else if (x0 == x1) {
55 return ES_FAILED;
56 } else {
57 (void) new (home) Nq<V0,V1>(home,x0,x1);
58 }
59 return ES_OK;
60 }
61
62 template<class V0, class V1>
66
67 template<class V0, class V1>
68 Actor*
70 return new (home) Nq<V0,V1>(home,*this);
71 }
72
73 template<class V0, class V1>
75 Nq<V0,V1>::cost(const Space&, const ModEventDelta&) const {
77 }
78
79 template<class V0, class V1>
82 if (x0.assigned()) {
83 GECODE_ME_CHECK(x1.nq(home,x0.val()));
84 } else {
85 GECODE_ME_CHECK(x0.nq(home,x1.val()));
86 }
87 return home.ES_SUBSUMED(*this);
88 }
89
90
91 /*
92 * Nary disequality propagator
93 */
94 template<class View>
98
99 template<class View>
101 NaryNq<View>::cost(const Space&, const ModEventDelta&) const {
102 return PropCost::linear(PropCost::LO,x.size());
103 }
104
105 template<class View>
109
110 template<class View>
111 Actor*
113 return new (home) NaryNq<View>(home,*this);
114 }
115
116 template<class View>
117 inline ExecStatus
119 x.unique();
120 // Try to find an assigned view
121 int n = x.size();
122 if (n <= 1)
123 return ES_FAILED;
124 for (int i=n; i--; )
125 if (x[i].assigned()) {
126 std::swap(x[0],x[i]);
127 break;
128 }
129 if (x[0].assigned()) {
130 int v = x[0].val();
131 // Eliminate all equal views and possibly find disequal view
132 for (int i=n-1; i>0; i--)
133 if (!x[i].in(v)) {
134 return ES_OK;
135 } else if (x[i].assigned()) {
136 assert(x[i].val() == v);
137 x[i]=x[--n];
138 }
139 x.size(n);
140 }
141 if (n == 1)
142 return ES_FAILED;
143 if (n == 2)
144 return Nq<View,View>::post(home,x[0],x[1]);
145 (void) new (home) NaryNq(home,x);
146 return ES_OK;
147 }
148
149 template<class View>
150 forceinline size_t
153 return sizeof(*this);
154 }
155
156 template<class View>
159 // Make sure that x[0] is assigned
160 if (!x[0].assigned()) {
161 // Note that there is at least one assigned view
162 for (int i=1; true; i++)
163 if (x[i].assigned()) {
164 std::swap(x[0],x[i]);
165 break;
166 }
167 }
168 int v = x[0].val();
169 int n = x.size();
170 for (int i=n-1; i>0; i--)
171 if (!x[i].in(v)) {
172 x.size(n);
173 return home.ES_SUBSUMED(*this);
174 } else if (x[i].assigned()) {
175 assert(x[i].val() == v);
176 x[i] = x[--n];
177 }
178 x.size(n);
179 if (n == 1)
180 return ES_FAILED;
181 if (n == 2) {
182 GECODE_ME_CHECK(x[1].nq(home,v));
183 return home.ES_SUBSUMED(*this);
184 }
185 return ES_FIX;
186 }
187
188
189
190}}}
191
192// STATISTICS: int-prop
int p
Number of positive literals for node type.
int n
Number of negative literals for node type.
Base-class for both propagators and branchers.
Definition core.hpp:628
Home class for posting propagators
Definition core.hpp:856
Nary disequality propagator.
Definition rel.hh:318
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition nq.hpp:151
NaryNq(Home home, ViewArray< View > &x)
Constructor for posting.
Definition nq.hpp:96
static ExecStatus post(Home home, ViewArray< View > &x)
Post propagator .
Definition nq.hpp:118
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition nq.hpp:112
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nq.hpp:158
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition nq.hpp:101
Binary disequality propagator.
Definition rel.hh:460
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low unary)
Definition nq.hpp:75
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nq.hpp:81
static ExecStatus post(Home home, V0 x0, V1 x1)
Post propagator .
Definition nq.hpp:49
Nq(Space &home, Nq< V0, V1 > &p)
Constructor for cloning p.
Definition nq.hpp:64
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition nq.hpp:69
Mixed binary propagator.
Definition pattern.hpp:204
n-ary propagator
Definition pattern.hpp:142
Propagation cost.
Definition core.hpp:486
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition core.hpp:4813
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition core.hpp:4796
Computation spaces.
Definition core.hpp:1742
View arrays.
Definition array.hpp:253
ExecStatus ES_SUBSUMED(Propagator &p)
Definition core.hpp:3563
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
const Gecode::PropCond PC_INT_VAL
Propagate when a view becomes assigned (single value)
Definition var-type.hpp:82
Gecode toplevel namespace
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:477
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
Post propagator for SetVar x
Definition set.hh:767
#define forceinline
Definition config.hpp:187