Generated on Thu Mar 16 2017 03:24:13 for Gecode by doxygen 1.8.13
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: 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 
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  : IntMaximizeScript(opt),
96  g(opt.size() == 0 ? g_20_10 : g_40_20),
97  v(*this,g.n_v,0,1), k(*this,0,g.n_v) {
98  const int* e = g.e;
99  for (int i = g.n_e; i--; ) {
100  const int* e1 = e++; const int* e2 = e++;
101  rel(*this, v[*e1], BOT_AND, v[*e2], 0);
102  }
103  linear(*this, v, IRT_EQ, k);
104  branch(*this, v, INT_VAR_NONE(), INT_VAL_MIN());
105  }
106 
108  IndSet(bool share, IndSet& s) : IntMaximizeScript(share,s), g(s.g) {
109  v.update(*this, share, s.v);
110  k.update(*this, share, s.k);
111  }
113  virtual Space*
114  copy(bool share) {
115  return new IndSet(share,*this);
116  }
118  virtual void
119  print(std::ostream& os) const {
120  os << "\tk = " << k << std::endl
121  << "\tv[] = " << v << std::endl;
122  }
124  virtual IntVar cost(void) const {
125  return k;
126  }
127 };
128 
129 
133 int
134 main(int argc, char* argv[]) {
135  SizeOptions opt("IndSet");
136  opt.solutions(0);
137  opt.size(1);
138  opt.iterations(2000);
139  opt.parse(argc,argv);
140  IntMaximizeScript::run<IndSet,BAB,SizeOptions>(opt);
141  return 0;
142 }
143 
144 // STATISTICS: example-any
145 
void size(unsigned int s)
Set default size.
Definition: options.hpp:557
Options for scripts with additional size parameter
Definition: driver.hh:649
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
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:589
const int n_v
Number of vertices.
Definition: ind-set.cpp:53
Conjunction.
Definition: int.hh:918
IndSet(bool share, IndSet &s)
Constructor for cloning s.
Definition: ind-set.cpp:108
virtual IntVar cost(void) const
Return solution cost.
Definition: ind-set.cpp:124
virtual Space * copy(bool share)
Copy during cloning.
Definition: ind-set.cpp:114
Computation spaces.
Definition: core.hpp:1672
Parametric base-class for scripts.
Definition: driver.hh:703
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:465
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
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
Gecode::IntArgs i(4, 1, 2, 3, 4)
Equality ( )
Definition: int.hh:905
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:787
Integer variables.
Definition: int.hh:351
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:287
const Graph & g
Graph used
Definition: ind-set.cpp:87
BoolVarArray v
Whether vertex included in independent set.
Definition: ind-set.cpp:89
virtual void print(std::ostream &os) const
Print solution.
Definition: ind-set.cpp:119
Gecode toplevel namespace
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:134