Generated on Sat Oct 20 2018 12:43:45 for Gecode by doxygen 1.8.13
partition.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, 2003
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 
45 class Partition : public Script {
46 protected:
51 public:
54  : Script(opt),
55  x(*this,opt.size(),1,2*opt.size()),
56  y(*this,opt.size(),1,2*opt.size()) {
57  const int n = opt.size();
58 
59  // Break symmetries by ordering numbers in each group
60  rel(*this, x, IRT_LE);
61  rel(*this, y, IRT_LE);
62 
63  rel(*this, x[0], IRT_LE, y[0]);
64 
65  IntVarArgs xy(2*n);
66  for (int i = n; i--; ) {
67  xy[i] = x[i]; xy[n+i] = y[i];
68  }
69  distinct(*this, xy, opt.ipl());
70 
71  IntArgs c(2*n);
72  for (int i = n; i--; ) {
73  c[i] = 1; c[n+i] = -1;
74  }
75  linear(*this, c, xy, IRT_EQ, 0);
76 
77  // Array of products
78  IntVarArgs sxy(2*n), sx(n), sy(n);
79 
80  for (int i = n; i--; ) {
81  sx[i] = sxy[i] = expr(*this, sqr(x[i]));
82  sy[i] = sxy[n+i] = expr(*this, sqr(y[i]));
83  }
84  linear(*this, c, sxy, IRT_EQ, 0);
85 
86  // Redundant constraints
87  linear(*this, x, IRT_EQ, 2*n*(2*n+1)/4);
88  linear(*this, y, IRT_EQ, 2*n*(2*n+1)/4);
89  linear(*this, sx, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
90  linear(*this, sy, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
91 
92  branch(*this, xy, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
93  }
94 
97  x.update(*this, s.x);
98  y.update(*this, s.y);
99  }
101  virtual Space*
102  copy(void) {
103  return new Partition(*this);
104  }
106  virtual void
107  print(std::ostream& os) const {
108  os << "\t";
109  int a, b;
110  a = b = 0;
111  for (int i = 0; i < x.size(); i++) {
112  a += x[i].val();
113  b += x[i].val()*x[i].val();
114  os << x[i] << ", ";
115  }
116  os << " = " << a << ", " << b << std::endl << "\t";
117  a = b = 0;
118  for (int i = 0; i < y.size(); i++) {
119  a += y[i].val();
120  b += y[i].val()*y[i].val();
121  os << y[i] << ", ";
122  }
123  os << " = " << a << ", " << b << std::endl;
124  }
125 };
126 
131 int
132 main(int argc, char* argv[]) {
133  SizeOptions opt("Partition");
134  opt.size(32);
135  opt.ipl(IPL_BND);
136  opt.parse(argc,argv);
137  Script::run<Partition,DFS,SizeOptions>(opt);
138  return 0;
139 }
140 
141 
142 // STATISTICS: example-any
143 
Bounds propagation.
Definition: int.hh:978
void size(unsigned int s)
Set default size.
Definition: options.hpp:586
IntVarArray x
First group of numbers.
Definition: partition.cpp:48
Options for scripts with additional size parameter
Definition: driver.hh:675
Example: partition numbers into two groups
Definition: partition.cpp:45
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
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:908
virtual void print(std::ostream &os) const
Print solution.
Definition: partition.cpp:107
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:995
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:666
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition: linear.cpp:41
Integer variable array.
Definition: int.hh:763
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:216
Partition(const SizeOptions &opt)
Actual model.
Definition: partition.cpp:53
Computation spaces.
Definition: core.hpp:1701
Parametric base-class for scripts.
Definition: driver.hh:729
void decay(double d)
Set default decay factor.
Definition: options.hpp:238
Gecode::FloatVal c(-8, 8)
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
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Equality ( )
Definition: int.hh:926
Options opt
The options.
Definition: test.cpp:97
Gecode::IntArgs i({1, 2, 3, 4})
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:95
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:55
struct Gecode::@593::NNF::@62::@63 b
For binary nodes (and, or, eqv)
unsigned int size(I &i)
Size of all ranges of range iterator i.
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:46
virtual Space * copy(void)
Copying during cloning.
Definition: partition.cpp:102
Less ( )
Definition: int.hh:929
Passing integer variables.
Definition: int.hh:656
Passing integer arguments.
Definition: int.hh:628
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:627
Partition(Partition &s)
Constructor used during cloning s.
Definition: partition.cpp:96
IntVarArray y
Second group of numbers.
Definition: partition.cpp:50
int main(int argc, char *argv[])
Main-functiona.
Definition: partition.cpp:132
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
Gecode toplevel namespace
struct Gecode::@593::NNF::@62::@64 a
For atomic nodes.