如何在Julia+JuMP中定义类AMPL集和参数?
我需要在Julia+JuMP中定义一些常量参数,类似于在AMPL中定义如何在Julia+JuMP中定义类AMPL集和参数?,julia,julia-jump,Julia,Julia Jump,我需要在Julia+JuMP中定义一些常量参数,类似于在AMPL中定义 set A := a0 a1 a2; param p := a0 1 a1 5 a2 10 ; 如何在Julia中定义A和p之类的东西?除了Julia中可用的语法外,JuMP本身并没有为索引集定义特殊的语法。例如,您可以定义 A = [:a0, :a1, :a2] 其中:a0定义了一个符号 如果要在此集合上为变量编制索引,则语法为: m = Model() @variable(m, x[A]) 跳转也不像AMPL那样
set A := a0 a1 a2;
param p :=
a0 1
a1 5
a2 10 ;
如何在Julia中定义A和p之类的东西?除了Julia中可用的语法外,JuMP本身并没有为索引集定义特殊的语法。例如,您可以定义
A = [:a0, :a1, :a2]
其中:a0定义了一个符号
如果要在此集合上为变量编制索引,则语法为:
m = Model()
@variable(m, x[A])
跳转也不像AMPL那样区分数据和模型,因此没有真正的参数概念。相反,您只需在使用数据时提供数据。如果我正确理解你的问题,你可以这样做
p = Dict(:a0 => 1, :a1 => 5, :a2 => 10)
@constraint(m, sum(p[i]*x[i] for i in A) <= 20)
其中foo是一个任意的Julia函数,可以执行数据库查找、计算pi的位数等。跳转本身没有为索引集定义特殊语法,超出了Julia中可用的语法。例如,您可以定义
A = [:a0, :a1, :a2]
其中:a0定义了一个符号
如果要在此集合上为变量编制索引,则语法为:
m = Model()
@variable(m, x[A])
跳转也不像AMPL那样区分数据和模型,因此没有真正的参数概念。相反,您只需在使用数据时提供数据。如果我正确理解你的问题,你可以这样做
p = Dict(:a0 => 1, :a1 => 5, :a2 => 10)
@constraint(m, sum(p[i]*x[i] for i in A) <= 20)
其中foo是一个任意的Julia函数,可以执行数据库查找、计算pi的位数等。我无法得到@mlubin工作的原始答案。此外,网络上的许多示例都使用基于位置的索引,这让我感觉不太自然,因此我用字典改写了GAMS教程的示例。。感觉更接近gams/ampl集合
#=
Transposition in JuMP of the basic transport model used in the GAMS tutorial
This problem finds a least cost shipping schedule that meets requirements at markets and supplies at factories.
- Original formulation: Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions.
Princeton University Press, Princeton, New Jersey, 1963.
- Gams implementation: This formulation is described in detail in:
Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide.
The Scientific Press, Redwood City, California, 1988.
- JuMP implementation: Antonello Lobianco
=#
using JuMP, DataFrames
# Sets
plants = ["seattle","san_diego"] # canning plants
markets = ["new_york","chicago","topeka"] # markets
# Parameters
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,
)
# distance in thousands of miles
d_table = wsv"""
plants new_york chicago topeka
seattle 2.5 1.7 1.8
san_diego 2.5 1.8 1.4
"""
d = Dict( (r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table), m in markets)
f = 90 # freight in dollars per case per thousand miles
c = Dict() # transport cost in thousands of dollars per case ;
[ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets]
# Model declaration
trmodel = Model() # transport model
# 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
# Objective
@objective trmodel Min begin
sum(c[p,m]*x[p,m] for p in plants, m in markets)
end
print(trmodel)
status = solve(trmodel)
if status == :Optimal
println("Objective value: ", getobjectivevalue(trmodel))
println("Shipped quantities: ")
println(getvalue(x))
println("Shadow prices of supply:")
[println("$p = $(getdual(supply[p]))") for p in plants]
println("Shadow prices of demand:")
[println("$m = $(getdual(demand[m]))") for m in markets]
else
println("Model didn't solved")
println(status)
end
# Expected result:
# obj= 153.675
#['seattle','new-york'] = 50
#['seattle','chicago'] = 300
#['seattle','topeka'] = 0
#['san-diego','new-york'] = 275
#['san-diego','chicago'] = 0
#['san-diego','topeka'] = 275
我的网站上有一个评论更多的版本。我无法得到@mlubin工作的原始答案。此外,网络上的许多示例都使用基于位置的索引,这让我感觉不太自然,因此我用字典改写了GAMS教程的示例。。感觉更接近gams/ampl集合
#=
Transposition in JuMP of the basic transport model used in the GAMS tutorial
This problem finds a least cost shipping schedule that meets requirements at markets and supplies at factories.
- Original formulation: Dantzig, G B, Chapter 3.3. In Linear Programming and Extensions.
Princeton University Press, Princeton, New Jersey, 1963.
- Gams implementation: This formulation is described in detail in:
Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide.
The Scientific Press, Redwood City, California, 1988.
- JuMP implementation: Antonello Lobianco
=#
using JuMP, DataFrames
# Sets
plants = ["seattle","san_diego"] # canning plants
markets = ["new_york","chicago","topeka"] # markets
# Parameters
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,
)
# distance in thousands of miles
d_table = wsv"""
plants new_york chicago topeka
seattle 2.5 1.7 1.8
san_diego 2.5 1.8 1.4
"""
d = Dict( (r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table), m in markets)
f = 90 # freight in dollars per case per thousand miles
c = Dict() # transport cost in thousands of dollars per case ;
[ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets]
# Model declaration
trmodel = Model() # transport model
# 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
# Objective
@objective trmodel Min begin
sum(c[p,m]*x[p,m] for p in plants, m in markets)
end
print(trmodel)
status = solve(trmodel)
if status == :Optimal
println("Objective value: ", getobjectivevalue(trmodel))
println("Shipped quantities: ")
println(getvalue(x))
println("Shadow prices of supply:")
[println("$p = $(getdual(supply[p]))") for p in plants]
println("Shadow prices of demand:")
[println("$m = $(getdual(demand[m]))") for m in markets]
else
println("Model didn't solved")
println(status)
end
# Expected result:
# obj= 153.675
#['seattle','new-york'] = 50
#['seattle','chicago'] = 300
#['seattle','topeka'] = 0
#['san-diego','new-york'] = 275
#['san-diego','chicago'] = 0
#['san-diego','topeka'] = 275
my上提供了更多评论版本。此答案非常有用。无论如何,我认为数据不应该在代码中。我看到两种解决方案:1如果可能,从文件中读取常量。2通过脚本生成代码,该脚本填充从xml读取的数据。从文件中读取数据当然是合理的。JuMP的理念是让用户决定如何构造输入,而不是强加某些文件格式。例如,在上面,您可以使用Julia的本机I/O函数或软件包从文件中填充p。非常感谢,我尝试搜索有关它的内容。@mlubin您如何继续定义目标?我在A中尝试了@objectivem,Max,sumx[I]+p[I],但没有成功。实际上,Julia 0.5在编写CONSTRAINT语句时给了我一个错误:LoadError:不支持类型为元组{Symbol}的索引数组{Pair{Symbol,Int64},1}。我已经更正了p定义中的一个错误。代码现在应该可以工作了。这个答案非常有用。无论如何,我认为数据不应该在代码中。我看到两种解决方案:1如果可能,从文件中读取常量。2通过脚本生成代码,该脚本填充从xml读取的数据。从文件中读取数据当然是合理的。JuMP的理念是让用户决定如何构造输入,而不是强加某些文件格式。例如,在上面,您可以使用Julia的本机I/O函数或软件包从文件中填充p。非常感谢,我尝试搜索有关它的内容。@mlubin您如何继续定义目标?我在A中尝试了@objectivem,Max,sumx[I]+p[I],但没有成功。实际上,Julia 0.5在编写CONSTRAINT语句时给了我一个错误:LoadError:不支持类型为元组{Symbol}的索引数组{Pair{Symbol,Int64},1}。我已经更正了p定义中的一个错误。代码现在应该可以工作了。