Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
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: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
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:
64  : n(opt.size()), x(*this,n*n,1,n*n) {
65  // Number of fields on square
66  const int nn = n*n;
67 
68  // Sum of all a row, column, or diagonal
69  const int s = nn*(nn+1) / (2*n);
70 
71  // Matrix-wrapper for the square
72  Matrix<IntVarArray> m(x, n, n);
73 
74  for (int i = n; i--; ) {
75  linear(*this, m.row(i), IRT_EQ, s, opt.icl());
76  linear(*this, m.col(i), IRT_EQ, s, opt.icl());
77  }
78  // Both diagonals must have sum s
79  {
80  IntVarArgs d1y(n);
81  IntVarArgs d2y(n);
82  for (int i = n; i--; ) {
83  d1y[i] = m(i,i);
84  d2y[i] = m(n-i-1,i);
85  }
86  linear(*this, d1y, IRT_EQ, s, opt.icl());
87  linear(*this, d2y, IRT_EQ, s, opt.icl());
88  }
89 
90  // All fields must be distinct
91  distinct(*this, x, opt.icl());
92 
93  // Break some (few) symmetries
94  rel(*this, m(0,0), IRT_GR, m(0,n-1));
95  rel(*this, m(0,0), IRT_GR, m(n-1,0));
96 
98  }
99 
101  MagicSquare(bool share, MagicSquare& s) : Script(share,s), n(s.n) {
102  x.update(*this, share, s.x);
103  }
104 
106  virtual Space*
107  copy(bool share) {
108  return new MagicSquare(share,*this);
109  }
111  virtual void
112  print(std::ostream& os) const {
113  // Matrix-wrapper for the square
114  Matrix<IntVarArray> m(x, n, n);
115  for (int i = 0; i<n; i++) {
116  os << "\t";
117  for (int j = 0; j<n; j++) {
118  os.width(2);
119  os << m(i,j) << " ";
120  }
121  os << std::endl;
122  }
123  }
124 
125 };
126 
130 int
131 main(int argc, char* argv[]) {
132  SizeOptions opt("MagicSquare");
133  opt.iterations(1);
134  opt.size(7);
135  opt.parse(argc,argv);
136  Script::run<MagicSquare,DFS,SizeOptions>(opt);
137  return 0;
138 }
139 
140 // STATISTICS: example-any
141 
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
Options for scripts with additional size parameter
Definition: driver.hh:567
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
virtual Space * copy(bool share)
Copy during cloning.
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:212
Integer variable array.
Definition: int.hh:741
Greater ( )
Definition: int.hh:909
Example: Magic squares
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
virtual void print(std::ostream &os) const
Print solution.
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Equality ( )
Definition: int.hh:904
Options opt
The options.
Definition: test.cpp:101
unsigned int size(I &i)
Size of all ranges of range iterator i.
Slice< A > row(int r) const
Access row r.
Definition: matrix.hpp:181
Passing integer variables.
Definition: int.hh:636
int main(int argc, char *argv[])
Main-function.
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
MagicSquare(const SizeOptions &opt)
Post constraints.
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
void distinct(Home home, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:47
Matrix-interface for arrays.
Definition: minimodel.hh:1924
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition: val.hpp:88
Gecode toplevel namespace
Slice< A > col(int c) const
Access column c.
Definition: matrix.hpp:187
BrancherHandle 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
MagicSquare(bool share, MagicSquare &s)
Constructor for cloning s.
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194