首页 > 解决方案 > 在 Julia 中使用 MathProgBase 的非线性约束最大化问题

问题描述

目标函数为 f(x,y)=sqrt(x^2+2*y^2-xy),服从 10 > x > 0, 10 > y > 0, x > y。我将找到最大化目标函数的 x 和 y。我需要在 MathProgBase.jl 包中使用非线性模型。https://mathprogbasejl.readthedocs.io/en/latest/nlp.html的教程对我来说很难理解,因为我是初学者。我真的很感谢你的帮助!

标签: julia

解决方案


似乎JuMP 不支持严格的更大/更低的约束(因此大多数求解器引擎也不支持)。

您的问题在 Julia 中的建模将是:

using JuMP, Ipopt

m = Model(with_optimizer(Ipopt.Optimizer, print_level=0))

@variable(m, 0 <= x <= 10, start=1)
@variable(m, 0 <= y <= 10, start=1)

@constraint(m, c1,     y <= x )

@NLobjective(m, Max, sqrt(x^2+2*y^2-x*y))

optimize!(m)

status = termination_status(m)

if (status == MOI.OPTIMAL || status == MOI.LOCALLY_SOLVED || status == MOI.TIME_LIMIT) && has_values(m)
    if (status == MOI.OPTIMAL)
        println("** Problem solved correctly **")
    else
        println("** Problem returned a (possibly suboptimal) solution **")
    end
    println("- Objective value : ", objective_value(m))
    println("- Optimal solutions:")
    println("x: $(value.(x))")
    println("y: $(value.(y))")
else
    println("The model was not solved correctly.")
    println(status)
end

(有关各个步骤的说明,请参见https://lobianco.org/antonello/personal/blog/2017/0203_jump_for_gams_users )

该脚本的结果是:

** Problem returned a (possibly suboptimal) solution **
- Objective value : 14.14213575988668
- Optimal solutions:
x: 10.0
y: 10.0

推荐阅读