如何在JuMP中定义一个集合(JuliaOpt)

时间:2017-11-20 10:49:27

标签: optimization julia julia-jump

它应该很容易,但是我差不多3天在JuMP中定义一个集合。然后,我需要为属于集合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

任何帮助?

2 个答案:

答案 0 :(得分:3)

来自GAMS我最初遇到了同样的问题。

最终,JuMP并没有真正拥有(也不需要)GAMS,AMPL或Pyomo中定义的集合的概念,但它使用核心Julia语言中提供的本机容器。

您可以选择两种方法:您可以使用数组和基于位置的索引(应该更快)或使用字典并使用基于名称的索引(更好地阅读模型)。 我更喜欢后者。

这是在Jump中翻译的GAMS教程transport.gms的摘录(整个例子是here):

# 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

答案 1 :(得分:0)

我应该找到问题的解决方案。这是为集合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