# How to set std value as objective?

**URL:** <https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, gurobi\
**Created:** [September 27, 2023, 8:48am UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304 "2023-09-27T08:48:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack\_lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_lee/32/47431_2.png) [@jack\_lee](https://discourse.julialang.org/u/jack_lee)\
**Post date:** [September 27, 2023, 8:48am UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304/1 "2023-09-27T08:48:28Z")

</div>

```julia
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 ?

```julia
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

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [September 27, 2023, 8:12pm UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304/2 "2023-09-27T20:12:10Z")

</div>

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](https://jump.dev/blog/1.15.0-release/)

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 27, 2023, 8:18pm UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304/3 "2023-09-27T20:18:08Z")

</div>

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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![jack\_lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_lee/32/47431_2.png) [@jack\_lee](https://discourse.julialang.org/u/jack_lee)\
**Post date:** [September 28, 2023, 3:04am UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304/4 "2023-09-28T03:04:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 28, 2023, 3:04am UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304/5 "2023-09-28T03:04:59Z")

</div>

v1.15 won’t help in this case.

---

<div class="post-metadata">

**Author:** ![jack\_lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_lee/32/47431_2.png) [@jack\_lee](https://discourse.julialang.org/u/jack_lee)\
**Post date:** [September 28, 2023, 3:06am UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304/6 "2023-09-28T03:06:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [September 28, 2023, 7:58am UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304/7 "2023-09-28T07:58:16Z")

</div>

But does it have to be Gurobi?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 29, 2023, 7:56pm UTC](https://discourse.julialang.org/t/how-to-set-std-value-as-objective/104304/8 "2023-09-29T19:56:21Z")

</div>

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.
