magic-sequence.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "examples/support.hh"
00023 #include "gecode/minimodel.hh"
00024
00037 class MagicSequence : public Example {
00038 private:
00040 const int n;
00042 IntVarArray s;
00043 public:
00045 void
00046 exactly(IntVarArray& v, IntVar& x, int i) {
00047
00048 BoolVarArgs b(v.size());
00049 for (int j = v.size(); j--; )
00050 b[j] = post(this, ~(v[j] == i));
00051 linear(this, b, IRT_EQ, x);
00052 }
00054 MagicSequence(const Options& opt)
00055 : n(opt.size), s(this,n,0,n-1) {
00056 if (opt.naive)
00057 for (int i=n; i--; )
00058 exactly(s, s[i], i);
00059 else
00060 for (int i=n; i--; )
00061 count(this, s, i, IRT_EQ, s[i]);
00062 linear(this, s, IRT_EQ, n);
00063 IntArgs c(n);
00064 for (int j = n; j--; )
00065 c[j] = j-1;
00066 linear(this, c, s, IRT_EQ, 0);
00067 branch(this, s, BVAR_NONE, BVAL_SPLIT_MAX);
00068 }
00069
00071 MagicSequence(bool share, MagicSequence& e) : Example(share,e), n(e.n) {
00072 s.update(this, share, e.s);
00073 }
00075 virtual Space*
00076 copy(bool share) {
00077 return new MagicSequence(share,*this);
00078 }
00080 virtual
00081 void print(void) {
00082 std::cout << "\t";
00083 for (int i = 0; i<n; i++) {
00084 std::cout << s[i] << ", ";
00085 if ((i+1) % 20 == 0)
00086 std::cout << std::endl << "\t";
00087 }
00088 std::cout << std::endl;
00089 }
00090
00091 };
00092
00096 int
00097 main(int argc, char** argv) {
00098 Options opt("MagicSequence");
00099 opt.solutions = 0;
00100 opt.iterations = 4;
00101 opt.size = 500;
00102 opt.parse(argc,argv);
00103 Example::run<MagicSequence,DFS>(opt);
00104 return 0;
00105 }
00106
00107
00108