Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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
38using namespace Gecode;
39
50class MagicSquare : public Script {
51private:
53 const int n;
55 IntVarArray x;
56
57public:
59 enum {
62 };
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:
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 }
118
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
138int
139main(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);
148 return 0;
149}
150
151// STATISTICS: example-any
152
static void run(const Options &opt, Script *s=NULL)
Passing integer variables.
Definition int.hh:662
Matrix-interface for arrays.
Slice< A > col(int c) const
Access column c.
Definition matrix.hpp:183
Slice< A > row(int r) const
Access row r.
Definition matrix.hpp:177
Options for scripts with additional size parameter
Definition driver.hh:675
Computation spaces.
Definition core.hpp:1744
Example: Magic squares
@ BRANCH_AFC_SIZE
Branch by size over AFC.
@ BRANCH_SIZE
Branch by size.
MagicSquare(MagicSquare &s)
Constructor for cloning s.
MagicSquare(const SizeOptions &opt)
Post constraints.
virtual void print(std::ostream &os) const
Print solution.
virtual Space * copy(void)
Copy during cloning.
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
@ IRT_EQ
Equality ( )
Definition int.hh:941
@ IRT_GR
Greater ( )
Definition int.hh:946
Gecode toplevel namespace
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition val.hpp:75
IntVarBranch INT_VAR_AFC_SIZE_MAX(double d=1.0, BranchTbl tbl=nullptr)
Select variable with largest accumulated failure count divided by domain size with decay factor d.
Definition var.hpp:236
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl=nullptr)
Select variable with smallest domain size.
Definition var.hpp:206
Options opt
The options.
Definition test.cpp:97
int main(int argc, char *argv[])
Definition test.cpp:208