我有以下简单的问题要用NLopt来解决:
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)一起使用?
发布于 2014-11-24 03:21:08
嗯,我无法使用NLopt解决这个问题,但我设法用Ipopt解决了它。
使用Ipopt的解决方案很简单。首先,你必须从这个site下载Ipopt (我现在用的是Windows版本,我也会在Linux上试试),并把它放到路径中(如果你把它放在路径中,进入命令行并输入ipopt,它必须不会显示错误-它实际上会显示ipopt选项)。只需在最后找到最新版本即可。
然后,我巧妙地修改了我之前提供的代码,以便以这种方式说明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
https://stackoverflow.com/questions/27092592
复制相似问题