| ... | ... |
@@ -1032,97 +1032,97 @@ |
| 1032 | 1032 |
/// |
| 1033 | 1033 |
/// \pre \ref run() must be called before using this function. |
| 1034 | 1034 |
template <typename PotentialMap> |
| 1035 | 1035 |
void potentialMap(PotentialMap &map) const {
|
| 1036 | 1036 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1037 | 1037 |
map.set(n, _pi[_node_id[n]]); |
| 1038 | 1038 |
} |
| 1039 | 1039 |
} |
| 1040 | 1040 |
|
| 1041 | 1041 |
/// @} |
| 1042 | 1042 |
|
| 1043 | 1043 |
private: |
| 1044 | 1044 |
|
| 1045 | 1045 |
// Initialize internal data structures |
| 1046 | 1046 |
bool init() {
|
| 1047 | 1047 |
if (_node_num == 0) return false; |
| 1048 | 1048 |
|
| 1049 | 1049 |
// Check the sum of supply values |
| 1050 | 1050 |
_sum_supply = 0; |
| 1051 | 1051 |
for (int i = 0; i != _node_num; ++i) {
|
| 1052 | 1052 |
_sum_supply += _supply[i]; |
| 1053 | 1053 |
} |
| 1054 | 1054 |
if ( !((_stype == GEQ && _sum_supply <= 0) || |
| 1055 | 1055 |
(_stype == LEQ && _sum_supply >= 0)) ) return false; |
| 1056 | 1056 |
|
| 1057 | 1057 |
// Remove non-zero lower bounds |
| 1058 | 1058 |
if (_have_lower) {
|
| 1059 | 1059 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1060 | 1060 |
Value c = _lower[i]; |
| 1061 | 1061 |
if (c >= 0) {
|
| 1062 | 1062 |
_cap[i] = _upper[i] < MAX ? _upper[i] - c : INF; |
| 1063 | 1063 |
} else {
|
| 1064 | 1064 |
_cap[i] = _upper[i] < MAX + c ? _upper[i] - c : INF; |
| 1065 | 1065 |
} |
| 1066 | 1066 |
_supply[_source[i]] -= c; |
| 1067 | 1067 |
_supply[_target[i]] += c; |
| 1068 | 1068 |
} |
| 1069 | 1069 |
} else {
|
| 1070 | 1070 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1071 | 1071 |
_cap[i] = _upper[i]; |
| 1072 | 1072 |
} |
| 1073 | 1073 |
} |
| 1074 | 1074 |
|
| 1075 | 1075 |
// Initialize artifical cost |
| 1076 | 1076 |
Cost ART_COST; |
| 1077 | 1077 |
if (std::numeric_limits<Cost>::is_exact) {
|
| 1078 | 1078 |
ART_COST = std::numeric_limits<Cost>::max() / 2 + 1; |
| 1079 | 1079 |
} else {
|
| 1080 |
ART_COST = |
|
| 1080 |
ART_COST = 0; |
|
| 1081 | 1081 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1082 | 1082 |
if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
| 1083 | 1083 |
} |
| 1084 | 1084 |
ART_COST = (ART_COST + 1) * _node_num; |
| 1085 | 1085 |
} |
| 1086 | 1086 |
|
| 1087 | 1087 |
// Initialize arc maps |
| 1088 | 1088 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1089 | 1089 |
_flow[i] = 0; |
| 1090 | 1090 |
_state[i] = STATE_LOWER; |
| 1091 | 1091 |
} |
| 1092 | 1092 |
|
| 1093 | 1093 |
// Set data for the artificial root node |
| 1094 | 1094 |
_root = _node_num; |
| 1095 | 1095 |
_parent[_root] = -1; |
| 1096 | 1096 |
_pred[_root] = -1; |
| 1097 | 1097 |
_thread[_root] = 0; |
| 1098 | 1098 |
_rev_thread[0] = _root; |
| 1099 | 1099 |
_succ_num[_root] = _node_num + 1; |
| 1100 | 1100 |
_last_succ[_root] = _root - 1; |
| 1101 | 1101 |
_supply[_root] = -_sum_supply; |
| 1102 | 1102 |
_pi[_root] = 0; |
| 1103 | 1103 |
|
| 1104 | 1104 |
// Add artificial arcs and initialize the spanning tree data structure |
| 1105 | 1105 |
if (_sum_supply == 0) {
|
| 1106 | 1106 |
// EQ supply constraints |
| 1107 | 1107 |
_search_arc_num = _arc_num; |
| 1108 | 1108 |
_all_arc_num = _arc_num + _node_num; |
| 1109 | 1109 |
for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
|
| 1110 | 1110 |
_parent[u] = _root; |
| 1111 | 1111 |
_pred[u] = e; |
| 1112 | 1112 |
_thread[u] = u + 1; |
| 1113 | 1113 |
_rev_thread[u + 1] = u; |
| 1114 | 1114 |
_succ_num[u] = 1; |
| 1115 | 1115 |
_last_succ[u] = u; |
| 1116 | 1116 |
_cap[e] = INF; |
| 1117 | 1117 |
_state[e] = STATE_TREE; |
| 1118 | 1118 |
if (_supply[u] >= 0) {
|
| 1119 | 1119 |
_forward[u] = true; |
| 1120 | 1120 |
_pi[u] = 0; |
| 1121 | 1121 |
_source[e] = u; |
| 1122 | 1122 |
_target[e] = _root; |
| 1123 | 1123 |
_flow[e] = _supply[u]; |
| 1124 | 1124 |
_cost[e] = 0; |
| 1125 | 1125 |
} else {
|
| 1126 | 1126 |
_forward[u] = false; |
| 1127 | 1127 |
_pi[u] = ART_COST; |
| 1128 | 1128 |
_source[e] = _root; |
| ... | ... |
@@ -1544,78 +1544,78 @@ |
| 1544 | 1544 |
case CANDIDATE_LIST: |
| 1545 | 1545 |
return start<CandidateListPivotRule>(); |
| 1546 | 1546 |
case ALTERING_LIST: |
| 1547 | 1547 |
return start<AlteringListPivotRule>(); |
| 1548 | 1548 |
} |
| 1549 | 1549 |
return INFEASIBLE; // avoid warning |
| 1550 | 1550 |
} |
| 1551 | 1551 |
|
| 1552 | 1552 |
template <typename PivotRuleImpl> |
| 1553 | 1553 |
ProblemType start() {
|
| 1554 | 1554 |
PivotRuleImpl pivot(*this); |
| 1555 | 1555 |
|
| 1556 | 1556 |
// Perform heuristic initial pivots |
| 1557 | 1557 |
if (!initialPivots()) return UNBOUNDED; |
| 1558 | 1558 |
|
| 1559 | 1559 |
// Execute the Network Simplex algorithm |
| 1560 | 1560 |
while (pivot.findEnteringArc()) {
|
| 1561 | 1561 |
findJoinNode(); |
| 1562 | 1562 |
bool change = findLeavingArc(); |
| 1563 | 1563 |
if (delta >= MAX) return UNBOUNDED; |
| 1564 | 1564 |
changeFlow(change); |
| 1565 | 1565 |
if (change) {
|
| 1566 | 1566 |
updateTreeStructure(); |
| 1567 | 1567 |
updatePotential(); |
| 1568 | 1568 |
} |
| 1569 | 1569 |
} |
| 1570 | 1570 |
|
| 1571 | 1571 |
// Check feasibility |
| 1572 | 1572 |
for (int e = _search_arc_num; e != _all_arc_num; ++e) {
|
| 1573 | 1573 |
if (_flow[e] != 0) return INFEASIBLE; |
| 1574 | 1574 |
} |
| 1575 | 1575 |
|
| 1576 | 1576 |
// Transform the solution and the supply map to the original form |
| 1577 | 1577 |
if (_have_lower) {
|
| 1578 | 1578 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1579 | 1579 |
Value c = _lower[i]; |
| 1580 | 1580 |
if (c != 0) {
|
| 1581 | 1581 |
_flow[i] += c; |
| 1582 | 1582 |
_supply[_source[i]] += c; |
| 1583 | 1583 |
_supply[_target[i]] -= c; |
| 1584 | 1584 |
} |
| 1585 | 1585 |
} |
| 1586 | 1586 |
} |
| 1587 | 1587 |
|
| 1588 | 1588 |
// Shift potentials to meet the requirements of the GEQ/LEQ type |
| 1589 | 1589 |
// optimality conditions |
| 1590 | 1590 |
if (_sum_supply == 0) {
|
| 1591 | 1591 |
if (_stype == GEQ) {
|
| 1592 |
Cost max_pot = std::numeric_limits<Cost>:: |
|
| 1592 |
Cost max_pot = -std::numeric_limits<Cost>::max(); |
|
| 1593 | 1593 |
for (int i = 0; i != _node_num; ++i) {
|
| 1594 | 1594 |
if (_pi[i] > max_pot) max_pot = _pi[i]; |
| 1595 | 1595 |
} |
| 1596 | 1596 |
if (max_pot > 0) {
|
| 1597 | 1597 |
for (int i = 0; i != _node_num; ++i) |
| 1598 | 1598 |
_pi[i] -= max_pot; |
| 1599 | 1599 |
} |
| 1600 | 1600 |
} else {
|
| 1601 | 1601 |
Cost min_pot = std::numeric_limits<Cost>::max(); |
| 1602 | 1602 |
for (int i = 0; i != _node_num; ++i) {
|
| 1603 | 1603 |
if (_pi[i] < min_pot) min_pot = _pi[i]; |
| 1604 | 1604 |
} |
| 1605 | 1605 |
if (min_pot < 0) {
|
| 1606 | 1606 |
for (int i = 0; i != _node_num; ++i) |
| 1607 | 1607 |
_pi[i] -= min_pot; |
| 1608 | 1608 |
} |
| 1609 | 1609 |
} |
| 1610 | 1610 |
} |
| 1611 | 1611 |
|
| 1612 | 1612 |
return OPTIMAL; |
| 1613 | 1613 |
} |
| 1614 | 1614 |
|
| 1615 | 1615 |
}; //class NetworkSimplex |
| 1616 | 1616 |
|
| 1617 | 1617 |
///@} |
| 1618 | 1618 |
|
| 1619 | 1619 |
} //namespace lemon |
| 1620 | 1620 |
|
| 1621 | 1621 |
#endif //LEMON_NETWORK_SIMPLEX_H |
0 comments (0 inline)