Generated on Thu Feb 21 2013 23:11:43 for Gecode by doxygen 1.8.3.1
graph.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, 2011
8  *
9  * Last modified:
10  * $Date: 2011-09-28 22:14:32 +1000 (Wed, 28 Sep 2011) $ by $Author: tack $
11  * $Revision: 12417 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Int { namespace NValues {
39 
42  : n_matched(0) {}
43 
44  forceinline int
45  Graph::size(void) const {
46  return n_matched;
47  }
48 
49  forceinline void
50  Graph::init(Space& home, const ValSet& vs, const ViewArray<IntView>& x) {
51  using namespace ViewValGraph;
52  n_view = x.size() + vs.size();
53  view = home.alloc<ViewNode<IntView>*>(n_view);
54 
55  // Create nodes corresponding to the value set vs
56  {
57  int i = x.size();
58  ValSet::Ranges vsr(vs);
59  ValNode<IntView>** v = &val;
60  for (Iter::Ranges::ToValues<ValSet::Ranges> n(vsr); n(); ++n) {
61  // Create view node
62  view[i] = new (home) ViewNode<IntView>();
63  // Create and link value node
64  ValNode<IntView>* nv = new (home) ValNode<IntView>(n.val());
65  *v = nv; v = nv->next_val_ref();
66  // Create and link single edge
67  Edge<IntView>** e = view[i]->val_edges_ref();
68  *e = new (home) Edge<IntView>(nv,view[i],NULL);
69  // Match edge
70  (*e)->revert(view[i]); nv->matching(*e);
71  i++;
72  }
73  *v = NULL;
74  n_val = vs.size();
75  n_matched = vs.size();
76  assert(i - x.size() == vs.size());
77  }
78 
79  // Initialize real view nodes
80  for (int i=x.size(); i--; ) {
81  view[i] = new (home) ViewNode<IntView>(x[i]);
83  }
84 
85  // Match the real view nodes, if possible
86  Region r(home);
88  for (int i = x.size(); i--; )
89  if (match(m,view[i]))
90  n_matched++;
91  }
92 
93  forceinline void
94  Graph::sync(Space& home) {
95  using namespace ViewValGraph;
96  Region r(home);
97 
98  // Whether to rematch
99  bool rematch = false;
100 
101  // Synchronize nodes
102  for (int i = n_view; i--; ) {
103  ViewNode<IntView>* x = view[i];
104  // Skip faked view nodes, they correspond to values in the value set
105  if (!x->fake()) {
106  if (x->changed()) {
107  ViewRanges<IntView> r(x->view());
108  Edge<IntView>* m = x->matched() ? x->edge_fst() : NULL;
109  Edge<IntView>** p = x->val_edges_ref();
110  Edge<IntView>* e = *p;
111  do {
112  while (e->val(x)->val() < r.min()) {
113  // Skip edge
114  e->unlink(); e->mark();
115  e = e->next_edge();
116  }
117  *p = e;
118  assert(r.min() == e->val(x)->val());
119  // This edges must be kept
120  for (unsigned int j=r.width(); j--; ) {
121  e->free();
122  p = e->next_edge_ref();
123  e = e->next_edge();
124  }
125  ++r;
126  } while (r());
127  *p = NULL;
128  while (e != NULL) {
129  e->unlink(); e->mark();
130  e = e->next_edge();
131  }
132  if ((m != NULL) && m->marked()) {
133  // Matching has been deleted!
134  m->val(x)->matching(NULL);
135  rematch = true;
136  n_matched--;
137  }
138  } else {
139  // Just free edges
140  for (Edge<IntView>* e=x->val_edges(); e != NULL; e = e->next_edge())
141  e->free();
142  }
143  }
144  }
145 
146  if (rematch) {
148  for (int i = n_view; i--; )
149  if (!view[i]->matched() && match(m,view[i]))
150  n_matched++;
151  }
152  }
153 
154  forceinline bool
156  using namespace ViewValGraph;
157  Region r(home);
158 
159  int n_view_visited = 0;
160  {
161  // Marks all edges as used that are on simple paths in the graph
162  // that start from a free value node by depth-first-search
164 
165  // Insert all free value nodes
166  count++;
167  {
168  ValNode<IntView>** v = &val;
169  while (*v != NULL)
170  // Is the node free?
171  if (!(*v)->matching()) {
172  // Eliminate empty value nodes
173  if ((*v)->empty()) {
174  *v = (*v)->next_val();
175  n_val--;
176  } else {
177  (*v)->min = count;
178  visit.push(*v);
179  v = (*v)->next_val_ref();
180  }
181  } else {
182  v = (*v)->next_val_ref();
183  }
184  }
185 
186  // Invariant: only value nodes are on the stack!
187  while (!visit.empty()) {
188  ValNode<IntView>* n = visit.pop();
189  for (Edge<IntView>* e = n->edge_fst(); e != n->edge_lst();
190  e = e->next()) {
191  // Is the view node is matched: the path must be alternating!
192  ViewNode<IntView>* x = e->view(n);
193  if (x->matched()) {
194  // Mark the edge as used
195  e->use();
196  if (x->min < count) {
197  n_view_visited++;
198  x->min = count;
199  assert(x->edge_fst()->next() == x->edge_lst());
200  ValNode<IntView>* m = x->edge_fst()->val(x);
201  x->edge_fst()->use();
202  if (m->min < count) {
203  m->min = count;
204  visit.push(m);
205  }
206  }
207  }
208  }
209  }
210 
211  }
212 
213  if (n_view_visited < n_view) {
214  // Mark all edges as used starting from a free view node on
215  // an alternating path by depth-first search.
217 
218  // Insert all free view nodes
219  count++;
220  for (int i = n_view; i--; )
221  if (!view[i]->matched()) {
222  view[i]->min = count;
223  visit.push(view[i]);
224  }
225 
226  // Invariant: only view nodes are on the stack!
227  while (!visit.empty()) {
228  n_view_visited++;
229  ViewNode<IntView>* x = visit.pop();
230  for (Edge<IntView>* e = x->val_edges(); e != NULL; e = e->next_edge())
231  // Consider only free edges
232  if (e != x->edge_fst()) {
233  ValNode<IntView>* n = e->val(x);
234  // Is there a matched edge from the value node to a view node?
235  if (n->matching() != NULL) {
236  e->use();
237  n->matching()->use();
238  ViewNode<IntView>* y = n->matching()->view(n);
239  if (y->min < count) {
240  y->min = count;
241  visit.push(y);
242  }
243  }
244  }
245  }
246 
247  }
248 
249  if (n_view_visited < n_view) {
250  scc(home);
251  return true;
252  } else {
253  return false;
254  }
255  }
256 
259  using namespace ViewValGraph;
260  // Tell constraints and also eliminate nodes and edges
261  for (int i = n_view; i--; ) {
262  ViewNode<IntView>* x = view[i];
263  if (!x->fake()) {
264  if (x->matched() && !x->edge_fst()->used(x)) {
265  GECODE_ME_CHECK(x->view().eq(home,x->edge_fst()->val(x)->val()));
266  x->edge_fst()->val(x)->matching(NULL);
267  for (Edge<IntView>* e = x->val_edges(); e != NULL; e=e->next_edge())
268  e->unlink();
269  view[i] = view[--n_view];
270  } else {
271  IterPruneVal<IntView> pv(x);
272  GECODE_ME_CHECK(x->view().minus_v(home,pv,false));
273  }
274  }
275  }
276 
277  return ES_OK;
278  }
279 
280 }}}
281 
282 // STATISTICS: int-prop
283