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
magic-sequence.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
* Guido Tack <tack@gecode.org>
6
*
7
* Copyright:
8
* Christian Schulte, 2001
9
* Guido Tack, 2006
10
*
11
* Last modified:
12
* $Date: 2015-09-11 16:29:45 +0200 (Fri, 11 Sep 2015) $ by $Author: schulte $
13
* $Revision: 14672 $
14
*
15
* This file is part of Gecode, the generic constraint
16
* development environment:
17
* http://www.gecode.org
18
*
19
* Permission is hereby granted, free of charge, to any person obtaining
20
* a copy of this software and associated documentation files (the
21
* "Software"), to deal in the Software without restriction, including
22
* without limitation the rights to use, copy, modify, merge, publish,
23
* distribute, sublicense, and/or sell copies of the Software, and to
24
* permit persons to whom the Software is furnished to do so, subject to
25
* the following conditions:
26
*
27
* The above copyright notice and this permission notice shall be
28
* included in all copies or substantial portions of the Software.
29
*
30
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37
*
38
*/
39
40
#include <
gecode/driver.hh
>
41
#include <
gecode/int.hh
>
42
#include <
gecode/minimodel.hh
>
43
44
using namespace
Gecode
;
45
63
class
MagicSequence
:
public
Script
{
64
private
:
66
const
int
n
;
68
IntVarArray
s;
69
public
:
71
enum
{
72
PROP_COUNT
,
73
PROP_GCC
74
};
76
MagicSequence
(
const
SizeOptions
&
opt
)
77
:
Script
(
opt
),
n
(
opt
.
size
()), s(*this,
n
,0,
n
-1) {
78
switch
(
opt
.propagation()) {
79
case
PROP_COUNT:
80
for
(
int
i
=
n
;
i
--; )
81
count
(*
this
, s,
i
,
IRT_EQ
, s[
i
]);
82
linear
(*
this
, s,
IRT_EQ
,
n
);
83
break
;
84
case
PROP_GCC:
85
count
(*
this
, s, s,
opt
.ipl());
86
break
;
87
}
88
linear
(*
this
,
IntArgs::create
(
n
,-1,1), s,
IRT_EQ
, 0);
89
branch
(*
this
, s,
INT_VAR_NONE
(),
INT_VAL_MAX
());
90
}
91
93
MagicSequence
(
bool
share,
MagicSequence
& e) :
Script
(share,e),
n
(e.
n
) {
94
s.
update
(*
this
, share, e.s);
95
}
97
virtual
Space
*
98
copy
(
bool
share) {
99
return
new
MagicSequence
(share,*
this
);
100
}
102
virtual
103
void
print
(std::ostream& os)
const
{
104
os <<
"\t"
;
105
for
(
int
i
= 0;
i
<
n
;
i
++) {
106
os << s[
i
] <<
", "
;
107
if
((
i
+1) % 20 == 0)
108
os << std::endl <<
"\t"
;
109
}
110
os << std::endl;
111
}
112
113
};
114
118
int
119
main
(
int
argc,
char
* argv[]) {
120
SizeOptions
opt
(
"MagicSequence"
);
121
opt
.
solutions
(0);
122
opt
.
iterations
(4);
123
opt
.size(500);
124
opt
.
propagation
(
MagicSequence::PROP_COUNT
);
125
opt
.
propagation
(
MagicSequence::PROP_COUNT
,
"count"
);
126
opt
.
propagation
(
MagicSequence::PROP_GCC
,
"gcc"
);
127
opt
.
parse
(argc,argv);
128
Script::run<MagicSequence,DFS,SizeOptions>(
opt
);
129
return
0;
130
}
131
132
// STATISTICS: example-any
133
Gecode::Options::propagation
void propagation(int v)
Set default propagation value.
Definition:
options.hpp:207
minimodel.hh
Gecode::IntArgs::create
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition:
array.hpp:72
Gecode::INT_VAR_NONE
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition:
var.hpp:100
int.hh
Gecode::Iter::Ranges::size
unsigned int size(I &i)
Size of all ranges of range iterator i.
Definition:
ranges-operations.hpp:78
MagicSequence
Example: Magic sequence
Definition:
magic-sequence.cpp:63
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::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::Options::iterations
void iterations(unsigned int i)
Set default number of iterations.
Definition:
options.hpp:465
MagicSequence::print
virtual void print(std::ostream &os) const
Print sequence.
Definition:
magic-sequence.cpp:103
MagicSequence::MagicSequence
MagicSequence(const SizeOptions &opt)
The actual model.
Definition:
magic-sequence.cpp:76
Gecode::IntVarArray
Integer variable array.
Definition:
int.hh:744
driver.hh
Gecode
Gecode toplevel namespace
MagicSequence::MagicSequence
MagicSequence(bool share, MagicSequence &e)
Constructor for cloning e.
Definition:
magic-sequence.cpp:93
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
MagicSequence::copy
virtual Space * copy(bool share)
Copy during cloning.
Definition:
magic-sequence.cpp:98
MagicSequence::PROP_COUNT
@ PROP_COUNT
Use count constraints.
Definition:
magic-sequence.cpp:72
Gecode::linear
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition:
linear.cpp:45
Gecode::count
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel)
Post propagator for .
Definition:
count.cpp:44
MagicSequence::PROP_GCC
@ PROP_GCC
Use single global cardinality constraint.
Definition:
magic-sequence.cpp:73
Gecode::IRT_EQ
@ IRT_EQ
Equality ( )
Definition:
int.hh:907
Gecode::Options::solutions
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition:
options.hpp:287
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:238
MagicSequence::main
int main(int argc, char *argv[])
Main-function.
Definition:
magic-sequence.cpp:119
Gecode::INT_VAL_MAX
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition:
val.hpp:69
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition:
driver.hh:649