Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions highs/ipm/ipx/guess_basis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static void ProcessRemaining(const Control& control, const Model& model,
const Int* Ap = AI.colptr();
const Int* Ai = AI.rowidx();
std::vector<Int> cheap(Ap, Ap+n+m);
std::vector<Int> work(m), work2(m+1), work3(m+1);
std::vector<Int> work(2*(m+1));
Int num_matched = 0;
Int num_failed = 0;

Expand All @@ -181,8 +181,7 @@ static void ProcessRemaining(const Control& control, const Model& model,
if (weights[j] == 0.0)
break;
bool matched = AugmentingPath(j, Ap, Ai, jmatch.data(), cheap.data(),
marked.data(), work.data(), work2.data(),
work3.data());
marked.data(), work.data());
if (matched) {
basis->push_back(j);
num_matched++;
Expand Down
169 changes: 91 additions & 78 deletions highs/ipm/ipx/sparse_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,120 @@

namespace ipx {

// Iterative depth-first search. istack[0..depth] holds the nodes on the path
// from istart to the current node, and work[d] is the position in the
// neighbour list of istack[d] where the scan resumes. A node is written to
// the output (istack below top, growing downward) once all its neighbours
// have been visited, so the output is in reverse postorder.
//
// The path and the output share istack. They do not overlap, because each
// node in either part was unmarked on entry and the caller guarantees that
// istack[0..top-1] has room for all such nodes.
Int DepthFirstSearch(Int istart, const Int* Ap, const Int* Ai,
const Int* colmap, Int top, Int* istack, Int* marked,
Int marker, Int* work) {
// The current node i and the position p in its list of neighbours are
// kept in local variables. When moving to a neighbour, (i,p) is saved in
// istack[depth], work[depth]. Nodes are marked when reached and moved to
// the output when all their neighbours have been processed.
assert(marked[istart] != marker);
auto neighbours_begin = [&](Int i) -> Int {
const Int j = colmap ? colmap[i] : i;
return j >= 0 ? Ap[j] : 0;
};
auto neighbours_end = [&](Int i) -> Int {
const Int j = colmap ? colmap[i] : i;
return j >= 0 ? Ap[j+1] : 0;
};

Int depth = 0;
Int i = istart;
marked[i] = marker;
Int j = colmap ? colmap[i] : i;
Int p = j >= 0 ? Ap[j] : 0;
Int pend = j >= 0 ? Ap[j+1] : 0;
for (;;) {
istack[0] = istart;
marked[istart] = marker;
work[0] = neighbours_begin(istart);
while (depth >= 0) {
const Int i = istack[depth];
const Int pend = neighbours_end(i);
Int p = work[depth];
while (p < pend && marked[Ai[p]] == marker)
p++;
if (p < pend) {
const Int inext = Ai[p++];
if (marked[inext] != marker) {
// save current node and go to neighbour
marked[inext] = marker;
istack[depth] = i;
work[depth] = p;
depth++;
i = inext;
j = colmap ? colmap[i] : i;
p = j >= 0 ? Ap[j] : 0;
pend = j >= 0 ? Ap[j+1] : 0;
}
// descend to the first unmarked neighbour
const Int inext = Ai[p];
work[depth] = p+1;
depth++;
istack[depth] = inext;
marked[inext] = marker;
work[depth] = neighbours_begin(inext);
} else {
// node finished, go back to the last saved node
istack[--top] = i;
if (depth == 0)
break;
// all neighbours of i visited
depth--;
i = istack[depth];
p = work[depth];
j = colmap ? colmap[i] : i;
pend = j >= 0 ? Ap[j+1] : 0;
istack[--top] = i;
}
}
return top;
}

// Returns a free row in column j, or -1 if there is none. Rows of column j
// before position cheap[j] are known to be matched.
static Int FindFreeRow(Int j, const Int* Ap, const Int* Ai, const Int* jmatch,
Int* cheap) {
for (Int p = cheap[j]; p < Ap[j+1]; p++) {
if (jmatch[Ai[p]] == -1) {
cheap[j] = p+1;
return Ai[p];
}
}
cheap[j] = Ap[j+1];
return -1;
}

// Depth-first search for an alternating path that starts at column jstart,
// alternates between unmatched and matched entries, and ends at a free row.
//
// Each level d of the path uses two entries of @work: work[2*d] is the column
// on the path, and work[2*d+1] is the position in that column where the scan
// resumes. When the path leaves the column at level d, it does so through row
// Ai[work[2*d+1]-1], so the rows on the path do not need their own stack.
// The path has at most m+1 columns, because each column after jstart is
// matched to a different row; hence @work needs 2*(m+1) entries.
//
// When a column is entered for the first time, its entries from cheap[j]
// onward are checked for a free row ("cheap assignment"). Matched rows never
// become free, so cheap[j] only moves forward and the total cost of these
// checks over all calls is O(nnz(A)).
bool AugmentingPath(Int jstart, const Int* Ap, const Int* Ai, Int* jmatch,
Int* cheap, Int* marked, Int* work, Int* work2, Int* work3){
// Depth first search for an alternating path from column jstart to a free
// row. The current column j and the position p in it are kept in local
// variables. When the path moves along row i to column jmatch[i],
// (i,j,p) is saved in work[depth], work2[depth], work3[depth].
Int* cheap, Int* marked, Int* work) {
Int depth = 0;
Int j = jstart;
marked[j] = jstart;
Int ifree = FindFreeRow(j, Ap, Ai, jmatch, cheap);
Int p = Ap[j];
while (ifree < 0) {
if (p < Ap[j+1]) {
const Int i = Ai[p++];
const Int jnext = jmatch[i];
assert(jnext != -1);
// follow row i unless it is to be ignored or its column is
// already in the path
if (jnext >= 0 && marked[jnext] != jstart) {
work[depth] = i;
work2[depth] = j;
work3[depth] = p;
depth++;
j = jnext;
marked[j] = jstart;
ifree = FindFreeRow(j, Ap, Ai, jmatch, cheap);
p = Ap[j];
bool entered = true;

for (;;) {
if (entered) {
entered = false;
work[2*depth] = j;
marked[j] = jstart;
while (cheap[j] < Ap[j+1]) {
const Int i = Ai[cheap[j]++];
if (jmatch[i] == -1) {
// flip the path: match i to the last column and the
// row leaving each earlier column to that column
jmatch[i] = j;
for (Int d = depth-1; d >= 0; d--)
jmatch[Ai[work[2*d+1]-1]] = work[2*d];
return true;
}
}
work[2*depth+1] = Ap[j];
}

// look for a matched row in column j whose column is not on the path
// and has not been visited in this search
const Int pend = Ap[j+1];
Int p = work[2*depth+1];
Int jnext = -1;
for (; p < pend; p++) {
const Int jm = jmatch[Ai[p]];
assert(jm != -1); // no free rows remain in column j
if (jm >= 0 && marked[jm] != jstart) {
jnext = jm;
break;
}
}
if (jnext >= 0) {
work[2*depth+1] = p+1;
depth++;
j = jnext;
entered = true;
} else {
// no path through column j, go back
// no alternating path through column j
if (depth == 0)
return false;
depth--;
j = work2[depth];
p = work3[depth];
j = work[2*depth];
}
}
// match ifree to the last column and each saved row to its saved column
jmatch[ifree] = j;
while (depth > 0) {
depth--;
jmatch[work[depth]] = work2[depth];
}
return true;
}

} // namespace ipx
18 changes: 7 additions & 11 deletions highs/ipm/ipx/sparse_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ namespace ipx {
// @marked, @marker Node i is "marked" iff @marked[i] == @marker.
// @work workspace of size # rows of A.
//
// The code follows the algorithm of cs_dfs.c, included in the CSPARSE
// package [1].
//
// [1] T. Davis, "Direct methods for sparse linear systems" (2006)
// The nodes are returned in reverse postorder, which is a topological order
// if the graph is acyclic.
//
Int DepthFirstSearch(Int istart, const Int* Ap, const Int* Ai,
const Int* colmap, Int top, Int* istack, Int* marked,
Expand All @@ -39,19 +37,17 @@ Int DepthFirstSearch(Int istart, const Int* Ap, const Int* Ai,
// It must be unchanged between subsequent calls.
// @marked array of size n. On entry @marked[j] != @jstart for all j.
// On return some entries were set to @jstart.
// @work size m workspace.
// @work2 size m+1 workspace.
// @work3 size m+1 workspace.
// @work workspace of size 2*(m+1).
//
// Returns true if the matching was extended.
//
// The code follows the algorithm of cs_augment.c, included in the CSPARSE
// package [1].
// The method is depth-first search with cheap assignment, as in [1].
//
// [1] T. Davis, "Direct methods for sparse linear systems" (2006)
// [1] I. S. Duff, "On algorithms for obtaining a maximum transversal",
// ACM Trans. Math. Software 7 (1981), pp. 315-330.
//
bool AugmentingPath(Int jstart, const Int* Ap, const Int* Ai, Int* jmatch,
Int* cheap, Int* marked, Int* work, Int* work2, Int* work3);
Int* cheap, Int* marked, Int* work);

} // namespace ipx

Expand Down
8 changes: 3 additions & 5 deletions highs/ipm/ipx/symbolic_invert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,22 @@ static std::vector<Int> Matching(const Model& model,
std::vector<Int> jmatch(m, -1);
std::vector<Int> cheap(Ap, Ap+n+m);
std::vector<Int> marked(n+m, -1);
std::vector<Int> work(m), work2(m+1), work3(m+1);
std::vector<Int> work(2*(m+1));

// Match singletons first, since then they are all cheap matches.
for (Int j : basis) {
if (Ap[j+1] == Ap[j]+1) {
bool matched = AugmentingPath(j, Ap, Ai, jmatch.data(),
cheap.data(), marked.data(),
work.data(), work2.data(),
work3.data());
work.data());
assert(matched);
}
}
for (Int j : basis) {
if (Ap[j+1] != Ap[j]+1) {
bool matched = AugmentingPath(j, Ap, Ai, jmatch.data(),
cheap.data(), marked.data(),
work.data(), work2.data(),
work3.data());
work.data());
assert(matched);
}
}
Expand Down
Loading