main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Jan 28 2020 00:00:00 for Gecode by
doxygen
1.8.17
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
* Last modified:
10
* $Date: 2015-09-11 16:29:45 +0200 (Fri, 11 Sep 2015) $ by $Author: schulte $
11
* $Revision: 14672 $
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
#include <iomanip>
43
44
using namespace
Gecode
;
45
66
class
GolombRuler
:
public
IntMinimizeScript
{
67
protected
:
69
IntVarArray
m
;
70
public
:
72
GolombRuler
(
const
SizeOptions
&
opt
)
73
:
IntMinimizeScript
(
opt
),
74
m(*this,
opt
.
size
(),0,
75
(
opt
.
size
() < 31) ? (1 << (
opt
.
size
()-1))-1 : Int::Limits::
max
) {
76
77
// Assume first mark to be zero
78
rel
(*
this
, m[0],
IRT_EQ
, 0);
79
80
// Order marks
81
rel
(*
this
, m,
IRT_LE
);
82
83
// Number of marks and differences
84
const
int
n
= m.
size
();
85
const
int
n_d = (
n
*
n
-
n
)/2;
86
87
// Array of differences
88
IntVarArgs
d
(n_d);
89
90
// Setup difference constraints
91
for
(
int
k=0,
i
=0;
i
<
n
-1;
i
++)
92
for
(
int
j=
i
+1; j<
n
; j++, k++)
93
// d[k] is m[j]-m[i] and must be at least sum of first j-i integers
94
rel
(*
this
,
d
[k] =
expr
(*
this
, m[j]-m[
i
]),
95
IRT_GQ
, (j-
i
)*(j-
i
+1)/2);
96
97
distinct
(*
this
,
d
,
opt
.ipl());
98
99
// Symmetry breaking
100
if
(
n
> 2)
101
rel
(*
this
,
d
[0],
IRT_LE
,
d
[n_d-1]);
102
103
branch
(*
this
, m,
INT_VAR_NONE
(),
INT_VAL_MIN
());
104
}
105
107
virtual
IntVar
cost
(
void
)
const
{
108
return
m[m.
size
()-1];
109
}
110
112
virtual
void
113
print
(std::ostream& os)
const
{
114
os <<
"\tm["
<< m.
size
() <<
"] = "
<< m << std::endl;
115
}
116
118
GolombRuler
(
bool
share,
GolombRuler
& s)
119
:
IntMinimizeScript
(share,s) {
120
m.
update
(*
this
, share, s.
m
);
121
}
123
virtual
Space
*
124
copy
(
bool
share) {
125
return
new
GolombRuler
(share,*
this
);
126
}
127
};
128
132
int
133
main
(
int
argc,
char
* argv[]) {
134
SizeOptions
opt
(
"GolombRuler"
);
135
opt
.
solutions
(0);
136
opt
.size(10);
137
opt
.
ipl
(
IPL_BND
);
138
opt
.
parse
(argc,argv);
139
if
(
opt
.size() > 0)
140
IntMinimizeScript::run<GolombRuler,BAB,SizeOptions>(
opt
);
141
return
0;
142
}
143
144
// STATISTICS: example-any
145
GolombRuler::main
int main(int argc, char *argv[])
Main-function.
Definition:
golomb-ruler.cpp:133
GolombRuler::print
virtual void print(std::ostream &os) const
Print solution.
Definition:
golomb-ruler.cpp:113
minimodel.hh
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition:
arithmetic.cpp:53
Gecode::IRT_GQ
@ IRT_GQ
Greater or equal ( )
Definition:
int.hh:911
Gecode::INT_VAR_NONE
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition:
var.hpp:100
Gecode::IntVarArgs
Passing integer variables.
Definition:
int.hh:639
int.hh
Gecode::expr
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition:
bool-expr.cpp:632
Gecode::Iter::Ranges::size
unsigned int size(I &i)
Size of all ranges of range iterator i.
Definition:
ranges-operations.hpp:78
Gecode::IRT_LE
@ IRT_LE
Less ( )
Definition:
int.hh:910
GolombRuler::cost
virtual IntVar cost(void) const
Return cost.
Definition:
golomb-ruler.cpp:107
Gecode::VarArray::update
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition:
array.hpp:1072
Test::Int::Basic::i
Gecode::IntArgs i(4, 1, 2, 3, 4)
Gecode::Options::ipl
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition:
options.hpp:220
GolombRuler::GolombRuler
GolombRuler(const SizeOptions &opt)
Actual model.
Definition:
golomb-ruler.cpp:72
Gecode::Space
Computation spaces.
Definition:
core.hpp:1748
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:43
Gecode::INT_VAL_MIN
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition:
val.hpp:59
Gecode::IntVarArray
Integer variable array.
Definition:
int.hh:744
driver.hh
Gecode::VarArray::size
int size(void) const
Return size of array (number of elements)
Definition:
array.hpp:985
Gecode
Gecode toplevel namespace
Test::opt
Options opt
The options.
Definition:
test.cpp:101
Gecode::Driver::ScriptBase
Parametric base-class for scripts.
Definition:
driver.hh:703
Gecode::BaseOptions::parse
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition:
options.cpp:510
Gecode::IntVar
Integer variables.
Definition:
int.hh:353
Gecode::IPL_BND
@ IPL_BND
Bounds propagation.
Definition:
int.hh:959
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition:
rel.cpp:47
GolombRuler::GolombRuler
GolombRuler(bool share, GolombRuler &s)
Constructor for cloning s.
Definition:
golomb-ruler.cpp:118
Test::Int::Distinct::d
Gecode::IntSet d(v, 7)
GolombRuler::m
IntVarArray m
Array for ruler marks.
Definition:
golomb-ruler.cpp:69
GolombRuler
Example: Finding optimal Golomb rulers
Definition:
golomb-ruler.cpp:66
Gecode::IRT_EQ
@ IRT_EQ
Equality ( )
Definition:
int.hh:907
Gecode::distinct
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition:
distinct.cpp:50
Gecode::Options::solutions
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition:
options.hpp:287
GolombRuler::copy
virtual Space * copy(bool share)
Copy during cloning.
Definition:
golomb-ruler.cpp:124
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:238
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition:
driver.hh:649