Generated on Wed Jan 24 2018 21:22:26 for Gecode by doxygen 1.8.13
view-values.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, 2008
8  *
9  * Last modified:
10  * $Date: 2017-04-01 20:27:10 +0200 (Sat, 01 Apr 2017) $ by $Author: schulte $
11  * $Revision: 15623 $
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 Branch {
39 
42  private:
44  class PosMin {
45  public:
47  unsigned int pos;
49  int min;
50  };
52  unsigned int n;
54  PosMin* pm;
55  public:
58  PosValuesChoice(const Brancher& b, const Pos& p, IntView x);
61  PosValuesChoice(const Brancher& b, unsigned int alt, Pos p, Archive& e);
63  int val(unsigned int a) const;
66  virtual size_t size(void) const;
69  virtual ~PosValuesChoice(void);
72  virtual void archive(Archive& e) const;
73  };
74 
75  forceinline int
76  PosValuesChoice::val(unsigned int a) const {
77  PosMin* l = &pm[0];
78  PosMin* r = &pm[n-1];
79  while (true) {
80  PosMin* m = l + (r-l)/2;
81  if (a < m->pos) {
82  r=m-1;
83  } else if (a >= (m+1)->pos) {
84  l=m+1;
85  } else {
86  return m->min + static_cast<int>(a - m->pos);
87  }
88  }
90  return 0;
91  }
92 
93 
94  template<int n, bool min, class Filter, class Print>
98  ViewSel<IntView>* vs[n],
99  IntBranchFilter bf,
100  IntVarValPrint vvp)
101  : ViewBrancher<IntView,Filter,n>(home,x,vs,bf), p(vvp) {
102  if (p.notice())
103  home.notice(*this,AP_DISPOSE,true);
104  }
105 
106  template<int n, bool min, class Filter, class Print>
107  forceinline void
111  IntBranchFilter bf,
112  IntVarValPrint vvp) {
113  (void) new (home) ViewValuesBrancher<n,min,Filter,Print>(home,x,vs,bf,vvp);
114  }
115 
116  template<int n, bool min, class Filter, class Print>
120  : ViewBrancher<IntView,Filter,n>(home,shared,b), p(home,shared,b.p) {}
121 
122  template<int n, bool min, class Filter, class Print>
123  Actor*
126  (home,shared,*this);
127  }
128 
129  template<int n, bool min, class Filter, class Print>
130  const Choice*
133  return new PosValuesChoice(*this,p,
135  }
136 
137  template<int n, bool min, class Filter, class Print>
138  const Choice*
140  (const Space& home, Archive& e) {
141  (void) home;
142  int p;
143  unsigned int a;
144  e >> p >> a;
145  return new PosValuesChoice(*this,a,p,e);
146  }
147 
148  template<int n, bool min, class Filter, class Print>
149  ExecStatus
151  unsigned int a) {
152  const PosValuesChoice& pvc
153  = static_cast<const PosValuesChoice&>(c);
155  unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
156  return me_failed(x.eq(home,pvc.val(b))) ? ES_FAILED : ES_OK;
157  }
158 
159  template<int n, bool min, class Filter, class Print>
160  NGL*
162  unsigned int a) const {
163  const PosValuesChoice& pvc
164  = static_cast<const PosValuesChoice&>(c);
166  unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
167  return new (home) EqNGL<IntView>(home,x,pvc.val(b));
168  }
169 
170  template<int n, bool min, class Filter, class Print>
171  void
173  const Choice& c,
174  unsigned int a,
175  std::ostream& o) const {
176  const PosValuesChoice& pvc
177  = static_cast<const PosValuesChoice&>(c);
179  unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
180  int nn = pvc.val(b);
181  if (p)
182  p(home,*this,a,x,pvc.pos().pos,nn,o);
183  else
184  o << "var[" << pvc.pos().pos << "] = " << nn;
185  }
186 
187  template<int n, bool min, class Filter, class Print>
188  forceinline size_t
190  if (p.notice())
191  home.ignore(*this,AP_DISPOSE,true);
194  }
195 
196  template<int n, bool min>
197  forceinline void
200  IntBranchFilter bf,
201  IntVarValPrint vvp) {
202  if (bf) {
203  if (vvp) {
206  ::post(home,x,vs,bf,vvp);
207  } else {
210  ::post(home,x,vs,bf,vvp);
211  }
212  } else {
213  if (vvp) {
216  ::post(home,x,vs,bf,vvp);
217  } else {
220  ::post(home,x,vs,bf,vvp);
221  }
222  }
223  }
224 
225 
226 }}}
227 
228 // STATISTICS: int-branch
unsigned int alternatives(void) const
Return number of alternatives.
Definition: core.hpp:3821
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
Actor must always be disposed.
Definition: core.hpp:630
ModEvent eq(Space &home, int n)
Restrict domain values to be equal to n.
Definition: int.hpp:160
virtual void print(const Space &home, const Choice &c, unsigned int a, std::ostream &o) const
Print branch for choice c and alternative a.
Generic brancher by view selection.
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
virtual const Choice * choice(Space &home)
Return choice.
ViewArray< IntView > x
Views to branch on.
Computation spaces.
Definition: core.hpp:1748
Class storing a print function.
const Pos & pos(void) const
Return position in array.
No-good literal for equality.
Definition: branch.hh:411
Class without print function.
Brancher by view and values selection
Definition: branch.hh:626
Base-class for both propagators and branchers.
Definition: core.hpp:696
ViewValuesBrancher(Space &home, bool shared, ViewValuesBrancher &b)
Constructor for cloning b.
ViewSel< IntView > * vs[n]
View selection objects.
Pos pos(Space &home)
Return position information.
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Base-class for branchers.
Definition: core.hpp:1446
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Execution has resulted in failure.
Definition: core.hpp:542
unsigned int size(I &i)
Size of all ranges of range iterator i.
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
Choice storing position and values for integer views
Definition: view-values.hpp:41
Choices storing position
std::function< bool(const Space &home, IntVar x, int i)> IntBranchFilter
Branch filter function type for integer variables.
Definition: int.hh:3843
Position information.
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3321
std::function< void(const Space &home, const Brancher &b, unsigned int a, IntVar x, int i, const int &n, std::ostream &o)> IntVarValPrint
Function type for printing branching alternatives for integer variables.
Definition: int.hh:4201
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:784
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:71
Integer view for integer variables.
Definition: view.hpp:129
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:4141
static void post(Home home, ViewArray< IntView > &x, ViewSel< IntView > *vs[n], IntBranchFilter bf, IntVarValPrint vvp)
Post function for creation.
Choice for performing commit
Definition: core.hpp:1414
Archive representation
Definition: archive.hpp:45
virtual NGL * ngl(Space &home, const Choice &c, unsigned int a) const
Create no-good literal for choice c and alternative a.
ExecStatus
Definition: core.hpp:540
#define forceinline
Definition: config.hpp:173
virtual Actor * copy(Space &home, bool share)
Perform cloning.
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Post propagator for SetVar x
Definition: set.hh:784
Execution is okay.
Definition: core.hpp:544
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int a)
Perform commit for choice c and alternative a.
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:702
Gecode toplevel namespace
int val(unsigned int a) const
Return value to branch with for alternative a.
Definition: view-values.hpp:76
#define GECODE_VTABLE_EXPORT
Definition: support.hh:76
const int pos
Position of view.
#define GECODE_INT_EXPORT
Definition: int.hh:81
Home class for posting propagators
Definition: core.hpp:922
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
void postviewvaluesbrancher(Home home, ViewArray< IntView > &x, ViewSel< IntView > *vs[n], IntBranchFilter bf, IntVarValPrint vvp)
Post brancher for view and values.
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition: modevent.hpp:58
No-good literal recorded during search.
Definition: core.hpp:1342