我是AMPL和做MC问题的新手。我已经包括了我的代码。我之前要求帮助解决其他错误,此时程序正在工作,但最优解是0,并给出一个奇怪的错误。什么是问题?也许我的约束条件错了?
错误:
执行"解决"错误;命令:
错误处理约束c2[1,1]:
无效下标流[1,4,1]
代码:
set NODES; #define set of nodes
set ARCS within {NODES, NODES};
set COMMODITIES;
param demand {COMMODITIES};
param origin {COMMODITIES};
param dest {COMMODITIES};
param cap{ARCS} >= 0;
param cost{ARCS} >= 0;
# Decision Variables
var flow{(u,v,k) in ARCS cross COMMODITIES}; #flow assos with commodity k
var x{(u,v,k) in ARCS cross COMMODITIES} binary; #fraction of flow assos with commodity k
# Objective Function
minimize Total_Cost:
sum {(u,v) in ARCS, k in COMMODITIES}
cost [u,v] *(flow[u,v,k]*x[u,v,k]);
# Capacity constraints
subject to c1 {(u,v) in ARCS}:
sum{(u,v,k) in ARCS cross COMMODITIES}
(flow[u,v,k]*x[u,v,k]) <= cap[u,v];
#Flow conservation constraints
subject to c2 {k in COMMODITIES, u in NODES diff {origin[k], dest[k]}}:
sum{(v,u) in ARCS} (flow[u,v,k]*x[v,u,k]) - sum{(u,v) in ARCS} (flow[u,v,k]*x[u,v,k]) = 0;
# Source and sink constraints
subject to c3{k in COMMODITIES, u in NODES diff {origin[k],dest[k]}}:
sum{(u,v) in ARCS} flow[u,v,k] - sum{(v,u) in ARCS} flow[v,u,k] =
if u = origin[k] then demand[k] else if u = dest[k] then -demand[k] else 0;
# Commodity flow constraints
subject to c4 {(u,v,k) in ARCS cross COMMODITIES}:
flow[u,v,k] <= x[u,v,k] * cap[u,v];
# Non-negativity constraints
subject to c5 {(u,v,k) in ARCS cross COMMODITIES}:
x[u,v,k] >= 0;
data;
set NODES := 1 2 3 4 5 6;
param: ARCS: cost cap :=
1,2 3 15
2,3 6 1
4,1 8 6
2,5 4 10
2,4 4 10
3,4 3 6
3,6 8 7
5,3 12 4
4,6 10 8 ;
param: COMMODITIES: origin dest demand :=
1 3 5 6
2 1 6 4;
程序没有错误,但最优解是0。我不明白问题出在哪里,哪些约束是错误的。
在约束c2
中,您有以下内容:
sum{(v,u) in ARCS} (flow[u,v,k]*x[v,u,k])
然而,flow
在ARCS上被索引。如果在ARCS中有一个弧(v,u)
,则不能保证也有一个弧(u,v)
(相反方向),在这种情况下,flow[u,v,k]
将是一个无效的下标。