Generated on Thu Jan 20 2022 00:00:00 for Gecode by doxygen 1.9.1
int-eq.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, 2006
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 namespace Gecode { namespace Int { namespace Count {
35 
36  template<class VX, class VY>
38  EqInt<VX,VY>::EqInt(Home home, ViewArray<VX>& x, int n_s, VY y, int c)
39  : IntBase<VX,VY>(home,x,n_s,y,c) {}
40 
41  template<class VX, class VY>
43  EqInt<VX,VY>::post(Home home, ViewArray<VX>& x, VY y, int c) {
44  // Eliminate decided views
45  int n_x = x.size();
46  for (int i=n_x; i--; )
47  switch (holds(x[i],y)) {
48  case RT_FALSE:
49  x[i] = x[--n_x]; break;
50  case RT_TRUE:
51  x[i] = x[--n_x]; c--; break;
52  case RT_MAYBE:
53  break;
54  default:
56  }
57  x.size(n_x);
58  // RHS too small or too large
59  if ((c < 0) || (c > n_x))
60  return ES_FAILED;
61  // All views must be different
62  if (c == 0)
63  return post_false(home,x,y);
64  // All views must be equal
65  if (c == n_x)
66  return post_true(home,x,y);
67  // Compute how many subscriptions must be created
68  int n_s = std::max(c,n_x-c)+1;
69  assert(n_s <= n_x);
70  (void) new (home) EqInt<VX,VY>(home,x,n_s,y,c);
71  return ES_OK;
72  }
73 
74  template<class VX, class VY>
77  : IntBase<VX,VY>(home,p) {}
78 
79  template<class VX, class VY>
80  Actor*
82  return new (home) EqInt<VX,VY>(home,*this);
83  }
84 
85  template<class VX, class VY>
88  // Eliminate decided views from subscribed views
89  int n_x = x.size();
90  for (int i=n_s; i--; )
91  switch (holds(x[i],y)) {
92  case RT_FALSE:
93  x[i].cancel(home,*this,PC_INT_DOM);
94  x[i]=x[--n_s]; x[n_s]=x[--n_x];
95  break;
96  case RT_TRUE:
97  x[i].cancel(home,*this,PC_INT_DOM);
98  x[i]=x[--n_s]; x[n_s]=x[--n_x]; c--;
99  break;
100  case RT_MAYBE:
101  break;
102  default:
103  GECODE_NEVER;
104  }
105  x.size(n_x);
106  if ((c < 0) || (c > n_x))
107  return ES_FAILED;
108  // Eliminate decided views from unsubscribed views
109  for (int i=n_x; i-- > n_s; )
110  switch (holds(x[i],y)) {
111  case RT_FALSE: x[i]=x[--n_x]; break;
112  case RT_TRUE: x[i]=x[--n_x]; c--; break;
113  case RT_MAYBE: break;
114  default: GECODE_NEVER;
115  }
116  x.size(n_x);
117  if ((c < 0) || (c > n_x))
118  return ES_FAILED;
119  if (c == 0) {
120  // All views must be different
121  GECODE_ES_CHECK(post_false(home,x,y));
122  return home.ES_SUBSUMED(*this);
123  }
124  if (c == n_x) {
125  // All views must be equal
126  GECODE_ES_CHECK(post_true(home,x,y));
127  return home.ES_SUBSUMED(*this);
128  }
129  int m = std::max(c,n_x-c)+1;
130  assert(m <= n_x);
131  // Now, there must be new subscriptions from x[n_s] up to x[m-1]
132  while (n_s < m)
133  x[n_s++].subscribe(home,*this,PC_INT_DOM,false);
134  return ES_FIX;
135  }
136 
137 }}}
138 
139 // STATISTICS: int-prop
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Base-class for both propagators and branchers.
Definition: core.hpp:628
Home class for posting propagators
Definition: core.hpp:856
Propagator for counting views (equal integer to number of equal views)
Definition: count.hh:172
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: int-eq.hpp:87
virtual Actor * copy(Space &home)
Create copy during cloning.
Definition: int-eq.hpp:81
EqInt(Space &home, EqInt &p)
Constructor for cloning p.
Definition: int-eq.hpp:76
static ExecStatus post(Home home, ViewArray< VX > &x, VY y, int c)
Post propagator for .
Definition: int-eq.hpp:43
Baseclass for count propagators (integer)
Definition: count.hh:138
Computation spaces.
Definition: core.hpp:1742
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 SetOpType SetVar y
Definition: set.hh:767
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:3563
int ModEventDelta
Modification event deltas.
Definition: core.hpp:89
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:91
const FloatNum max
Largest allowed float value.
Definition: float.hh:844
RelTest holds(VX x, ConstIntView y)
Test whether x and y are equal.
Definition: rel.hpp:115
ExecStatus post_true(Home home, VX x, ConstIntView y)
Definition: rel.hpp:151
ExecStatus post_false(Home home, VX x, ConstIntView y)
Definition: rel.hpp:194
const Gecode::PropCond PC_INT_DOM
Propagate when domain changes.
Definition: var-type.hpp:100
@ RT_TRUE
Relation does hold.
Definition: view.hpp:1737
@ RT_MAYBE
Relation may hold or not.
Definition: view.hpp:1736
@ RT_FALSE
Relation does not hold.
Definition: view.hpp:1735
Gecode::FloatVal c(-8, 8)
Gecode::IntArgs i({1, 2, 3, 4})
#define forceinline
Definition: config.hpp:194
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:56