| ... | ... |
@@ -1013,65 +1013,65 @@ |
| 1013 | 1013 |
|
| 1014 | 1014 |
// Check the sum of supply values |
| 1015 | 1015 |
_sum_supply = 0; |
| 1016 | 1016 |
for (int i = 0; i != _node_num; ++i) {
|
| 1017 | 1017 |
_sum_supply += _supply[i]; |
| 1018 | 1018 |
} |
| 1019 | 1019 |
if ( !((_stype == GEQ && _sum_supply <= 0) || |
| 1020 | 1020 |
(_stype == LEQ && _sum_supply >= 0)) ) return false; |
| 1021 | 1021 |
|
| 1022 | 1022 |
// Remove non-zero lower bounds |
| 1023 | 1023 |
if (_have_lower) {
|
| 1024 | 1024 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1025 | 1025 |
Value c = _lower[i]; |
| 1026 | 1026 |
if (c >= 0) {
|
| 1027 | 1027 |
_cap[i] = _upper[i] < INF ? _upper[i] - c : INF; |
| 1028 | 1028 |
} else {
|
| 1029 | 1029 |
_cap[i] = _upper[i] < INF + c ? _upper[i] - c : INF; |
| 1030 | 1030 |
} |
| 1031 | 1031 |
_supply[_source[i]] -= c; |
| 1032 | 1032 |
_supply[_target[i]] += c; |
| 1033 | 1033 |
} |
| 1034 | 1034 |
} else {
|
| 1035 | 1035 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1036 | 1036 |
_cap[i] = _upper[i]; |
| 1037 | 1037 |
} |
| 1038 | 1038 |
} |
| 1039 | 1039 |
|
| 1040 | 1040 |
// Initialize artifical cost |
| 1041 | 1041 |
Cost ART_COST; |
| 1042 | 1042 |
if (std::numeric_limits<Cost>::is_exact) {
|
| 1043 | 1043 |
ART_COST = std::numeric_limits<Cost>::max() / 2 + 1; |
| 1044 | 1044 |
} else {
|
| 1045 |
ART_COST = |
|
| 1045 |
ART_COST = 0; |
|
| 1046 | 1046 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1047 | 1047 |
if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
| 1048 | 1048 |
} |
| 1049 | 1049 |
ART_COST = (ART_COST + 1) * _node_num; |
| 1050 | 1050 |
} |
| 1051 | 1051 |
|
| 1052 | 1052 |
// Initialize arc maps |
| 1053 | 1053 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1054 | 1054 |
_flow[i] = 0; |
| 1055 | 1055 |
_state[i] = STATE_LOWER; |
| 1056 | 1056 |
} |
| 1057 | 1057 |
|
| 1058 | 1058 |
// Set data for the artificial root node |
| 1059 | 1059 |
_root = _node_num; |
| 1060 | 1060 |
_parent[_root] = -1; |
| 1061 | 1061 |
_pred[_root] = -1; |
| 1062 | 1062 |
_thread[_root] = 0; |
| 1063 | 1063 |
_rev_thread[0] = _root; |
| 1064 | 1064 |
_succ_num[_root] = _node_num + 1; |
| 1065 | 1065 |
_last_succ[_root] = _root - 1; |
| 1066 | 1066 |
_supply[_root] = -_sum_supply; |
| 1067 | 1067 |
_pi[_root] = 0; |
| 1068 | 1068 |
|
| 1069 | 1069 |
// Add artificial arcs and initialize the spanning tree data structure |
| 1070 | 1070 |
if (_sum_supply == 0) {
|
| 1071 | 1071 |
// EQ supply constraints |
| 1072 | 1072 |
_search_arc_num = _arc_num; |
| 1073 | 1073 |
_all_arc_num = _arc_num + _node_num; |
| 1074 | 1074 |
for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
|
| 1075 | 1075 |
_parent[u] = _root; |
| 1076 | 1076 |
_pred[u] = e; |
| 1077 | 1077 |
_thread[u] = u + 1; |
| ... | ... |
@@ -1428,62 +1428,62 @@ |
| 1428 | 1428 |
while (pivot.findEnteringArc()) {
|
| 1429 | 1429 |
findJoinNode(); |
| 1430 | 1430 |
bool change = findLeavingArc(); |
| 1431 | 1431 |
if (delta >= INF) return UNBOUNDED; |
| 1432 | 1432 |
changeFlow(change); |
| 1433 | 1433 |
if (change) {
|
| 1434 | 1434 |
updateTreeStructure(); |
| 1435 | 1435 |
updatePotential(); |
| 1436 | 1436 |
} |
| 1437 | 1437 |
} |
| 1438 | 1438 |
|
| 1439 | 1439 |
// Check feasibility |
| 1440 | 1440 |
for (int e = _search_arc_num; e != _all_arc_num; ++e) {
|
| 1441 | 1441 |
if (_flow[e] != 0) return INFEASIBLE; |
| 1442 | 1442 |
} |
| 1443 | 1443 |
|
| 1444 | 1444 |
// Transform the solution and the supply map to the original form |
| 1445 | 1445 |
if (_have_lower) {
|
| 1446 | 1446 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1447 | 1447 |
Value c = _lower[i]; |
| 1448 | 1448 |
if (c != 0) {
|
| 1449 | 1449 |
_flow[i] += c; |
| 1450 | 1450 |
_supply[_source[i]] += c; |
| 1451 | 1451 |
_supply[_target[i]] -= c; |
| 1452 | 1452 |
} |
| 1453 | 1453 |
} |
| 1454 | 1454 |
} |
| 1455 | 1455 |
|
| 1456 | 1456 |
// Shift potentials to meet the requirements of the GEQ/LEQ type |
| 1457 | 1457 |
// optimality conditions |
| 1458 | 1458 |
if (_sum_supply == 0) {
|
| 1459 | 1459 |
if (_stype == GEQ) {
|
| 1460 |
Cost max_pot = std::numeric_limits<Cost>:: |
|
| 1460 |
Cost max_pot = -std::numeric_limits<Cost>::max(); |
|
| 1461 | 1461 |
for (int i = 0; i != _node_num; ++i) {
|
| 1462 | 1462 |
if (_pi[i] > max_pot) max_pot = _pi[i]; |
| 1463 | 1463 |
} |
| 1464 | 1464 |
if (max_pot > 0) {
|
| 1465 | 1465 |
for (int i = 0; i != _node_num; ++i) |
| 1466 | 1466 |
_pi[i] -= max_pot; |
| 1467 | 1467 |
} |
| 1468 | 1468 |
} else {
|
| 1469 | 1469 |
Cost min_pot = std::numeric_limits<Cost>::max(); |
| 1470 | 1470 |
for (int i = 0; i != _node_num; ++i) {
|
| 1471 | 1471 |
if (_pi[i] < min_pot) min_pot = _pi[i]; |
| 1472 | 1472 |
} |
| 1473 | 1473 |
if (min_pot < 0) {
|
| 1474 | 1474 |
for (int i = 0; i != _node_num; ++i) |
| 1475 | 1475 |
_pi[i] -= min_pot; |
| 1476 | 1476 |
} |
| 1477 | 1477 |
} |
| 1478 | 1478 |
} |
| 1479 | 1479 |
|
| 1480 | 1480 |
return OPTIMAL; |
| 1481 | 1481 |
} |
| 1482 | 1482 |
|
| 1483 | 1483 |
}; //class NetworkSimplex |
| 1484 | 1484 |
|
| 1485 | 1485 |
///@} |
| 1486 | 1486 |
|
| 1487 | 1487 |
} //namespace lemon |
| 1488 | 1488 |
|
| 1489 | 1489 |
#endif //LEMON_NETWORK_SIMPLEX_H |
0 comments (0 inline)