# Learning JuMP and Julia through Kaggle

**URL:** <https://discourse.julialang.org/t/learning-jump-and-julia-through-kaggle/32709>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 26, 2019, 8:14am UTC](https://discourse.julialang.org/t/learning-jump-and-julia-through-kaggle/32709 "2019-12-26T08:14:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tog/32/11858_2.png) [@tog](https://discourse.julialang.org/u/tog)\
**Post date:** [December 26, 2019, 8:14am UTC](https://discourse.julialang.org/t/learning-jump-and-julia-through-kaggle/32709/1 "2019-12-26T08:14:12Z")

</div>

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.

Thanks  
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)  
end  
end

#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]])  
end

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)

optimize!(model)

---

<div class="post-metadata">

**Author:** ![JosiahPohl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josiahpohl/32/12012_2.png) [@JosiahPohl](https://discourse.julialang.org/u/JosiahPohl)\
**Post date:** [December 26, 2019, 6:03pm UTC](https://discourse.julialang.org/t/learning-jump-and-julia-through-kaggle/32709/2 "2019-12-26T18:03:36Z")

</div>

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:

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