black-hole.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Mikael Lagerkvist <lagerkvist@gecode.org>
*
* Copyright:
* Mikael Lagerkvist, 2006
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace Gecode;
namespace {
using std::vector;
vector<vector<int> > layout;
vector<int> layer, pile;
void generate(int seed) {
// The layout consists of 17 piles of 3 cards each
layout = vector<vector<int> >(17, vector<int>(3));
// Deck without the Ace of Spades
vector<int> deck(51);
for (int i = 51; i--; ) deck[i] = i+1;
Support::RandomGenerator rnd(seed+1);
std::random_shuffle(deck.begin(), deck.end(), rnd);
// Place cards from deck
int pos = 0;
for (int i = 17; i--; )
for (int j = 3; j--; )
layout[i][j] = deck[pos++];
// Location-information for each card
layer = vector<int>(52);
pile = vector<int>(52);
for (int i = 17; i--; ) {
for (int j = 3; j--; ) {
layer[layout[i][j]] = j;
pile[ layout[i][j]] = i;
}
}
}
}
class BlackHole : public Script {
protected:
IntVarArray x,
y;
std::string
card(int val) const {
const char* suit = "SCHD";
std::ostringstream o;
o << std::setw(2) << (1 + (val%13)) << suit[val/13];
return o.str();
}
public:
enum {
SYMMETRY_NONE,
SYMMETRY_CONDITIONAL
};
enum {
PROPAGATION_REIFIED,
PROPAGATION_DFA,
PROPAGATION_TUPLE_SET
};
BlackHole(const SizeOptions& opt)
: Script(opt), x(*this, 52, 0,51), y(*this, 52, 0,51) {
// Black ace at bottom
rel(*this, x[0], IRT_EQ, 0);
// x is order and y is placement
channel(*this, x, y, opt.ipl());
// The placement rules: the absolute value of the difference
// between two consecutive cards is 1 or 12.
if (opt.propagation() == PROPAGATION_REIFIED) {
// Build table for accessing the rank of a card
IntArgs modtable(52);
for (int i = 0; i < 52; ++i) {
modtable[i] = i%13;
}
for (int i = 0; i < 51; ++i) {
IntVar x1(*this, 0, 12), x2(*this, 0, 12);
element(*this, modtable, x[i], x1);
element(*this, modtable, x[i+1], x2);
const int dr[2] = {1, 12};
IntVar diff(*this, IntSet(dr, 2));
rel(*this, abs(x1-x2) == diff, IPL_DOM);
}
} else if (opt.propagation() == PROPAGATION_DFA) {
// Build table for allowed tuples
REG expression;
for (int r = 13; r--; ) {
for (int s1 = 4; s1--; ) {
for (int s2 = 4; s2--; ) {
for (int i = -1; i <= 1; i+=2) {
REG r1 = REG(r+13*s1);
REG r2 = REG((r+i+52+13*s2)%52);
REG r = r1 + r2;
expression |= r;
}
}
}
}
DFA table(expression);
for (int i = 51; i--; )
extensional(*this, IntVarArgs({x[i],x[i+1]}), table);
} else { // opt.propagation() == PROPAGATION_TUPLE_SET)
// Build table for allowed tuples
TupleSet ts(2);
for (int r = 13; r--; )
for (int s1 = 4; s1--; )
for (int s2 = 4; s2--; )
for (int i = -1; i <= 1; i+=2)
ts.add({r+13*s1, (r+i+52+13*s2)%52});
ts.finalize();
for (int i = 51; i--; )
extensional(*this, IntVarArgs({x[i],x[i+1]}), ts);
}
// A card must be played before the one under it.
for (int i = 17; i--; )
for (int j = 2; j--; )
rel(*this, y[layout[i][j]] < y[layout[i][j+1]]);
// Compute and break the conditional symmetries that are dependent
// on the current layout.
// Two cards with the same rank but different suits are symmetric
// with respect to their placement in the black hole if changing
// their order does not affect any other card.
if (opt.symmetry() == SYMMETRY_CONDITIONAL) {
// For all ranks
for (int r = 13; r--; ) {
// For all pairs of suits
for (int s1 = 4; s1--; ) {
for (int s2 = s1; s2--; ) {
int c1 = 13*s1 + r,
c2 = 13*s2 + r;
// The ace of spades is already placed
if (c1 == 0 || c2 == 0) continue;
// Piles are handled by the rules of the game
if (pile[c1] == pile[c2]) continue;
// Fix the right order of the cards
int o1 = c1, o2 = c2;
if (pile[c1] > pile[c2] && layer[c2] >= layer[c1])
std::swap(o1, o2);
// cond is the condition for the symmetry
BoolVarArgs ba;
// Both cards played after the ones on top of them
for (int i = 0; i < layer[o1]; ++i)
ba << expr(*this, (y[layout[pile[o1]][i]] < y[o2]));
for (int i = 0; i < layer[o2]; ++i)
ba << expr(*this, (y[layout[pile[o2]][i]] < y[o1]));
// Both cards played before the ones under them
for (int i = layer[o1]+1; i < 3; ++i)
ba << expr(*this, (y[o2] < y[layout[pile[o1]][i]]));
for (int i = layer[o2]+1; i < 3; ++i)
ba << expr(*this, (y[o1] < y[layout[pile[o2]][i]]));
// Cond holds when all the above holds
BoolVar cond(*this, 0, 1);
rel(*this, BOT_AND, ba, cond);
// If cond is fulfilled, then we can order the cards
// cond -> (y[o1] < y[o2])
rel(*this, !cond || (y[o1] < y[o2]));
}
}
}
}
// Install custom brancher
branch(*this, x, INT_VAR_NONE(), INT_VAL(&val));
}
static int val(const Space&, IntVar x, int) {
int v = -1;
int w = 4;
for (IntVarValues vals(x); vals(); ++vals)
if (layer[vals.val()] < w) {
v = vals.val();
if ((w = layer[vals.val()]) == 0)
break;
}
assert(v >= 1 && v < 52);
return v;
}
virtual void
print(std::ostream& os) const {
os << "Layout:" << std::endl;
for (int i = 0; i < 17; i++) {
for (int j = 0; j < 3; j++)
os << card(layout[i][j]) << " ";
if ((i+1) % 3 == 0)
os << std::endl;
else
os << " \t";
}
os << std::endl << std::endl;
os << "Solution:" << std::endl;
for (int i = 0; i < 52; ++i) {
if (x[i].assigned())
os << card(x[i].val()) << " ";
else
os << " ";
if ((i + 1) % 13 == 0)
os << std::endl;
}
os << std::endl;
os << std::endl;
}
BlackHole(BlackHole& s) : Script(s) {
x.update(*this, s.x);
y.update(*this, s.y);
}
virtual Space*
copy(void) {
return new BlackHole(*this);
}
};
int
main(int argc, char* argv[]) {
SizeOptions opt("Black Hole patience");
opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL);
opt.symmetry(BlackHole::SYMMETRY_NONE,"none",
"no symmetry breaking");
opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL,"conditional",
"break conditional symmetries");
opt.propagation(BlackHole::PROPAGATION_TUPLE_SET);
opt.propagation(BlackHole::PROPAGATION_REIFIED,
"reified", "use reified propagation");
opt.propagation(BlackHole::PROPAGATION_DFA,
"dfa", "use DFA-based extensional propagation");
opt.propagation(BlackHole::PROPAGATION_TUPLE_SET,
"tuple-set", "use TupleSet-based extensional propagation");
opt.ipl(IPL_DOM);
opt.parse(argc,argv);
// Generates the new board
generate(opt.size());
Script::run<BlackHole,DFS,SizeOptions>(opt);
return 0;
}
// STATISTICS: example-any
BlackHole::main
int main(int argc, char *argv[])
Main-function.
Definition black-hole.cpp:294
BlackHole::BlackHole
BlackHole(BlackHole &s)
Constructor for cloning s.
Definition black-hole.cpp:279
BlackHole::SYMMETRY_CONDITIONAL
@ SYMMETRY_CONDITIONAL
Breaking conditional symmetries.
Definition black-hole.cpp:118
BlackHole::SYMMETRY_NONE
@ SYMMETRY_NONE
No symmetry breaking.
Definition black-hole.cpp:117
BlackHole::x
IntVarArray x
Card at position.
Definition black-hole.cpp:102
BlackHole::card
std::string card(int val) const
Return a string representing the card of value val.
Definition black-hole.cpp:107
BlackHole::BlackHole
BlackHole(const SizeOptions &opt)
Actual model.
Definition black-hole.cpp:127
BlackHole::copy
virtual Space * copy(void)
Copy during cloning.
Definition black-hole.cpp:285
BlackHole::val
static int val(const Space &, IntVar x, int)
Value selection function for branching.
Definition black-hole.cpp:239
BlackHole::y
IntVarArray y
Position of card.
Definition black-hole.cpp:103
BlackHole::PROPAGATION_REIFIED
@ PROPAGATION_REIFIED
Reified propagation.
Definition black-hole.cpp:122
BlackHole::PROPAGATION_DFA
@ PROPAGATION_DFA
Extensional propagation using automatons.
Definition black-hole.cpp:123
BlackHole::PROPAGATION_TUPLE_SET
@ PROPAGATION_TUPLE_SET
Extensional propagation using tables.
Definition black-hole.cpp:124
BlackHole::print
virtual void print(std::ostream &os) const
Print instance and solution.
Definition black-hole.cpp:253
Gecode::BoolVarArgs
Passing Boolean variables.
Definition int.hh:712
Gecode::BoolVar
Boolean integer variables.
Definition int.hh:512
Gecode::DFA
Deterministic finite automaton (DFA).
Definition int.hh:2049
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::IntArgs
Passing integer arguments.
Definition int.hh:628
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntVarArgs
Passing integer variables.
Definition int.hh:656
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::IntVarValues
Value iterator for integer variables.
Definition int.hh:490
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::REG
Regular expressions over integer values.
Definition minimodel.hh:1625
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition driver.hh:675
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::TupleSet
Class represeting a set of tuples.
Definition int.hh:2191
Gecode::TupleSet::add
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
Definition tuple-set.hpp:142
Gecode::TupleSet::finalize
void finalize(void)
Finalize tuple set.
Definition tuple-set.hpp:155
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.hh
Gecode::Support::RandomGenerator
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition random.hpp:120
Gecode::Script
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
Gecode::branch
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::extensional
void extensional(Home home, const IntVarArgs &x, DFA d, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for extensional constraint described by a DFA.
Definition extensional-regular.cpp:39
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::BOT_AND
@ BOT_AND
Conjunction.
Definition int.hh:951
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
Gecode::Float::Arithmetic::pos
bool pos(const View &x)
Test whether x is postive.
Definition mult.hpp:41
Gecode
Gecode toplevel namespace
Gecode::channel
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
Gecode::expr
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
Gecode::INT_VAL
IntValBranch INT_VAL(IntBranchVal v, IntBranchCommit c=nullptr)
Select value as defined by the value function v and commit function c Uses a commit function as defau...
Definition val.hpp:95
Gecode::ba
IntPropLevel ba(IntPropLevel ipl)
Extract basic or advanced from propagation level.
Definition ipl.hpp:43
Gecode::INT_VAR_NONE
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
Gecode::abs
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition arithmetic.cpp:41
Gecode::element
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})