Created
April 26, 2014 13:12
-
-
Save dvberkel/11319765 to your computer and use it in GitHub Desktop.
Gecode constrain variables to tuples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <gecode/int.hh> | |
#include <gecode/search.hh> | |
using namespace Gecode; | |
class Tuple : public Space { | |
protected: | |
IntVar x; | |
IntVar y; | |
public: | |
Tuple(std::vector<int> values){ | |
{ | |
auto minimum = std::min_element(std::begin(values), std::end(values)); | |
auto maximum = std::max_element(std::begin(values), std::end(values)); | |
x = IntVar(*this, 0, values.size() - 1); | |
y = IntVar(*this, *minimum, *maximum); | |
} | |
{ | |
std::vector<int>::iterator v = values.begin(); | |
for (IntVarValues i(x); i(); ++i, ++v) { | |
BoolVar b = BoolVar(*this, 0, 1); | |
rel(*this, x, IRT_EQ, i.val(), b); | |
rel(*this, y, IRT_EQ, *v, b); | |
} | |
} | |
branch(*this, x, INT_VAL_MIN()); | |
} | |
Tuple(bool share, Tuple& t) : Space(share, t) { | |
x.update(*this, share, t.x); | |
y.update(*this, share, t.y); | |
} | |
virtual Space* copy(bool share) { | |
return new Tuple(share, *this); | |
} | |
void print(void) const { | |
std::cout << "(" << x << "," << y << ")" << std::endl; | |
} | |
}; | |
int main(int argc, char* argv[]) { | |
Tuple* t = new Tuple({ 3, 1, 4, 2, 6, 5 }); | |
DFS<Tuple> engine(t); | |
delete t; | |
while (Tuple* solution = engine.next()) { | |
solution->print(); | |
delete(solution); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment