void VACExtension::enforcePass1() { // VACVariable* xi; VACVariable *xj; VACBinaryConstraint *cij; //if (ToulBar2::verbose > 1) cout << "VAC Enforce Pass 1" << endl; while (!VAC.empty()) { xj = (VACVariable *) VAC.pop_first(); //list<Constraint*> l; for (ConstraintList::iterator itc = xj->getConstrs()->begin(); itc != xj->getConstrs()->end(); ++itc) { Constraint *c = (*itc).constr; if (c->arity() == 2 && !c->isSep()) { cij = (VACBinaryConstraint *) c; // xi = (VACVariable *)cij->getVarDiffFrom(xj); //if(xj->getMaxK(nbIterations) > 2) l.push_back(cij); else if (enforcePass1(xj, cij)) return; } } /*for (list<Constraint*>::iterator itl = l.begin(); itl != l.end(); ++itl) { cij = (VACConstraint *) *itl; if(enforcePass1(xj,cij)) return; } */ } inconsistentVariable = -1; }
void Mistral::ConsolidateListener::notify_add_con(Constraint c) { //std::cout << "ADD CON " << c << std::endl; Variable *scope = c.get_scope(); int arity = c.arity(); for(int i=0; i<arity; ++i) { constraints[scope[i].id()].add(c); constraints[scope[i].id()].back().set_index(i); } }
bool VACExtension::checkPass1() const { VACBinaryConstraint *cij; VACVariable *xi, *xj; bool supportFound; TreeDecomposition *td = wcsp->getTreeDec(); for (unsigned int i = 0; i < wcsp->numberOfVariables(); i++) { xi = (VACVariable *) wcsp->getVar(i); if (td && !td->isActiveAndInCurrentClusterSubTree(xi-> getCluster())) continue; for (ConstraintList::iterator iter = xi->getConstrs()->begin(); iter != xj->getConstrs()->end(); ++iter) { Constraint *c = (*iter).constr; if (c->arity() == 2 && !c->isSep()) { cij = (VACBinaryConstraint *) c; xj = (VACVariable *) cij->getVarDiffFrom(xi); for (EnumeratedVariable::iterator iti = xi->begin(); iti != xi->end(); ++iti) { Value v = *iti; supportFound = false; for (EnumeratedVariable::iterator itj = xj->begin(); itj != xj->end(); ++itj) { Value w = *itj; if ((xi->getVACCost(v) == MIN_COST) && (xj->getVACCost(w) == MIN_COST) && (cij-> getVACCost(xi, xj, v, w) == MIN_COST)) { supportFound = true; break; } } if (!supportFound) { return false; } } } } } return true; }
set<Constraint *> subConstraint(){ set <Constraint *> subcstr; set<int> scope; for(int k=0; k < arity(); k++) { scope.insert(getVar(k)->wcspIndex); } for(set<int>::iterator itx = scope.begin(); itx != scope.end(); ++itx){ ConstraintList* xctrs = (wcsp->getVar(*itx))->getConstrs(); for (ConstraintList::iterator it=xctrs->begin(); it != xctrs->end(); ++it){ Constraint * ctr = (*it).constr; if(ctr->arity() < arity() && scopeIncluded(ctr)) subcstr.insert(ctr); } } return subcstr; }
void VACExtension::afterPreprocessing() { int discarded = 0; for (unsigned int i = 0; i < wcsp->numberOfConstraints(); i++) { Constraint *c = wcsp->getCtr(i); if (c->connected() && (c->arity() <= 3) && !c->isSep()) { if (c->getTightness() < to_double(ToulBar2::relaxThreshold)) { c->deconnect(); discarded++; } } } if (discarded) cout << "WARNING num of discarded ctrs: " << discarded << endl; }
Mistral::ConsolidateListener::ConsolidateListener(Mistral::Solver *s) : VariableListener(), ConstraintListener(), solver(s) { sequence = &(solver->sequence); constraints.initialise(solver->variables.size); // int n = solver->variables.size; // for(int i=0; i<n; ++i) { // Vector< Constraint > neighborhood; // neighborhood.initialise(solver->variables[i].get_degree()); // for(int k=0; k<3; ++k) { // for(int j=solver->constraint_graph[i].on[k].size-1; j>=0; --j) // neighborhood.add(solver->constraint_graph[i].on[k][j]); // } // constraints.add(neighborhood); // } //std::cout << "Variables size: " << solver->variables.size << std::endl; Constraint c; Variable *scope; int i, j, arity, n=solver->variables.size; for(i=0; i<n; ++i) { constraints[i].initialise(solver->variables[i].get_degree()); } n = solver->constraints.size; for(i=0; i<n; ++i) { c = solver->constraints[i]; scope = c.get_scope(); arity = c.arity(); for(j=0; j<arity; ++j) { if(!scope[j].is_ground()) { //std::cout << " " << scope[j] << std::endl; c.set_index(j); constraints[scope[j].id()].add(c); } } } //std::cout << constraints[66] << std::endl; solver->add((VariableListener*)this); solver->add((ConstraintListener*)this); }
bool VACVariable::averaging() { Cost Top = wcsp->getUb(); bool change = false; EnumeratedVariable* x; EnumeratedVariable* y; Constraint* ctr = NULL; ConstraintList::iterator itc = getConstrs()->begin(); if(itc != getConstrs()->end()) ctr = (*itc).constr; while(ctr) { if(ctr->arity() == 2 && !ctr->isSep()) { BinaryConstraint* bctr = (BinaryConstraint*) ctr; x = (EnumeratedVariable*) bctr->getVarDiffFrom( (Variable*) this ); for (iterator it = begin(); it != end(); ++it) { Cost cu = getCost(*it); Cost cmin = Top; for (iterator itx = x->begin(); itx != x->end(); ++itx) { Cost cbin = bctr->getCost(this,x,*it,*itx); if(cbin < cmin) cmin = cbin; } assert(cmin < Top); Double mean = to_double(cmin + cu) / 2.; Double extc = to_double(cu) - mean; if(abs(extc) >= 1) { Cost costi = (Long) extc; for (iterator itx = x->begin(); itx != x->end(); ++itx) { bctr->addcost(this,x,*it,*itx,costi); } if(mean > to_double(cu)) project(*it, -costi); else extend(*it, costi); change = true; } } } else if(ctr->arity() == 3 && !ctr->isSep()) { TernaryConstraint* tctr = (TernaryConstraint*) ctr; x = (EnumeratedVariable*) tctr->getVar( 0 ); if(x == this) x = (EnumeratedVariable*) tctr->getVar( 1 ); y = (EnumeratedVariable*) tctr->getVarDiffFrom((Variable*) this, (Variable*)x); for (iterator it = begin(); it != end(); ++it) { Cost cu = getCost(*it); Cost cmin = Top; for (iterator itx = x->begin(); itx != x->end(); ++itx) { for (iterator ity = y->begin(); ity != y->end(); ++ity) { Cost ctern = tctr->getCost(this,x,y,*it,*itx,*ity); if(ctern < cmin) cmin = ctern; }} assert(cmin < Top); Double mean = to_double(cmin + cu) / 2.; Double extc = to_double(cu) - mean; if(abs(extc) >= 1) { Cost costi = (Long) extc; for (iterator itx = x->begin(); itx != x->end(); ++itx) { for (iterator ity = y->begin(); ity != y->end(); ++ity) { tctr->addCost(this,x,y,*it,*itx,*ity,costi); }} if(mean > to_double(cu)) project(*it, -costi); else extend(*it, costi); change = true; } } } else if(ctr->arity() >= 4 && ctr->extension() && !ctr->isSep()) { NaryConstraint* nctr = (NaryConstraint*) ctr; for (iterator it = begin(); it != end(); ++it) { Cost cu = getCost(*it); Cost cmin = Top; int tindex = nctr->getIndex(this); String tuple; Cost cost; Long nbtuples = 0; nctr->first(); while (nctr->next(tuple,cost)) { nbtuples++; if (toValue(tuple[tindex] - CHAR_FIRST)==(*it) && cost < cmin) cmin = cost; } if (nctr->getDefCost() < cmin && nbtuples < nctr->getDomainSizeProduct()) cmin = nctr->getDefCost(); // assert(cmin < Top); Double mean = to_double(cmin + cu) / 2.; Double extc = to_double(cu) - mean; if(abs(extc) >= 1) { Cost costi = (Cost) extc; if(nctr->getDefCost() < Top) { nctr->firstlex(); while( nctr->nextlex(tuple,cost) ) { if (toValue(tuple[tindex] - CHAR_FIRST)==(*it)) { if(cost + costi < Top) nctr->setTuple(tuple, cost + costi); else nctr->setTuple(tuple, Top); } } nctr->setDefCost(Top); } else { nctr->first(); while( nctr->next(tuple,cost) ) { if (toValue(tuple[tindex] - CHAR_FIRST)==(*it)) { if(cost + costi < Top) nctr->addtoTuple(tuple, costi); else nctr->setTuple(tuple, Top); } } } if(mean > to_double(cu)) project(*it, -costi); else extend(*it, costi); change = true; } } } ++itc; if(itc != getConstrs()->end()) ctr = (*itc).constr; else ctr = NULL; } return change; }