Generated on Mon Sep 22 2014 12:49:22 for Gecode by doxygen 1.8.7
bibd.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2004
8  *
9  * Bugfixes provided by:
10  * Olof Sivertsson <olsi0767@student.uu.se>
11  *
12  * Last modified:
13  * $Date: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
14  * $Revision: 13820 $
15  *
16  * This file is part of Gecode, the generic constraint
17  * development environment:
18  * http://www.gecode.org
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  *
39  */
40 
41 #include <gecode/driver.hh>
42 #include <gecode/int.hh>
43 
44 using namespace Gecode;
45 
50 class BIBDOptions : public Options {
51 public:
52  int v, k, lambda;
53  int b, r;
54  void derive(void) {
56  b = (v*(v-1)*lambda)/(k*(k-1));
57  r = (lambda*(v-1)) / (k-1);
58  }
60  BIBDOptions(const char* s,
61  int v0, int k0, int lambda0)
62  : Options(s), v(v0), k(k0), lambda(lambda0) {
63  derive();
64  }
66  void parse(int& argc, char* argv[]) {
67  Options::parse(argc,argv);
68  if (argc < 4)
69  return;
70  v = atoi(argv[1]);
71  k = atoi(argv[2]);
72  lambda = atoi(argv[3]);
73  derive();
74  }
76  virtual void help(void) {
77  Options::help();
78  std::cerr << "\t(unsigned int) default: " << v << std::endl
79  << "\t\tparameter v" << std::endl
80  << "\t(unsigned int) default: " << k << std::endl
81  << "\t\tparameter k" << std::endl
82  << "\t(unsigned int) default: " << lambda << std::endl
83  << "\t\tparameter lambda" << std::endl;
84  }
85 };
86 
87 
96 class BIBD : public Script {
97 protected:
99  const BIBDOptions& opt;
102 public:
104  enum {
107  SYMMETRY_LDSB
108  };
109 
111  BIBD(const BIBDOptions& o)
112  : opt(o), _p(*this,opt.v*opt.b,0,1) {
113  Matrix<BoolVarArray> p(_p,opt.b,opt.v);
114 
115  // r ones per row
116  for (int i=0; i<opt.v; i++)
117  linear(*this, p.row(i), IRT_EQ, opt.r);
118 
119  // k ones per column
120  for (int j=0; j<opt.b; j++)
121  linear(*this, p.col(j), IRT_EQ, opt.k);
122 
123  // Exactly lambda ones in scalar product between two different rows
124  for (int i1=0; i1<opt.v; i1++)
125  for (int i2=i1+1; i2<opt.v; i2++) {
126  BoolVarArgs row(opt.b);
127  for (int j=0; j<opt.b; j++)
128  row[j] = expr(*this, p(j,i1) && p(j,i2));
129  linear(*this, row, IRT_EQ, opt.lambda);
130  }
131 
132  // IntVarArray ints(*this, opt.v*opt.b, 0, 1);
133  // for (int i = 0 ; i < opt.v*opt.b ; i++)
134  // rel(*this, ints[i] == _p[i]);
135  // Matrix<IntVarArray> intm(ints, opt.b, opt.v);
136 
137  if (opt.symmetry() == SYMMETRY_LDSB) {
138  Symmetries s;
139  s << rows_interchange(p);
140  s << columns_interchange(p);
141  branch(*this, _p, INT_VAR_NONE(), INT_VAL_MIN(), s);
142  } else {
143  if (opt.symmetry() == SYMMETRY_LEX) {
144  for (int i=1; i<opt.v; i++)
145  rel(*this, p.row(i-1), IRT_GQ, p.row(i));
146  for (int j=1; j<opt.b; j++)
147  rel(*this, p.col(j-1), IRT_GQ, p.col(j));
148  }
149  branch(*this, _p, INT_VAR_NONE(), INT_VAL_MIN());
150  }
151 
152  }
153 
155  virtual void
156  print(std::ostream& os) const {
157  os << "\tBIBD("
158  << opt.v << "," << opt.k << ","
159  << opt.lambda << ")" << std::endl;
160  Matrix<BoolVarArray> p(_p,opt.b,opt.v);
161  for (int i = 0; i<opt.v; i++) {
162  os << "\t\t";
163  for (int j = 0; j<opt.b; j++)
164  os << p(j,i) << " ";
165  os << std::endl;
166  }
167  os << std::endl;
168  }
169 
171  BIBD(bool share, BIBD& s)
172  : Script(share,s), opt(s.opt) {
173  _p.update(*this,share,s._p);
174  }
175 
177  virtual Space*
178  copy(bool share) {
179  return new BIBD(share,*this);
180  }
181 
182 };
183 
187 int
188 main(int argc, char* argv[]) {
189  BIBDOptions opt("BIBD",7,3,60);
190 
192  opt.symmetry(BIBD::SYMMETRY_NONE,"none");
193  opt.symmetry(BIBD::SYMMETRY_LEX,"lex");
194  opt.symmetry(BIBD::SYMMETRY_LDSB,"ldsb");
195 
196  opt.parse(argc,argv);
197 
198  /*
199  * Other interesting instances:
200  * BIBD(7,3,1), BIBD(6,3,2), BIBD(7,3,20), ...
201  */
202 
203  Script::run<BIBD,DFS,BIBDOptions>(opt);
204  return 0;
205 }
206 
207 // STATISTICS: example-any
208 
int r
Derived parameters Derive additional parameters.
Definition: bibd.cpp:53
Options for BIBD problems
Definition: bibd.cpp:50
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:108
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
LDSB on rows/columns.
Definition: bibd.cpp:107
int v
Definition: bibd.cpp:52
virtual void help(void)
Print help message.
Definition: bibd.cpp:76
BIBD(const BIBDOptions &o)
Actual model.
Definition: bibd.cpp:111
Collection of symmetries.
Definition: int.hh:4228
SymmetryHandle columns_interchange(const Matrix< A > &m)
Interchangeable columns symmetry specification.
Definition: ldsb.hpp:55
Computation spaces.
Definition: core.hpp:1325
Greater or equal ( )
Definition: int.hh:906
Parametric base-class for scripts.
Definition: driver.hh:622
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual Space * copy(bool share)
Copy during cloning.
Definition: bibd.cpp:178
Equality ( )
Definition: int.hh:902
Options opt
The options.
Definition: test.cpp:101
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Lex-constraints on rows/columns.
Definition: bibd.cpp:106
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
const BIBDOptions & opt
Options providing access to parameters.
Definition: bibd.cpp:99
BoolVarArray _p
Matrix of Boolean variables.
Definition: bibd.cpp:101
Slice< A > row(int r) const
Access row r.
Definition: matrix.hpp:181
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: bibd.cpp:66
Example: Balanced incomplete block design (BIBD)
Definition: bibd.cpp:96
struct Gecode::@512::NNF::@54::@55 b
For binary nodes (and, or, eqv)
Passing Boolean variables.
Definition: int.hh:688
Boolean variable array.
Definition: int.hh:784
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:331
const int v[7]
Definition: distinct.cpp:206
int main(int argc, char *argv[])
Main-function.
Definition: bibd.cpp:188
No symmetry breaking.
Definition: bibd.cpp:105
BoolVar expr(Home home, const BoolExpr &e, IntConLevel icl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
void symmetry(int v)
Set default symmetry value.
Definition: options.hpp:168
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
SymmetryHandle rows_interchange(const Matrix< A > &m)
Interchangeable rows symmetry specification.
Definition: ldsb.hpp:44
virtual void print(std::ostream &os) const
Print solution.
Definition: bibd.cpp:156
BIBD(bool share, BIBD &s)
Constructor for cloning s.
Definition: bibd.cpp:171
Matrix-interface for arrays.
Definition: minimodel.hh:1924
Slice< A > col(int c) const
Access column c.
Definition: matrix.hpp:187
BrancherHandle branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
BIBDOptions(const char *s, int v0, int k0, int lambda0)
Initialize options for example with name s.
Definition: bibd.cpp:60
Options for scripts
Definition: driver.hh:326
virtual void help(void)
Print help text.
Definition: options.cpp:284