Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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 * 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
34namespace Gecode { namespace Int { namespace Branch {
35
38 private:
40 class PosMin {
41 public:
43 unsigned int pos;
45 int min;
46 };
48 unsigned int n;
50 PosMin* pm;
51 public:
54 PosValuesChoice(const Brancher& b, const Pos& p, IntView x);
57 PosValuesChoice(const Brancher& b, unsigned int alt, Pos p, Archive& e);
59 int val(unsigned int a) const;
62 virtual ~PosValuesChoice(void);
65 virtual void archive(Archive& e) const;
66 };
67
68 forceinline int
69 PosValuesChoice::val(unsigned int a) const {
70 PosMin* l = &pm[0];
71 PosMin* r = &pm[n-1];
72 while (true) {
73 PosMin* m = l + (r-l)/2;
74 if (a < m->pos) {
75 r=m-1;
76 } else if (a >= (m+1)->pos) {
77 l=m+1;
78 } else {
79 return m->min + static_cast<int>(a - m->pos);
80 }
81 }
83 return 0;
84 }
85
86
87 template<int n, bool min, class Filter, class Print>
94 : ViewBrancher<IntView,Filter,n>(home,x,vs,bf), p(vvp) {
95 if (p.notice())
96 home.notice(*this,AP_DISPOSE,true);
97 }
98
99 template<int n, bool min, class Filter, class Print>
100 forceinline void
108
109 template<int n, bool min, class Filter, class Print>
114
115 template<int n, bool min, class Filter, class Print>
116 Actor*
121
122 template<int n, bool min, class Filter, class Print>
123 const Choice*
129
130 template<int n, bool min, class Filter, class Print>
131 const Choice*
133 (const Space& home, Archive& e) {
134 (void) home;
135 int p;
136 unsigned int a;
137 e >> p >> a;
138 return new PosValuesChoice(*this,a,p,e);
139 }
140
141 template<int n, bool min, class Filter, class Print>
144 unsigned int a) {
145 const PosValuesChoice& pvc
146 = static_cast<const PosValuesChoice&>(c);
148 unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
149 return me_failed(x.eq(home,pvc.val(b))) ? ES_FAILED : ES_OK;
150 }
151
152 template<int n, bool min, class Filter, class Print>
153 NGL*
155 unsigned int a) const {
156 const PosValuesChoice& pvc
157 = static_cast<const PosValuesChoice&>(c);
159 unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
160 return new (home) EqNGL<IntView>(home,x,pvc.val(b));
161 }
162
163 template<int n, bool min, class Filter, class Print>
164 void
166 const Choice& c,
167 unsigned int a,
168 std::ostream& o) const {
169 const PosValuesChoice& pvc
170 = static_cast<const PosValuesChoice&>(c);
172 unsigned int b = min ? a : (pvc.alternatives() - 1 - a);
173 int nn = pvc.val(b);
174 if (p)
175 p(home,*this,a,x,pvc.pos().pos,nn,o);
176 else
177 o << "var[" << pvc.pos().pos << "] = " << nn;
178 }
179
180 template<int n, bool min, class Filter, class Print>
181 forceinline size_t
183 if (p.notice())
184 home.ignore(*this,AP_DISPOSE,true);
187 }
188
189 template<int n, bool min>
190 forceinline void
192 ViewSel<IntView>* vs[n],
194 IntVarValPrint vvp) {
195 if (bf) {
196 if (vvp) {
199 ::post(home,x,vs,bf,vvp);
200 } else {
203 ::post(home,x,vs,bf,vvp);
204 }
205 } else {
206 if (vvp) {
209 ::post(home,x,vs,bf,vvp);
210 } else {
213 ::post(home,x,vs,bf,vvp);
214 }
215 }
216 }
217
218
219}}}
220
221// STATISTICS: int-branch
Base-class for both propagators and branchers.
Definition core.hpp:628
Archive representation
Definition archive.hpp:42
Class without print function.
Definition print.hpp:73
Class storing a print function.
Definition print.hpp:47
Base-class for branchers.
Definition core.hpp:1444
friend class Space
Definition core.hpp:1446
friend class Choice
Definition core.hpp:1447
Choice for performing commit
Definition core.hpp:1414
unsigned int alternatives(void) const
Return number of alternatives.
Definition core.hpp:3777
Home class for posting propagators
Definition core.hpp:856
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:3223
No-good literal for equality.
Definition branch.hh:411
Choice storing position and values for integer views
int val(unsigned int a) const
Return value to branch with for alternative a.
PosValuesChoice(const Brancher &b, const Pos &p, IntView x)
Initialize choice for brancher b, position p, and view x.
virtual void archive(Archive &e) const
Archive into e.
Brancher by view and values selection
Definition branch.hh:626
virtual void print(const Space &home, const Choice &c, unsigned int a, std::ostream &o) const
Print branch for choice c and alternative a.
ViewValuesBrancher(Space &home, ViewValuesBrancher &b)
Constructor for cloning b.
virtual Actor * copy(Space &home)
Perform cloning.
static void post(Home home, ViewArray< IntView > &x, ViewSel< IntView > *vs[n], IntBranchFilter bf, IntVarValPrint vvp)
Post function for creation.
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int a)
Perform commit for choice c and alternative a.
virtual const Choice * choice(Space &home)
Return choice.
virtual size_t dispose(Space &home)
Delete brancher and return its size.
virtual NGL * ngl(Space &home, const Choice &c, unsigned int a) const
Create no-good literal for choice c and alternative a.
Integer view for integer variables.
Definition view.hpp:129
No-good literal recorded during search.
Definition core.hpp:1342
const Pos & pos(void) const
Return position in array.
Definition view.hpp:126
PosChoice(const PosChoice &c)
Initialize.
Position information.
Definition view.hpp:44
const int pos
Position of view.
Definition view.hpp:47
View arrays.
Definition array.hpp:253
Pos pos(Space &home)
Return position information.
Definition view.hpp:173
ViewBrancher(Space &home, ViewBrancher< IntView, Filter, n > &b)
Definition view.hpp:152
View view(const Pos &p) const
Return view according to position information p.
Definition view.hpp:213
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition view.hpp:219
Abstract class for view selection.
Definition view-sel.hpp:44
#define GECODE_INT_EXPORT
Definition int.hh:81
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4081
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition modevent.hpp:54
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
std::function< bool(const Space &home, IntVar x, int i)> IntBranchFilter
Branch filter function type for integer variables.
Definition int.hh:4191
Integer branchers.
void postviewvaluesbrancher(Home home, ViewArray< IntView > &x, ViewSel< IntView > *vs[n], IntBranchFilter bf, IntVarValPrint vvp)
Post brancher for view and values.
Finite domain integers.
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:773
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
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:4567
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
Post propagator for SetVar x
Definition set.hh:773
#define forceinline
Definition config.hpp:194
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56
#define GECODE_VTABLE_EXPORT
Definition support.hh:72