39 #include <coin/CoinPackedMatrix.hpp> 43 #define OGDF_STEINERTREE_LPRELAXATIONSER_SEPARATE_CONNECTED_COMPONENTS // this is faster 44 #define OGDF_STEINERTREE_LPRELAXATIONSER_SEPARATE_YVAR_CONSTRAINTS // if not defined: generate all yvar constraints in the beginning 47 namespace steiner_tree {
67 #ifdef OGDF_STEINERTREE_LPRELAXATIONSER_SEPARATE_CONNECTED_COMPONENTS 109 for (
node t : m_terminals) {
113 for (
int j = 0; j < activeComponents.
size(); ++j) {
114 const int i = activeComponents[j];
115 const double cap = m_fullCompStore.
extra(i);
119 const auto it0 = terminals.
begin();
121 capacity[G.
newEdge(source, rC)] = cap;
122 if (terminals.
size() > 2) {
124 capacity[G.
newEdge(rC, v)] = cap;
125 for (
auto it = it0 + 1; it != terminals.
end(); ++it) {
127 capacity[G.
newEdge(v, w)] = cap;
136 for (
node t : m_terminals) {
143 y_v += capacity[adj->
theEdge()];
147 #ifdef OGDF_STEINERTREE_LPRELAXATIONSER_SEPARATE_YVAR_CONSTRAINTS 155 capacity[G.
newEdge(v, target)] = y_v;
165 std::cout <<
" " << e->
source();
173 std::cout <<
" " << capacity[e] <<
"\n";
193 T upperBound = 0,
int cliqueSize = 0,
double eps = 1e-8)
195 , m_isTerminal(isTerminal)
196 , m_terminals(terminals)
197 , m_fullCompStore(fullCompStore)
198 , m_osiSolver(
CoinManager::createCorrectOsiSolverInterface())
199 , m_matrix(new CoinPackedMatrix)
200 , m_objective(new double[m_fullCompStore.size()])
201 , m_lowerBounds(new double[m_fullCompStore.size()])
202 , m_upperBounds(new double[m_fullCompStore.size()])
203 , m_upperBound(upperBound)
204 , m_separateCliqueSize(cliqueSize)
206 , m_separationStage(0)
213 #ifndef OGDF_STEINERTREE_LPRELAXATIONSER_SEPARATE_YVAR_CONSTRAINTS 214 for (
node t : m_terminals) {
241 for (
int i = 0; i < n; ++i) {
245 for (
int i = 0; i < n; ++i) {
251 CoinPackedVector row;
261 CoinPackedVector row;
277 CoinPackedVector row;
282 int intersectionCard = 0;
284 node *it1 = terminals.begin();
285 auto it2 = subset.
begin();
286 while (it1 != terminals.end()
287 && it2 != subset.
end()) {
288 if ((*it1)->index() < (*it2)->index()) {
291 if ((*it1)->index() > (*it2)->index()) {
300 if (intersectionCard > 1) {
301 row.insert(i, intersectionCard - 1);
305 if (test > subset.
size() - 1) {
318 CoinPackedVector row;
332 bool initialIteration =
true;
335 if (initialIteration) {
337 #ifdef OGDF_STEINERTREE_LPRELAXATIONSER_LOGGING 338 std::cout <<
"Objective value " <<
m_osiSolver->getObjValue() <<
" of initial solution." << std::endl;
340 initialIteration =
false;
343 #ifdef OGDF_STEINERTREE_LPRELAXATIONSER_LOGGING 344 std::cout <<
"Objective value " <<
m_osiSolver->getObjValue() <<
" after resolve." << std::endl;
352 std::cerr <<
"Failed to optimize LP!" << std::endl;
358 const double *constSol =
m_osiSolver->getColSolution();
359 const int numberOfColumns =
m_osiSolver->getNumCols();
361 for (
int i = 0; i < numberOfColumns; ++i) {
365 #ifdef OGDF_STEINERTREE_LPRELAXATIONSER_OUTPUT_LP 376 const double *constSol =
m_osiSolver->getColSolution();
382 activeComponents.
push(i);
386 #ifdef OGDF_STEINERTREE_LPRELAXATIONSER_SEPARATE_CONNECTED_COMPONENTS 407 setID[t] = uf.makeSet();
411 for (
int j = 0; j < activeComponents.
size(); ++j) {
413 auto it = terminals.
begin();
414 const int s1 = setID[*it];
415 for (++it; it != terminals.end(); ++it) {
416 uf.link(uf.find(s1), uf.find(setID[*it]));
420 if (uf.getNumberOfSets() == 1) {
426 for (
node t : m_terminals) {
427 const int k = uf.find(setID[t]);
428 if (components[k].empty()) {
431 components[k].push(t);
434 for (
const int k : usedComp) {
451 #ifdef OGDF_STEINERTREE_LPRELAXATIONSER_SEPARATE_YVAR_CONSTRAINTS 458 capacity[auxG.
newEdge(pseudotarget, target)] = y_R;
467 capacity[v_to_target] = std::numeric_limits<double>::max();
469 maxFlow.
init(auxG, &flow);
471 const double cutVal = maxFlow.
computeValue(capacity, source, target);
472 if (cutVal - y_R < 1 -
m_eps) {
473 minSTCut.
call(auxG, capacity, flow, source, target);
475 for (
node tOrig : m_terminals) {
476 const node tCopy = auxG.
copy(tOrig);
487 return cutsFound != 0;
499 for (
int i : activeComponents) {
503 const int i1 =
id[u1];
506 const int i2 =
id[u2];
509 int intersectionCard = 0;
512 while (it1 != terminals1.
end()
513 && it2 != terminals2.
end()) {
514 if ((*it1)->index() < (*it2)->index()) {
517 if ((*it1)->index() > (*it2)->index()) {
521 if (intersectionCard == 2) {
547 degrees[idx].pushBack(v);
550 for (
int k = degrees.
size(); k >= 2; --k) {
551 degrees[k-2].conc(degrees[k-1]);
552 if (degrees[k-2].size() >= k) {
554 for (nodeSubset.
begin(k); nodeSubset.
valid(); nodeSubset.
next()) {
555 int countEdges = (k * (k-1)) / 2;
556 for (
int j = 0; j < nodeSubset.
size(); ++j) {
557 test[nodeSubset[j]] =
true;
566 if (countEdges == 0) {
569 CoinPackedVector row;
571 for (
int j = 0; j < nodeSubset.
size(); ++j) {
572 int i =
id[nodeSubset[j]];
576 if (val >= 1 +
m_eps) {
581 for (
int j = 0; j < nodeSubset.
size(); ++j) {
582 test[nodeSubset[j]] =
false;
node target() const
Returns the target node of the edge.
void addYConstraint(const node t)
Add constraint that the sum of x_C over all components C spanning terminal t is at least 1 to ensure ...
bool separateConnected(const ArrayBuffer< int > &activeComponents)
Separate to ensure that the solution is connected.
edge theEdge() const
Returns the edge associated with this adjacency entry.
node copy(node v) const
Returns the node in the graph copy corresponding to v.
node newNode(node vOrig)
Creates a new node in the graph copy with original node vOrig.
void init()
Reinitializes the array. Associates the array with no graph.
Data type for general directed graphs (adjacency list representation).
If you use COIN-OR, you should use this class.
virtual void init(const Graph &graph, EdgeArray< TCap > *flow=nullptr)
Initialize the problem with a graph and optional flow array. If no flow array is given, a new ("internal") array will be created. If a flow array is given, the algorithm uses this "external" array.
int degree() const
Returns the degree of the node (indegree + outdegree).
const Array< node > & terminals(int id) const
Returns the list of terminals in the full component with given id.
Min-st-cut algorithm, that calculates the cut via maxflow.
void push(E e)
Puts a new element in the buffer.
void createEmpty(const Graph &G)
Associates the graph copy with G, but does not create any nodes or edges.
bool separateCycles(const ArrayBuffer< int > &activeComponents)
Perform the separation algorithm for cycle constraints (to obtain stronger LP solution) ...
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
FullComponentWithExtraStore< T, double > & m_fullCompStore
all enumerated full components, with solution
OsiSolverInterface * m_osiSolver
bool valid() const
Checks if the current subset is valid. If not, the subset is either not initialized or all subsets ha...
const NodeArray< bool > & m_isTerminal
iterator begin()
Returns an iterator to the first element.
node succ() const
Returns the successor in the list of all nodes.
reverse_iterator rbegin()
Returns an reverse iterator to the last element.
bool solve()
Solve the LP. The solution will be written to the extra data of the full component store...
bool isTerminal(int id, node t) const
checks if a given node t is a terminal in the full component with given id
edge newEdge(edge eOrig)
Creates a new edge (v,w) with original edge eOrig.
virtual void delEdge(edge e) override
Removes edge e and clears the list of edges corresponding to e's original edge.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
int numberOfEdges() const
Returns the number of edges in the graph.
bool separateMinCut(const ArrayBuffer< int > &activeComponents)
Perform the general cut-based separation algorithm.
void begin(int low, int high)
Initializes the SubsetEnumerator to enumerate subsets of cardinalities from low to high...
INDEX size() const
Returns the size (number of elements) of the array.
double generateMinCutSeparationGraph(const ArrayBuffer< int > &activeComponents, node &source, node &target, GraphCopy &G, EdgeArray< double > &capacity, int &cutsFound)
Generates an auxiliary multi-graph for separation (during LP solving): directed, with special source ...
Enumerator for k-subsets of a given type.
Definition of the FullComponentStore class template.
Doubly linked lists (maintaining the length of the list).
edge newEdge(node v, node w)
Creates a new edge (v,w) and returns it.
TCap computeValue(const EdgeArray< TCap > &cap, const node &s, const node &t)
Compute only the value of the flow.
A Union/Find data structure for maintaining disjoint sets.
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Class for the representation of edges.
Class managing the component-based subtour elimination LP relaxation for the Steiner tree problem and...
Copies of graphs supporting edge splitting.
Class for the representation of nodes.
virtual bool call(const Graph &graph, const EdgeArray< TCost > &weight, node s, node t, List< edge > &edgeList, edge e_st=nullptr) override
The actual algorithm call.
const int m_separateCliqueSize
INDEX size() const
Returns number of elements in the buffer.
CoinPackedMatrix * m_matrix
const Graph & original() const
Returns a reference to the original graph.
#define OGDF_STEINERTREE_LPRELAXATIONSER_SEPARATE_CONNECTED_COMPONENTS
iterator begin()
Returns an iterator to the first element.
bool separate()
Perform all available separation algorithms.
void generateProblem()
Generate the basic LP model.
const EdgeWeightedGraph< T > & m_G
iterator end()
Returns an iterator to one past the last element.
void next()
Obtains the next subset if possible. The result should be checked using the valid() method...
bool addSubsetCoverConstraint(const ArrayBuffer< node > &subset)
Add subset cover constraints to the LP for a given subset of terminals.
const double m_eps
epsilon for double operations
iterator end()
Returns an iterator to one past the last element.
node source() const
Returns the source node of the edge.
Definition of ogdf::CoinManager.
bool isInBackCut(const node v) const
Returns whether this node is part of the back cut.
int size() const
Returns the cardinality of the subset.
The namespace for all OGDF objects.
T cost(int i) const
Returns the sum of edge costs of this full component.
const List< node > & m_terminals
List of terminals.
node newNode()
Creates a new node and returns it.
int size() const
Returns the number of full components in the store.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
void addTerminalCoverConstraint()
Add terminal cover constraints to the LP.
A class that allows to enumerate k-subsets.
LPRelaxationSER(const EdgeWeightedGraph< T > &G, const List< node > &terminals, const NodeArray< bool > &isTerminal, FullComponentWithExtraStore< T, double > &fullCompStore, T upperBound=0, int cliqueSize=0, double eps=1e-8)
Initialize the LP.
Computes a max flow via Preflow-Push (global relabeling and gap relabeling heuristic).
Class for adjacency list elements.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Declaration of min-st-cut algorithms parameterized by max-flow alorithm.
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.