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

Fra*_*nti 0 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
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

Ant*_*llo 5

来自 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
Run Code Online (Sandbox Code Playgroud)