Generated on Fri Oct 19 11:24:48 2018 for Gecode by doxygen 1.6.3

bibd.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2004
00008  *
00009  *  Bugfixes provided by:
00010  *     Olof Sivertsson <olsi0767@student.uu.se>
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  Permission is hereby granted, free of charge, to any person obtaining
00017  *  a copy of this software and associated documentation files (the
00018  *  "Software"), to deal in the Software without restriction, including
00019  *  without limitation the rights to use, copy, modify, merge, publish,
00020  *  distribute, sublicense, and/or sell copies of the Software, and to
00021  *  permit persons to whom the Software is furnished to do so, subject to
00022  *  the following conditions:
00023  *
00024  *  The above copyright notice and this permission notice shall be
00025  *  included in all copies or substantial portions of the Software.
00026  *
00027  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00028  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00029  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00030  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00031  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00032  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00033  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00034  *
00035  */
00036 
00037 #include <gecode/driver.hh>
00038 #include <gecode/int.hh>
00039 
00040 using namespace Gecode;
00041 
00046 class BIBDOptions : public Options {
00047 public:
00048   int v, k, lambda; 
00049   int b, r;         
00050 
00051   void derive(void) {
00052     b = (v*(v-1)*lambda)/(k*(k-1));
00053     r = (lambda*(v-1)) / (k-1);
00054   }
00056   BIBDOptions(const char* s,
00057               int v0, int k0, int lambda0)
00058     : Options(s), v(v0), k(k0), lambda(lambda0) {
00059     derive();
00060   }
00062   void parse(int& argc, char* argv[]) {
00063     Options::parse(argc,argv);
00064     if (argc < 4)
00065       return;
00066     v = atoi(argv[1]);
00067     k = atoi(argv[2]);
00068     lambda = atoi(argv[3]);
00069     derive();
00070   }
00072   virtual void help(void) {
00073     Options::help();
00074     std::cerr << "\t(unsigned int) default: " << v << std::endl
00075               << "\t\tparameter v" << std::endl
00076               << "\t(unsigned int) default: " << k << std::endl
00077               << "\t\tparameter k" << std::endl
00078               << "\t(unsigned int) default: " << lambda << std::endl
00079               << "\t\tparameter lambda" << std::endl;
00080   }
00081 };
00082 
00083 
00092 class BIBD : public Script {
00093 protected:
00095   const BIBDOptions& opt;
00097   BoolVarArray _p;
00098 public:
00100   enum {
00101     SYMMETRY_NONE,      
00102     SYMMETRY_LEX,       
00103     SYMMETRY_LDSB       
00104   };
00105 
00107   BIBD(const BIBDOptions& o)
00108     : Script(o), opt(o), _p(*this,opt.v*opt.b,0,1) {
00109     Matrix<BoolVarArray> p(_p,opt.b,opt.v);
00110 
00111     // r ones per row
00112     for (int i=0; i<opt.v; i++)
00113       linear(*this, p.row(i), IRT_EQ, opt.r);
00114 
00115     // k ones per column
00116     for (int j=0; j<opt.b; j++)
00117       linear(*this, p.col(j), IRT_EQ, opt.k);
00118 
00119     // Exactly lambda ones in scalar product between two different rows
00120     for (int i1=0; i1<opt.v; i1++)
00121       for (int i2=i1+1; i2<opt.v; i2++) {
00122         BoolVarArgs row(opt.b);
00123         for (int j=0; j<opt.b; j++)
00124           row[j] = expr(*this, p(j,i1) && p(j,i2));
00125         linear(*this, row, IRT_EQ, opt.lambda);
00126       }
00127 
00128     if (opt.symmetry() == SYMMETRY_LDSB) {
00129       Symmetries s;
00130       s << rows_interchange(p);
00131       s << columns_interchange(p);
00132       branch(*this, _p, BOOL_VAR_NONE(), BOOL_VAL_MIN(), s);
00133     } else {
00134       if (opt.symmetry() == SYMMETRY_LEX) {
00135         for (int i=1; i<opt.v; i++)
00136           rel(*this, p.row(i-1), IRT_GQ, p.row(i));
00137         for (int j=1; j<opt.b; j++)
00138           rel(*this, p.col(j-1), IRT_GQ, p.col(j));
00139       }
00140       branch(*this, _p, BOOL_VAR_NONE(), BOOL_VAL_MIN());
00141     }
00142 
00143   }
00144 
00146   virtual void
00147   print(std::ostream& os) const {
00148     os << "\tBIBD("
00149        << opt.v << "," << opt.k << ","
00150        << opt.lambda << ")" << std::endl;
00151     Matrix<BoolVarArray> p(_p,opt.b,opt.v);
00152     for (int i = 0; i<opt.v; i++) {
00153       os << "\t\t";
00154       for (int j = 0; j<opt.b; j++)
00155         os << p(j,i) << " ";
00156       os << std::endl;
00157     }
00158     os << std::endl;
00159   }
00160 
00162   BIBD(BIBD& s)
00163     : Script(s), opt(s.opt) {
00164     _p.update(*this, s._p);
00165   }
00166 
00168   virtual Space*
00169   copy(void) {
00170     return new BIBD(*this);
00171   }
00172 
00173 };
00174 
00178 int
00179 main(int argc, char* argv[]) {
00180   BIBDOptions opt("BIBD",7,3,60);
00181 
00182   opt.symmetry(BIBD::SYMMETRY_LEX);
00183   opt.symmetry(BIBD::SYMMETRY_NONE,"none");
00184   opt.symmetry(BIBD::SYMMETRY_LEX,"lex");
00185   opt.symmetry(BIBD::SYMMETRY_LDSB,"ldsb");
00186 
00187   opt.parse(argc,argv);
00188 
00189   /*
00190    * Other interesting instances:
00191    * BIBD(7,3,1), BIBD(6,3,2), BIBD(7,3,20), ...
00192    */
00193 
00194   Script::run<BIBD,DFS,BIBDOptions>(opt);
00195   return 0;
00196 }
00197 
00198 // STATISTICS: example-any
00199