Skip to content

Commit 2ef7e18

Browse files
author
Christian Schulte
committed
Support for randomization
git-svn-id: file:///Users/tack/GecodeGitMigration/gecode-svn-mirror/gecode/trunk@14400 e85b7adc-8362-4630-8c63-7469d557c915
1 parent 4f0ff1d commit 2ef7e18

File tree

1 file changed

+80
-15
lines changed

1 file changed

+80
-15
lines changed

examples/qcp.cpp

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,37 @@
4242

4343
using namespace Gecode;
4444

45-
// Instance data
45+
/// Instance data
4646
namespace {
4747

48-
// Instances
48+
49+
/// %Options for %QCP problems
50+
class QCPOptions : public InstanceOptions {
51+
protected:
52+
/// Tie-breaking factor
53+
Driver::DoubleOption _tbf;
54+
public:
55+
/// Initialize options for example with name \a s
56+
QCPOptions(const char* s)
57+
: InstanceOptions(s),
58+
_tbf("-tbf", "tie-breaking factor",0.0) {
59+
// Add options
60+
add(_tbf);
61+
}
62+
/// Return tie-breaking limit
63+
double tbf(void) const {
64+
return _tbf.value();
65+
}
66+
/// Set tie-breaking limit
67+
void tbf(double d) {
68+
_tbf.value(d);
69+
}
70+
};
71+
72+
73+
/// Instances
4974
extern const int* qcp[];
50-
// Instance names
75+
/// Instance names
5176
extern const char* name[];
5277

5378
/// A wrapper class for instance data
@@ -106,6 +131,8 @@ class QCP : public Script {
106131
const Spec spec;
107132
/// Field elements e
108133
IntVarArray e;
134+
/// Tie-breaking factor
135+
double tbf;
109136
public:
110137
/// Propagation to use for model
111138
enum {
@@ -118,9 +145,10 @@ class QCP : public Script {
118145
BRANCH_AFC_SIZE, ///< Use largest AFC divided by domain size
119146
};
120147
/// Actual model
121-
QCP(const InstanceOptions& opt)
148+
QCP(const QCPOptions& opt)
122149
: spec(opt.instance()),
123-
e(*this, spec.size() * spec.size(), 0, spec.size()-1) {
150+
e(*this, spec.size() * spec.size(), 0, spec.size()-1),
151+
tbf(opt.tbf()) {
124152
// Problem size
125153
int n = spec.size();
126154
// Matrix for elements
@@ -150,18 +178,55 @@ class QCP : public Script {
150178
break;
151179
}
152180

153-
switch (opt.branching()) {
154-
case BRANCH_SIZE:
155-
branch(*this, e, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
156-
break;
157-
case BRANCH_AFC_SIZE:
158-
branch(*this, e, INT_VAR_AFC_SIZE_MAX(), INT_VAL_MIN());
159-
break;
181+
if (tbf > 0.0) {
182+
Rnd r(opt.seed());
183+
switch (opt.branching()) {
184+
case BRANCH_SIZE:
185+
branch(*this, e, tiebreak(INT_VAR_SIZE_MIN(&tbl_min),
186+
INT_VAR_RND(r)),
187+
INT_VAL_MIN());
188+
break;
189+
case BRANCH_AFC_SIZE:
190+
{
191+
IntAFC afc(*this, e, opt.decay());
192+
branch(*this, e, tiebreak(INT_VAR_AFC_SIZE_MAX(afc, &tbl_max),
193+
INT_VAR_RND(r)),
194+
INT_VAL_MIN());
195+
break;
196+
}
197+
}
198+
} else {
199+
switch (opt.branching()) {
200+
case BRANCH_SIZE:
201+
branch(*this, e, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
202+
break;
203+
case BRANCH_AFC_SIZE:
204+
branch(*this, e, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
205+
break;
206+
}
160207
}
161208
}
209+
/// Tie-breaking minimum limit function
210+
double _tbl_min(double w, double b) const {
211+
assert(w >= b);
212+
return b + (w - b) * tbf;
213+
}
214+
/// Tie-breaking minimum limit function
215+
static double tbl_min(const Space& home, double w, double b) {
216+
return static_cast<const QCP&>(home)._tbl_min(w,b);
217+
}
218+
/// Tie-breaking maximum limit function
219+
double _tbl_max(double w, double b) const {
220+
assert(b >= w);
221+
return b - (b - w) * tbf;
222+
}
223+
/// Tie-breaking maximum limit function
224+
static double tbl_max(const Space& home, double w, double b) {
225+
return static_cast<const QCP&>(home)._tbl_max(w,b);
226+
}
162227
/// Constructor for cloning \a s
163228
QCP(bool share, QCP& s)
164-
: Script(share,s), spec(s.spec) {
229+
: Script(share,s), spec(s.spec), tbf(s.tbf) {
165230
e.update(*this, share, s.e);
166231
}
167232
/// Copy during cloning
@@ -192,7 +257,7 @@ class QCP : public Script {
192257
*/
193258
int
194259
main(int argc, char* argv[]) {
195-
InstanceOptions opt("QCP");
260+
QCPOptions opt("QCP");
196261

197262
opt.branching(QCP::BRANCH_AFC_SIZE);
198263
opt.branching(QCP::BRANCH_SIZE, "size");
@@ -211,7 +276,7 @@ main(int argc, char* argv[]) {
211276
std::cerr << "Error: unkown instance" << std::endl;
212277
return 1;
213278
}
214-
Script::run<QCP,DFS,InstanceOptions>(opt);
279+
Script::run<QCP,DFS,QCPOptions>(opt);
215280
return 0;
216281
}
217282

0 commit comments

Comments
 (0)