void FactorQTZRep<T>::inverse( Matrix_<T>& inverse ) const { Matrix_<T> iden(mn,mn); inverse.resize(mn,mn); iden = 1.0; doSolve( iden, inverse ); return; }
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; }
int totalNQueens(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function results = 0; vector<int> cur(n); for (int i = 0; i < n; i++) { cur[i] = -1; } doSolve(cur, n, 0); return results; }
void FactorQTZRep<T>::solve( const Matrix_<T>& b, Matrix_<T>& x ) const { SimTK_APIARGCHECK_ALWAYS(isFactored ,"FactorQTZ","solve", "No matrix was passed to FactorQTZ. \n" ); SimTK_APIARGCHECK2_ALWAYS(b.nrow()==nRow,"FactorQTZ","solve", "number of rows in right hand side=%d does not match number of rows in original matrix=%d \n", b.nrow(), nRow ); x.resize(nCol, b.ncol() ); Matrix_<T> tb; tb.resize(maxmn, b.ncol() ); for(int j=0;j<b.ncol();j++) for(int i=0;i<b.nrow();i++) tb(i,j) = b(i,j); doSolve(tb, x); }
void FactorQTZRep<T>::solve( const Vector_<T>& b, Vector_<T> &x ) const { SimTK_APIARGCHECK_ALWAYS(isFactored ,"FactorQTZ","solve", "No matrix was passed to FactorQTZ. \n" ); SimTK_APIARGCHECK2_ALWAYS(b.size()==nRow,"FactorQTZ","solve", "number of rows in right hand side=%d does not match number of rows in original matrix=%d \n", b.size(), nRow ); Matrix_<T> m(maxmn,1); for(int i=0;i<b.size();i++) { m(i,0) = b(i); } Matrix_<T> r(nCol, 1 ); doSolve( m, r ); x.copyAssign(r); }
void doSolve(vector<int> &cur, int n, int index) { if (index == n) { results++; return; } int i = index; for (int j = 0; j < n; j++) { int k; for (k = 0; k < index; k++) { if (j == cur[k] || abs(i - k) == abs(j - cur[k])) { break; } } if (k == index) { cur[k] = j; doSolve(cur, n, index + 1); } } }
// Constraints up to but not including iterator it have been unified. // The current solution is soln // The set of all solutions is in solns bool Constraints::doSolve(std::list<Exp*>::iterator it, ConstraintMap& soln, std::list<ConstraintMap>& solns) { LOG << "Begin doSolve at level " << ++level << "\n"; LOG << "Soln now: " << soln.prints() << "\n"; if (it == disjunctions.end()) { // We have gotten to the end with no unification failures // Copy the current set of constraints as a solution //if (soln.size() == 0) // Awkward. There is a trivial solution, but we have no constraints // So make a constraint of always-true //soln.insert(new Terminal(opTrue)); // Copy the fixed constraints soln.makeUnion(fixed); solns.push_back(soln); LOG << "Exiting doSolve at level " << level-- << " returning true\n"; return true; } Exp* dj = *it; // Iterate through each disjunction d of dj Exp* rem1 = dj; // Remainder bool anyUnified = false; Exp* d; while ((d = nextDisjunct(rem1)) != NULL) { LOG << " $$ d is " << d << ", rem1 is " << ((rem1==0)?"NULL":rem1->prints()) << " $$\n"; // Match disjunct d against the fixed types; it could be compatible, // compatible and generate an additional constraint, or be // incompatible ConstraintMap extra; // Just for this disjunct Exp* c; Exp* rem2 = d; bool unified = true; while ((c = nextConjunct(rem2)) != NULL) { LOG << " $$ c is " << c << ", rem2 is " << ((rem2==0)?"NULL":rem2->prints()) << " $$\n"; if (c->isFalse()) { unified = false; break; } assert(c->isEquality()); Exp* lhs = ((Binary*)c)->getSubExp1(); Exp* rhs = ((Binary*)c)->getSubExp2(); extra.insert(lhs, rhs); ConstraintMap::iterator kk; kk = fixed.find(lhs); if (kk != fixed.end()) { unified &= unify(rhs, kk->second, extra); LOG << "Unified now " << unified << "; extra now " << extra.prints() << "\n"; if (!unified) break; } } if (unified) // True if any disjuncts had all the conjuncts satisfied anyUnified = true; if (!unified) continue; // Use this disjunct // We can't just difference out extra if this fails; it may remove // elements from soln that should not be removed // So need a copy of the old set in oldSoln ConstraintMap oldSoln = soln; soln.makeUnion(extra); doSolve(++it, soln, solns); // Revert to the previous soln (whether doSolve returned true or not) // If this recursion did any good, it will have gotten to the end and // added the resultant soln to solns soln = oldSoln; LOG << "After doSolve returned: soln back to: " << soln.prints() << "\n"; // Back to the current disjunction it--; // Continue for more disjuncts this disjunction } // We have run out of disjuncts. Return true if any disjuncts had no // unification failures LOG << "Exiting doSolve at level " << level-- << " returning " << anyUnified << "\n"; return anyUnified; }
bool Constraints::solve(std::list<ConstraintMap>& solns) { LOG << conSet.size() << " constraints:"; std::ostringstream os; conSet.print(os); LOG << os.str().c_str(); // Replace Ta[loc] = ptr(alpha) with // Tloc = alpha LocationSet::iterator cc; for (cc = conSet.begin(); cc != conSet.end(); cc++) { Exp* c = *cc; if (!c->isEquality()) continue; Exp* left = ((Binary*)c)->getSubExp1(); if (!left->isTypeOf()) continue; Exp* leftSub = ((Unary*)left)->getSubExp1(); if (!leftSub->isAddrOf()) continue; Exp* right = ((Binary*)c)->getSubExp2(); if (!right->isTypeVal()) continue; Type* t = ((TypeVal*)right)->getType(); if (!t->isPointer()) continue; // Don't modify a key in a map Exp* clone = c->clone(); // left is typeof(addressof(something)) -> typeof(something) left = ((Binary*)clone)->getSubExp1(); leftSub = ((Unary*)left)->getSubExp1(); Exp* something = ((Unary*)leftSub)->getSubExp1(); ((Unary*)left)->setSubExp1ND(something); ((Unary*)leftSub)->setSubExp1ND(NULL); delete leftSub; // right is <alpha*> -> <alpha> right = ((Binary*)clone)->getSubExp2(); t = ((TypeVal*)right)->getType(); ((TypeVal*)right)->setType(((PointerType*)t)->getPointsTo()->clone()); delete t; conSet.remove(c); conSet.insert(clone); delete c; } // Sort constraints into a few categories. Disjunctions go to a special // list, always true is just ignored, and constraints of the form // typeof(x) = y (where y is a type value) go to a map called fixed. // Constraint terms of the form Tx = Ty go into a map of LocationSets // called equates for fast lookup for (cc = conSet.begin(); cc != conSet.end(); cc++) { Exp* c = *cc; if (c->isTrue()) continue; if (c->isFalse()) { if (VERBOSE || DEBUG_TA) LOG << "Constraint failure: always false constraint\n"; return false; } if (c->isDisjunction()) { disjunctions.push_back(c); continue; } // Break up conjunctions into terms Exp* rem = c, *term; while ((term = nextConjunct(rem)) != NULL) { assert(term->isEquality()); Exp* lhs = ((Binary*)term)->getSubExp1(); Exp* rhs = ((Binary*)term)->getSubExp2(); if (rhs->isTypeOf()) { // Of the form typeof(x) = typeof(z) // Insert into equates equates.addEquate(lhs, rhs); } else { // Of the form typeof(x) = <typeval> // Insert into fixed assert(rhs->isTypeVal()); fixed[lhs] = rhs; } } } {LOG << "\n" << (unsigned)disjunctions.size() << " disjunctions: "; std::list<Exp*>::iterator dd; for (dd = disjunctions.begin(); dd != disjunctions.end(); dd++) LOG << *dd << ",\n"; LOG << "\n";} LOG << fixed.size() << " fixed: " << fixed.prints(); LOG << equates.size() << " equates: " << equates.prints(); // Substitute the fixed types into the disjunctions substIntoDisjuncts(fixed); // Substitute the fixed types into the equates. This may generate more // fixed types substIntoEquates(fixed); LOG << "\nAfter substitute fixed into equates:\n"; {LOG << "\n" << (unsigned)disjunctions.size() << " disjunctions: "; std::list<Exp*>::iterator dd; for (dd = disjunctions.begin(); dd != disjunctions.end(); dd++) LOG << *dd << ",\n"; LOG << "\n";} LOG << fixed.size() << " fixed: " << fixed.prints(); LOG << equates.size() << " equates: " << equates.prints(); // Substitute again the fixed types into the disjunctions // (since there may be more fixed types from the above) substIntoDisjuncts(fixed); LOG << "\nAfter second substitute fixed into disjunctions:\n"; {LOG << "\n" << (unsigned)disjunctions.size() << " disjunctions: "; std::list<Exp*>::iterator dd; for (dd = disjunctions.begin(); dd != disjunctions.end(); dd++) LOG << *dd << ",\n"; LOG << "\n";} LOG << fixed.size() << " fixed: " << fixed.prints(); LOG << equates.size() << " equates: " << equates.prints(); ConstraintMap soln; bool ret = doSolve(disjunctions.begin(), soln, solns); if (ret) { // For each solution, we need to find disjunctions of the form // <alphaN> = <type> or // <type> = <alphaN> // and substitute these into each part of the solution std::list<ConstraintMap>::iterator it; for (it = solns.begin(); it != solns.end(); it++) it->substAlpha(); } return ret; }