首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Julia和Ipopt替代方案中的NLopt错误

Julia和Ipopt替代方案中的NLopt错误
EN

Stack Overflow用户
提问于 2014-11-24 02:23:41
回答 1查看 994关注 0票数 2

我有以下简单的问题要用NLopt来解决:

代码语言:javascript
复制
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上工作,如果没有,这段代码如何改变,以便与其他可以安装在Julia中的免费优化器(可能是Ipopt,但不是Gurobi)一起使用?

EN

回答 1

Stack Overflow用户

发布于 2014-11-24 03:21:08

嗯,我无法使用NLopt解决这个问题,但我设法用Ipopt解决了它。

使用Ipopt的解决方案很简单。首先,你必须从这个site下载Ipopt (我现在用的是Windows版本,我也会在Linux上试试),并把它放到路径中(如果你把它放在路径中,进入命令行并输入ipopt,它必须不会显示错误-它实际上会显示ipopt选项)。只需在最后找到最新版本即可。

然后,我巧妙地修改了我之前提供的代码,以便以这种方式说明Ipopt:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27092592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档