1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2017
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
namespace Gecode { namespace FlatZinc {
forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, double d)
: VarBranch<IntVar>(d), s(s0) {}
forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, IntAFC i, BoolAFC b)
: s(s0), iafc(i), bafc(b) {}
forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, IntAction i, BoolAction b)
: s(s0), iaction(i), baction(b) {}
forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, IntCHB i, BoolCHB b)
: s(s0), ichb(i), bchb(b) {}
forceinline IntBoolVarBranch::Select
IntBoolVarBranch::select(void) const {
return s;
}
forceinline IntAFC
IntBoolVarBranch::intafc(void) const {
return iafc;
}
forceinline BoolAFC
IntBoolVarBranch::boolafc(void) const {
return bafc;
}
forceinline IntAction
IntBoolVarBranch::intaction(void) const {
return iaction;
}
forceinline BoolAction
IntBoolVarBranch::boolaction(void) const {
return baction;
}
forceinline IntCHB
IntBoolVarBranch::intchb(void) const {
return ichb;
}
forceinline BoolCHB
IntBoolVarBranch::boolchb(void) const {
return bchb;
}
forceinline void
IntBoolVarBranch::expand(Home home, const IntVarArgs& x, const BoolVarArgs& y) {
switch (select()) {
case IntBoolVarBranch::SEL_AFC_MAX:
case IntBoolVarBranch::SEL_AFC_SIZE_MAX:
if (!iafc)
iafc = IntAFC(home,x,decay());
if (!bafc)
bafc = BoolAFC(home,y,decay());
break;
case IntBoolVarBranch::SEL_ACTION_MAX:
case IntBoolVarBranch::SEL_ACTION_SIZE_MAX:
if (!iaction)
iaction = IntAction(home,x,decay());
if (!baction)
baction = BoolAction(home,y,decay());
break;
case IntBoolVarBranch::SEL_CHB_MAX:
case IntBoolVarBranch::SEL_CHB_SIZE_MAX:
if (!ichb)
ichb = IntCHB(home,x);
if (!bchb)
bchb = BoolCHB(home,y);
break;
default: ;
}
}
inline IntBoolVarBranch
INTBOOL_VAR_AFC_MAX(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_MAX,d);
}
inline IntBoolVarBranch
INTBOOL_VAR_AFC_MAX(IntAFC ia, BoolAFC ba) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_MAX,ia,ba);
}
inline IntBoolVarBranch
INTBOOL_VAR_ACTION_MAX(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_MAX,d);
}
inline IntBoolVarBranch
INTBOOL_VAR_ACTION_MAX(IntAction ia, BoolAction ba) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_MAX,ia,ba);
}
inline IntBoolVarBranch
INTBOOL_VAR_CHB_MAX(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_MAX,d);
}
inline IntBoolVarBranch
INTBOOL_VAR_CHB_MAX(IntCHB ic, BoolCHB bc) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_MAX,ic,bc);
}
inline IntBoolVarBranch
INTBOOL_VAR_AFC_SIZE_MAX(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_SIZE_MAX,d);
}
inline IntBoolVarBranch
INTBOOL_VAR_AFC_SIZE_MAX(IntAFC ia, BoolAFC ba) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_SIZE_MAX,ia,ba);
}
inline IntBoolVarBranch
INTBOOL_VAR_ACTION_SIZE_MAX(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_SIZE_MAX,d);
}
inline IntBoolVarBranch
INTBOOL_VAR_ACTION_SIZE_MAX(IntAction ia, BoolAction ba) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_SIZE_MAX,ia,ba);
}
inline IntBoolVarBranch
INTBOOL_VAR_CHB_SIZE_MAX(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_SIZE_MAX,d);
}
inline IntBoolVarBranch
INTBOOL_VAR_CHB_SIZE_MAX(IntCHB ic, BoolCHB bc) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_SIZE_MAX,ic,bc);
}
forceinline
MeritMaxAFC::MeritMaxAFC(Space&, const IntBoolVarBranch& ibvb)
: iafc(ibvb.intafc()), bafc(ibvb.boolafc()) {}
forceinline
MeritMaxAFC::MeritMaxAFC(Space&, MeritMaxAFC& m)
: iafc(m.iafc), bafc(m.bafc) {}
forceinline double
MeritMaxAFC::operator ()(Int::IntView x, int) const {
return x.afc();
}
forceinline double
MeritMaxAFC::operator ()(Int::BoolView x, int) const {
return x.afc();
}
forceinline void
MeritMaxAFC::dispose(void) {
iafc.~IntAFC();
bafc.~BoolAFC();
}
forceinline
MeritMaxAFCSize::MeritMaxAFCSize(Space&, const IntBoolVarBranch& ibvb)
: iafc(ibvb.intafc()), bafc(ibvb.boolafc()) {}
forceinline
MeritMaxAFCSize::MeritMaxAFCSize(Space&, MeritMaxAFCSize& m)
: iafc(m.iafc), bafc(m.bafc) {}
forceinline double
MeritMaxAFCSize::operator ()(Int::IntView x, int) const {
return x.afc() / x.size();
}
forceinline double
MeritMaxAFCSize::operator ()(Int::BoolView x, int) const {
return x.afc() / 2.0;
}
forceinline void
MeritMaxAFCSize::dispose(void) {
iafc.~IntAFC();
bafc.~BoolAFC();
}
forceinline
MeritMaxAction::MeritMaxAction(Space&, const IntBoolVarBranch& ibvb)
: iaction(ibvb.intaction()), baction(ibvb.boolaction()) {}
forceinline
MeritMaxAction::MeritMaxAction(Space&, MeritMaxAction& m)
: iaction(m.iaction), baction(m.baction) {}
forceinline double
MeritMaxAction::operator ()(Int::IntView, int i) const {
return iaction[i];
}
forceinline double
MeritMaxAction::operator ()(Int::BoolView, int i) const {
return baction[i];
}
forceinline void
MeritMaxAction::dispose(void) {
iaction.~IntAction();
baction.~BoolAction();
}
forceinline
MeritMaxActionSize::MeritMaxActionSize(Space&, const IntBoolVarBranch& ibvb)
: iaction(ibvb.intaction()), baction(ibvb.boolaction()) {}
forceinline
MeritMaxActionSize::MeritMaxActionSize(Space&, MeritMaxActionSize& m)
: iaction(m.iaction), baction(m.baction) {}
forceinline double
MeritMaxActionSize::operator ()(Int::IntView x, int i) const {
return iaction[i] / x.size();
}
forceinline double
MeritMaxActionSize::operator ()(Int::BoolView, int i) const {
return baction[i] / 2.0;
}
forceinline void
MeritMaxActionSize::dispose(void) {
iaction.~IntAction();
baction.~BoolAction();
}
forceinline
MeritMaxCHB::MeritMaxCHB(Space&, const IntBoolVarBranch& ibvb)
: ichb(ibvb.intchb()), bchb(ibvb.boolchb()) {}
forceinline
MeritMaxCHB::MeritMaxCHB(Space&, MeritMaxCHB& m)
: ichb(m.ichb), bchb(m.bchb) {}
forceinline double
MeritMaxCHB::operator ()(Int::IntView, int i) const {
return ichb[i];
}
forceinline double
MeritMaxCHB::operator ()(Int::BoolView, int i) const {
return bchb[i];
}
forceinline void
MeritMaxCHB::dispose(void) {
ichb.~IntCHB();
bchb.~BoolCHB();
}
forceinline
MeritMaxCHBSize::MeritMaxCHBSize(Space&, const IntBoolVarBranch& ibvb)
: ichb(ibvb.intchb()), bchb(ibvb.boolchb()) {}
forceinline
MeritMaxCHBSize::MeritMaxCHBSize(Space&, MeritMaxCHBSize& m)
: ichb(m.ichb), bchb(m.bchb) {}
forceinline double
MeritMaxCHBSize::operator ()(Int::IntView x, int i) const {
return ichb[i] / x.size();
}
forceinline double
MeritMaxCHBSize::operator ()(Int::BoolView, int i) const {
return bchb[i] / 2.0;
}
forceinline void
MeritMaxCHBSize::dispose(void) {
ichb.~IntCHB();
bchb.~BoolCHB();
}
forceinline
PosIntChoice::PosIntChoice(const Brancher& b, unsigned int a, int p, int n)
: Choice(b,a), _pos(p), _val(n) {}
forceinline int
PosIntChoice::pos(void) const {
return _pos;
}
forceinline int
PosIntChoice::val(void) const {
return _val;
}
forceinline
IntBoolBrancherBase::
IntBoolBrancherBase(Home home,
ViewArray<Int::IntView> x0,
ViewArray<Int::BoolView> y0,
ValSelCommitBase<Int::IntView,int>* xvsc0,
ValSelCommitBase<Int::BoolView,int>* yvsc0)
: Brancher(home), x(x0), y(y0), start(0), xvsc(xvsc0), yvsc(yvsc0) {
home.notice(*this,AP_DISPOSE,true);
}
forceinline
IntBoolBrancherBase::
IntBoolBrancherBase(Space& home, IntBoolBrancherBase& b)
: Brancher(home,b), start(b.start),
xvsc(b.xvsc->copy(home)), yvsc(b.yvsc->copy(home)) {
x.update(home,b.x);
y.update(home,b.y);
}
forceinline size_t
IntBoolBrancherBase::dispose(Space& home) {
home.ignore(*this,AP_DISPOSE,true);
xvsc->dispose(home);
yvsc->dispose(home);
(void) Brancher::dispose(home);
return sizeof(IntBoolBrancherBase);
}
template<class Merit>
forceinline
IntBoolBrancher<Merit>::
IntBoolBrancher(Home home,
ViewArray<Int::IntView> x,
ViewArray<Int::BoolView> y,
Merit& m,
ValSelCommitBase<Int::IntView,int>* xvsc,
ValSelCommitBase<Int::BoolView,int>* yvsc)
: IntBoolBrancherBase(home,x,y,xvsc,yvsc), merit(m) {}
template<class Merit>
forceinline void
IntBoolBrancher<Merit>::
post(Home home,
ViewArray<Int::IntView> x,
ViewArray<Int::BoolView> y,
Merit& m,
ValSelCommitBase<Int::IntView,int>* xvsc,
ValSelCommitBase<Int::BoolView,int>* yvsc) {
(void) new (home) IntBoolBrancher<Merit>(home, x, y, m, xvsc, yvsc);
}
template<class Merit>
forceinline
IntBoolBrancher<Merit>::
IntBoolBrancher(Space& home, IntBoolBrancher<Merit>& b)
: IntBoolBrancherBase(home,b), merit(home, b.merit) {
}
template<class Merit>
Actor*
IntBoolBrancher<Merit>::copy(Space& home) {
return new (home) IntBoolBrancher<Merit>(home,*this);
}
template<class Merit>
const Choice*
IntBoolBrancher<Merit>::choice(Space& home) {
int p = start;
double m;
if (p < x.size()) {
assert(!x[p].assigned());
m=merit(x[p],p);
for (int i=p+1; i<x.size(); i++)
if (!x[i].assigned()) {
double mi = merit(x[i],i);
if (mi > m) {
m=mi; p=i;
}
}
for (int i=0; i<y.size(); i++)
if (!y[i].assigned()) {
double mi = merit(y[i],i);
if (mi > m) {
m=mi; p=i+x.size();
}
}
} else {
assert(!y[p-x.size()].assigned());
m=merit(y[p-x.size()],p-x.size());
for (int i=p-x.size()+1; i<y.size(); i++)
if (!y[i].assigned()) {
double mi = merit(y[i],i);
if (mi > m) {
m=mi; p=i+x.size();
}
}
}
int v;
if (p < x.size()) {
v=xvsc->val(home,x[p],p);
} else {
v=yvsc->val(home,y[p-x.size()],p-x.size());
}
return new PosIntChoice(*this,2,p,v);
}
template<class Merit>
size_t
IntBoolBrancher<Merit>::dispose(Space& home) {
merit.dispose();
(void) IntBoolBrancherBase::dispose(home);
return sizeof(IntBoolBrancher<Merit>);
}
forceinline BoolValBranch
i2b(const IntValBranch& ivb) {
switch (ivb.select()) {
case IntValBranch::SEL_MIN:
case IntValBranch::SEL_MED:
case IntValBranch::SEL_SPLIT_MIN:
case IntValBranch::SEL_RANGE_MIN:
return BOOL_VAL_MIN();
case IntValBranch::SEL_MAX:
case IntValBranch::SEL_SPLIT_MAX:
case IntValBranch::SEL_RANGE_MAX:
return BOOL_VAL_MAX();
case IntValBranch::SEL_RND:
return BOOL_VAL_RND(ivb.rnd());
case IntValBranch::SEL_VAL_COMMIT:
default:
GECODE_NEVER;
}
GECODE_NEVER;
return BOOL_VAL_MIN();
}
}}
// STATISTICS: flatzinc-branch
|