How to set std value as objective?

function solution()
    model = Model(Gurobi.Optimizer);set_silent(model)
    @variable(model,x[1:3000,1:125],Bin)
    @constraint(model,[i=1:3000],sum(x[i,:]) == 5)
    @constraint(model,[i=1:125],sum(x[:,i]) == 120)
    @NLobjective(model,Min,std(sum(x[:,i] .* x[:,j]) for i in 1:125 for j in (i+1):125))
#     println(model)
    optimize!(model)
    value.(x)
end

Here I wanna minimize the std value ,but I don’t know how to code it correctly.Can someone help me fix it ?

LoadError: Unsupported generator `:std`
in expression starting at In[13]:6

Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:35
 [2] _parse_generator_expression(code::Expr, x::Expr, operators::Set{Tuple{Symbol, Int64}})
   @ JuMP C:\Users\ASUS\.julia\packages\JuMP\KiENn\src\macros.jl:2382
 [3] _parse_nonlinear_expression_inner(code::Expr, x::Expr, operators::Set{Tuple{Symbol, Int64}})
   @ JuMP C:\Users\ASUS\.julia\packages\JuMP\KiENn\src\macros.jl:2342
 [4] _parse_nonlinear_expression(model::Expr, x::Expr)
   @ JuMP C:\Users\ASUS\.julia\packages\JuMP\KiENn\src\macros.jl:2250
 [5] var"@NLobjective"(__source__::LineNumberNode, __module__::Module, model::Any, sense::Any, x::Any)
   @ JuMP C:\Users\ASUS\.julia\packages\JuMP\KiENn\src\macros.jl:2813
1 Like

Which version of JuMP are you using? I think starting with 1.15 nonlinear modeling like the kind you’re doing has become much easier: JuMP 1.15.0 is released | JuMP

1 Like

Gurobi.jl doesn’t support nonlinear functions directly, so you can’t use something like std or sqrt.

You could write your model like this:

using JuMP, Gurobi
M, N = 3000, 125
model = Model(Gurobi.Optimizer)
set_silent(model)
@variable(model, x[1:M, 1:N], Bin)
@constraint(model, [i=1:M], sum(x[i, :]) == 5)
@constraint(model, [i=1:N], sum(x[:, i]) == 120)
@variable(model, y[i=1:N, j=(i+1):N])
@constraint(model, [i=1:N, j=(i+1):N], y[i, j] == sum(x[:,i] .* x[:,j]))
@variable(model, μ)
@constraint(model, μ == sum(y) / length(y))
@objective(model, Min, sum((y[i, j] - μ)^2 for i in 1:N, j in (i+1):N))  # ignore sqrt(...), doesn't change solution
optimize!(model)
value.(x)

but it likely won’t solve (I have’t tried) because you have a large number of bilinear terms.

What is the background of the model and what are you trying to achieve?

Seems like my JuMP version is 1.14,I’ll try to update it later.

1 Like

v1.15 won’t help in this case.

It’s a problem from the Math Modeling Competition,as you said it has a large number of bilinear terms,so I use the Genetic Algorithm to solve it finally.

1 Like

But does it have to be Gurobi?

No, but the binary variables rule out solvers like Ipopt, and the number of bilinear terms in the expression is very large, so a naive call to Statistics.std didn’t even finish building when I tried to run it.