2014-11-23 169 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