main page
modules
namespaces
classes
files
Gecode home
Generated on Wed Jan 1 2020 10:37:59 for Gecode by
doxygen
1.8.16
examples
magic-square.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
using namespace
Gecode
;
39
50
class
MagicSquare
:
public
Script
{
51
private
:
53
const
int
n
;
55
IntVarArray
x
;
56
57
public
:
59
enum
{
60
BRANCH_SIZE,
61
BRANCH_AFC_SIZE
62
};
64
MagicSquare
(
const
SizeOptions
&
opt
)
65
:
Script
(
opt
),
n
(
opt
.
size
()),
x
(*this,
n
*
n
,1,
n
*
n
) {
66
// Number of fields on square
67
const
int
nn =
n
*
n
;
68
69
// Sum of all a row, column, or diagonal
70
const
int
s = nn*(nn+1) / (2*
n
);
71
72
// Matrix-wrapper for the square
73
Matrix<IntVarArray>
m(
x
,
n
,
n
);
74
75
for
(
int
i
=
n
;
i
--; ) {
76
linear
(*
this
, m.
row
(
i
),
IRT_EQ
, s,
opt
.ipl());
77
linear
(*
this
, m.
col
(
i
),
IRT_EQ
, s,
opt
.ipl());
78
}
79
// Both diagonals must have sum s
80
{
81
IntVarArgs
d1y(
n
);
82
IntVarArgs
d2y(
n
);
83
for
(
int
i
=
n
;
i
--; ) {
84
d1y[
i
] = m(
i
,
i
);
85
d2y[
i
] = m(
n
-
i
-1,
i
);
86
}
87
linear
(*
this
, d1y,
IRT_EQ
, s,
opt
.ipl());
88
linear
(*
this
, d2y,
IRT_EQ
, s,
opt
.ipl());
89
}
90
91
// All fields must be distinct
92
distinct
(*
this
,
x
,
opt
.ipl());
93
94
// Break some (few) symmetries
95
rel
(*
this
, m(0,0),
IRT_GR
, m(0,
n
-1));
96
rel
(*
this
, m(0,0),
IRT_GR
, m(
n
-1,0));
97
98
switch
(
opt
.branching()) {
99
case
BRANCH_SIZE:
100
branch
(*
this
,
x
,
INT_VAR_SIZE_MIN
(),
INT_VAL_SPLIT_MIN
());
101
break
;
102
case
BRANCH_AFC_SIZE:
103
branch
(*
this
,
x
,
INT_VAR_AFC_SIZE_MAX
(
opt
.decay()),
INT_VAL_SPLIT_MIN
());
104
break
;
105
}
106
}
107
109
MagicSquare
(
MagicSquare
& s) :
Script
(s),
n
(s.
n
) {
110
x
.
update
(*
this
, s.x);
111
}
112
114
virtual
Space
*
115
copy
(
void
) {
116
return
new
MagicSquare
(*
this
);
117
}
119
virtual
void
120
print
(std::ostream& os)
const
{
121
// Matrix-wrapper for the square
122
Matrix<IntVarArray>
m(
x
,
n
,
n
);
123
for
(
int
i
= 0;
i
<
n
;
i
++) {
124
os <<
"\t"
;
125
for
(
int
j = 0; j<
n
; j++) {
126
os.width(2);
127
os << m(
i
,j) <<
" "
;
128
}
129
os << std::endl;
130
}
131
}
132
133
};
134
138
int
139
main
(
int
argc,
char
* argv[]) {
140
SizeOptions
opt
(
"MagicSquare"
);
141
opt
.
iterations
(1);
142
opt
.size(7);
143
opt
.
branching
(
MagicSquare::BRANCH_SIZE
);
144
opt
.
branching
(
MagicSquare::BRANCH_SIZE
,
"size"
);
145
opt
.
branching
(
MagicSquare::BRANCH_AFC_SIZE
,
"afc-size"
);
146
opt
.
parse
(argc,argv);
147
Script::run<MagicSquare,DFS,SizeOptions>(
opt
);
148
return
0;
149
}
150
151
// STATISTICS: example-any
152
Gecode::x
Post propagator for SetVar x
Definition:
set.hh:767
MagicSquare::print
virtual void print(std::ostream &os) const
Print solution.
Definition:
magic-square.cpp:120
minimodel.hh
Gecode::IntVarArgs
Passing integer variables.
Definition:
int.hh:656
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::Matrix::row
Slice< A > row(int r) const
Access row r.
Definition:
matrix.hpp:177
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::Options::iterations
void iterations(unsigned int i)
Set default number of iterations.
Definition:
options.hpp:461
Gecode::IntVarArray
Integer variable array.
Definition:
int.hh:763
driver.hh
Gecode
Gecode toplevel namespace
Test::opt
Options opt
The options.
Definition:
test.cpp:97
MagicSquare::MagicSquare
MagicSquare(MagicSquare &s)
Constructor for cloning s.
Definition:
magic-square.cpp:109
Gecode::Driver::ScriptBase
Parametric base-class for scripts.
Definition:
driver.hh:729
Gecode::Options::branching
void branching(int v)
Set default branching value.
Definition:
options.hpp:225
Gecode::BaseOptions::parse
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition:
options.cpp:548
Gecode::Matrix
Matrix-interface for arrays.
Definition:
minimodel.hh:2087
MagicSquare::BRANCH_SIZE
Branch by size.
Definition:
magic-square-partial.cpp:75
Gecode::linear
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition:
linear.cpp:41
Gecode::INT_VAR_SIZE_MIN
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition:
var.hpp:206
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition:
rel.cpp:43
Gecode::Matrix::col
Slice< A > col(int c) const
Access column c.
Definition:
matrix.hpp:183
Gecode::VarImpVar::update
void update(Space &home, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition:
var.hpp:116
Gecode::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::INT_VAL_SPLIT_MIN
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition:
val.hpp:75
Gecode::INT_VAR_AFC_SIZE_MAX
IntVarBranch INT_VAR_AFC_SIZE_MAX(double d, BranchTbl tbl)
Select variable with largest accumulated failure count divided by domain size with decay factor d.
Definition:
var.hpp:236
MagicSquare::MagicSquare
MagicSquare(const SizeOptions &opt)
Post constraints.
Definition:
magic-square.cpp:64
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:234
Gecode::IRT_GR
Greater ( )
Definition:
int.hh:931
main
int main(int argc, char *argv[])
Definition:
test.cpp:208
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})
MagicSquare::BRANCH_AFC_SIZE
Branch by size over AFC.
Definition:
magic-square-partial.cpp:76
MagicSquare::copy
virtual Space * copy(void)
Copy during cloning.
Definition:
magic-square.cpp:115
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition:
driver.hh:675
MagicSquare
Example: Magic squares
Definition:
magic-square-partial.cpp:61