【问题标题】:Quadratically constrained MIQP with julia and gurobi使用 julia 和 gurobi 的二次约束 MIQP
【发布时间】:2017-03-04 17:11:05
【问题描述】:

这是试图回答以下问题:https://matheducators.stackexchange.com/questions/11757/small-data-sets-with-integral-sample-standard-deviations

因此,以下代码的目的是查找具有整数标准差的小型数据集的示例。这可以表述为一个二次约束的混合整数二次程序,所以我尝试使用来自 Julia 的 Gurobin。以下是我的代码:

using JuMP  
using Gurobi

m = Model(solver = GurobiSolver() )
@variable(m,  0<= x[1:20] <= 100,  Int)
@variable(m,  Gj,  Int)
@constraint(m,  Gj == sum(x[1:20])/20 )
@variable(m,  Var,  Int)
@constraint(m,  Var == sum( (x[1:20]-Gj).^2/19) )
@variable(m,  sd,  Int)
@constraint(m, sd * sd == Var)
### We need some restrictions to avoid all equal, < or zero,  solutions:
@constraint(m,  sd >= 5)
@objective(m, Min, sd)

print(m)

status = solve(m)

println("Objective value: ", getobjectivevalue(m) )

x = getvalue(x)

运行此结果:

ERROR: Gurobi.GurobiError(10021, "Quadratic equality constraints")
Stacktrace:
 [1] optimize(::Gurobi.Model) at /home/kjetil/.julia/v0.6/Gurobi/src/grb_solve.jl:7
 [2] optimize!(::Gurobi.GurobiMathProgModel) at /home/kjetil/.julia/v0.6/Gurobi/src/GurobiSolverInterface.jl:294
 [3] #solve#101(::Bool, ::Bool, ::Bool, ::Array{Any,1}, ::Function, ::JuMP.Model) at /home/kjetil/.julia/v0.6/JuMP/src/solvers.jl:173
 [4] solve(::JuMP.Model) at /home/kjetil/.julia/v0.6/JuMP/src/solvers.jl:148

有什么想法吗?

【问题讨论】:

    标签: julia gurobi julia-jump


    【解决方案1】:

    像 Gurobi Optimizer 这样的数学编程求解器无法求解具有二次等式约束的模型。 Here are the types of constraints that Gurobi Optimizer can solve。要使用 Gurobi Optimizer 求解模型,您必须将约束转换为其中一种形式,例如二次不等式约束。

    【讨论】:

    • 我不明白:来自here,看来这是可能的,但我得到了同样的错误。我想念什么?另请参阅The optional sense string defines the sense of the quadratic constrint. Allowed values are '&lt;', '=' or '&gt;'. If not present, the default sense is '&lt;'. It is stored in model$quadcon[[i]]$sense. (没有 sense 参数,我的代码会运行...)
    【解决方案2】:

    主要问题是,一般来说,二次等式不是凸的,并且大多数求解器仅适用于凸问题(加上整数约束)。两个二进制变量的乘积很容易线性化(相当于逻辑与),一个二进制变量和一个连续变量的乘积也很容易;剩下的就没那么容易了。

    从 Gurobi 9 开始,您可以解决非凸双线性问题,尤其是具有二次等式约束的问题。 You just have to add the right parameter。对于 Gurobi.jl,如果 m 是您的 JuMP 模型,您可以这样做:

    set_optimizer_attribute(m, "NonConvex", 2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多