Learning JuMP and Julia through Kaggle

Hi All

I am learning Julia & JuMP by trying to solve the Kaggle Santa Klaus problem of the year. My current model of the problem is posted below. It does not seem fully legit. My question is to understand how I can mix julia operations on variables to handle complex constraints - see for example what I am doing with daily_occupancy which os not define as a variable at the moment.

PS: any feedback on the code is welcome

using JuMP
using MathOptInterface

using GLPK
model = Model(with_optimizer(GLPK.Optimizer))

using Cbc
model = Model(with_optimizer(Cbc.Optimizer, logLevel=1, seconds=14400.0))

numchoices = 10

candidates = [Int for i=1:numdays]
for i in 1:nbfamily
for j in 1:numchoices
day = choices[j, i]
push!(candidates[day], i)

#X = Array{Bool}(undef, nbfamily, numdays)
@variable(model, X[1:nbfamily, 1:numchoices], Bin)

daily_occupancy = Array{Int}(undef, numdays)
for j in 1:numdays
daily_occupancy[j] = sum([value(X[i, j]) * familysize[i] for i in candidates[j]])

family_presence = [sum([X[i, j] for j in choices[:, i]]) for i in 1:nbfamily]

preference_cost = sum([var1[i, j] * X[i,j] for i in 1:nbfamily for j in choices[:, i] ])

@objective(model, Min, preference_cost)

for j in 1:numdays
@constraint(model, daily_occupancy[j] - daily_occupancy[j+1] <= 23)
@constraint(model, daily_occupancy[j+1] - daily_occupancy[j ] <= 23)

for i in 1:nbfamily
@constraint(family_presence[i] == 1)

for j in 1:numdays:
@constraint(daily_occupancy[j] >= mino)
@constraint(daily_occupancy[j] <= maxo)


You should check out the @expression macro from the JuMP package. It will allow you to define daily_occupancy as a linear combination of optimization variables without creating additional variables that need to be optimized. It would look something like:

@expression(model, daily_occupency[j in 1:numchoices], 
            sum(X[i, j] * familysize[i], for i in candidates[j]))

Then you can use daily_occupency in your constraint definitions.

A couple additional notes:

  1. The value() function is used to query the results of the optimization and shouldn’t be used in the model formulation
  2. When posting code samples, please use back-ticks (``` * ```, with your code where the * is) for nice formatting to make things easier to read

Hope this helps!