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
00040
00041 #include <gecode/driver.hh>
00042 #include <gecode/int.hh>
00043
00044 using namespace Gecode;
00045
00050 class BIBDOptions : public Options {
00051 public:
00052 int v, k, lambda;
00053 int b, r;
00054
00055 void derive(void) {
00056 b = (v*(v-1)*lambda)/(k*(k-1));
00057 r = (lambda*(v-1)) / (k-1);
00058 }
00060 BIBDOptions(const char* s,
00061 int v0, int k0, int lambda0)
00062 : Options(s), v(v0), k(k0), lambda(lambda0) {
00063 derive();
00064 }
00066 void parse(int& argc, char* argv[]) {
00067 Options::parse(argc,argv);
00068 if (argc < 4)
00069 return;
00070 v = atoi(argv[1]);
00071 k = atoi(argv[2]);
00072 lambda = atoi(argv[3]);
00073 derive();
00074 }
00076 virtual void help(void) {
00077 Options::help();
00078 std::cerr << "\t(unsigned int) default: " << v << std::endl
00079 << "\t\tparameter v" << std::endl
00080 << "\t(unsigned int) default: " << k << std::endl
00081 << "\t\tparameter k" << std::endl
00082 << "\t(unsigned int) default: " << lambda << std::endl
00083 << "\t\tparameter lambda" << std::endl;
00084 }
00085 };
00086
00087
00096 class BIBD : public Script {
00097 protected:
00099 const BIBDOptions& opt;
00101 BoolVarArray _p;
00102 public:
00104 enum {
00105 SYMMETRY_NONE,
00106 SYMMETRY_LEX,
00107 SYMMETRY_LDSB
00108 };
00109
00111 BIBD(const BIBDOptions& o)
00112 : opt(o), _p(*this,opt.v*opt.b,0,1) {
00113 Matrix<BoolVarArray> p(_p,opt.b,opt.v);
00114
00115
00116 for (int i=0; i<opt.v; i++)
00117 linear(*this, p.row(i), IRT_EQ, opt.r);
00118
00119
00120 for (int j=0; j<opt.b; j++)
00121 linear(*this, p.col(j), IRT_EQ, opt.k);
00122
00123
00124 for (int i1=0; i1<opt.v; i1++)
00125 for (int i2=i1+1; i2<opt.v; i2++) {
00126 BoolVarArgs row(opt.b);
00127 for (int j=0; j<opt.b; j++)
00128 row[j] = expr(*this, p(j,i1) && p(j,i2));
00129 linear(*this, row, IRT_EQ, opt.lambda);
00130 }
00131
00132
00133
00134
00135
00136
00137 if (opt.symmetry() == SYMMETRY_LDSB) {
00138 Symmetries s;
00139 s << rows_interchange(p);
00140 s << columns_interchange(p);
00141 branch(*this, _p, INT_VAR_NONE(), INT_VAL_MIN(), s);
00142 } else {
00143 if (opt.symmetry() == SYMMETRY_LEX) {
00144 for (int i=1; i<opt.v; i++)
00145 rel(*this, p.row(i-1), IRT_GQ, p.row(i));
00146 for (int j=1; j<opt.b; j++)
00147 rel(*this, p.col(j-1), IRT_GQ, p.col(j));
00148 }
00149 branch(*this, _p, INT_VAR_NONE(), INT_VAL_MIN());
00150 }
00151
00152 }
00153
00155 virtual void
00156 print(std::ostream& os) const {
00157 os << "\tBIBD("
00158 << opt.v << "," << opt.k << ","
00159 << opt.lambda << ")" << std::endl;
00160 Matrix<BoolVarArray> p(_p,opt.b,opt.v);
00161 for (int i = 0; i<opt.v; i++) {
00162 os << "\t\t";
00163 for (int j = 0; j<opt.b; j++)
00164 os << p(j,i) << " ";
00165 os << std::endl;
00166 }
00167 os << std::endl;
00168 }
00169
00171 BIBD(bool share, BIBD& s)
00172 : Script(share,s), opt(s.opt) {
00173 _p.update(*this,share,s._p);
00174 }
00175
00177 virtual Space*
00178 copy(bool share) {
00179 return new BIBD(share,*this);
00180 }
00181
00182 };
00183
00187 int
00188 main(int argc, char* argv[]) {
00189 BIBDOptions opt("BIBD",7,3,60);
00190
00191 opt.symmetry(BIBD::SYMMETRY_LEX);
00192 opt.symmetry(BIBD::SYMMETRY_NONE,"none");
00193 opt.symmetry(BIBD::SYMMETRY_LEX,"lex");
00194 opt.symmetry(BIBD::SYMMETRY_LDSB,"ldsb");
00195
00196 opt.parse(argc,argv);
00197
00198
00199
00200
00201
00202
00203 Script::run<BIBD,DFS,BIBDOptions>(opt);
00204 return 0;
00205 }
00206
00207
00208