main page
modules
namespaces
classes
files
Gecode home
Generated on Mon Jul 27 2020 00:00:00 for Gecode by
doxygen
1.8.18
examples
golomb-ruler.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
* This file is part of Gecode, the generic constraint
10
* development environment:
11
* http://www.gecode.org
12
*
13
* Permission is hereby granted, free of charge, to any person obtaining
14
* a copy of this software and associated documentation files (the
15
* "Software"), to deal in the Software without restriction, including
16
* without limitation the rights to use, copy, modify, merge, publish,
17
* distribute, sublicense, and/or sell copies of the Software, and to
18
* permit persons to whom the Software is furnished to do so, subject to
19
* the following conditions:
20
*
21
* The above copyright notice and this permission notice shall be
22
* included in all copies or substantial portions of the Software.
23
*
24
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
*
32
*/
33
34
#include <
gecode/driver.hh
>
35
#include <
gecode/int.hh
>
36
#include <
gecode/minimodel.hh
>
37
38
#include <iomanip>
39
40
using namespace
Gecode
;
41
62
class
GolombRuler
:
public
IntMinimizeScript
{
63
protected
:
65
IntVarArray
m
;
66
public
:
68
GolombRuler
(
const
SizeOptions
&
opt
)
69
:
IntMinimizeScript
(
opt
),
70
m(*this,
opt
.
size
(),0,
71
(
opt
.
size
() < 31) ? (1 << (
opt
.
size
()-1))-1 : Int::Limits::
max
) {
72
73
// Assume first mark to be zero
74
rel
(*
this
, m[0],
IRT_EQ
, 0);
75
76
// Order marks
77
rel
(*
this
, m,
IRT_LE
);
78
79
// Number of marks and differences
80
const
int
n
= m.
size
();
81
const
int
n_d = (
n
*
n
-
n
)/2;
82
83
// Array of differences
84
IntVarArgs
d
(n_d);
85
86
// Setup difference constraints
87
for
(
int
k=0,
i
=0;
i
<
n
-1;
i
++)
88
for
(
int
j=
i
+1; j<
n
; j++, k++)
89
// d[k] is m[j]-m[i] and must be at least sum of first j-i integers
90
rel
(*
this
,
d
[k] =
expr
(*
this
, m[j]-m[
i
]),
91
IRT_GQ
, (j-
i
)*(j-
i
+1)/2);
92
93
distinct
(*
this
,
d
,
opt
.ipl());
94
95
// Symmetry breaking
96
if
(
n
> 2)
97
rel
(*
this
,
d
[0],
IRT_LE
,
d
[n_d-1]);
98
99
branch
(*
this
, m,
INT_VAR_NONE
(),
INT_VAL_MIN
());
100
}
101
103
virtual
IntVar
cost
(
void
)
const
{
104
return
m[m.
size
()-1];
105
}
106
108
virtual
void
109
print
(std::ostream& os)
const
{
110
os <<
"\tm["
<< m.
size
() <<
"] = "
<< m << std::endl;
111
}
112
114
GolombRuler
(
GolombRuler
& s)
115
:
IntMinimizeScript
(s) {
116
m.
update
(*
this
, s.
m
);
117
}
119
virtual
Space
*
120
copy
(
void
) {
121
return
new
GolombRuler
(*
this
);
122
}
123
};
124
128
int
129
main
(
int
argc,
char
* argv[]) {
130
SizeOptions
opt
(
"GolombRuler"
);
131
opt
.
solutions
(0);
132
opt
.size(10);
133
opt
.
ipl
(
IPL_BND
);
134
opt
.
parse
(argc,argv);
135
if
(
opt
.size() > 0)
136
IntMinimizeScript::run<GolombRuler,BAB,SizeOptions>(
opt
);
137
return
0;
138
}
139
140
// STATISTICS: example-any
141
GolombRuler::main
int main(int argc, char *argv[])
Main-function.
Definition:
golomb-ruler.cpp:129
GolombRuler::print
virtual void print(std::ostream &os) const
Print solution.
Definition:
golomb-ruler.cpp:109
minimodel.hh
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition:
arithmetic.cpp:49
Gecode::IRT_GQ
@ IRT_GQ
Greater or equal ( )
Definition:
int.hh:930
Gecode::INT_VAR_NONE
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition:
var.hpp:96
Gecode::IntVarArgs
Passing integer variables.
Definition:
int.hh:656
GolombRuler::copy
virtual Space * copy(void)
Copy during cloning.
Definition:
golomb-ruler.cpp:120
int.hh
Gecode::Iter::Ranges::size
unsigned int size(I &i)
Size of all ranges of range iterator i.
Definition:
ranges-operations.hpp:74
Gecode::IRT_LE
@ IRT_LE
Less ( )
Definition:
int.hh:929
GolombRuler::cost
virtual IntVar cost(void) const
Return cost.
Definition:
golomb-ruler.cpp:103
Gecode::Options::ipl
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition:
options.hpp:216
GolombRuler::GolombRuler
GolombRuler(const SizeOptions &opt)
Actual model.
Definition:
golomb-ruler.cpp:68
Gecode::Space
Computation spaces.
Definition:
core.hpp:1742
Gecode::branch
void 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:39
Gecode::INT_VAL_MIN
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition:
val.hpp:55
Gecode::IntVarArray
Integer variable array.
Definition:
int.hh:763
driver.hh
Gecode::VarArray::size
int size(void) const
Return size of array (number of elements)
Definition:
array.hpp:926
Gecode
Gecode toplevel namespace
Test::opt
Options opt
The options.
Definition:
test.cpp:97
Gecode::expr
BoolVar expr(Home home, const BoolExpr &e, const IntPropLevels &ipls)
Post Boolean expression and return its value.
Definition:
bool-expr.cpp:629
Gecode::Driver::ScriptBase
Parametric base-class for scripts.
Definition:
driver.hh:729
Gecode::BaseOptions::parse
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition:
options.cpp:548
Gecode::IntVar
Integer variables.
Definition:
int.hh:371
Gecode::IPL_BND
@ IPL_BND
Bounds propagation.
Definition:
int.hh:978
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition:
rel.cpp:43
Test::Int::Distinct::d
Gecode::IntSet d(v, 7)
GolombRuler::m
IntVarArray m
Array for ruler marks.
Definition:
golomb-ruler.cpp:65
GolombRuler
Example: Finding optimal Golomb rulers
Definition:
golomb-ruler.cpp:62
Gecode::IRT_EQ
@ IRT_EQ
Equality ( )
Definition:
int.hh:926
Gecode::distinct
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition:
distinct.cpp:46
Gecode::Options::solutions
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition:
options.hpp:283
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:234
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})
GolombRuler::GolombRuler
GolombRuler(GolombRuler &s)
Constructor for cloning s.
Definition:
golomb-ruler.cpp:114
Gecode::VarArray::update
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition:
array.hpp:1013
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition:
driver.hh:675