Permalink
Browse files

Fixes for new Gecode trunk

  • Loading branch information...
Vincent Barichard Vincent Barichard
Vincent Barichard authored and Vincent Barichard committed Nov 4, 2015
1 parent 588c49a commit 98769ebc4007358cd3cfb6ec925960d6399b3f35
Showing with 52 additions and 41 deletions.
  1. +1 −0 CMakeLists.txt
  2. +37 −37 examples/connect-four.cpp
  3. +2 −2 examples/matrix-game.cpp
  4. +3 −1 quacode/qcsp.hh
  5. +9 −1 quacode/search/sequential/qdfs.hh
View
@@ -55,6 +55,7 @@ IF(NOT DEFINED CMAKE_BUILD_TYPE)
ENDIF()
PROJECT(Quacode)
SET(CMAKE_CXX_FLAGS "-std=c++11")
SET(CMAKE_VERBOSE_MAKEFILE TRUE)
View
@@ -257,7 +257,7 @@ class ConnectFourAllState : public Script, public QSpaceInfo {
}
m << U[k/2];
for (int i=0; i < col; i++)
rel(*this, ((gameWinner[k-1] == Nil) && (h[(k-1)*col+i] < row) && (M[k] == i)) >> (U[k/2] == i), ICL_DOM); // Forbid illegal move
rel(*this, ((gameWinner[k-1] == Nil) && (h[(k-1)*col+i] < row) && (M[k] == i)) >> (U[k/2] == i), IPL_DOM); // Forbid illegal move
}
} else {
// Model from P. Nightingale but we add cut and prune universal in order
@@ -271,25 +271,25 @@ class ConnectFourAllState : public Script, public QSpaceInfo {
else uWm << getUnWatched(M[k]);
// Some moves has been recorded, we play them here
if (rMoves.size() > k) rel(*this, uWm[k] == rMoves[k], ICL_DOM);
if (rMoves.size() > k) rel(*this, uWm[k] == rMoves[k], IPL_DOM);
// Set the move-here variables
if (k==0)
for (int i=0; i < col; i++)
rel(*this, (m[0] == i) == (mh[0*col+i] && moveDone[0]), ICL_DOM);
rel(*this, (m[0] == i) == (mh[0*col+i] && moveDone[0]), IPL_DOM);
else {
for (int i=0; i < col; i++) {
if (opt.QCSPmodel() <= 1) {
// Not exactly as the article, we have drop the part with !lineMove[k-1].
// We have to do this because it is not compatible with the constraints
// which force the last board to be full
// rel(*this, (!lineMove[k-1] && (h[(k-1)*col+i] < row) && (m[k] == i)) == mh[k*col+i], ICL_DOM);
rel(*this, ((h[(k-1)*col+i] < row) && (m[k] == i)) == mh[k*col+i], ICL_DOM);
// rel(*this, (!lineMove[k-1] && (h[(k-1)*col+i] < row) && (m[k] == i)) == mh[k*col+i], IPL_DOM);
rel(*this, ((h[(k-1)*col+i] < row) && (m[k] == i)) == mh[k*col+i], IPL_DOM);
} else { // opt.QCSPmodel() > 1
// rel(*this, (!lineMove[k-1] && (h[(k-1)*col+i] < row) && (m[k] == i)) == (mh[k*col+i] && moveDone[k]), ICL_DOM);
rel(*this, ((h[(k-1)*col+i] < row) && (m[k] == i)) == (mh[k*col+i] && moveDone[k]), ICL_DOM);
// rel(*this, (!lineMove[k-1] && (h[(k-1)*col+i] < row) && (m[k] == i)) == (mh[k*col+i] && moveDone[k]), IPL_DOM);
rel(*this, ((h[(k-1)*col+i] < row) && (m[k] == i)) == (mh[k*col+i] && moveDone[k]), IPL_DOM);
// Prune for universal
rel(*this, (h[(k-1)*col+i] == row) >> (uWm[k] != i), ICL_DOM); // Prune illegal move from universal
rel(*this, (h[(k-1)*col+i] == row) >> (uWm[k] != i), IPL_DOM); // Prune illegal move from universal
// Add cut
if ((k%2) == 1) cut(*this, (gameWinner[k-1] == Red) && moveDone[k-1]);
@@ -306,24 +306,24 @@ class ConnectFourAllState : public Script, public QSpaceInfo {
be = expr(*this, board[offSet+i*row+j] != (((k%2)==0)?Black:Red));
for (int jj=j+1; jj < row; jj++)
be = expr(*this, be && (board[offSet+i*row+jj] == Nil));
rel(*this, pos[offSet+i*row+j] == be, ICL_DOM);
rel(*this, pos[offSet+i*row+j] == be, IPL_DOM);
}
for (int k=0, offSet = 0; k<nbDecisionVar; k++, offSet += row*col)
if (k == 0) {
for (int i=0; i < col; i++) {
rel(*this, pos[offSet+i*row], ICL_DOM);
rel(*this, pos[offSet+i*row+row-1], ICL_DOM);
rel(*this, !mh[0*col+i] >> (board[offSet+i*row] == Nil), ICL_DOM);
rel(*this, mh[0*col+i] >> (board[offSet+i*row] == Red), ICL_DOM);
rel(*this, pos[offSet+i*row], IPL_DOM);
rel(*this, pos[offSet+i*row+row-1], IPL_DOM);
rel(*this, !mh[0*col+i] >> (board[offSet+i*row] == Nil), IPL_DOM);
rel(*this, mh[0*col+i] >> (board[offSet+i*row] == Red), IPL_DOM);
}
} else {
for (int i=0; i < col; i++) {
rel(*this, (h[(k-1)*col+i] == row) == !pos[offSet+i*row+row-1], ICL_DOM);
rel(*this, (h[(k-1)*col+i] == row) == !pos[offSet+i*row+row-1], IPL_DOM);
for (int j=0; j < row; j++) {
rel(*this, (h[(k-1)*col+i] == j) >> pos[offSet+i*row+j], ICL_DOM);
rel(*this, (!mh[k*col+i] && (h[(k-1)*col+i] == j)) >> (board[offSet+i*row+j] == Nil), ICL_DOM);
rel(*this, ( mh[k*col+i] && (h[(k-1)*col+i] == j)) >> (board[offSet+i*row+j] == (((k%2)==0)?Red:Black)), ICL_DOM);
rel(*this, (h[(k-1)*col+i] == j) >> pos[offSet+i*row+j], IPL_DOM);
rel(*this, (!mh[k*col+i] && (h[(k-1)*col+i] == j)) >> (board[offSet+i*row+j] == Nil), IPL_DOM);
rel(*this, ( mh[k*col+i] && (h[(k-1)*col+i] == j)) >> (board[offSet+i*row+j] == (((k%2)==0)?Red:Black)), IPL_DOM);
}
}
}
@@ -332,20 +332,20 @@ class ConnectFourAllState : public Script, public QSpaceInfo {
for (int k=1, offSet = row*col; k<nbDecisionVar; k++, offSet += row*col)
for (int i=0; i < col; i++)
for (int j=0; j < row; j++) {
rel(*this, (board[(offSet-row*col)+i*row+j] == Black) >> (board[offSet+i*row+j] == Black), ICL_DOM);
rel(*this, (board[(offSet-row*col)+i*row+j] == Red) >> (board[offSet+i*row+j] == Red), ICL_DOM);
rel(*this, (board[(offSet-row*col)+i*row+j] == Black) >> (board[offSet+i*row+j] == Black), IPL_DOM);
rel(*this, (board[(offSet-row*col)+i*row+j] == Red) >> (board[offSet+i*row+j] == Red), IPL_DOM);
}
// Link height and board state
for (int k=0, offSet = 0; k<nbDecisionVar; k++, offSet += row*col)
for (int i=0; i < col; i++)
for (int j=0; j < row+1; j++)
if (j==0)
rel(*this, (board[offSet+i*row] == Nil) >> (h[k*col+i] == 0), ICL_DOM);
rel(*this, (board[offSet+i*row] == Nil) >> (h[k*col+i] == 0), IPL_DOM);
else if (j==row)
rel(*this, (board[offSet+i*row+j-1] != Nil) >> (h[k*col+i] == row), ICL_DOM);
rel(*this, (board[offSet+i*row+j-1] != Nil) >> (h[k*col+i] == row), IPL_DOM);
else
rel(*this, ((board[offSet+i*row+j-1] != Nil) && (board[offSet+i*row+j] == Nil)) >> (h[k*col+i] == j), ICL_DOM);
rel(*this, ((board[offSet+i*row+j-1] != Nil) && (board[offSet+i*row+j] == Nil)) >> (h[k*col+i] == j), IPL_DOM);
// Detect lines
BoolVarArgs l;
@@ -379,50 +379,50 @@ class ConnectFourAllState : public Script, public QSpaceInfo {
lk << bRed;
l << bRed;
if (k>0)
rel(*this,(lineMove[k-1] || (x[0] != Red) || (x[1] != Red) || (x[2] != Red) || (x[3] != Red)) == !bRed, ICL_DOM);
rel(*this,(lineMove[k-1] || (x[0] != Red) || (x[1] != Red) || (x[2] != Red) || (x[3] != Red)) == !bRed, IPL_DOM);
else
rel(*this,((x[0] != Red) || (x[1] != Red) || (x[2] != Red) || (x[3] != Red)) == !bRed, ICL_DOM);
rel(*this,((x[0] != Red) || (x[1] != Red) || (x[2] != Red) || (x[3] != Red)) == !bRed, IPL_DOM);
} else {
BoolVar bBlack(*this,0,1);
lk << bBlack;
l << bBlack;
rel(*this,(lineMove[k-1] || (x[0] != Black) || (x[1] != Black) || (x[2] != Black) || (x[3] != Black)) == !bBlack, ICL_DOM);
rel(*this,(lineMove[k-1] || (x[0] != Black) || (x[1] != Black) || (x[2] != Black) || (x[3] != Black)) == !bBlack, IPL_DOM);
}
}
}
}
}
if (k>0) lk << lineMove[k-1];
rel(*this, BOT_OR, lk, lineMove[k], ICL_DOM);
rel(*this, BOT_OR, lk, lineMove[k], IPL_DOM);
}
line = BoolVarArray(*this, l);
// Set GameState variables
rel(*this, gameWinner[0] == Nil, ICL_DOM);
rel(*this, gameWinner[0] == Nil, IPL_DOM);
for (int k=1; k < nbDecisionVar; k++) {
rel(*this, (gameWinner[k-1] == Black) >> (gameWinner[k] == Black), ICL_DOM);
rel(*this, (gameWinner[k-1] == Red) >> (gameWinner[k] == Red), ICL_DOM);
rel(*this, ((gameWinner[k-1] == Nil) && !lineMove[k]) >> (gameWinner[k] == Nil), ICL_DOM);
rel(*this, (gameWinner[k-1] == Black) >> (gameWinner[k] == Black), IPL_DOM);
rel(*this, (gameWinner[k-1] == Red) >> (gameWinner[k] == Red), IPL_DOM);
rel(*this, ((gameWinner[k-1] == Nil) && !lineMove[k]) >> (gameWinner[k] == Nil), IPL_DOM);
if ((k%2) == 0)
rel(*this, ((gameWinner[k-1] == Nil) && lineMove[k]) >> (gameWinner[k] == Red), ICL_DOM);
rel(*this, ((gameWinner[k-1] == Nil) && lineMove[k]) >> (gameWinner[k] == Red), IPL_DOM);
else
rel(*this, ((gameWinner[k-1] == Nil) && lineMove[k]) >> (gameWinner[k] == Black), ICL_DOM);
rel(*this, ((gameWinner[k-1] == Nil) && lineMove[k]) >> (gameWinner[k] == Black), IPL_DOM);
if (opt.QCSPmodel() == 3) {
// If not winner before, only current player have a chance to win
// the game at this move -- NOT IN INITIAL MODEL
if ((k%2) == 0)
rel(*this, (gameWinner[k-1] == Nil) >> (gameWinner[k] != Black), ICL_DOM);
rel(*this, (gameWinner[k-1] == Nil) >> (gameWinner[k] != Black), IPL_DOM);
else
rel(*this, (gameWinner[k-1] == Nil) >> (gameWinner[k] != Red), ICL_DOM);
rel(*this, (gameWinner[k-1] == Nil) >> (gameWinner[k] != Red), IPL_DOM);
}
}
// For first move, symmetry is broken by removing the rightmost (upper): col - (col div 2)
if (kOffset == 0) rel(*this, m[0], IRT_LE, col - (col / 2), ICL_DOM);
if (kOffset == 0) rel(*this, m[0], IRT_LE, col - (col / 2), IPL_DOM);
// Force a winner at the end of the game
rel(*this, gameWinner[nbDecisionVar-1], IRT_EQ, Red, ICL_DOM);
rel(*this, gameWinner[nbDecisionVar-1], IRT_EQ, Red, IPL_DOM);
if (opt.QCSPmodel() == 1) {
// Set the last board full.
@@ -431,7 +431,7 @@ class ConnectFourAllState : public Script, public QSpaceInfo {
// doesn't have to be filled.
for (int i=0; i < col; i++)
for (int j=0; j < row; j++)
rel(*this, board[(nbDecisionVar-1)*row*col+i*row+j] != Nil, ICL_DOM);
rel(*this, board[(nbDecisionVar-1)*row*col+i*row+j] != Nil, IPL_DOM);
}
}
View
@@ -176,8 +176,8 @@ class QCSPMatrixGame : public Script, public QSpaceInfo {
// Goal constaints
IntVar cstUn(*this,1,1);
IntVar boardIdx(*this,0,boardSize*boardSize);
linear(*this, access, x, IRT_EQ, boardIdx, ICL_DOM);
element(*this, board, boardIdx, cstUn, ICL_DOM);
linear(*this, access, x, IRT_EQ, boardIdx, IPL_DOM);
element(*this, board, boardIdx, cstUn, IPL_DOM);
branch(*this, X, INT_VAR_NONE(), INT_VAL_MIN());
}
View
@@ -241,13 +241,15 @@ namespace Gecode {
* \ingroup TaskModelSearch
*/
template<class T>
class QDFS : public Search::EngineBase<T> {
class QDFS : public Search::Base<T> {
private:
/// The actual search engine
Search::Engine* e;
public:
/// Initialize search engine for space \a s with options \a o
QDFS(T* s, const Search::Options& o=Search::Options::def);
/// Whether engine does best solution search
static const bool best = false;
/// Return next solution (NULL, if none exists or search has been stopped)
T* next(void);
/// Return statistics
@@ -76,6 +76,8 @@ namespace Gecode { namespace Search { namespace Sequential {
Space* next(void);
/// Return statistics
Statistics statistics(void) const;
/// Constrain future solutions to be better than \a b (should never be called)
void constrain(const Space& b);
/// Reset engine to restart at space \a s
void reset(Space* s);
/// Return no-goods
@@ -217,7 +219,13 @@ namespace Gecode { namespace Search { namespace Sequential {
return *this;
}
forceinline
forceinline void
QDFS::constrain(const Space& b) {
(void) b;
assert(false);
}
forceinline
QDFS::~QDFS(void) {
delete cur;
path.reset();

0 comments on commit 98769eb

Please sign in to comment.