Generated on Thu Apr 5 2018 19:44:19 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  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
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 
49 class Partition : public Script {
50 protected:
55 public:
58  : Script(opt),
59  x(*this,opt.size(),1,2*opt.size()),
60  y(*this,opt.size(),1,2*opt.size()) {
61  const int n = opt.size();
62 
63  // Break symmetries by ordering numbers in each group
64  rel(*this, x, IRT_LE);
65  rel(*this, y, IRT_LE);
66 
67  rel(*this, x[0], IRT_LE, y[0]);
68 
69  IntVarArgs xy(2*n);
70  for (int i = n; i--; ) {
71  xy[i] = x[i]; xy[n+i] = y[i];
72  }
73  distinct(*this, xy, opt.ipl());
74 
75  IntArgs c(2*n);
76  for (int i = n; i--; ) {
77  c[i] = 1; c[n+i] = -1;
78  }
79  linear(*this, c, xy, IRT_EQ, 0);
80 
81  // Array of products
82  IntVarArgs sxy(2*n), sx(n), sy(n);
83 
84  for (int i = n; i--; ) {
85  sx[i] = sxy[i] = expr(*this, sqr(x[i]));
86  sy[i] = sxy[n+i] = expr(*this, sqr(y[i]));
87  }
88  linear(*this, c, sxy, IRT_EQ, 0);
89 
90  // Redundant constraints
91  linear(*this, x, IRT_EQ, 2*n*(2*n+1)/4);
92  linear(*this, y, IRT_EQ, 2*n*(2*n+1)/4);
93  linear(*this, sx, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
94  linear(*this, sy, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
95 
96  branch(*this, xy, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
97  }
98 
101  x.update(*this, s.x);
102  y.update(*this, s.y);
103  }
105  virtual Space*
106  copy(void) {
107  return new Partition(*this);
108  }
110  virtual void
111  print(std::ostream& os) const {
112  os << "\t";
113  int a, b;
114  a = b = 0;
115  for (int i = 0; i < x.size(); i++) {
116  a += x[i].val();
117  b += x[i].val()*x[i].val();
118  os << x[i] << ", ";
119  }
120  os << " = " << a << ", " << b << std::endl << "\t";
121  a = b = 0;
122  for (int i = 0; i < y.size(); i++) {
123  a += y[i].val();
124  b += y[i].val()*y[i].val();
125  os << y[i] << ", ";
126  }
127  os << " = " << a << ", " << b << std::endl;
128  }
129 };
130 
135 int
136 main(int argc, char* argv[]) {
137  SizeOptions opt("Partition");
138  opt.size(32);
139  opt.ipl(IPL_BND);
140  opt.parse(argc,argv);
141  Script::run<Partition,DFS,SizeOptions>(opt);
142  return 0;
143 }
144 
145 
146 // STATISTICS: example-any
147 
Bounds propagation.
Definition: int.hh:957
void size(unsigned int s)
Set default size.
Definition: options.hpp:590
IntVarArray x
First group of numbers.
Definition: partition.cpp:52
Options for scripts with additional size parameter
Definition: driver.hh:679
Example: partition numbers into two groups
Definition: partition.cpp:49
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
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:973
virtual void print(std::ostream &os) const
Print solution.
Definition: partition.cpp:111
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1060
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:670
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition: linear.cpp:45
Integer variable array.
Definition: int.hh:742
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:220
Partition(const SizeOptions &opt)
Actual model.
Definition: partition.cpp:57
Computation spaces.
Definition: core.hpp:1668
Parametric base-class for scripts.
Definition: driver.hh:733
void decay(double d)
Set default decay factor.
Definition: options.hpp:242
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:240
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:905
Options opt
The options.
Definition: test.cpp:101
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:99
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:59
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:50
virtual Space * copy(void)
Copying during cloning.
Definition: partition.cpp:106
Less ( )
Definition: int.hh:908
Passing integer variables.
Definition: int.hh:637
Passing integer arguments.
Definition: int.hh:608
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
Partition(Partition &s)
Constructor used during cloning s.
Definition: partition.cpp:100
IntVarArray y
Second group of numbers.
Definition: partition.cpp:54
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
int main(int argc, char *argv[])
Main-functiona.
Definition: partition.cpp:136
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Gecode toplevel namespace