void SweepAndPrune::checkAndAdd(handle box1, handle box2) { if (box1 == box2) { return; } if (isSentinel(box1) || isSentinel(box2)) { //one of the boxes is a sentinal so we will ignore it return; } auto& bbox1 = _objects[box1]; auto& bbox2 = _objects[box2]; bool areIntersecting = false; std::array<bool, 3> axisIntersections = { false, false, false }; std::pair<handle, handle> collisionPair; collisionPair.first = box1 < box2 ? box1 : box2; collisionPair.second = box1 < box2 ? box2 : box1; auto collisionidx = std::find(activePairs.begin(), activePairs.end(), collisionPair); bool isActive = !(collisionidx == activePairs.end()); for (unsigned int i = 0; i < 3; ++i) { if ((bbox1.max[i] > bbox2.min[i]) && (bbox1.min[i] < bbox2.max[i])) { axisIntersections[i] = true; } } areIntersecting = (axisIntersections[0] && axisIntersections[1] && axisIntersections[2]); if (areIntersecting && isActive) { return; } else if (!areIntersecting && isActive) { activePairs.erase(collisionidx); } else if (areIntersecting) { activePairs.push_back(std::move(collisionPair)); } }
bool SolveAlgorithm::solve(SharedContext& ctx, const SolveParams& p, const LitVec& assume) { assumptions_ = assume; if (!isSentinel(ctx.tagLiteral())) { assumptions_.push_back(ctx.tagLiteral()); } bool more = limits_.conflicts == 0 || doSolve(*ctx.master(), p); ctx.enumerator()->reportResult(!more); if (!isSentinel(ctx.tagLiteral())) { assumptions_.pop_back(); } ctx.detach(*ctx.master()); return more; }
EnumerationConstraint* ModelEnumerator::doInit(SharedContext& ctx, MinimizeConstraint* min, int numModels) { delete queue_; queue_ = 0; initProjection(ctx); uint32 st = strategy(); if (detectStrategy() || (ctx.concurrency() > 1 && !ModelEnumerator::supportsParallel())) { st = 0; } bool optOne = minimizer() && minimizer()->mode() == MinimizeMode_t::optimize; bool trivial = optOne || std::abs(numModels) == 1; if (optOne && project_) { const SharedMinimizeData* min = minimizer(); for (const WeightLiteral* it = min->lits; !isSentinel(it->first) && trivial; ++it) { trivial = ctx.varInfo(it->first.var()).project(); } if (!trivial) { ctx.report(warning(Event::subsystem_prepare, "Projection: Optimization may depend on enumeration order.")); } } if (st == strategy_auto) { st = trivial || (project_ && ctx.concurrency() > 1) ? strategy_record : strategy_backtrack; } if (trivial) { st |= trivial_flag; } if (ctx.concurrency() > 1 && !trivial && st != strategy_backtrack) { queue_ = new SolutionQueue(ctx.concurrency()); queue_->reserve(ctx.concurrency() + 1); } options_ &= ~uint32(strategy_opts_mask); options_ |= st; Solver& s = *ctx.master(); EnumerationConstraint* c = st == strategy_backtrack ? static_cast<ConPtr>(new BacktrackFinder(s, min, project_, projectOpts())) : static_cast<ConPtr>(new RecordFinder(s, min, project_, queue_)); if (projectionEnabled()) { setIgnoreSymmetric(true); } return c; }
EnumerationConstraint* ModelEnumerator::doInit(SharedContext& ctx, SharedMinimizeData* opt, int numModels) { initProjection(ctx); uint32 st = strategy(); if (detectStrategy() || (ctx.concurrency() > 1 && !ModelEnumerator::supportsParallel())) { st = 0; } bool optOne = opt && opt->mode() == MinimizeMode_t::optimize; bool trivial = optOne || std::abs(numModels) == 1; if (optOne && projectionEnabled()) { for (const WeightLiteral* it = minimizer()->lits; !isSentinel(it->first) && trivial; ++it) { trivial = ctx.varInfo(it->first.var()).project(); } if (!trivial) { ctx.report(warning(Event::subsystem_prepare, "Projection: Optimization may depend on enumeration order.")); } } if (st == strategy_auto) { st = trivial || (projectionEnabled() && ctx.concurrency() > 1) ? strategy_record : strategy_backtrack; } if (trivial) { st |= trivial_flag; } options_ &= ~uint32(strategy_opts_mask); options_ |= st; const LitVec* dom = (projectOpts() & project_dom_lits) != 0 ? (ctx.heuristic.domRec = &domRec_) : 0; EnumerationConstraint* c = st == strategy_backtrack ? static_cast<ConPtr>(new BacktrackFinder(projectOpts())) : static_cast<ConPtr>(new RecordFinder(dom)); if (projectionEnabled()) { setIgnoreSymmetric(true); } return c; }
void ProgramBuilder::getAssumptions(LitVec& out) const { CLASP_ASSERT_CONTRACT(ctx_ && frozen()); if (!isSentinel(ctx_->stepLiteral())) { out.push_back(ctx_->stepLiteral()); } doGetAssumptions(out); }
int P_TraverseIntercepts(traverser_t callback, void *parameters) { for(InterceptNode *node = head.next; !isSentinel(*node); node = node->next) { int result = callback(&node->intercept, parameters); if(result) return result; // Stop iteration. } return false; // Continue iteration. }
static InterceptNode *newInterceptNode() { // Can we reuse an existing intercept? if(!isSentinel(*interceptFirst)) { InterceptNode *node = interceptFirst; interceptFirst = node->next; return node; } return (InterceptNode *) ZBlockSet_Allocate(interceptNodeSet); }
bool ClauseHead::toImplication(Solver& s) { ConstraintType t = ClauseHead::type(); uint32 sz = isSentinel(head_[1]) ? 1 : 2 + (!s.isFalse(head_[2]) || s.level(head_[2].var()) > 0); ClauseRep rep = ClauseRep::create(head_, sz, ClauseInfo(t).setLbd(2).setTagged(tagged())); bool implicit = s.allowImplicit(rep); bool locked = ClauseHead::locked(s) && s.decisionLevel() > 0; rep.prep = 1; if ((locked || !implicit) && sz > 1) { return false; } s.add(rep, false); detach(s); return true; }
Literal RestrictedUnit::doSelect(Solver& s) { s.addPost(look_); Literal choice = look_->propagateFixpoint(s) ? look_->heuristic(s) : negLit(0); s.removePost(look_); // if all fld-candidates are currently assigned, use // decorated heuristic to select next choice - // if all vars are assigned after fld, we return posLit(0) // as the "noop" choice. if (choice == posLit(0) && s.numFreeVars() > 0) { choice = default_->doSelect(s); } if (!isSentinel(choice) && --numChoices_ == 0) { destroy(s); } return choice; }
void ClingoPropagator::toClause(Solver& s, const Potassco::LitSpan& clause, Potassco::Clause_t prop) { POTASSCO_REQUIRE(todo_.empty(), "Assignment not propagated"); Literal max; LitVec& mem = todo_.mem; for (const Potassco::Lit_t* it = Potassco::begin(clause); it != Potassco::end(clause); ++it) { Literal p = decodeLit(*it); if (max < p) { max = p; } mem.push_back(p); } if (aux_ < max) { aux_ = max; } if ((Potassco::Clause_t::isVolatile(prop) || s.auxVar(max.var())) && !isSentinel(s.sharedContext()->stepLiteral())) { mem.push_back(~s.sharedContext()->stepLiteral()); } todo_.clause = ClauseCreator::prepare(s, mem, Clasp::ClauseCreator::clause_force_simplify, Constraint_t::Other); todo_.flags = ccFlags_s[int(Potassco::Clause_t::isStatic(prop))]; if (mem.empty()) { mem.push_back(lit_false()); } }
void ClaspBerkmin::updateReason(const Solver&, const LitVec& lits, Literal r) { for (LitVec::size_type i = 0, e = lits.size(); i != e; ++i) { order_.inc(~lits[i]); } if (!isSentinel(r)) { order_.inc(r); } }