Skip to content

Commit 2ec0b54

Browse files
author
Christian Schulte
committed
Cleaned up
git-svn-id: file:///Volumes/GecodeGitMigration/gecode-svn-mirror/gecode/trunk@15219 e85b7adc-8362-4630-8c63-7469d557c915
1 parent c7962c6 commit 2ec0b54

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

examples/photo.cpp

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,49 @@
4141

4242
using namespace Gecode;
4343

44-
45-
/// Specifications for the photo example
44+
/// Specifications for photo example
4645
class PhotoSpec {
46+
protected:
47+
/// Array of data
48+
const int* data;
4749
public:
48-
const int n_names; ///< Number of people on picture
49-
const int n_prefs; ///< Number of preferences
50-
const int* prefs; ///< Array of preferences
51-
PhotoSpec(const int n_n, const int n_p, const int* p)
52-
: n_names(n_n), n_prefs(n_p), prefs(p) {}
50+
/// Initialize
51+
PhotoSpec(const int* d) : data(d) {}
52+
/// Return number of people
53+
int people(void) const {
54+
return data[0];
55+
}
56+
/// Return number of preferences
57+
int preferences(void) const {
58+
return data[1];
59+
}
60+
/// Access preference \a p with position \a i
61+
int preference(int p, int i) const {
62+
return data[2+2*p+i];
63+
}
5364
};
5465

55-
/// Preferences for small example
56-
const int s_prefs[] = {
66+
/// Small Photo example
67+
const int small[] = {
68+
/// Number of people on picture
69+
5,
70+
/// Number of preferences
71+
8,
72+
/// Array of preferences
5773
0,2, 1,4, 2,3, 2,4, 3,0, 4,3, 4,0, 4,1
5874
};
59-
/// Small Photo example
60-
const PhotoSpec p_small(5, 8, s_prefs);
6175

62-
/// Preferences for large example
63-
const int l_prefs[] = {
76+
/// Large Photo example
77+
const int large[] = {
78+
/// Number of people on picture
79+
9,
80+
/// Number of preferences
81+
17,
82+
/// Array of preferences
6483
0,2, 0,4, 0,7, 1,4, 1,8, 2,3, 2,4, 3,0, 3,4,
6584
4,5, 4,0, 5,0, 5,8, 6,2, 6,7, 7,8, 7,6
6685
};
67-
/// Large Photo example
68-
const PhotoSpec p_large(9,17, l_prefs);
86+
6987

7088
/**
7189
* \brief %Example: Placing people on a photo
@@ -93,22 +111,23 @@ class Photo : public IntMinimizeScript {
93111
public:
94112
/// Branching to use for model
95113
enum {
96-
BRANCH_NONE, ///< Choose variables from left to right
97-
BRANCH_DEGREE ///< Choose variable with largest degree
114+
BRANCH_NONE, ///< Choose variables from left to right
115+
BRANCH_DEGREE, ///< Choose variable with largest degree
116+
BRANCH_AFC_SIZE ///< Choose variable with largest afc over size
98117
};
99118
/// Actual model
100119
Photo(const SizeOptions& opt) :
101120
IntMinimizeScript(opt),
102-
spec(opt.size() == 0 ? p_small : p_large),
103-
pos(*this,spec.n_names, 0, spec.n_names-1),
104-
violations(*this,0,spec.n_prefs),
121+
spec(opt.size() == 0 ? small : large),
122+
pos(*this,spec.people(), 0, spec.people()-1),
123+
violations(*this,0,spec.preferences()),
105124
rnd(opt.seed()), p(opt.relax())
106125
{
107126
// Map preferences to violation
108-
BoolVarArgs viol(spec.n_prefs);
109-
for (int i=0; i<spec.n_prefs; i++) {
110-
int pa = spec.prefs[2*i+0];
111-
int pb = spec.prefs[2*i+1];
127+
BoolVarArgs viol(spec.preferences());
128+
for (int i=0; i<spec.preferences(); i++) {
129+
int pa = spec.preference(i,0);
130+
int pb = spec.preference(i,1);
112131
viol[i] = expr(*this, abs(pos[pa]-pos[pb]) > 1);
113132
}
114133
rel(*this, violations == sum(viol));
@@ -118,11 +137,17 @@ class Photo : public IntMinimizeScript {
118137
// Break some symmetries
119138
rel(*this, pos[0] < pos[1]);
120139

121-
if (opt.branching() == BRANCH_NONE) {
140+
switch (opt.branching()) {
141+
case BRANCH_NONE:
122142
branch(*this, pos, INT_VAR_NONE(), INT_VAL_MIN());
123-
} else {
143+
break;
144+
case BRANCH_DEGREE:
124145
branch(*this, pos, tiebreak(INT_VAR_DEGREE_MAX(),INT_VAR_SIZE_MIN()),
125146
INT_VAL_MIN());
147+
break;
148+
case BRANCH_AFC_SIZE:
149+
branch(*this, pos, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
150+
break;
126151
}
127152
}
128153
/// Slave function for restarts
@@ -171,8 +196,9 @@ main(int argc, char* argv[]) {
171196
opt.ipl(IPL_BND);
172197
opt.relax(0.7);
173198
opt.branching(Photo::BRANCH_DEGREE);
174-
opt.branching(Photo::BRANCH_NONE, "none");
199+
opt.branching(Photo::BRANCH_NONE, "none");
175200
opt.branching(Photo::BRANCH_DEGREE, "degree");
201+
opt.branching(Photo::BRANCH_AFC_SIZE, "afc");
176202
opt.parse(argc,argv);
177203
IntMinimizeScript::run<Photo,BAB,SizeOptions>(opt);
178204
return 0;

0 commit comments

Comments
 (0)