00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <gecode/driver.hh>
00040 #include <gecode/int.hh>
00041 #include <gecode/minimodel.hh>
00042 #include <gecode/set.hh>
00043
00044 using namespace Gecode;
00045
00050 IntSet* A;
00051
00071 class QueenArmies : public MaximizeScript {
00072 public:
00073 const int n;
00074 SetVar U,
00075 W;
00076 BoolVarArray w,
00077 b;
00078 IntVar q;
00079
00081 enum {
00082 BRANCH_NAIVE,
00083 BRANCH_SPECIFIC
00084 };
00085
00087 enum {
00088 SEARCH_BAB,
00089 SEARCH_RESTART
00090 };
00091
00093 QueenArmies(const SizeOptions& opt) :
00094 n(opt.size()),
00095 U(*this, IntSet::empty, IntSet(0, n*n)),
00096 W(*this, IntSet::empty, IntSet(0, n*n)),
00097 w(*this, n*n, 0, 1),
00098 b(*this, n*n, 0, 1),
00099 q(*this, 0, n*n)
00100 {
00101
00102 for (int i = n*n; i--; ) {
00103
00104 rel(*this, w[i] == (U || A[i]));
00105
00106 rel(*this, !w[i] || !b[i]);
00107
00108 rel(*this, b[i] == (singleton(i) <= U));
00109 }
00110
00111
00112 linear(*this, w, IRT_EQ, q);
00113 linear(*this, b, IRT_GQ, q);
00114
00115
00116 IntVar unknowns = expr(*this, cardinality(U));
00117 rel(*this, q <= unknowns);
00118 linear(*this, b, IRT_EQ, unknowns);
00119
00120 if (opt.branching() == BRANCH_NAIVE) {
00121 branch(*this, w, INT_VAR_NONE, INT_VAL_MAX);
00122 branch(*this, b, INT_VAR_NONE, INT_VAL_MAX);
00123 } else {
00124 QueenBranch::post(*this);
00125 assign(*this, b, INT_ASSIGN_MAX);
00126 }
00127 }
00129 QueenArmies(bool share, QueenArmies& s)
00130 : MaximizeScript(share,s), n(s.n) {
00131 U.update(*this, share, s.U);
00132 W.update(*this, share, s.W);
00133 w.update(*this, share, s.w);
00134 b.update(*this, share, s.b);
00135 q.update(*this, share, s.q);
00136 }
00138 virtual Space*
00139 copy(bool share) {
00140 return new QueenArmies(share,*this);
00141 }
00143 virtual IntVar cost(void) const {
00144 return q;
00145 }
00147 virtual void
00148 print(std::ostream& os) const {
00149 os << '\t';
00150 for (int i = 0; i < n*n; ++i) {
00151 if (w[i].assigned() && w[i].val()) os << "W";
00152 else if (b[i].assigned() && b[i].val()) os << "B";
00153 else if (!w[i].assigned() && !b[i].assigned()) os << " ";
00154 else os << ".";
00155 if ((i+1)%n == 0) os << std::endl << (i!=(n*n-1)?"\t":"");
00156 }
00157 os << "Number of white queens: " << q << std::endl << std::endl;
00158 }
00159
00167 class QueenBranch : public Brancher {
00168 private:
00170 mutable int start;
00172 class Choice : public Gecode::Choice {
00173 public:
00175 int pos;
00177 bool val;
00181 Choice(const Brancher& b, int pos0, bool val0)
00182 : Gecode::Choice(b,2), pos(pos0), val(val0) {}
00184 virtual size_t size(void) const {
00185 return sizeof(Choice);
00186 }
00188 virtual void archive(Archive& e) const {
00189 Gecode::Choice::archive(e);
00190 e << pos << val;
00191 }
00192 };
00193
00195 QueenBranch(Home home)
00196 : Brancher(home), start(0) {}
00198 QueenBranch(Space& home, bool share, QueenBranch& b)
00199 : Brancher(home, share, b), start(b.start) {}
00200
00201 public:
00203 virtual bool status(const Space& home) const {
00204 const QueenArmies& q = static_cast<const QueenArmies&>(home);
00205 for (int i = start; i < q.n*q.n; ++i)
00206 if (!q.w[i].assigned()) {
00207 start = i;
00208 return true;
00209 }
00210
00211 return false;
00212 }
00214 virtual Gecode::Choice* choice(Space& home) {
00215 const QueenArmies& q = static_cast<const QueenArmies&>(home);
00216 int maxsize = -1;
00217 int pos = -1;
00218
00219 for (int i = start; i < q.n*q.n; ++i) {
00220 if (q.w[i].assigned()) continue;
00221 IntSetRanges ai(A[i]);
00222 SetVarUnknownRanges qU(q.U);
00223 Iter::Ranges::Inter<IntSetRanges, SetVarUnknownRanges> r(ai, qU);
00224 int size = Iter::Ranges::size(r);
00225 if (size > maxsize) {
00226 maxsize = size;
00227 pos = i;
00228 }
00229 }
00230
00231 assert(pos != -1);
00232 return new Choice(*this, pos, true);
00233 }
00235 virtual Choice* choice(const Space&, Archive& e) {
00236 int pos, val;
00237 e >> pos >> val;
00238 return new Choice(*this, pos, val);
00239 }
00243 virtual ExecStatus commit(Space& home, const Gecode::Choice& _c,
00244 unsigned int a) {
00245 QueenArmies& q = static_cast<QueenArmies&>(home);
00246 const Choice& c = static_cast<const Choice&>(_c);
00247 bool val = (a == 0) ? c.val : !c.val;
00248 return me_failed(Int::BoolView(q.w[c.pos]).eq(q, val))
00249 ? ES_FAILED
00250 : ES_OK;
00251 }
00253 virtual Actor* copy(Space& home, bool share) {
00254 return new (home) QueenBranch(home, share, *this);
00255 }
00257 static void post(QueenArmies& home) {
00258 (void) new (home) QueenBranch(home);
00259 }
00261 virtual size_t dispose(Space&) {
00262 return sizeof(*this);
00263 }
00264 };
00265 };
00266
00271 int pos(int i, int j, int n) {
00272 return i*n + j;
00273 }
00274
00278 int
00279 main(int argc, char* argv[]) {
00280 SizeOptions opt("QueenArmies");
00281 opt.size(6);
00282 opt.branching(QueenArmies::BRANCH_SPECIFIC);
00283 opt.branching(QueenArmies::BRANCH_NAIVE, "naive");
00284 opt.branching(QueenArmies::BRANCH_SPECIFIC, "specific");
00285 opt.search(QueenArmies::SEARCH_BAB);
00286 opt.search(QueenArmies::SEARCH_BAB, "bab");
00287 opt.search(QueenArmies::SEARCH_RESTART, "restart");
00288 opt.solutions(0);
00289 opt.parse(argc,argv);
00290
00291
00292
00293 int n = opt.size();
00294 A = new IntSet[n*n];
00295 int *p = new int[std::max(n*n, 25)];
00296 int pn = 0;
00297 for (int i = n; i--; ) {
00298 for (int j = n; j--; ) {
00299 int dir[][2] = {
00300 { 0, 1},
00301 { 1, 1},
00302 { 1, 0},
00303 { 0, -1},
00304 {-1, -1},
00305 {-1, 0},
00306 { 1, -1},
00307 {-1, 1}
00308 };
00309 p[pn++] = pos(i, j, n);
00310 for (int k = 8; k--; ) {
00311 for (int l = 0; l < n
00312 && 0 <= (i+l*dir[k][0]) && (i+l*dir[k][0]) < n
00313 && 0 <= (j+l*dir[k][1]) && (j+l*dir[k][1]) < n; ++l) {
00314 p[pn++] = pos(i+l*dir[k][0], j+l*dir[k][1], n);
00315 }
00316 }
00317
00318 A[pos(i, j, n)] = IntSet(p, pn);
00319
00320 pn = 0;
00321 }
00322 }
00323 delete [] p;
00324
00325
00326 switch (opt.search()) {
00327 case QueenArmies::SEARCH_BAB:
00328 MaximizeScript::run<QueenArmies,BAB,SizeOptions>(opt); break;
00329 case QueenArmies::SEARCH_RESTART:
00330 MaximizeScript::run<QueenArmies,Restart,SizeOptions>(opt); break;
00331 }
00332 return 0;
00333 }
00334
00335