【问题标题】:The objective function `GenericQuadExpr{Float64,VariableRef}[0 0 0; 0 0 0; 0 0 0]` is not supported by JuMP目标函数 `GenericQuadExpr{Float64,VariableRef}[0 0 0; 0 0 0; JuMP 不支持 0 0 0]`
【发布时间】:2020-12-11 06:09:45
【问题描述】:

目标函数中包含数组的jump函数如何优化?

我在跳转中有如下约束优化问题:

m = Model(optimizer_with_attributes(Mosek.Optimizer, "QUIET" => false, "INTPNT_CO_TOL_DFEAS" => 1e-7))
N = 2

function Q_r(i::Number, l::Number, r::Number, tau::Float64)
    if i >= r && l >= r
        return 2 * fac1(r, i, l) * fac2(r, i, l, tau)
    else
        return 0.0
    end
end


function Q(i::Number, l::Number, tau::Number)
    elem = 0
    for r in 0:N
        println(penalties[r + 1], Q_r(i, l, r, tau))
        elem += penalties[r + 1] * Q_r(i, l, r, tau)
    end
    return elem
end


Q_mat = Array{Float64, 2}(undef, N+1, N+1)
for i in 1:N+1
    for j in 1:N+1
        Q_mat[i, j] = Q(i, j, 0.0)
    end
end

@variable(m, p)



@objective(m, Min, p'*Q_mat*p)

我想最小化 p'Q_matp,其中 p 是 N+1 的向量。但我收到以下错误:

ERROR: LoadError: The objective function `GenericQuadExpr{Float64,VariableRef}[0 0 0; 0 0 0; 0 0 0]` is not supported by JuMP.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] set_objective_function(::Model, ::Array{GenericQuadExpr{Float64,VariableRef},2}) at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/objective.jl:123
 [3] set_objective(::Model, ::MathOptInterface.OptimizationSense, ::Array{GenericQuadExpr{Float64,VariableRef},2}) at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/objective.jl:128
 [4] top-level scope at /Users/prikshetsharma/.julia/packages/JuMP/qhoVb/src/macros.jl:831
 [5] top-level scope at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:107
 [6] include(::Function, ::Module, ::String) at ./Base.jl:380
 [7] include(::Module, ::String) at ./Base.jl:368
 [8] exec_options(::Base.JLOptions) at ./client.jl:296
 [9] _start() at ./client.jl:506
in expression starting at /Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/trajectory.jl:107

如果不支持这个目标函数怎么做优化?

【问题讨论】:

标签: julia julia-jump


【解决方案1】:

你需要指出p是一个Vector:

julia> N = 3
3

julia> Q = rand(N,N)
3×3 Array{Float64,2}:
 0.340114  0.0884307  0.634788
 0.472685  0.782036   0.977641
 0.797464  0.608125   0.741983

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

julia> @variable(m, pp)
pp

julia> @variable(m, p[1:N])
3-element Array{VariableRef,1}:
 p[1]
 p[2]
 p[3]

julia> pp'*Q*pp
3×3 Array{GenericQuadExpr{Float64,VariableRef},2}:
 0.3401136703186225 pp²  0.08843073433149629 pp²  0.6347879182799103 pp²
 0.4726848249844562 pp²  0.7820361890132028 pp²   0.9776406368494128 pp²
 0.7974640186429511 pp²  0.6081254843767614 pp²   0.7419832389329017 pp²

julia> @objective(m, Min, p'*Q*p)
0.3401136703186225 p[1]² + 0.5611155593159525 p[1]*p[2] + 1.4322519369228615 p[1]*p[3] + 0.7820361890132028 p[2]² + 1.5857661212261742 p[2]*p[3] + 0.7419832389329017 p[3]²

【讨论】:

    猜你喜欢
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多