这应该很容易,但我试图在 JuMP 中定义一个集合几乎是 3 天。然后,我需要为属于集合 O 的每一对 (i', i) 设置我的变量 y。
O={(i,j): t[j] - t[i] + 30 < d[i,j]; i in D, j in K, i !=j }
y[i,j]=0, for each (i,j) in O
有什么帮助吗?
这应该很容易,但我试图在 JuMP 中定义一个集合几乎是 3 天。然后,我需要为属于集合 O 的每一对 (i', i) 设置我的变量 y。
O={(i,j): t[j] - t[i] + 30 < d[i,j]; i in D, j in K, i !=j }
y[i,j]=0, for each (i,j) in O
有什么帮助吗?
来自 GAMS 我最初遇到了同样的问题。
最终,JuMP 并没有(也不需要)GAMS、AMPL 或 Pyomo 中定义的集合概念,但它使用了核心 Julia 语言中可用的本机容器。
您可以选择两种方法:您可以使用数组和基于位置的索引(应该更快)或使用字典并使用基于名称的索引(更好地阅读模型)。我更喜欢后者。
这是在 Jump 中翻译的 GAMS 教程 transport.gms 的摘录(整个示例在这里):
# Define sets #
# Sets
# i canning plants / seattle, san-diego /
# j markets / new-york, chicago, topeka / ;
plants = ["seattle","san_diego"] # canning plants
markets = ["new_york","chicago","topeka"] # markets
# Define parameters #
# Parameters
# a(i) capacity of plant i in cases
# / seattle 350
# san-diego 600 /
a = Dict( # capacity of plant i in cases
"seattle" => 350,
"san_diego" => 600,
)
b = Dict( # demand at market j in cases
"new_york" => 325,
"chicago" => 300,
"topeka" => 275,
)
# Variables
@variables trmodel begin
x[p in plants, m in markets] >= 0 # shipment quantities in cases
end
# Constraints
@constraints trmodel begin
supply[p in plants], # observe supply limit at plant p
sum(x[p,m] for m in markets) <= a[p]
demand[m in markets], # satisfy demand at market m
sum(x[p,m] for p in plants) >= b[m]
end
我应该找到问题的解决方案。这是为集合 O 编写约束的代码:
for i=1:n, j=1:m
if (i != j && t[i] - t[j] + 30 < d[i,j])
@constraint(model, y[i,j] == 0)
end
end