【问题标题】:Matrix-vector product in JuMP objective function, where the matrix is a variableJuMP目标函数中的矩阵向​​量积,其中矩阵是一个变量
【发布时间】:2020-07-28 07:43:15
【问题描述】:

我正在使用 JuMP 在 Julia 中编写优化程序。我的 Julia 版本是 1.3.1,JuMP 版本是 0.21.2。

我的一个变量是一个矩阵,在我的例子中这是一个方便的结构。

using JuMP
using LinearAlgebra

c1H = Vector(1:3)

model = Model()
@variable(model, GiH[1:10, 1:3] >= 0)

test = rand(10,3)

在目标函数中,我将矩阵乘以(参数)向量,然后将结果条目的条目相加。我想这样写:

@objective(model, Min, sum(GiH*c1H))

相当于

@objective(model, Min, ones(10)'*(GiH*c1H))

当我用数字矩阵test 替换变量矩阵时,这运行良好。 但是,我使用可变矩阵 GiH 得到错误

MethodError: no method matching similar(::Array{Float64,1}, ::Type{GenericAffExpr{Float64,VariableRef}}, ::Array{Int64,1})
Closest candidates are:
  similar(::Array{T,1}, ::Type) where T at array.jl:331
  similar(::Array, ::Type, !Matched::Tuple{Vararg{Int64,N}}) where N at array.jl:334
  similar(::AbstractArray, ::Type{T}) where T at abstractarray.jl:626
  ...
*(::JuMP.Containers.DenseAxisArray{VariableRef,2,Tuple{Array{Int64,1},Array{Int64,1}},Tuple{Dict{Int64,Int64},Dict{Int64,Int64}}}, ::Array{Float64,1}) at matmul.jl:51
top-level scope at rewrite.jl:227
top-level scope at macros.jl:762

发生了什么事?好像没有为跳转变量矩阵定义矩阵乘法?

我知道我可以用嵌套的sum(... for ...) 替换这个矩阵乘法,但我想知道是否有可能以不同的方式进行。

【问题讨论】:

    标签: matrix julia multiplication julia-jump objective-function


    【解决方案1】:

    解决办法

    @objective(model, Min, ones(10)'*(GiH*c1H))
    

    为我工作。我正在使用 Julia v1.4 和 JuMP v0.21.2。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-23
      • 2021-03-18
      • 2017-12-19
      • 2016-12-03
      • 1970-01-01
      相关资源
      最近更新 更多