2016-07-12 7 views
0

この多目的問題の理想的なベクトルを計算する必要があります。 @objectiveのfuncts_BK1()の第1と第2の関数にアクセスする方法はできません。 どのようにすべてのダイナミックなn機能をサポートすることができますか?あなたの関数functs_BK1JuMP - MethodError: `getindex`に一致するメソッドがありませんgetindex(:: Function、:: Int64)

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) 

答えて

1

@NLexpressionマクロはその3番目の引数としてジャンプ変数から所望の発現を構築しなければならない一方で、あなたは、Float64値の配列であることをfを定義しています。 JuMP documentation for Nonlinear Expressionsに記載されているように、すべての非線形式は@NLexpressionマクロの内部で定義されていなければなりません。マクロではAffExprQuadExprオブジェクトは現在使用できません。

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に渡す必要があります。現在、Ju33変数のスカラ式を直接引数として@NLexpressionに渡す必要があります。

関連する問題