MinLCA algorithms
lp-model.hh
Go to the documentation of this file.
1 #ifndef __MINLCA_MINLCA_LP_MODEL_HH
2 #define __MINLCA_MINLCA_LP_MODEL_HH
3 
4 #include <vector>
5 #include <algorithm>
6 #include <utility>
7 #include <lemon/core.h>
8 #include <lemon/smart_graph.h>
9 #include <lemon/grb.h>
10 #include <utils/graph_utils.hh>
11 
14 
15 namespace minlca
16 {
17 
26 template<typename Graph = lemon::SmartGraph>
28 {
29 protected:
30  TEMPLATE_GRAPH_TYPEDEFS(Graph);
31  typedef lemon::GrbLp Lp;
32 
33  int _k;
34  std::vector<Lp::Col> _used_colours;
35  typename Graph::template NodeMap<vector<Lp::Col>>
37  typename Graph::template NodeMap<Lp::Expr>
39  typename Graph::template EdgeMap<Lp::Col>
41  Lp::Expr _lca_expr;
42  Lp _model;
43 public:
47  const Graph &g,
48  int maxK,
49  bool silent = true
50  )
51  : _k(maxK),
52  _used_colours(_k),
53  _vertex_colours(g),
54  _vertex_c(g),
55  _dist(g)
56  {
57  if (silent) {
58  _model.getEnv().set(GRB_IntParam_LogToConsole, 0);
59  }
60  else {
61  _model.getEnv().set(GRB_IntParam_LogToConsole, 1);
62  }
63 
64  _model.addColSet(_used_colours);
65 
66  for (NodeIt v(g); v != lemon::INVALID; ++v) {
67  _vertex_colours[v] = vector<Lp::Col>(_k);
68  _model.addColSet(_vertex_colours[v]);
69  }
70 
71  _model.addColSet(_dist);
72 
73  _model.forceModelUpdate();
74 
75  for (int c = 0; c < _k; ++c) {
76  _model.colBounds(_used_colours[c], 0, 1);
77  }
78 
79  for (NodeIt v(g); v != lemon::INVALID; ++v) {
80  Lp::Expr oneColourPerVertex = 0;
81 
82  for (int c = 0; c < _k; ++c) {
83  Lp::Col &nodeColour = _vertex_colours[v][c];
84  _model.colBounds(nodeColour, 0, 1);
85 
86  oneColourPerVertex += nodeColour;
87  _vertex_c[v] += c * nodeColour;
88  _model.addRow(nodeColour - _used_colours[c] <= 0);
89  }
90 
91  _vertex_c[v].simplify();
92  _model.addRow(1, oneColourPerVertex, 1);
93  }
94 
95  for (EdgeIt e(g); e != lemon::INVALID; ++e) {
96  Node u = g.u(e), v = g.v(e);
97 
98  for (int c = 0; c < _k; ++c) {
99  _model.addRow(_vertex_colours[u][c] + _vertex_colours[v][c] <= 1);
100  }
101 
102  _model.colBounds(_dist[e], 1, _k - 1);
103  _model.addRow(_vertex_c[u] - _vertex_c[v] <= _dist[e]);
104  _model.addRow(_vertex_c[v] - _vertex_c[u] <= _dist[e]);
105 
106  _lca_expr += _dist[e];
107  }
108 
109  _model.min();
110  _model.obj(_lca_expr);
111 
112  _model.forceModelUpdate();
113  }
114 
118  double solve()
119  {
120  _model.solve();
121  return _model.primal();
122  }
123 
127  double getValue()
128  {
129  return _model.primal();
130  }
131 
137  std::vector<int> getColourOrder(
138  const Node &v
139  )
140  {
141  std::vector<std::pair<double, int>> colourAndPrecedence(_k);
142 
143  for (int c = 0; c < _k; ++c) {
144  colourAndPrecedence[c] = std::make_pair(-_model.primal(_vertex_colours[v][c]),
145  c);
146  }
147 
148  std::sort(colourAndPrecedence.begin(), colourAndPrecedence.end());
149 
150  std::vector<int> colourOrder(_k);
151 
152  for (int i = 0; i < _k; ++i) {
153  colourOrder[i] = colourAndPrecedence[i].second;
154  }
155 
156  return colourOrder;
157  }
158 };
159 }
160 
161 #endif
Lp::Expr _lca_expr
Expression with the objective function (the cost)
Definition: lp-model.hh:41
Header of the LEMON-Gurobi solver interface.
Class defining a relaxed linear programming model for MinLCA.
Definition: lp-model.hh:27
Interface for the Gurobi LP solver.
Definition: grb.h:249
Graph::template NodeMap< Lp::Expr > _vertex_c
Map of expressions to calculate the colour of a vertex.
Definition: lp-model.hh:38
Graph::template EdgeMap< Lp::Col > _dist
Map of expressions to calculate the cost of an edge.
Definition: lp-model.hh:40
Default namespace Default namespace for MinLCA algorithms.
Definition: base.hh:15
void forceModelUpdate()
Force model update.
Definition: grb.cc:71
Functions for reading graphs and getting graph properties.
GRBEnv getEnv() const
Get the environment of the underlying Gurobi model.
Definition: grb.cc:45
MinLCALpModel(const Graph &g, int maxK, bool silent=true)
Constructor.
Definition: lp-model.hh:46
int _k
Index of the largest colour.
Definition: lp-model.hh:33
double solve()
Solve the linear model.
Definition: lp-model.hh:118
double getValue()
Retrieve the value of the objective function.
Definition: lp-model.hh:127
Lp _model
The internal Lp model.
Definition: lp-model.hh:42
std::vector< int > getColourOrder(const Node &v)
Get the colour preference induced by the solution.
Definition: lp-model.hh:137
Graph::template NodeMap< vector< Lp::Col > > _vertex_colours
Model variables for vertex colours.
Definition: lp-model.hh:36
std::vector< Lp::Col > _used_colours
Model variables for used colours.
Definition: lp-model.hh:34