2014-11-23 78 views
2

我有一個我想與NLopt解決以下幾個簡單的問題:NLopt錯誤的茱莉亞Ipopt替代

using JuMP 
using NLopt 

""" 
min_x = x1 * x4* (x1 + x2 + x3) + x3 

s.t. 
x1 * x2 * x3 * x4 >= 25 
x_1^2 + x_2^2 + x_3^2 + x_4^2 = 40 
1 <= x1,x2,x3,x4 <= 5 

starting values: vec(x) = (x1 = 1, x2 = 5, x3 = 5, x4 = 1) 
""" 


tic() 

m = Model(solver=NLoptSolver(algorithm=:LD_MMA)) 


@defVar(m, 1 <= x1 <= 5) 
@defVar(m, 1 <= x2 <= 5) 
@defVar(m, 1 <= x3 <= 5) 
@defVar(m, 1 <= x4 <= 5) 

@setNLObjective(m, Min, x1 * x4 * (x1 + x2 + x3) + x3) 
@addNLConstraint(m, x1^2 + x2^2 + x3^2 + x4^2 == 40) 
@addNLConstraint(m, x1 * x2 * x3 * x4 >= 25) 

setValue(x1, 1) 
setValue(x2, 5) 
setValue(x3, 5) 
setValue(x4, 1) 

status = solve(m) 

println("got ", getObjectiveValue(m), " at ", [getValue(x1),getValue(x2), getValue(x3), getValue(x4)]) 

toc() 

但是我得到的參數錯誤。有沒有什麼辦法可以使這個工作與NLopt,如果不是如何改變這個代碼,以便與其他免費優化器,可以安裝在朱莉婭(也許Ipopt,但不Gurobi)使用它?

+0

能您發佈錯誤消息? – IainDunning 2014-11-23 22:21:07

+0

錯誤:在等式約束中在array.jl:458處推送chk at(NLopt的路徑)中的ArgumentError(「無效的NLopt參數」)! at(NLopt path)while loading(my_file) – Echetlaeus 2014-11-23 22:35:12

回答

1

那麼,我無法使用NLopt來解決問題,而是我設法用Ipopt來解決它。

該解決方案使用Ipopt很簡單。首先,你必須從這個site下載Ipopt(我現在使用Windows版本,我也會嘗試在Linux中),並把它放在路徑中(如果你把它放在路徑中,並轉到命令行並鍵入ipopt它必須顯示沒有錯誤 - 它實際上會顯示ipopt選項)。只需在最後找到最新版本。

然後我sliglty修改,我用這種方式來解釋Ipopt之前提供的代碼:

using JuMP 

using Ipopt 


""" 
The problem that I want to solve has 4 variables and 6 constraints. 
It is the following: 


min_x = x1x4(x1+x2+x3) + x3 


s.t. 

x1*x2*x3*x4 >= 25 
x_1^2 + x_2^2 + x_3^2 + x_4^2 = 40 
1 <= x1,x2,x3,x4 <= 5 


starting values: x0 = (x1 = 1, x2 = 5, x3 = 5, x4 = 1) 
""" 


tic() 


m = Model(solver=IpoptSolver()) 


@defVar(m, 1 <= x1 <= 5) 
@defVar(m, 1 <= x2 <= 5) 
@defVar(m, 1 <= x3 <= 5) 
@defVar(m, 1 <= x4 <= 5) 


@setNLObjective(m, Min, x1 * x4 * (x1 + x2 + x3) + x3) 
@addNLConstraint(m, x1^2 + x2^2 + x3^2 + x4^2 == 40) 
@addNLConstraint(m, x1 * x2 * x3 * x4 >= 25) 


setValue(x1, 1) 
setValue(x2, 5) 
setValue(x3, 5) 
setValue(x4, 1) 

status = solve(m) 

println("got ", getObjectiveValue(m), " at ", [getValue(x1),getValue(x2), 
getValue(x3), getValue(x4)]) 

toc() 

有關求解器等的姓名權的更多信息可以在這裏找到:https://jump.readthedocs.org/en/latest/installation.html#getting-solvers

+3

'Pkg.add(「Ipopt」)'實際上會自動安裝Ipopt並準備好與Julia一起使用。 – IainDunning 2014-11-23 22:21:57

+0

要跟進Iain的評論,Ipopt.jl接口不使用ipopt命令行可執行文件。沒有必要手動設置它。 – mlubin 2014-11-24 23:06:20