Generated on Sat Feb 7 2015 02:01:10 for Gecode by doxygen 1.8.9.1
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: 2014-09-22 01:35:47 +0200 (Mon, 22 Sep 2014) $ by $Author: mears $
14  * $Revision: 14223 $
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  if (opt.symmetry() == SYMMETRY_LDSB) {
133  Symmetries s;
134  s << rows_interchange(p);
135  s << columns_interchange(p);
136  branch(*this, _p, INT_VAR_NONE(), INT_VAL_MIN(), s);
137  } else {
138  if (opt.symmetry() == SYMMETRY_LEX) {
139  for (int i=1; i<opt.v; i++)
140  rel(*this, p.row(i-1), IRT_GQ, p.row(i));
141  for (int j=1; j<opt.b; j++)
142  rel(*this, p.col(j-1), IRT_GQ, p.col(j));
143  }
144  branch(*this, _p, INT_VAR_NONE(), INT_VAL_MIN());
145  }
146 
147  }
148 
150  virtual void
151  print(std::ostream& os) const {
152  os << "\tBIBD("
153  << opt.v << "," << opt.k << ","
154  << opt.lambda << ")" << std::endl;
155  Matrix<BoolVarArray> p(_p,opt.b,opt.v);
156  for (int i = 0; i<opt.v; i++) {
157  os << "\t\t";
158  for (int j = 0; j<opt.b; j++)
159  os << p(j,i) << " ";
160  os << std::endl;
161  }
162  os << std::endl;
163  }
164 
166  BIBD(bool share, BIBD& s)
167  : Script(share,s), opt(s.opt) {
168  _p.update(*this,share,s._p);
169  }
170 
172  virtual Space*
173  copy(bool share) {
174  return new BIBD(share,*this);
175  }
176 
177 };
178 
182 int
183 main(int argc, char* argv[]) {
184  BIBDOptions opt("BIBD",7,3,60);
185 
187  opt.symmetry(BIBD::SYMMETRY_NONE,"none");
188  opt.symmetry(BIBD::SYMMETRY_LEX,"lex");
189  opt.symmetry(BIBD::SYMMETRY_LDSB,"ldsb");
190 
191  opt.parse(argc,argv);
192 
193  /*
194  * Other interesting instances:
195  * BIBD(7,3,1), BIBD(6,3,2), BIBD(7,3,20), ...
196  */
197 
198  Script::run<BIBD,DFS,BIBDOptions>(opt);
199  return 0;
200 }
201 
202 // STATISTICS: example-any
203 
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
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:4300
SymmetryHandle columns_interchange(const Matrix< A > &m)
Interchangeable columns symmetry specification.
Definition: ldsb.hpp:55
No symmetry breaking.
Definition: bibd.cpp:105
Computation spaces.
Definition: core.hpp:1362
Greater or equal ( )
Definition: int.hh:908
Parametric base-class for scripts.
Definition: driver.hh:622
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
int lambda
Parameters to be given on command line.
Definition: bibd.cpp:52
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual Space * copy(bool share)
Copy during cloning.
Definition: bibd.cpp:173
Equality ( )
Definition: int.hh:904
Options opt
The options.
Definition: test.cpp:101
int b
Definition: bibd.cpp:53
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
const BIBDOptions & opt
Options providing access to parameters.
Definition: bibd.cpp:99
LDSB on rows/columns.
Definition: bibd.cpp:107
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
Passing Boolean variables.
Definition: int.hh:690
Lex-constraints on rows/columns.
Definition: bibd.cpp:106
Boolean variable array.
Definition: int.hh:786
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:207
int main(int argc, char *argv[])
Main-function.
Definition: bibd.cpp:183
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:151
BIBD(bool share, BIBD &s)
Constructor for cloning s.
Definition: bibd.cpp:166
Matrix-interface for arrays.
Definition: minimodel.hh:1924
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
int k
Definition: bibd.cpp:52
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