我正在尝试创建一个线性优化模型。我有一套看起来像这样:
si=[1,51,39,400909,1244]
sj=[31,47,5]
此集合中的数字表示代码。我正在尝试循环通过集合以向模型添加约束,但我不想使用它们的值循环通过集合,我想基于它们的索引循环通过集合。
这是我现在的代码:
si=[1,51,39,400909,1244]
sj=[31,47,5]
c= [3 5 2;
4 3 5;
4 5 3;
5 4 3;
3 5 4]
b= [80;
75;
80;
120;
60]
# x_ij >= 0 â i = 1,...,5, j = 1,...,3
@defVar(m, x[i in si,j in sj] >= 0)
@setObjective(m,Min,sum{c[i,j]*x[i,j],i in si, j in sj})
# âj = 1,...,3
for j in sj
@addConstraint(m, sum{x[i,j],i in si} <= 480)
end
for i in si
@addConstraint(m, sum{x[i,j],j in sj} >= b[i])
end
我总是出错,因为集合中的数字太大了。有人知道如何循环索引吗?或者有人有其他方法吗?
我在打印解决方案时也遇到了问题。这是我的代码:
for i in n
for j in p
println("x",i,",",j,"= ", getValue(x[i,j]))
end
end (incorporating Iain Dunning's answer from below)
然而,输出仅读取
Objective value: 1165.0
x5,3= 0.0
你知道如何修复输出,以便我可以读取变量的值吗?