Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
ind-set.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, 2002
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 
50 class Graph {
52 public:
53  const int n_v;
54  const int n_e;
55  const int* e;
56  Graph(const int n_v0, const int n_e0, const int* e0)
57  : n_v(n_v0), n_e(n_e0), e(e0) {}
58 };
59 
60 const int e_20_10[] = {
61  0, 4, 2,12, 12,14, 18,19, 7,10,
62  9,12, 5,11, 6,15, 3,18, 7,16
63 };
64 
65 const Graph g_20_10(20,10,e_20_10);
66 
67 const int e_40_20[] = {
68  21,30, 11,30, 19,38, 20,25, 11,24,
69  20,33, 8,39, 4, 5, 6,16, 5,32,
70  0, 9, 5,24, 25,28, 36,38, 14,20,
71  19,25, 11,22, 13,30, 7,36, 15,33
72 };
73 
74 const Graph g_40_20(40, 20, e_40_20);
76 
77 
84 class IndSet : public IntMaximizeScript {
85 protected:
87  const Graph& g;
92 public:
95  : g(opt.size() == 0 ? g_20_10 : g_40_20),
96  v(*this,g.n_v,0,1), k(*this,0,g.n_v) {
97  const int* e = g.e;
98  for (int i = g.n_e; i--; ) {
99  const int* e1 = e++; const int* e2 = e++;
100  rel(*this, v[*e1], BOT_AND, v[*e2], 0);
101  }
102  linear(*this, v, IRT_EQ, k);
103  branch(*this, v, INT_VAR_NONE(), INT_VAL_MIN());
104  }
105 
107  IndSet(bool share, IndSet& s) : IntMaximizeScript(share,s), g(s.g) {
108  v.update(*this, share, s.v);
109  k.update(*this, share, s.k);
110  }
112  virtual Space*
113  copy(bool share) {
114  return new IndSet(share,*this);
115  }
117  virtual void
118  print(std::ostream& os) const {
119  os << "\tk = " << k << std::endl
120  << "\tv[] = " << v << std::endl;
121  }
123  virtual IntVar cost(void) const {
124  return k;
125  }
126 };
127 
128 
132 int
133 main(int argc, char* argv[]) {
134  SizeOptions opt("IndSet");
135  opt.solutions(0);
136  opt.size(1);
137  opt.iterations(2000);
138  opt.parse(argc,argv);
139  IntMaximizeScript::run<IndSet,BAB,SizeOptions>(opt);
140  return 0;
141 }
142 
143 // STATISTICS: example-any
144 
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
Options for scripts with additional size parameter
Definition: driver.hh:567
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:108
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
virtual void print(std::ostream &os) const
Print solution.
Definition: ind-set.cpp:118
void update(Space &home, bool share, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition: var.hpp:128
Graph specification
Definition: ind-set.cpp:51
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
const int n_v
Number of vertices.
Definition: ind-set.cpp:53
Conjunction.
Definition: int.hh:917
IndSet(bool share, IndSet &s)
Constructor for cloning s.
Definition: ind-set.cpp:107
virtual Space * copy(bool share)
Copy during cloning.
Definition: ind-set.cpp:113
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
Gecode::IntArgs i(4, 1, 2, 3, 4)
Equality ( )
Definition: int.hh:904
Options opt
The options.
Definition: test.cpp:101
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.
const int n_e
Number of edges.
Definition: ind-set.cpp:54
Boolean variable array.
Definition: int.hh:786
Integer variables.
Definition: int.hh:350
IntVar k
How many elements has indipendent set.
Definition: ind-set.cpp:91
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Example: Independent sets in a graph
Definition: ind-set.cpp:84
IndSet(const SizeOptions &opt)
Actual model.
Definition: ind-set.cpp:94
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:243
const Graph & g
Graph used
Definition: ind-set.cpp:87
BoolVarArray v
Whether vertex included in independent set.
Definition: ind-set.cpp:89
Gecode toplevel namespace
virtual IntVar cost(void) const
Return solution cost.
Definition: ind-set.cpp:123
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
Graph(const int n_v0, const int n_e0, const int *e0)
Definition: ind-set.cpp:56
const int * e
Arrays of edges (as vertex pairs)
Definition: ind-set.cpp:55
int main(int argc, char *argv[])
Main-function.
Definition: ind-set.cpp:133