/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christopher Mears * * Copyright: * Christopher Mears, 2012 * * 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. * */ #include #include namespace Gecode { namespace Int { namespace LDSB { forceinline Literal::Literal(void) : _variable(-1), _value(-1) {} forceinline Literal::Literal(int idx, int val) : _variable(idx), _value(val) {} forceinline bool Literal::operator <(const Literal &rhs) const { int d = rhs._variable - _variable; if (d > 0) return true; else if (d == 0) return rhs._value > _value; else return false; } template inline LDSBChoice::LDSBChoice(const Brancher& b, unsigned int a, const Pos& p, const Val& n, const Literal* literals, int nliterals) : PosValChoice(b,a,p,n), _literals(literals), _nliterals(nliterals) {} template LDSBChoice::~LDSBChoice(void) { delete [] _literals; } template forceinline const Literal* LDSBChoice::literals(void) const { return _literals; } template forceinline int LDSBChoice::nliterals(void) const { return _nliterals; } template void LDSBChoice::archive(Archive& e) const { PosValChoice::archive(e); e << _nliterals; for (int i = 0 ; i < _nliterals ; i++) { e << _literals[i]._variable; e << _literals[i]._value; } } template LDSBBrancher ::LDSBBrancher(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc, SymmetryImp** syms, int nsyms, BranchFilter::Var> bf, VarValPrint::Var,Val> vvp) : ViewValBrancher(home, x, vs, vsc, bf, vvp), _syms(syms), _nsyms(nsyms), _prevPos(-1) { home.notice(*this, AP_DISPOSE, true); } template forceinline void LDSBBrancher:: post(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc, SymmetryImp** syms, int nsyms, BranchFilter::Var> bf, VarValPrint::Var,Val> vvp) { (void) new (home) LDSBBrancher (home,x,vs,vsc,syms,nsyms,bf,vvp); } template forceinline LDSBBrancher:: LDSBBrancher(Space& home, LDSBBrancher& b) : ViewValBrancher(home,b), _nsyms(b._nsyms), _prevPos(b._prevPos) { _syms = home.alloc*>(_nsyms); for (int i = 0 ; i < _nsyms ; i++) _syms[i] = b._syms[i]->copy(home); } template Actor* LDSBBrancher::copy(Space& home) { return new (home) LDSBBrancher (home,*this); } // Compute choice template const Choice* LDSBBrancher::choice(Space& home) { // Making the PVC here is not so nice, I think. const Choice* c = ViewValBrancher::choice(home); const PosValChoice* pvc = static_cast* >(c); // Compute symmetries. int choicePos = pvc->pos().pos; int choiceVal = pvc->val(); delete c; _prevPos = choicePos; // TODO: It should be possible to use simpler containers than the // STL ones here. std::deque queue; std::set seen; seen.insert(Literal(choicePos, choiceVal)); queue.push_back(Literal(choicePos, choiceVal)); do { Literal l = queue[0]; queue.pop_front(); for (int i = 0 ; i < _nsyms ; i++) { ArgArray toExclude = _syms[i]->symmetric(l, this->x); for (int j = 0 ; j < toExclude.size() ; ++j) { if (seen.find(toExclude[j]) == seen.end()) queue.push_back(toExclude[j]); seen.insert(toExclude[j]); } } } while (queue.size() > 0); // Convert "seen" vector into array. int nliterals = static_cast(seen.size()); Literal* literals = new Literal[nliterals]; std::set::iterator it = seen.begin(); for (int i = 0 ; i < nliterals ; i++) { literals[i] = *it; ++it; } return new LDSBChoice(*this,a,choicePos,choiceVal, literals, nliterals); } template const Choice* LDSBBrancher::choice(const Space& home, Archive& e) { (void) home; int p; e >> p; Val v; e >> v; int nliterals; e >> nliterals; Literal* literals = new Literal[nliterals]; for (int i = 0 ; i < nliterals ; i++) { e >> literals[i]._variable; e >> literals[i]._value; } return new LDSBChoice(*this,a,p,v, literals, nliterals); } template <> inline ModEvent prune(Space& home, Int::IntView x, int v) { return x.nq(home, v); } template <> inline ModEvent prune(Space& home, Int::BoolView x, int v) { return x.nq(home, v); } template ExecStatus LDSBBrancher ::commit(Space& home, const Choice& c, unsigned int b) { const LDSBChoice& pvc = static_cast&>(c); int choicePos = pvc.pos().pos; int choiceVal = pvc.val(); if (b == 0) { // Post the branching constraint. ExecStatus fromBase = ViewValBrancher ::commit(home, c, b); GECODE_ES_CHECK(fromBase); for (int i = 0 ; i < this->_nsyms ; i++) this->_syms[i]->update(Literal(choicePos, choiceVal)); } else if (b == 1) { // Post the branching constraint. ExecStatus fromBase = ViewValBrancher ::commit(home, c, b); GECODE_ES_CHECK(fromBase); // Post prunings. int nliterals = pvc.nliterals(); const Literal* literals = pvc.literals(); for (int i = 0 ; i < nliterals ; i++) { const Literal& l = literals[i]; ModEvent me = prune(home, this->x[l._variable], l._value); GECODE_ME_CHECK(me); } } return ES_OK; } template size_t LDSBBrancher::dispose(Space& home) { home.ignore(*this,AP_DISPOSE,true); (void) ViewValBrancher::dispose(home); return sizeof(LDSBBrancher); } template forceinline void postldsbbrancher(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc, SymmetryImp** syms, int nsyms, BranchFilter bf, VarValPrint vvp) { if (bf) { if (vvp) { LDSBBrancher,BrancherPrint> ::post(home,x,vs,vsc,syms,nsyms,bf,vvp); } else { LDSBBrancher,BrancherNoPrint > ::post(home,x,vs,vsc,syms,nsyms,bf,vvp); } } else { if (vvp) { LDSBBrancher,BrancherPrint> ::post(home,x,vs,vsc,syms,nsyms,bf,vvp); } else { LDSBBrancher,BrancherNoPrint > ::post(home,x,vs,vsc,syms,nsyms,bf,vvp); } } } }}} // STATISTICS: int-branch