| ... | ... |
@@ -856,386 +856,386 @@ |
| 856 | 856 |
/// @} |
| 857 | 857 |
|
| 858 | 858 |
/// \name Execution Control |
| 859 | 859 |
/// The algorithm can be executed using \ref run(). |
| 860 | 860 |
|
| 861 | 861 |
/// @{
|
| 862 | 862 |
|
| 863 | 863 |
/// \brief Run the algorithm. |
| 864 | 864 |
/// |
| 865 | 865 |
/// This function runs the algorithm. |
| 866 | 866 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 867 | 867 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), |
| 868 | 868 |
/// \ref supplyType(). |
| 869 | 869 |
/// For example, |
| 870 | 870 |
/// \code |
| 871 | 871 |
/// NetworkSimplex<ListDigraph> ns(graph); |
| 872 | 872 |
/// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
| 873 | 873 |
/// .supplyMap(sup).run(); |
| 874 | 874 |
/// \endcode |
| 875 | 875 |
/// |
| 876 | 876 |
/// This function can be called more than once. All the parameters |
| 877 | 877 |
/// that have been given are kept for the next call, unless |
| 878 | 878 |
/// \ref reset() is called, thus only the modified parameters |
| 879 | 879 |
/// have to be set again. See \ref reset() for examples. |
| 880 | 880 |
/// However the underlying digraph must not be modified after this |
| 881 | 881 |
/// class have been constructed, since it copies and extends the graph. |
| 882 | 882 |
/// |
| 883 | 883 |
/// \param pivot_rule The pivot rule that will be used during the |
| 884 | 884 |
/// algorithm. For more information see \ref PivotRule. |
| 885 | 885 |
/// |
| 886 | 886 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 887 | 887 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 888 | 888 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 889 | 889 |
/// optimal flow and node potentials (primal and dual solutions), |
| 890 | 890 |
/// \n \c UNBOUNDED if the objective function of the problem is |
| 891 | 891 |
/// unbounded, i.e. there is a directed cycle having negative total |
| 892 | 892 |
/// cost and infinite upper bound. |
| 893 | 893 |
/// |
| 894 | 894 |
/// \see ProblemType, PivotRule |
| 895 | 895 |
ProblemType run(PivotRule pivot_rule = BLOCK_SEARCH) {
|
| 896 | 896 |
if (!init()) return INFEASIBLE; |
| 897 | 897 |
return start(pivot_rule); |
| 898 | 898 |
} |
| 899 | 899 |
|
| 900 | 900 |
/// \brief Reset all the parameters that have been given before. |
| 901 | 901 |
/// |
| 902 | 902 |
/// This function resets all the paramaters that have been given |
| 903 | 903 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 904 | 904 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(), \ref supplyType(). |
| 905 | 905 |
/// |
| 906 | 906 |
/// It is useful for multiple run() calls. If this function is not |
| 907 | 907 |
/// used, all the parameters given before are kept for the next |
| 908 | 908 |
/// \ref run() call. |
| 909 | 909 |
/// However the underlying digraph must not be modified after this |
| 910 | 910 |
/// class have been constructed, since it copies and extends the graph. |
| 911 | 911 |
/// |
| 912 | 912 |
/// For example, |
| 913 | 913 |
/// \code |
| 914 | 914 |
/// NetworkSimplex<ListDigraph> ns(graph); |
| 915 | 915 |
/// |
| 916 | 916 |
/// // First run |
| 917 | 917 |
/// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
| 918 | 918 |
/// .supplyMap(sup).run(); |
| 919 | 919 |
/// |
| 920 | 920 |
/// // Run again with modified cost map (reset() is not called, |
| 921 | 921 |
/// // so only the cost map have to be set again) |
| 922 | 922 |
/// cost[e] += 100; |
| 923 | 923 |
/// ns.costMap(cost).run(); |
| 924 | 924 |
/// |
| 925 | 925 |
/// // Run again from scratch using reset() |
| 926 | 926 |
/// // (the lower bounds will be set to zero on all arcs) |
| 927 | 927 |
/// ns.reset(); |
| 928 | 928 |
/// ns.upperMap(capacity).costMap(cost) |
| 929 | 929 |
/// .supplyMap(sup).run(); |
| 930 | 930 |
/// \endcode |
| 931 | 931 |
/// |
| 932 | 932 |
/// \return <tt>(*this)</tt> |
| 933 | 933 |
NetworkSimplex& reset() {
|
| 934 | 934 |
for (int i = 0; i != _node_num; ++i) {
|
| 935 | 935 |
_supply[i] = 0; |
| 936 | 936 |
} |
| 937 | 937 |
for (int i = 0; i != _arc_num; ++i) {
|
| 938 | 938 |
_lower[i] = 0; |
| 939 | 939 |
_upper[i] = INF; |
| 940 | 940 |
_cost[i] = 1; |
| 941 | 941 |
} |
| 942 | 942 |
_have_lower = false; |
| 943 | 943 |
_stype = GEQ; |
| 944 | 944 |
return *this; |
| 945 | 945 |
} |
| 946 | 946 |
|
| 947 | 947 |
/// @} |
| 948 | 948 |
|
| 949 | 949 |
/// \name Query Functions |
| 950 | 950 |
/// The results of the algorithm can be obtained using these |
| 951 | 951 |
/// functions.\n |
| 952 | 952 |
/// The \ref run() function must be called before using them. |
| 953 | 953 |
|
| 954 | 954 |
/// @{
|
| 955 | 955 |
|
| 956 | 956 |
/// \brief Return the total cost of the found flow. |
| 957 | 957 |
/// |
| 958 | 958 |
/// This function returns the total cost of the found flow. |
| 959 | 959 |
/// Its complexity is O(e). |
| 960 | 960 |
/// |
| 961 | 961 |
/// \note The return type of the function can be specified as a |
| 962 | 962 |
/// template parameter. For example, |
| 963 | 963 |
/// \code |
| 964 | 964 |
/// ns.totalCost<double>(); |
| 965 | 965 |
/// \endcode |
| 966 | 966 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 967 | 967 |
/// type of the algorithm, which is the default return type of the |
| 968 | 968 |
/// function. |
| 969 | 969 |
/// |
| 970 | 970 |
/// \pre \ref run() must be called before using this function. |
| 971 | 971 |
template <typename Number> |
| 972 | 972 |
Number totalCost() const {
|
| 973 | 973 |
Number c = 0; |
| 974 | 974 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 975 | 975 |
int i = _arc_id[a]; |
| 976 | 976 |
c += Number(_flow[i]) * Number(_cost[i]); |
| 977 | 977 |
} |
| 978 | 978 |
return c; |
| 979 | 979 |
} |
| 980 | 980 |
|
| 981 | 981 |
#ifndef DOXYGEN |
| 982 | 982 |
Cost totalCost() const {
|
| 983 | 983 |
return totalCost<Cost>(); |
| 984 | 984 |
} |
| 985 | 985 |
#endif |
| 986 | 986 |
|
| 987 | 987 |
/// \brief Return the flow on the given arc. |
| 988 | 988 |
/// |
| 989 | 989 |
/// This function returns the flow on the given arc. |
| 990 | 990 |
/// |
| 991 | 991 |
/// \pre \ref run() must be called before using this function. |
| 992 | 992 |
Value flow(const Arc& a) const {
|
| 993 | 993 |
return _flow[_arc_id[a]]; |
| 994 | 994 |
} |
| 995 | 995 |
|
| 996 | 996 |
/// \brief Return the flow map (the primal solution). |
| 997 | 997 |
/// |
| 998 | 998 |
/// This function copies the flow value on each arc into the given |
| 999 | 999 |
/// map. The \c Value type of the algorithm must be convertible to |
| 1000 | 1000 |
/// the \c Value type of the map. |
| 1001 | 1001 |
/// |
| 1002 | 1002 |
/// \pre \ref run() must be called before using this function. |
| 1003 | 1003 |
template <typename FlowMap> |
| 1004 | 1004 |
void flowMap(FlowMap &map) const {
|
| 1005 | 1005 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 1006 | 1006 |
map.set(a, _flow[_arc_id[a]]); |
| 1007 | 1007 |
} |
| 1008 | 1008 |
} |
| 1009 | 1009 |
|
| 1010 | 1010 |
/// \brief Return the potential (dual value) of the given node. |
| 1011 | 1011 |
/// |
| 1012 | 1012 |
/// This function returns the potential (dual value) of the |
| 1013 | 1013 |
/// given node. |
| 1014 | 1014 |
/// |
| 1015 | 1015 |
/// \pre \ref run() must be called before using this function. |
| 1016 | 1016 |
Cost potential(const Node& n) const {
|
| 1017 | 1017 |
return _pi[_node_id[n]]; |
| 1018 | 1018 |
} |
| 1019 | 1019 |
|
| 1020 | 1020 |
/// \brief Return the potential map (the dual solution). |
| 1021 | 1021 |
/// |
| 1022 | 1022 |
/// This function copies the potential (dual value) of each node |
| 1023 | 1023 |
/// into the given map. |
| 1024 | 1024 |
/// The \c Cost type of the algorithm must be convertible to the |
| 1025 | 1025 |
/// \c Value type of the map. |
| 1026 | 1026 |
/// |
| 1027 | 1027 |
/// \pre \ref run() must be called before using this function. |
| 1028 | 1028 |
template <typename PotentialMap> |
| 1029 | 1029 |
void potentialMap(PotentialMap &map) const {
|
| 1030 | 1030 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1031 | 1031 |
map.set(n, _pi[_node_id[n]]); |
| 1032 | 1032 |
} |
| 1033 | 1033 |
} |
| 1034 | 1034 |
|
| 1035 | 1035 |
/// @} |
| 1036 | 1036 |
|
| 1037 | 1037 |
private: |
| 1038 | 1038 |
|
| 1039 | 1039 |
// Initialize internal data structures |
| 1040 | 1040 |
bool init() {
|
| 1041 | 1041 |
if (_node_num == 0) return false; |
| 1042 | 1042 |
|
| 1043 | 1043 |
// Check the sum of supply values |
| 1044 | 1044 |
_sum_supply = 0; |
| 1045 | 1045 |
for (int i = 0; i != _node_num; ++i) {
|
| 1046 | 1046 |
_sum_supply += _supply[i]; |
| 1047 | 1047 |
} |
| 1048 |
if ( !(_stype == GEQ && _sum_supply <= 0 || |
|
| 1049 |
_stype == LEQ && _sum_supply >= 0) ) return false; |
|
| 1048 |
if ( !((_stype == GEQ && _sum_supply <= 0) || |
|
| 1049 |
(_stype == LEQ && _sum_supply >= 0)) ) return false; |
|
| 1050 | 1050 |
|
| 1051 | 1051 |
// Remove non-zero lower bounds |
| 1052 | 1052 |
if (_have_lower) {
|
| 1053 | 1053 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1054 | 1054 |
Value c = _lower[i]; |
| 1055 | 1055 |
if (c >= 0) {
|
| 1056 | 1056 |
_cap[i] = _upper[i] < INF ? _upper[i] - c : INF; |
| 1057 | 1057 |
} else {
|
| 1058 | 1058 |
_cap[i] = _upper[i] < INF + c ? _upper[i] - c : INF; |
| 1059 | 1059 |
} |
| 1060 | 1060 |
_supply[_source[i]] -= c; |
| 1061 | 1061 |
_supply[_target[i]] += c; |
| 1062 | 1062 |
} |
| 1063 | 1063 |
} else {
|
| 1064 | 1064 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1065 | 1065 |
_cap[i] = _upper[i]; |
| 1066 | 1066 |
} |
| 1067 | 1067 |
} |
| 1068 | 1068 |
|
| 1069 | 1069 |
// Initialize artifical cost |
| 1070 | 1070 |
Cost ART_COST; |
| 1071 | 1071 |
if (std::numeric_limits<Cost>::is_exact) {
|
| 1072 | 1072 |
ART_COST = std::numeric_limits<Cost>::max() / 4 + 1; |
| 1073 | 1073 |
} else {
|
| 1074 | 1074 |
ART_COST = std::numeric_limits<Cost>::min(); |
| 1075 | 1075 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1076 | 1076 |
if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
| 1077 | 1077 |
} |
| 1078 | 1078 |
ART_COST = (ART_COST + 1) * _node_num; |
| 1079 | 1079 |
} |
| 1080 | 1080 |
|
| 1081 | 1081 |
// Initialize arc maps |
| 1082 | 1082 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1083 | 1083 |
_flow[i] = 0; |
| 1084 | 1084 |
_state[i] = STATE_LOWER; |
| 1085 | 1085 |
} |
| 1086 | 1086 |
|
| 1087 | 1087 |
// Set data for the artificial root node |
| 1088 | 1088 |
_root = _node_num; |
| 1089 | 1089 |
_parent[_root] = -1; |
| 1090 | 1090 |
_pred[_root] = -1; |
| 1091 | 1091 |
_thread[_root] = 0; |
| 1092 | 1092 |
_rev_thread[0] = _root; |
| 1093 | 1093 |
_succ_num[_root] = _node_num + 1; |
| 1094 | 1094 |
_last_succ[_root] = _root - 1; |
| 1095 | 1095 |
_supply[_root] = -_sum_supply; |
| 1096 | 1096 |
_pi[_root] = _sum_supply < 0 ? -ART_COST : ART_COST; |
| 1097 | 1097 |
|
| 1098 | 1098 |
// Add artificial arcs and initialize the spanning tree data structure |
| 1099 | 1099 |
for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
|
| 1100 | 1100 |
_parent[u] = _root; |
| 1101 | 1101 |
_pred[u] = e; |
| 1102 | 1102 |
_thread[u] = u + 1; |
| 1103 | 1103 |
_rev_thread[u + 1] = u; |
| 1104 | 1104 |
_succ_num[u] = 1; |
| 1105 | 1105 |
_last_succ[u] = u; |
| 1106 | 1106 |
_cost[e] = ART_COST; |
| 1107 | 1107 |
_cap[e] = INF; |
| 1108 | 1108 |
_state[e] = STATE_TREE; |
| 1109 | 1109 |
if (_supply[u] > 0 || (_supply[u] == 0 && _sum_supply <= 0)) {
|
| 1110 | 1110 |
_flow[e] = _supply[u]; |
| 1111 | 1111 |
_forward[u] = true; |
| 1112 | 1112 |
_pi[u] = -ART_COST + _pi[_root]; |
| 1113 | 1113 |
} else {
|
| 1114 | 1114 |
_flow[e] = -_supply[u]; |
| 1115 | 1115 |
_forward[u] = false; |
| 1116 | 1116 |
_pi[u] = ART_COST + _pi[_root]; |
| 1117 | 1117 |
} |
| 1118 | 1118 |
} |
| 1119 | 1119 |
|
| 1120 | 1120 |
return true; |
| 1121 | 1121 |
} |
| 1122 | 1122 |
|
| 1123 | 1123 |
// Find the join node |
| 1124 | 1124 |
void findJoinNode() {
|
| 1125 | 1125 |
int u = _source[in_arc]; |
| 1126 | 1126 |
int v = _target[in_arc]; |
| 1127 | 1127 |
while (u != v) {
|
| 1128 | 1128 |
if (_succ_num[u] < _succ_num[v]) {
|
| 1129 | 1129 |
u = _parent[u]; |
| 1130 | 1130 |
} else {
|
| 1131 | 1131 |
v = _parent[v]; |
| 1132 | 1132 |
} |
| 1133 | 1133 |
} |
| 1134 | 1134 |
join = u; |
| 1135 | 1135 |
} |
| 1136 | 1136 |
|
| 1137 | 1137 |
// Find the leaving arc of the cycle and returns true if the |
| 1138 | 1138 |
// leaving arc is not the same as the entering arc |
| 1139 | 1139 |
bool findLeavingArc() {
|
| 1140 | 1140 |
// Initialize first and second nodes according to the direction |
| 1141 | 1141 |
// of the cycle |
| 1142 | 1142 |
if (_state[in_arc] == STATE_LOWER) {
|
| 1143 | 1143 |
first = _source[in_arc]; |
| 1144 | 1144 |
second = _target[in_arc]; |
| 1145 | 1145 |
} else {
|
| 1146 | 1146 |
first = _target[in_arc]; |
| 1147 | 1147 |
second = _source[in_arc]; |
| 1148 | 1148 |
} |
| 1149 | 1149 |
delta = _cap[in_arc]; |
| 1150 | 1150 |
int result = 0; |
| 1151 | 1151 |
Value d; |
| 1152 | 1152 |
int e; |
| 1153 | 1153 |
|
| 1154 | 1154 |
// Search the cycle along the path form the first node to the root |
| 1155 | 1155 |
for (int u = first; u != join; u = _parent[u]) {
|
| 1156 | 1156 |
e = _pred[u]; |
| 1157 | 1157 |
d = _forward[u] ? |
| 1158 | 1158 |
_flow[e] : (_cap[e] == INF ? INF : _cap[e] - _flow[e]); |
| 1159 | 1159 |
if (d < delta) {
|
| 1160 | 1160 |
delta = d; |
| 1161 | 1161 |
u_out = u; |
| 1162 | 1162 |
result = 1; |
| 1163 | 1163 |
} |
| 1164 | 1164 |
} |
| 1165 | 1165 |
// Search the cycle along the path form the second node to the root |
| 1166 | 1166 |
for (int u = second; u != join; u = _parent[u]) {
|
| 1167 | 1167 |
e = _pred[u]; |
| 1168 | 1168 |
d = _forward[u] ? |
| 1169 | 1169 |
(_cap[e] == INF ? INF : _cap[e] - _flow[e]) : _flow[e]; |
| 1170 | 1170 |
if (d <= delta) {
|
| 1171 | 1171 |
delta = d; |
| 1172 | 1172 |
u_out = u; |
| 1173 | 1173 |
result = 2; |
| 1174 | 1174 |
} |
| 1175 | 1175 |
} |
| 1176 | 1176 |
|
| 1177 | 1177 |
if (result == 1) {
|
| 1178 | 1178 |
u_in = first; |
| 1179 | 1179 |
v_in = second; |
| 1180 | 1180 |
} else {
|
| 1181 | 1181 |
u_in = second; |
| 1182 | 1182 |
v_in = first; |
| 1183 | 1183 |
} |
| 1184 | 1184 |
return result != 0; |
| 1185 | 1185 |
} |
| 1186 | 1186 |
|
| 1187 | 1187 |
// Change _flow and _state vectors |
| 1188 | 1188 |
void changeFlow(bool change) {
|
| 1189 | 1189 |
// Augment along the cycle |
| 1190 | 1190 |
if (delta > 0) {
|
| 1191 | 1191 |
Value val = _state[in_arc] * delta; |
| 1192 | 1192 |
_flow[in_arc] += val; |
| 1193 | 1193 |
for (int u = _source[in_arc]; u != join; u = _parent[u]) {
|
| 1194 | 1194 |
_flow[_pred[u]] += _forward[u] ? -val : val; |
| 1195 | 1195 |
} |
| 1196 | 1196 |
for (int u = _target[in_arc]; u != join; u = _parent[u]) {
|
| 1197 | 1197 |
_flow[_pred[u]] += _forward[u] ? val : -val; |
| 1198 | 1198 |
} |
| 1199 | 1199 |
} |
| 1200 | 1200 |
// Update the state of the entering and leaving arcs |
| 1201 | 1201 |
if (change) {
|
| 1202 | 1202 |
_state[in_arc] = STATE_TREE; |
| 1203 | 1203 |
_state[_pred[u_out]] = |
| 1204 | 1204 |
(_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER; |
| 1205 | 1205 |
} else {
|
| 1206 | 1206 |
_state[in_arc] = -_state[in_arc]; |
| 1207 | 1207 |
} |
| 1208 | 1208 |
} |
| 1209 | 1209 |
|
| 1210 | 1210 |
// Update the tree structure |
| 1211 | 1211 |
void updateTreeStructure() {
|
| 1212 | 1212 |
int u, w; |
| 1213 | 1213 |
int old_rev_thread = _rev_thread[u_out]; |
| 1214 | 1214 |
int old_succ_num = _succ_num[u_out]; |
| 1215 | 1215 |
int old_last_succ = _last_succ[u_out]; |
| 1216 | 1216 |
v_out = _parent[u_out]; |
| 1217 | 1217 |
|
| 1218 | 1218 |
u = _last_succ[u_in]; // the last successor of u_in |
| 1219 | 1219 |
right = _thread[u]; // the node after it |
| 1220 | 1220 |
|
| 1221 | 1221 |
// Handle the case when old_rev_thread equals to v_in |
| 1222 | 1222 |
// (it also means that join and v_out coincide) |
| 1223 | 1223 |
if (old_rev_thread == v_in) {
|
| 1224 | 1224 |
last = _thread[_last_succ[u_out]]; |
| 1225 | 1225 |
} else {
|
| 1226 | 1226 |
last = _thread[v_in]; |
| 1227 | 1227 |
} |
| 1228 | 1228 |
|
| 1229 | 1229 |
// Update _thread and _parent along the stem nodes (i.e. the nodes |
| 1230 | 1230 |
// between u_in and u_out, whose parent have to be changed) |
| 1231 | 1231 |
_thread[v_in] = stem = u_in; |
| 1232 | 1232 |
_dirty_revs.clear(); |
| 1233 | 1233 |
_dirty_revs.push_back(v_in); |
| 1234 | 1234 |
par_stem = v_in; |
| 1235 | 1235 |
while (stem != u_out) {
|
| 1236 | 1236 |
// Insert the next stem node into the thread list |
| 1237 | 1237 |
new_stem = _parent[stem]; |
| 1238 | 1238 |
_thread[u] = new_stem; |
| 1239 | 1239 |
_dirty_revs.push_back(u); |
| 1240 | 1240 |
|
| 1241 | 1241 |
// Remove the subtree of stem from the thread list |
0 comments (0 inline)