I’m brand new to JuMP.jl and optimization and I’m running into a bit of a roadblock figuring out how to solve what I thought would be a fairly straightforward problem. Imagine 6 jars of marbles, divided into two groups. Each jar has a different number of marbles in it. I have purchased 9 new marbles and I would like to distribute them amongst the 6 jars in a way that helps even out the number of marbles in each jar, subject to some constraints:
I cannot put more than 3 marbles in any one jar.
As the jars are divided into two groups, I cannot allocate more than 6 of the new marbles to a group.
I must use all of the 9 new marbles.
I cannot move marbles from one jar to another.
The goal is to, after some number of rounds, end up with the same number of marbles in each jar. Ultimately, I want to know how many rounds of purchasing new marbles/distributing them amongst the jars it will take to have the same number of marbles in each jar, given the constraints.
The trainwreck code below was my first attempt at optimizing a single round of this experiment :
using GLPK using JuMP using Statistics # number of new marbles N = 9 # numer of marbles currently in each of the 3 jars in group 1 m₁ = [5,3,2] # numer of marbles currently in each of the 3 jars in group 2 m₂ = [5,10,12] model = Model(GLPK.Optimizer) # set up x₁ integer-valued variables for newly-allocated group 1 marbles @variable(model, x₁[1:3], Int) # set up x₂ integer-valued variables for newly-allocated group 2 marbles @variable(model, x₂[1:3], Int) # the objective is to minimize the standard deviation of all jars @objective(model, Min, std(vcat(m₁ .+ x₁, m₂ .+ x₂))) # we must use all of the new marbles @constraint(model, sum(vcat(x₁, x₂)) == N) # no single jar can receive more than 3 new marbles @constraint(model, 0 .<= x₁ .<= 3) @constraint(model, 0 .<= x₂ .<= 3) # neither group can receive more than 6 of the new marbles @constraint(model, sum(x₁) <= 6) @constraint(model, sum(x₂) <= 6)
Executing this code results in this error:
ERROR: abs2 is not defined for type GenericAffExpr. Are you trying to build a nonlinear problem? Make sure you use @NLconstraint/@NLobjective.
And switching to
@NLobjective leads to a whole new set of errors/problems. I’m hoping someone more experienced in this realm can steer me in the right direction on this. Thanks so much!