Generated on Tue Oct 24 2017 22:44:07 for Gecode by doxygen 1.8.5
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: 2015-03-17 16:09:39 +0100 (Tue, 17 Mar 2015) $ by $Author: schulte $
11  * $Revision: 14447 $
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  IntMinimizeScript(opt),
98  spec(opt.size() == 0 ? p_small : p_large),
99  pos(*this,spec.n_names, 0, spec.n_names-1),
100  violations(*this,0,spec.n_prefs)
101  {
102  // Map preferences to violation
103  BoolVarArgs viol(spec.n_prefs);
104  for (int i=0; i<spec.n_prefs; i++) {
105  int pa = spec.prefs[2*i+0];
106  int pb = spec.prefs[2*i+1];
107  viol[i] = expr(*this, abs(pos[pa]-pos[pb]) > 1);
108  }
109  rel(*this, violations == sum(viol));
110 
111  distinct(*this, pos, opt.icl());
112 
113  // Break some symmetries
114  rel(*this, pos[0] < pos[1]);
115 
116  if (opt.branching() == BRANCH_NONE) {
117  branch(*this, pos, INT_VAR_NONE(), INT_VAL_MIN());
118  } else {
120  INT_VAL_MIN());
121  }
122  }
123 
125  Photo(bool share, Photo& s) :
126  IntMinimizeScript(share,s), spec(s.spec) {
127  pos.update(*this, share, s.pos);
128  violations.update(*this, share, s.violations);
129  }
131  virtual Space*
132  copy(bool share) {
133  return new Photo(share,*this);
134  }
136  virtual void
137  print(std::ostream& os) const {
138  os << "\tpos[] = " << pos << std::endl
139  << "\tviolations: " << violations << std::endl;
140  }
142  virtual IntVar cost(void) const {
143  return violations;
144  }
145 };
146 
150 int
151 main(int argc, char* argv[]) {
152  SizeOptions opt("Photo");
153  opt.solutions(0);
154  opt.size(1);
155  opt.iterations(10);
156  opt.icl(ICL_BND);
158  opt.branching(Photo::BRANCH_NONE, "none");
159  opt.branching(Photo::BRANCH_DEGREE, "degree");
160  opt.parse(argc,argv);
161  IntMinimizeScript::run<Photo,BAB,SizeOptions>(opt);
162  return 0;
163 }
164 
165 
166 // STATISTICS: example-any
167 
void size(unsigned int s)
Set default size.
Definition: options.hpp:485
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:579
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 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:437
virtual Space * copy(bool share)
Copy during cloning.
Definition: photo.cpp:132
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
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:633
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:403
int main(int argc, char *argv[])
Main-function.
Definition: photo.cpp:151
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:137
void branching(int v)
Set default branching value.
Definition: options.hpp:203
Choose variables from left to right.
Definition: photo.cpp:92
IntVarArray pos
Person&#39;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:142
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
Choose variable with largest degree.
Definition: photo.cpp:93
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:261
Photo(bool share, Photo &s)
Constructor for cloning s.
Definition: photo.cpp:125
Specifications for the photo example.
Definition: photo.cpp:45
Bounds propagation or consistency.
Definition: int.hh:939
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
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194