在您的函数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
.