繁体   English   中英

有没有办法将系数矩阵与 Julia JuMP 中的变量矩阵相结合(两个数组的元素乘积)

[英]Is there a way to combine a matrix of coefficients with a matrix of variables in Julia JuMP (element-wise product of two arrays)

我基本上有很多系数,我想将它们与另一个矩阵的变量配对,以节省写出约束的时间。

像这样的东西:

矩阵未知运算

在 Julia 中,我理想地希望能够说这样的话:

@variable(model, x[1:9])
A = collect[1:9]
@constraint(model, A =? x[1:9])

多谢!

您是否正在寻找两个数组的元素乘积? 如果是,则表示为.* ,遵循 Julia 中广泛使用的约定,即. 表示“广播”,即将函数按元素应用于集合:

julia> x = 1 .+ 0.1*rand(3,3)
3×3 Array{Float64,2}:
 1.01642  1.01822  1.08074
 1.01375  1.01617  1.04618
 1.06083  1.09773  1.07278

julia> A = reshape(1:9, 3, 3)
3×3 reshape(::UnitRange{Int64}, 3, 3) with eltype Int64:
 1  4  7
 2  5  8
 3  6  9

julia> A.*x
3×3 Array{Float64,2}:
 1.01642  4.07288  7.56517
 2.0275   5.08086  8.36943
 3.18249  6.58637  9.65506


请注意,广播是一种非常通用的技术,您可以在任何地方使用它。 JuMP 也不例外:

julia> using JuMP
julia> model = JuMP.Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x[1:3,1:3])
3×3 Array{VariableRef,2}:
 x[1,1]  x[1,2]  x[1,3]
 x[2,1]  x[2,2]  x[2,3]
 x[3,1]  x[3,2]  x[3,3]

julia> A = reshape(1:9, 3, 3)
3×3 reshape(::UnitRange{Int64}, 3, 3) with eltype Int64:
 1  4  7
 2  5  8
 3  6  9

       # Note how both * and <= are broadcasted to be applied element-wise
julia> @constraint(model, con, A .* x .<= 1)
3×3 Array{ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.LessThan{Float64}},ScalarShape},2}:
 x[1,1] ≤ 1.0    4 x[1,2] ≤ 1.0  7 x[1,3] ≤ 1.0
 2 x[2,1] ≤ 1.0  5 x[2,2] ≤ 1.0  8 x[2,3] ≤ 1.0
 3 x[3,1] ≤ 1.0  6 x[3,2] ≤ 1.0  9 x[3,3] ≤ 1.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM