有没有办法在 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)

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

像这样:

Matrix unknown operation

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

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

感激不尽!

您是在寻找两个数组的逐元素乘积吗?如果是这样,它被表示为 .*,遵循 Julia 中广泛使用的约定,. 表示 "broadcasting",即函数对集合的元素应用:

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