Generated on Sat Feb 7 2015 02:01:12 for Gecode by doxygen 1.8.9.1
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: 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 
49 class Partition : public Script {
50 protected:
55 public:
58  : x(*this,opt.size(),1,2*opt.size()),
59  y(*this,opt.size(),1,2*opt.size()) {
60  const int n = opt.size();
61 
62  // Break symmetries by ordering numbers in each group
63  rel(*this, x, IRT_LE);
64  rel(*this, y, IRT_LE);
65 
66  rel(*this, x[0], IRT_LE, y[0]);
67 
68  IntVarArgs xy(2*n);
69  for (int i = n; i--; ) {
70  xy[i] = x[i]; xy[n+i] = y[i];
71  }
72  distinct(*this, xy, opt.icl());
73 
74  IntArgs c(2*n);
75  for (int i = n; i--; ) {
76  c[i] = 1; c[n+i] = -1;
77  }
78  linear(*this, c, xy, IRT_EQ, 0);
79 
80  // Array of products
81  IntVarArgs sxy(2*n), sx(n), sy(n);
82 
83  for (int i = n; i--; ) {
84  sx[i] = sxy[i] = expr(*this, sqr(x[i]));
85  sy[i] = sxy[n+i] = expr(*this, sqr(y[i]));
86  }
87  linear(*this, c, sxy, IRT_EQ, 0);
88 
89  // Redundant constraints
90  linear(*this, x, IRT_EQ, 2*n*(2*n+1)/4);
91  linear(*this, y, IRT_EQ, 2*n*(2*n+1)/4);
92  linear(*this, sx, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
93  linear(*this, sy, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
94 
95  branch(*this, xy, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
96  }
97 
99  Partition(bool share, Partition& s) : Script(share,s) {
100  x.update(*this, share, s.x);
101  y.update(*this, share, s.y);
102  }
104  virtual Space*
105  copy(bool share) {
106  return new Partition(share,*this);
107  }
109  virtual void
110  print(std::ostream& os) const {
111  os << "\t";
112  int a, b;
113  a = b = 0;
114  for (int i = 0; i < x.size(); i++) {
115  a += x[i].val();
116  b += x[i].val()*x[i].val();
117  os << x[i] << ", ";
118  }
119  os << " = " << a << ", " << b << std::endl << "\t";
120  a = b = 0;
121  for (int i = 0; i < y.size(); i++) {
122  a += y[i].val();
123  b += y[i].val()*y[i].val();
124  os << y[i] << ", ";
125  }
126  os << " = " << a << ", " << b << std::endl;
127  }
128 };
129 
134 int
135 main(int argc, char* argv[]) {
136  SizeOptions opt("Partition");
137  opt.size(32);
138  opt.icl(ICL_BND);
139  opt.parse(argc,argv);
140  Script::run<Partition,DFS,SizeOptions>(opt);
141  return 0;
142 }
143 
144 
145 // STATISTICS: example-any
146 
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
IntVarArray x
First group of numbers.
Definition: partition.cpp:52
Options for scripts with additional size parameter
Definition: driver.hh:567
Example: partition numbers into two groups
Definition: partition.cpp:49
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
Partition(bool share, Partition &s)
Constructor used during cloning s.
Definition: partition.cpp:99
virtual void print(std::ostream &os) const
Print solution.
Definition: partition.cpp:110
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
Integer variable array.
Definition: int.hh:741
Partition(const SizeOptions &opt)
Actual model.
Definition: partition.cpp:57
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void decay(double d)
Set default decay factor.
Definition: options.hpp:216
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
Gecode::FloatVal c(-8, 8)
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
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:103
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
unsigned int size(I &i)
Size of all ranges of range iterator i.
Less ( )
Definition: int.hh:907
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
IntVarArray y
Second group of numbers.
Definition: partition.cpp:54
BoolVar expr(Home home, const BoolExpr &e, IntConLevel icl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
int main(int argc, char *argv[])
Main-functiona.
Definition: partition.cpp:135
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
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:242
void distinct(Home home, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:47
Bounds propagation or consistency.
Definition: int.hh:939
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
virtual Space * copy(bool share)
Copying during cloning.
Definition: partition.cpp:105
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
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.