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
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Samuel Gagnon <samuel.gagnon92@gmail.com>
*
* Copyright:
* Samuel Gagnon, 2018
*
* 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.
*
*/
#ifdef GECODE_HAS_CBS
namespace Gecode { namespace Int { namespace Branch {
template<class View>
forceinline void
CBSBrancher<View>::VarIdToPos::init() {
assert(object() == nullptr);
object(new VarIdToPosO());
}
template<class View>
forceinline bool
CBSBrancher<View>::VarIdToPos::isIn(unsigned int var_id) const {
auto *hm = &static_cast<VarIdToPosO*>(object())->_varIdToPos;
return hm->find(var_id) != hm->end();
}
template<class View>
forceinline int
CBSBrancher<View>::VarIdToPos::operator[](unsigned int i) const {
return static_cast<VarIdToPosO*>(object())->_varIdToPos.at(i);
}
template<class View>
forceinline void
CBSBrancher<View>::VarIdToPos::insert(unsigned int var_id,
unsigned int pos) {
static_cast<VarIdToPosO*>(object())
->_varIdToPos.insert(std::make_pair(var_id, pos));
}
template<class View>
CBSBrancher<View>::CBSBrancher(Home home, ViewArray<View>& x0)
: Brancher(home), x(x0),
logProp(typename decltype(logProp)::size_type(),
typename decltype(logProp)::hasher(),
typename decltype(logProp)::key_equal(),
typename decltype(logProp)::allocator_type(home)) {
home.notice(*this, AP_DISPOSE);
varIdToPos.init();
for (int i=0; i<x.size(); i++)
varIdToPos.insert(x[i].id(), i);
}
template<class View>
forceinline void
CBSBrancher<View>::post(Home home, ViewArray<View>& x) {
(void) new (home) CBSBrancher(home,x);
}
template<class View>
Actor* CBSBrancher<View>::copy(Space& home) {
return new (home) CBSBrancher(home,*this);
}
template<class View>
forceinline size_t
CBSBrancher<View>::dispose(Space& home) {
home.ignore(*this, AP_DISPOSE);
varIdToPos.~VarIdToPos();
(void) Brancher::dispose(home);
return sizeof(*this);
}
template<class View>
CBSBrancher<View>::CBSBrancher(Space& home, CBSBrancher& b)
: Brancher(home,b),
varIdToPos(b.varIdToPos),
logProp(b.logProp.begin(), b.logProp.end(),
typename decltype(logProp)::size_type(),
typename decltype(logProp)::hasher(),
typename decltype(logProp)::key_equal(),
typename decltype(logProp)::allocator_type(home)) {
x.update(home,b.x);
}
template<class View>
bool CBSBrancher<View>::status(const Space& home) const {
for (Propagators p(home, PropagatorGroup::all); p(); ++p) {
// Sum of domains of all variable in propagator
unsigned int domsum;
// Same, but for variables that are also in this brancher.
unsigned int domsum_b;
// If the propagator doesn't support counting-based search, domsum and
// domsum_b are going to be equal to 0.
p.propagator().domainsizesum([this](unsigned int var_id)
{ return inbrancher(var_id); },
domsum, domsum_b);
if (domsum_b > 0)
return true;
}
return false;
}
template<class View>
forceinline bool
CBSBrancher<View>::inbrancher(unsigned int varId) const {
return varIdToPos.isIn(varId);
}
template<class View>
const Choice* CBSBrancher<View>::choice(Space& home) {
// Structure for keeping the maximum solution density assignment
struct {
unsigned int var_id;
int val;
double dens;
} maxSD{0, 0, -1};
// Lambda we pass to propagators via solndistrib to query solution densities
auto SendMarginal = [this](unsigned int prop_id, unsigned int var_id,
int val, double dens) {
if (logProp[prop_id].dens < dens) {
logProp[prop_id].var_id = var_id;
logProp[prop_id].val = val;
logProp[prop_id].dens = dens;
}
};
for (auto& kv : logProp)
kv.second.visited = false;
for (Propagators p(home, PropagatorGroup::all); p(); ++p) {
unsigned int prop_id = p.propagator().id();
unsigned int domsum;
unsigned int domsum_b;
p.propagator().domainsizesum([this](unsigned int var_id)
{ return inbrancher(var_id); },
domsum, domsum_b);
// If the propagator doesn't share any unasigned variables with this
// brancher, we continue and it will be deleted from the log afterwards.
if (domsum_b == 0)
continue;
// New propagators can be created as we solve the problem. If this is the
// case we create a new entry in the log.
if (logProp.find(prop_id) == logProp.end())
logProp.insert(std::make_pair(prop_id, PropInfo{0, 0, 0, -1, true}));
else
logProp[prop_id].visited = true;
// If the domain size sum of all variables in the propagator has changed
// since the last time we called this function, we need to recompute
// solution densities. Otherwise, we can reuse them.
if (logProp[prop_id].domsum != domsum) {
logProp[prop_id].dens = -1;
// Solution density computation
p.propagator().solndistrib(home, SendMarginal);
logProp[prop_id].domsum = domsum;
}
}
// We delete unvisited propagators from the log and look for the highest
// solution density across all propagators.
for (const auto& kv : logProp) {
unsigned int prop_id = kv.first;
const PropInfo& info = kv.second;
if (!info.visited)
logProp.erase(prop_id);
else if (info.dens > maxSD.dens)
maxSD = {info.var_id, info.val, info.dens};
}
assert(maxSD.dens != -1);
assert(!x[varIdToPos[maxSD.var_id]].assigned());
return new PosValChoice<int>(*this, 2, varIdToPos[maxSD.var_id], maxSD.val);
}
template<class View>
forceinline const Choice*
CBSBrancher<View>::choice(const Space&, Archive& e) {
int pos, val;
e >> pos >> val;
return new PosValChoice<int>(*this, 2, pos, val);
}
template<class View>
forceinline ExecStatus
CBSBrancher<View>::commit(Space& home, const Choice& c, unsigned int a) {
const auto& pvc = static_cast<const PosValChoice<int>&>(c);
int pos = pvc.pos().pos;
int val = pvc.val();
if (a == 0)
return me_failed(x[pos].eq(home, val)) ? ES_FAILED : ES_OK;
else
return me_failed(x[pos].nq(home, val)) ? ES_FAILED : ES_OK;
}
template<class View>
forceinline void
CBSBrancher<View>::print(const Space&, const Choice& c, unsigned int a,
std::ostream& o) const {
const auto& pvc = static_cast<const PosValChoice<int>&>(c);
int pos=pvc.pos().pos, val=pvc.val();
if (a == 0)
o << "x[" << pos << "] = " << val;
else
o << "x[" << pos << "] != " << val;
}
}}}
#endif
// STATISTICS: int-branch
|