MCPcopy Create free account
hub / github.com/Gecode/gecode / CrowdedChess

Method CrowdedChess

examples/crowded-chess.cpp:236–340  ·  view source on GitHub ↗

The model of the problem

Source from the content-addressed store, hash-verified

234
235 /// The model of the problem
236 CrowdedChess(const SizeOptions& opt)
237 : Script(opt),
238 n(opt.size()),
239 s(*this, n*n, 0, PMAX-1),
240 queens(*this, n, 0, n-1),
241 rooks(*this, n, 0, n-1),
242 knights(*this, n*n, 0, 1) {
243 const int nkval = sizeof(kval)/sizeof(int);
244 const int nn = n*n, q = n, r = n, b = (2*n)-2,
245 k = n <= nkval ? kval[n-1] : kval[nkval-1];
246 const int e = nn - (q + r + b + k);
247
248 assert(nn == (e + q + r + b + k));
249
250 Matrix<IntVarArray> m(s, n);
251
252 // ***********************
253 // Basic model
254 // ***********************
255
256 count(*this, s, E, IRT_EQ, e, opt.ipl());
257 count(*this, s, Q, IRT_EQ, q, opt.ipl());
258 count(*this, s, R, IRT_EQ, r, opt.ipl());
259 count(*this, s, B, IRT_EQ, b, opt.ipl());
260 count(*this, s, K, IRT_EQ, k, opt.ipl());
261
262 // Collect rows and columns for handling rooks and queens
263 for (int i = 0; i < n; ++i) {
264 IntVarArgs aa = m.row(i), bb = m.col(i);
265
266 count(*this, aa, Q, IRT_EQ, 1, opt.ipl());
267 count(*this, bb, Q, IRT_EQ, 1, opt.ipl());
268 count(*this, aa, R, IRT_EQ, 1, opt.ipl());
269 count(*this, bb, R, IRT_EQ, 1, opt.ipl());
270
271 // Connect (queens|rooks)[i] to the row it is in
272 element(*this, aa, queens[i], Q, IPL_DOM);
273 element(*this, aa, rooks[i], R, IPL_DOM);
274 }
275
276 // N-queens constraints
277 distinct(*this, queens, IPL_DOM);
278 distinct(*this, IntArgs::create(n,0,1), queens, IPL_DOM);
279 distinct(*this, IntArgs::create(n,0,-1), queens, IPL_DOM);
280
281 // N-rooks constraints
282 distinct(*this, rooks, IPL_DOM);
283
284 // Collect diagonals for handling queens and bishops
285 for (int l = n; l--; ) {
286 const int il = (n-1) - l;
287 IntVarArgs d1(l+1), d2(l+1), d3(l+1), d4(l+1);
288 for (int i = 0; i <= l; ++i) {
289 d1[i] = m(i+il, i);
290 d2[i] = m(i, i+il);
291 d3[i] = m((n-1)-i-il, i);
292 d4[i] = m((n-1)-i, i+il);
293 }

Callers

nothing calls this directly

Calls 15

distinctFunction · 0.85
mFunction · 0.85
INT_VAR_MIN_MINFunction · 0.85
INT_VAL_MINFunction · 0.85
propagationMethod · 0.80
countFunction · 0.50
elementFunction · 0.50
channelFunction · 0.50
exprFunction · 0.50
extensionalFunction · 0.50
relFunction · 0.50
branchFunction · 0.50

Tested by

no test coverage detected