Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
38using namespace Gecode;
39
45class Partition : public Script {
46protected:
51public:
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
131int
132main(int argc, char* argv[]) {
133 SizeOptions opt("Partition");
134 opt.size(32);
135 opt.ipl(IPL_BND);
136 opt.parse(argc,argv);
138 return 0;
139}
140
141
142// STATISTICS: example-any
143
struct Gecode::@603::NNF::@65::@66 b
For binary nodes (and, or, eqv)
int n
Number of negative literals for node type.
struct Gecode::@603::NNF::@65::@67 a
For atomic nodes.
Parametric base-class for scripts.
Definition driver.hh:729
static void run(const Options &opt, Script *s=NULL)
Definition script.hpp:290
Passing integer arguments.
Definition int.hh:628
Passing integer variables.
Definition int.hh:656
Integer variable array.
Definition int.hh:763
Options for scripts with additional size parameter
Definition driver.hh:675
Computation spaces.
Definition core.hpp:1742
int size(void) const
Return size of array (number of elements)
Definition array.hpp:926
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition array.hpp:1013
Example: partition numbers into two groups
Definition partition.cpp:45
int main(int argc, char *argv[])
Main-functiona.
virtual void print(std::ostream &os) const
Print solution.
Partition(Partition &s)
Constructor used during cloning s.
Definition partition.cpp:96
IntVarArray y
Second group of numbers.
Definition partition.cpp:50
Partition(const SizeOptions &opt)
Actual model.
Definition partition.cpp:53
virtual Space * copy(void)
Copying during cloning.
IntVarArray x
First group of numbers.
Definition partition.cpp:48
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
@ IRT_EQ
Equality ( )
Definition int.hh:926
@ IRT_LE
Less ( )
Definition int.hh:929
@ IPL_BND
Bounds propagation.
Definition int.hh:978
Gecode toplevel namespace
IntVarBranch INT_VAR_AFC_SIZE_MAX(double d=1.0, BranchTbl tbl=nullptr)
Select variable with largest accumulated failure count divided by domain size with decay factor d.
Definition var.hpp:236
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55