this repo has no description
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, 2003
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 <climits>
35
36namespace Gecode { namespace Int { namespace Distinct {
37
38 template<class View>
39 forceinline
40 Dom<View>::Dom(Home home, ViewArray<View>& x)
41 : NaryPropagator<View,PC_INT_DOM>(home,x) {}
42
43 template<class View>
44 ExecStatus
45 Dom<View>::post(Home home, ViewArray<View>& x) {
46 if (x.size() == 2)
47 return Rel::Nq<View,View>::post(home,x[0],x[1]);
48 if (x.size() == 3)
49 return TerDom<View>::post(home,x[0],x[1],x[2]);
50 if (x.size() > 3) {
51 // Do bounds propagation to make view-value graph smaller
52 GECODE_ES_CHECK(prop_bnd<View>(home,x));
53 (void) new (home) Dom<View>(home,x);
54 }
55 return ES_OK;
56 }
57
58 template<class View>
59 forceinline
60 Dom<View>::Dom(Space& home, Dom<View>& p)
61 : NaryPropagator<View,PC_INT_DOM>(home,p) {}
62
63 template<class View>
64 PropCost
65 Dom<View>::cost(const Space&, const ModEventDelta& med) const {
66 if (View::me(med) == ME_INT_VAL)
67 return PropCost::linear(PropCost::LO, x.size());
68 else
69 return PropCost::quadratic(PropCost::HI, x.size());
70 }
71
72 template<class View>
73 Actor*
74 Dom<View>::copy(Space& home) {
75 return new (home) Dom<View>(home,*this);
76 }
77
78#ifdef GECODE_HAS_CBS
79 template<class View>
80 void
81 Dom<View>::solndistrib(Space& home, Propagator::SendMarginal send) const {
82 cbsdistinct(home,this->id(),x,send);
83 }
84
85 template<class View>
86 void
87 Dom<View>::domainsizesum(Propagator::InDecision in, unsigned int& size,
88 unsigned int& size_b) const {
89 cbssize(x,in,size,size_b);
90 }
91#endif
92
93
94 template<class View>
95 ExecStatus
96 Dom<View>::propagate(Space& home, const ModEventDelta& med) {
97 if (View::me(med) == ME_INT_VAL) {
98 ExecStatus es = prop_val<View,false>(home,x);
99 GECODE_ES_CHECK(es);
100 if (x.size() < 2)
101 return home.ES_SUBSUMED(*this);
102 if (es == ES_FIX)
103 return home.ES_FIX_PARTIAL(*this,View::med(ME_INT_DOM));
104 es = prop_bnd<View>(home,x);
105 GECODE_ES_CHECK(es);
106 if (x.size() < 2)
107 return home.ES_SUBSUMED(*this);
108 es = prop_val<View,true>(home,x);
109 GECODE_ES_CHECK(es);
110 if (x.size() < 2)
111 return home.ES_SUBSUMED(*this);
112 return home.ES_FIX_PARTIAL(*this,View::med(ME_INT_DOM));
113 }
114
115 if (x.size() == 2)
116 GECODE_REWRITE(*this,(Rel::Nq<View,View>::post(home(*this),x[0],x[1])));
117 if (x.size() == 3)
118 GECODE_REWRITE(*this,TerDom<View>::post(home(*this),x[0],x[1],x[2]));
119
120 if (dc.available()) {
121 GECODE_ES_CHECK(dc.sync());
122 } else {
123 GECODE_ES_CHECK(dc.init(home,x));
124 }
125
126 bool assigned;
127 GECODE_ES_CHECK(dc.propagate(home,assigned));
128
129 return ES_FIX;
130 }
131
132}}}
133
134// STATISTICS: int-prop
135