Generated on Sat Feb 7 2015 02:01:12 for Gecode by doxygen 1.8.9.1
photo.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, 2001
8  *
9  * Last modified:
10  * $Date: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/driver.hh>
39 #include <gecode/int.hh>
40 #include <gecode/minimodel.hh>
41 
42 using namespace Gecode;
43 
45 class PhotoSpec {
46 public:
47  const int n_names;
48  const int n_prefs;
49  const int* prefs;
50  PhotoSpec(const int n_n, const int n_p, const int* p)
51  : n_names(n_n), n_prefs(n_p), prefs(p) {}
52 };
53 
55 const int s_prefs[] = {
56  0,2, 1,4, 2,3, 2,4, 3,0, 4,3, 4,0, 4,1
57 };
59 const PhotoSpec p_small(5, 8, s_prefs);
60 
62 const int l_prefs[] = {
63  0,2, 0,4, 0,7, 1,4, 1,8, 2,3, 2,4, 3,0, 3,4,
64  4,5, 4,0, 5,0, 5,8, 6,2, 6,7, 7,8, 7,6
65 };
67 const PhotoSpec p_large(9,17, l_prefs);
68 
80 class Photo : public IntMinimizeScript {
81 protected:
83  const PhotoSpec& spec;
88 
89 public:
91  enum {
93  BRANCH_DEGREE
94  };
96  Photo(const SizeOptions& opt) :
97  spec(opt.size() == 0 ? p_small : p_large),
98  pos(*this,spec.n_names, 0, spec.n_names-1),
99  violations(*this,0,spec.n_prefs)
100  {
101  // Map preferences to violation
102  BoolVarArgs viol(spec.n_prefs);
103  for (int i=0; i<spec.n_prefs; i++) {
104  int pa = spec.prefs[2*i+0];
105  int pb = spec.prefs[2*i+1];
106  viol[i] = expr(*this, abs(pos[pa]-pos[pb]) > 1);
107  }
108  rel(*this, violations == sum(viol));
109 
110  distinct(*this, pos, opt.icl());
111 
112  // Break some symmetries
113  rel(*this, pos[0] < pos[1]);
114 
115  if (opt.branching() == BRANCH_NONE) {
116  branch(*this, pos, INT_VAR_NONE(), INT_VAL_MIN());
117  } else {
119  INT_VAL_MIN());
120  }
121  }
122 
124  Photo(bool share, Photo& s) :
125  IntMinimizeScript(share,s), spec(s.spec) {
126  pos.update(*this, share, s.pos);
127  violations.update(*this, share, s.violations);
128  }
130  virtual Space*
131  copy(bool share) {
132  return new Photo(share,*this);
133  }
135  virtual void
136  print(std::ostream& os) const {
137  os << "\tpos[] = " << pos << std::endl
138  << "\tviolations: " << violations << std::endl;
139  }
141  virtual IntVar cost(void) const {
142  return violations;
143  }
144 };
145 
149 int
150 main(int argc, char* argv[]) {
151  SizeOptions opt("Photo");
152  opt.solutions(0);
153  opt.size(1);
154  opt.iterations(10);
155  opt.icl(ICL_BND);
157  opt.branching(Photo::BRANCH_NONE, "none");
158  opt.branching(Photo::BRANCH_DEGREE, "degree");
159  opt.parse(argc,argv);
160  IntMaximizeScript::run<Photo,BAB,SizeOptions>(opt);
161  return 0;
162 }
163 
164 
165 // STATISTICS: example-any
166 
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
PhotoSpec(const int n_n, const int n_p, const int *p)
Definition: photo.cpp:50
Options for scripts with additional size parameter
Definition: driver.hh:567
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:108
const PhotoSpec p_small(5, 8, s_prefs)
Small Photo example.
Example: Placing people on a photo
Definition: photo.cpp:80
const PhotoSpec & spec
Photo specification.
Definition: photo.cpp:83
void update(Space &home, bool share, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition: var.hpp:128
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:49
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
virtual Space * copy(bool share)
Copy during cloning.
Definition: photo.cpp:131
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:212
Integer variable array.
Definition: int.hh:741
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
const int n_names
Number of people on picture.
Definition: photo.cpp:47
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
int main(int argc, char *argv[])
Main-function.
Definition: photo.cpp:150
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
const int l_prefs[]
Preferences for large example.
Definition: photo.cpp:62
Gecode::IntArgs i(4, 1, 2, 3, 4)
IntVarBranch INT_VAR_DEGREE_MAX(BranchTbl tbl)
Select variable with largest degree.
Definition: var.hpp:147
Options opt
The options.
Definition: test.cpp:101
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
unsigned int size(I &i)
Size of all ranges of range iterator i.
virtual void print(std::ostream &os) const
Print solution.
Definition: photo.cpp:136
void branching(int v)
Set default branching value.
Definition: options.hpp:203
IntVarArray pos
Person's position on photo.
Definition: photo.cpp:85
Passing Boolean variables.
Definition: int.hh:690
virtual IntVar cost(void) const
Return solution cost.
Definition: photo.cpp:141
const int s_prefs[]
Preferences for small example.
Definition: photo.cpp:55
const int * prefs
Array of preferences.
Definition: photo.cpp:49
TieBreak< VarBranch > tiebreak(VarBranch a, VarBranch b)
Combine variable selection criteria a and b for tie-breaking.
BoolVar expr(Home home, const BoolExpr &e, IntConLevel icl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
const int n_prefs
Number of preferences.
Definition: photo.cpp:48
Integer variables.
Definition: int.hh:350
const PhotoSpec p_large(9, 17, l_prefs)
Large Photo example.
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Photo(const SizeOptions &opt)
Actual model.
Definition: photo.cpp:96
void distinct(Home home, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:47
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:243
Choose variables from left to right.
Definition: photo.cpp:92
Photo(bool share, Photo &s)
Constructor for cloning s.
Definition: photo.cpp:124
Specifications for the photo example.
Definition: photo.cpp:45
Bounds propagation or consistency.
Definition: int.hh:939
Gecode toplevel namespace
LinFloatExpr sum(const FloatVarArgs &x)
Construct linear float expression as sum of float variables.
Definition: float-expr.cpp:548
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
IntVar violations
Number of violated preferences.
Definition: photo.cpp:87
Choose variable with largest degree.
Definition: photo.cpp:93
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194