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-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
* 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
using namespace
Gecode
;
43
54
class
MagicSquare
:
public
Script
{
55
private
:
57
const
int
n
;
59
IntVarArray
x
;
60
61
public
:
63
enum
{
64
BRANCH_SIZE
,
65
BRANCH_AFC_SIZE
66
};
68
MagicSquare
(
const
SizeOptions
&
opt
)
69
:
Script
(
opt
),
n
(
opt
.
size
()),
x
(*this,
n
*
n
,1,
n
*
n
) {
70
// Number of fields on square
71
const
int
nn =
n
*
n
;
72
73
// Sum of all a row, column, or diagonal
74
const
int
s = nn*(nn+1) / (2*
n
);
75
76
// Matrix-wrapper for the square
77
Matrix<IntVarArray>
m(
x
,
n
,
n
);
78
79
for
(
int
i
=
n
;
i
--; ) {
80
linear
(*
this
, m.
row
(
i
),
IRT_EQ
, s,
opt
.ipl());
81
linear
(*
this
, m.
col
(
i
),
IRT_EQ
, s,
opt
.ipl());
82
}
83
// Both diagonals must have sum s
84
{
85
IntVarArgs
d1y(
n
);
86
IntVarArgs
d2y(
n
);
87
for
(
int
i
=
n
;
i
--; ) {
88
d1y[
i
] = m(
i
,
i
);
89
d2y[
i
] = m(
n
-
i
-1,
i
);
90
}
91
linear
(*
this
, d1y,
IRT_EQ
, s,
opt
.ipl());
92
linear
(*
this
, d2y,
IRT_EQ
, s,
opt
.ipl());
93
}
94
95
// All fields must be distinct
96
distinct
(*
this
,
x
,
opt
.ipl());
97
98
// Break some (few) symmetries
99
rel
(*
this
, m(0,0),
IRT_GR
, m(0,
n
-1));
100
rel
(*
this
, m(0,0),
IRT_GR
, m(
n
-1,0));
101
102
switch
(
opt
.branching()) {
103
case
BRANCH_SIZE:
104
branch
(*
this
,
x
,
INT_VAR_SIZE_MIN
(),
INT_VAL_SPLIT_MIN
());
105
break
;
106
case
BRANCH_AFC_SIZE:
107
branch
(*
this
,
x
,
INT_VAR_AFC_SIZE_MAX
(
opt
.decay()),
INT_VAL_SPLIT_MIN
());
108
break
;
109
}
110
}
111
113
MagicSquare
(
bool
share,
MagicSquare
& s) :
Script
(share,s),
n
(s.
n
) {
114
x
.
update
(*
this
, share, s.x);
115
}
116
118
virtual
Space
*
119
copy
(
bool
share) {
120
return
new
MagicSquare
(share,*
this
);
121
}
123
virtual
void
124
print
(std::ostream& os)
const
{
125
// Matrix-wrapper for the square
126
Matrix<IntVarArray>
m(
x
,
n
,
n
);
127
for
(
int
i
= 0;
i
<
n
;
i
++) {
128
os <<
"\t"
;
129
for
(
int
j = 0; j<
n
; j++) {
130
os.width(2);
131
os << m(
i
,j) <<
" "
;
132
}
133
os << std::endl;
134
}
135
}
136
137
};
138
142
int
143
main
(
int
argc,
char
* argv[]) {
144
SizeOptions
opt
(
"MagicSquare"
);
145
opt
.
iterations
(1);
146
opt
.size(7);
147
opt
.
branching
(
MagicSquare::BRANCH_SIZE
);
148
opt
.
branching
(
MagicSquare::BRANCH_SIZE
,
"size"
);
149
opt
.
branching
(
MagicSquare::BRANCH_AFC_SIZE
,
"afc-size"
);
150
opt
.
parse
(argc,argv);
151
Script::run<MagicSquare,DFS,SizeOptions>(
opt
);
152
return
0;
153
}
154
155
// STATISTICS: example-any
156
Gecode::x
Post propagator for SetVar x
Definition:
set.hh:784
MagicSquare::MagicSquare
MagicSquare(bool share, MagicSquare &s)
Constructor for cloning s.
Definition:
magic-square.cpp:113
MagicSquare::print
virtual void print(std::ostream &os) const
Print solution.
Definition:
magic-square.cpp:124
minimodel.hh
Gecode::IntVarArgs
Passing integer variables.
Definition:
int.hh:639
int.hh
Gecode::Iter::Ranges::size
unsigned int size(I &i)
Size of all ranges of range iterator i.
Definition:
ranges-operations.hpp:78
Gecode::Matrix::row
Slice< A > row(int r) const
Access row r.
Definition:
matrix.hpp:181
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
Gecode::VarImpVar::update
void update(Space &home, bool share, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition:
var.hpp:128
Gecode::IntVarArray
Integer variable array.
Definition:
int.hh:744
driver.hh
Gecode
Gecode toplevel namespace
Test::opt
Options opt
The options.
Definition:
test.cpp:101
MagicSquare::BRANCH_SIZE
@ BRANCH_SIZE
Branch by size.
Definition:
magic-square.cpp:64
Gecode::Driver::ScriptBase
Parametric base-class for scripts.
Definition:
driver.hh:703
Gecode::Options::branching
void branching(int v)
Set default branching value.
Definition:
options.hpp:229
Gecode::BaseOptions::parse
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition:
options.cpp:510
Gecode::Matrix
Matrix-interface for arrays.
Definition:
minimodel.hh:1923
Gecode::linear
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition:
linear.cpp:45
Gecode::INT_VAR_SIZE_MIN
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition:
var.hpp:210
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition:
rel.cpp:47
Gecode::Matrix::col
Slice< A > col(int c) const
Access column c.
Definition:
matrix.hpp:187
MagicSquare::copy
virtual Space * copy(bool share)
Copy during cloning.
Definition:
magic-square.cpp:119
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::INT_VAL_SPLIT_MIN
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition:
val.hpp:79
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:240
MagicSquare::main
int main(int argc, char *argv[])
Main-function.
Definition:
magic-square.cpp:143
MagicSquare::MagicSquare
MagicSquare(const SizeOptions &opt)
Post constraints.
Definition:
magic-square.cpp:68
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:238
Gecode::IRT_GR
@ IRT_GR
Greater ( )
Definition:
int.hh:912
MagicSquare::BRANCH_AFC_SIZE
@ BRANCH_AFC_SIZE
Branch by size over AFC.
Definition:
magic-square.cpp:65
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition:
driver.hh:649
MagicSquare
Example: Magic squares
Definition:
magic-square.cpp:54