Generated on Sun Mar 19 2017 08:30:25 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: 2015-03-17 16:09:39 +0100 (Tue, 17 Mar 2015) $ by $Author: schulte $
11  * $Revision: 14447 $
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.icl());
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 
100  Partition(bool share, Partition& s) : Script(share,s) {
101  x.update(*this, share, s.x);
102  y.update(*this, share, s.y);
103  }
105  virtual Space*
106  copy(bool share) {
107  return new Partition(share,*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.icl(ICL_BND);
140  opt.parse(argc,argv);
141  Script::run<Partition,DFS,SizeOptions>(opt);
142  return 0;
143 }
144 
145 
146 // STATISTICS: example-any
147 
void size(unsigned int s)
Set default size.
Definition: options.hpp:485
IntVarArray x
First group of numbers.
Definition: partition.cpp:52
Options for scripts with additional size parameter
Definition: driver.hh:579
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
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
virtual void print(std::ostream &os) const
Print solution.
Definition: partition.cpp:111
Partition(bool share, Partition &s)
Constructor used during cloning s.
Definition: partition.cpp:100
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:437
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:633
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.
struct Gecode::@519::NNF::@60::@62 a
For atomic nodes.
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
struct Gecode::@519::NNF::@60::@61 b
For binary nodes (and, or, eqv)
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:136
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
Gecode toplevel namespace
virtual Space * copy(bool share)
Copying during cloning.
Definition: partition.cpp:106
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
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194