0

我必须计算这个多目标问题的理想向量。我无法访问@objective 中functs_BK1() 的第一个和第二个函数。知道如何使所有动态并支持 n 函数吗?

using JuMP
using Ipopt


function functs_BK1(x::Vector)
    f = zeros(2)
    f[1] = x[1]^2 + x[2]^2
    f[2] = (x[1]-5.0)^2 + (x[2]-5.0)^2

    return f
end

function bounds_BK1()
    return ([-5.0;-5.0],[10.0;10.0])
end


m = Model(solver=IpoptSolver(print_level=0))
@variable(m, x[i=1:2])

@NLexpression(m, F1, functs_BK1()[1]) #<--- Problem is here
@NLexpression(m, F2, functs_BK1()[2]) #<--- here

@constraint(m, x .>= bounds_BK1()[1])
@constraint(m, x .<= bounds_BK1()[2])
s=solve(m)
4

1 回答 1

1

在您的函数functs_BK1中,您已定义f为一个Float64值数组,而@NLexpression宏必须从 JuMP 变量构建所需的表达式作为其第三个参数。如非线性表达式的JuMP 文档中所述,所有非线性表达式都必须在@NLexpression宏内部定义,并且AffExpr当前QuadExpr不能在@NLexpression宏内部使用对象。

以下命令集导致通过solve操作找到解决方案:

julia> using JuMP

julia> using Ipopt

julia> m = Model(solver=IpoptSolver(print_level=0))
Feasibility problem with:
 * 0 linear constraints
 * 0 variables
Solver is Ipopt

julia> @variable(m, x[i=1:2])
2-element Array{JuMP.Variable,1}:
 x[1]
 x[2]

julia> function bounds_BK1()
           return ([-5.0;-5.0],[10.0;10.0])
       end
bounds_BK1 (generic function with 1 method)

julia> @NLexpression(m, F1, x[1]^2 + x[2]^2)
JuMP.NonlinearExpression(Feasibility problem with:
 * 0 linear constraints
 * 2 variables
Solver is Ipopt,1)

julia> @NLexpression(m, F2, (x[1]-5.0)^2 + (x[2]-5.0)^2)
JuMP.NonlinearExpression(Feasibility problem with:
 * 0 linear constraints
 * 2 variables
Solver is Ipopt,2)

julia> @constraint(m, x .>= bounds_BK1()[1])
2-element Array{JuMP.ConstraintRef{JuMP.Model,JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,JuMP.Variable}}},1}:
 x[1] ≥ -5
 x[2] ≥ -5

julia> @constraint(m, x .<= bounds_BK1()[2])
2-element Array{JuMP.ConstraintRef{JuMP.Model,JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,JuMP.Variable}}},1}:
 x[1] ≤ 10
 x[2] ≤ 10

julia> s = solve(m)

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

:Optimal

julia> getvalue(x)
2-element Array{Float64,1}:
 0.261454
 0.261454

我不相信您目前可以在您的内部构建一组表达式functs_BK1,然后将其传递给@NLexpression. 目前,需要将 JuMP 变量的标量表达式作为参数直接传递给@NLexpression.

于 2016-08-30T15:50:18.400 回答