void SudokuSolver::forwardCheck (SudokuPuzzle &puzzle, Variable variable, int level) { // cout << "Grabbing Neighbors of var=" << variable._position._x << "," << variable._position._y << " : " << variable << "... "; std::vector<Variable> neighbors = getNeighbors(puzzle,variable._position); // for (int i = 0; i < neighbors.size(); ++i) {cout << neighbors[i]._position._x << "," << neighbors[i]._position._y << ";";} cout << endl; for (std::size_t i = 0; i < neighbors.size(); ++i) { for (std::size_t j = 0; j < neighbors[i]._domain._domain.size(); ++j) { // cout << "Neighbor=" << i << ",val=" << neighbors[i]._domain._domain[j] << endl; // cout << "Checking for match... "; if (neighbors[i]._domain._domain[j] == variable._value) { // cout << "MATCH" << endl; Domain d; // cout << "Domain=" << neighbors[i]._position._x << "," << neighbors[i]._position._y << " : " << variable._value << endl; d.add(variable._value); // cout << "Book Keeping" << endl; bookKeep(level,puzzle.sudoku()[neighbors[i]._position._x][neighbors[i]._position._y],d); // cout << "Domain for neighbor=" << i << " now: "; // for (int z = 0; z < puzzle.sudoku()[neighbors[i]._position._x][neighbors[i]._position._y]._domain._domain.size (); ++z) {cout << puzzle.sudoku()[neighbors[i]._position._x][neighbors[i]._position._y]._domain._domain[z] << ",";} cout << endl; j = neighbors[i]._domain._domain.size(); } else { // cout << "no match" << endl; } } } }
void SudokuSolver::assignValue(Variable& v, char value, int level) { Domain d; for (std::size_t i = 0; i < v._domain._domain.size (); ++i) { if (v._domain._domain[i] != value) { d.add(v._domain._domain[i]); } } bookKeep(level,v,d); v._value = value; }
void SudokuSolver::forwardCheck (SudokuPuzzle &puzzle, Variable variable, int level) { std::vector<Variable> neighbors = getNeighbors(puzzle,variable._position); for (std::size_t i = 0; i < neighbors.size(); ++i) { for (std::size_t j = 0; j < neighbors[i]._domain._domain.size(); ++j) { if (neighbors[i]._domain._domain[j] == variable._value) { Domain d; d.add(variable._value); bookKeep(level,puzzle.sudoku()[neighbors[i]._position._x][neighbors[i]._position._y],d); j = neighbors[i]._domain._domain.size(); } } } }
void SudokuSolver::assignValue(Variable& v, char value, int level) { // cout << "In Assign Value" << endl; Domain d; for (std::size_t i = 0; i < v._domain._domain.size (); ++i) { if (v._domain._domain[i] != value) { d.add(v._domain._domain[i]); } } // for (std::size_t i = 0; i < d._domain.size(); ++i) {cout << "d=" << d._domain[i] << " ";} cout << std::endl; // cout << "Book Keeping" << endl; bookKeep(level,v,d); // cout << "Assigning value=" << value << endl; v._value = value; }
Pointer * Pointer::getElementPtr(const std::vector<Domain*> &offsets, const llvm::PointerType &type, const Constructors &constructors) const { CANAL_ASSERT_MSG(!offsets.empty(), "getElementPtr must be called with some offsets."); // Check that all offsets are 64-bit integers. std::vector<Domain*>::const_iterator offsetIt = offsets.begin(); for (; offsetIt != offsets.end(); ++offsetIt) { CANAL_ASSERT_MSG(Integer::Utils::getBitWidth(**offsetIt) == 64, "GetElementPtr offsets must have 64 bits!"); } Pointer *result = new Pointer(*this, type); // Iterate over all targets, and adjust the target offsets. PlaceTargetMap::iterator targetIt = result->mTargets.begin(); for (; targetIt != result->mTargets.end(); ++targetIt) { std::vector<Domain*> &targetOffsets = targetIt->second->mOffsets; std::vector<Domain*>::const_iterator offsetIt = offsets.begin(); for (; offsetIt != offsets.end(); ++offsetIt) { if (offsetIt == offsets.begin() && !targetOffsets.empty()) { Domain *newLast = constructors.createInteger(64); newLast->add(*targetOffsets.back(), **offsets.begin()); delete targetOffsets.back(); targetOffsets.pop_back(); targetOffsets.push_back(newLast); continue; } targetOffsets.push_back((*offsetIt)->clone()); } } // Delete the offsets, because this method takes ownership of them // and it no longer needs them. std::for_each(offsets.begin(), offsets.end(), llvm::deleter<Domain>); return result; }
bool SudokuSolver::applyAC3 (SudokuPuzzle &puzzle, int level) { std::vector<std::pair<Position,Position>> arcs; for (std::size_t x = 0; x < puzzle.n(); ++x) { for (std::size_t y = 0; y < puzzle.n(); ++y) { std::vector<Variable> neighbors = getNeighbors (puzzle,puzzle.sudoku()[x][y]._position); for (std::size_t j = 0; j < neighbors.size(); ++j) { Position c(x,y); std::pair<Position,Position> p(c,neighbors[j]._position); arcs.insert (arcs.end(),p); } } } for (std::size_t i = 0; i < arcs.size(); ++i) { char fail; Position cur1 = arcs[i].first; Position cur2 = arcs[i].second; arcs.erase (arcs.begin ()); --i; if (!checkArc (puzzle,cur1,cur2,fail)) { Domain d; d.add(fail); int x = cur1._x, y = cur1._y; bookKeep(level,puzzle.sudoku()[x][y],d); if (puzzle.sudoku()[x][y]._domain._domain.empty()) { return false; } std::vector<Variable> neighbors = getNeighbors (puzzle,cur1); for (std::size_t j = 0; j < neighbors.size(); ++j) { bool in = false; std::pair<Position,Position> p(neighbors[j]._position,cur1); for (std::size_t k = 0; k < arcs.size(); ++k) { if (p.first._x == arcs[k].first._x && p.first._y == arcs[k].first._y && p.second._x == arcs[k].second._x && p.second._y == arcs[k].second._y) { in = true; } } if (!in) { arcs.insert (arcs.end (),p); } } } } return true; }
Domain SudokuSolver::applyLCV(SudokuPuzzle puzzle, Variable variable) { std::vector<std::pair<char,int>> lcvVector; for (std::size_t i = 0; i < variable._domain._domain.size(); ++i) { std::pair<char,int> p(variable._domain._domain[i],getConstraints (puzzle,variable._position,variable._domain._domain[i])); lcvVector.insert (lcvVector.end(),p); } //Sort from smallest to largest for (std::size_t i = 0; i < lcvVector.size(); ++i) { int j = i; while (j > 0 && lcvVector[j].second < lcvVector[j - 1].second) { std::pair<char,int> temp = lcvVector[j]; lcvVector[j] = lcvVector[j - 1]; lcvVector[j - 1] = temp; j--; } } //Create domain Domain d; for (std::size_t i = 0; i < lcvVector.size(); ++i) { d.add(lcvVector[i].first); } return d; }
bool SudokuSolver::applyAC3 (SudokuPuzzle &puzzle, int level) { std::vector<std::pair<Position,Position>> arcs; // cout << "Grabbing all arcs" << endl; for (std::size_t x = 0; x < puzzle.n(); ++x) { for (std::size_t y = 0; y < puzzle.n(); ++y) { std::vector<Variable> neighbors = getNeighbors (puzzle,puzzle.sudoku()[x][y]._position); for (std::size_t j = 0; j < neighbors.size(); ++j) { Position c(x,y); std::pair<Position,Position> p(c,neighbors[j]._position); arcs.insert (arcs.end(),p); // cout << "Arc: " << c._x << "," << c._y << " to " << neighbors[j]._position._x << "," << neighbors[j]._position._y << ";" << endl; } } } // cout << "Arc Size=" << arcs.size() << endl; for (std::size_t i = 0; i < arcs.size(); ++i) { char fail; // cout << "i=" << i <<"Checking Arc Consistency between " << arcs[i].first._x << "," << arcs[i].first._y << " and " << arcs[i].second._x << "," << arcs[i].second._y << "..."; Position cur1 = arcs[i].first; Position cur2 = arcs[i].second; // cout << "Erasing this..." << arcs.begin()->first._x << "," << arcs.begin()->first._y << " to " << arcs.begin()->second._x << "," << arcs.begin()->second._y << endl; arcs.erase (arcs.begin ()); --i; // cout << "ArcSize=" << arcs.size() << endl; if (!checkArc (puzzle,cur1,cur2,fail)) { // cout <<"Checking Arc Consistency between " << cur1._x << "," << cur1._y << " and " << cur2._x << "," << cur2._y << "..."; // cout << "Not Consistent" << endl; // cout << "Failing value=" << fail << endl; // cout << "Domain of " << cur1._x << "," << cur1._y << ": "; // for (int z = 0; z < puzzle.sudoku()[cur1._x][cur1._y]._domain._domain.size(); ++z) {cout << puzzle.sudoku()[cur1._x][cur1._y]._domain._domain[z] << ";"; }cout << endl; // cout << "Domain of " << cur2._x << "," << cur2._y << ": "; // for (int z = 0; z < puzzle.sudoku()[cur2._x][cur2._y]._domain._domain.size(); ++z) {cout << puzzle.sudoku()[cur2._x][cur2._y]._domain._domain[z] << ";";}cout << endl; Domain d; d.add(fail); int x = cur1._x, y = cur1._y; bookKeep(level,puzzle.sudoku()[x][y],d); // cout << "Domain is..."; if (puzzle.sudoku()[x][y]._domain._domain.empty()) { // cout << "Empty" << endl; return false; } else { // cout << "Not Empty" << endl; } // cout << "Adding propagated neighbors of " << cur1._x << "," << arcs[i].first._y << ": "; std::vector<Variable> neighbors = getNeighbors (puzzle,cur1); // cout << "NeighborsSize=" << neighbors.size() << endl; for (std::size_t j = 0; j < neighbors.size(); ++j) { bool in = false; std::pair<Position,Position> p(neighbors[j]._position,cur1); for (std::size_t k = 0; k < arcs.size(); ++k) { if (p.first._x == arcs[k].first._x && p.first._y == arcs[k].first._y && p.second._x == arcs[k].second._x && p.second._y == arcs[k].second._y) { in = true; } } if (!in) { // std::pair<Position,Position> p(cur1,neighbors[j]._position); arcs.insert (arcs.end (),p); // cout << "Arc " << p.first._x << "," << p.first._y << " to " << p.second. _x << "," << p.second._y << ";"; } } // cout << endl; } else { // cout << "Consistent" << endl; } } return true; }