# How to minimize the variance of time intervals between scheduled slots?

**URL:** <https://discourse.julialang.org/t/how-to-minimize-the-variance-of-time-intervals-between-scheduled-slots/116591>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [July 3, 2024, 2:27pm UTC](https://discourse.julialang.org/t/how-to-minimize-the-variance-of-time-intervals-between-scheduled-slots/116591 "2024-07-03T14:27:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scheidan1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheidan1/32/24771_2.png) [@scheidan1](https://discourse.julialang.org/u/scheidan1)\
**Post date:** [July 3, 2024, 2:27pm UTC](https://discourse.julialang.org/t/how-to-minimize-the-variance-of-time-intervals-between-scheduled-slots/116591/1 "2024-07-03T14:27:43Z")

</div>

Hi,  
I’m trying to setup an optimization in JuMP. I do not have much experience in this field but it seems trivial:  
The aim is assigns speaker to slots with respect to some conditions. I want to minimizes the variance of the time intervals between the scheduled slots for each speaker.

I’ve implemented a matrix of binary decision variables `a[i,j]` that is `1` if slot `i` is assigned to speaker `j`. With this I can encode my conditions easily, such as

```Julia
@variable(model, a[1:n_slots, 1:n_speakers], Bin)
@constraint(model, sum(a, dims=1) .== 1) # only one speaker per slot

```

Unfortunately, I get stuck with the loss. A possible loss function for speaker j is  
 l\_k = \sum\_k | (t\_{k,j} - t\_{k-1, j}) - \bar{d} |   
where t\_k is the slot number of the k-th assignment (i.e. the k -th occurrence of `a[:,j] == 1`) of speaker j, and \bar{d} the average time interval without constrains: \bar{d} = \frac{n\_\text{speakers}}{n\_\text{slots}}. (kind of a linearized variance)

My questions are:

- How can I code such a loss compatible with `JuMP`? My problem is to get from the matrix `a` to the slot times t\_k.
- Could I use a different loss function instead?
- I feel I probably should set up the problem complete differently. I’d appreciate any hint!

Many thanks in advance!  
Andreas

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [July 4, 2024, 7:55am UTC](https://discourse.julialang.org/t/how-to-minimize-the-variance-of-time-intervals-between-scheduled-slots/116591/2 "2024-07-04T07:55:58Z")

</div>

You could use [indicator constraints](https://jump.dev/JuMP.jl/stable/manual/constraints/#Indicator-constraints) to create `t_k - t_{k-1}`:

```julia
@variable(model, time_since_last[1:nslots, 1:n_speakers])
@constraint(model, [j=1:n_speakers], time_since_last[1, j] == 0)
@constraint(model, [i=2:(n_slots-1), j=1:n_speakers], a[i, j] --> {time_since_last[i + 1, j] == 0})
@constraint(model, [i=2:n_slots, j=1:n_speakers], !a[i, j] --> {time_since_last[i, j] == time_since_last[i - 1, j] + 1})

```

For the objective, you can do as follows:

```julia
@variable(model, costs[1:nslots, 1:n_speakers] >= 0)
@constraint(model, [i=2:n_slots, j=1:n_speakers], a[i, j] --> {costs[i, j] >= time_since_last[i, j] - bar_d})
@constraint(model, [i=2:n_slots, j=1:n_speakers], a[i, j] --> {costs[i, j] >= bar_d - time_since_last[i, j]})
@objective(model, Min, sum(costs))

```

---

<div class="post-metadata">

**Author:** ![scheidan1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scheidan1/32/24771_2.png) [@scheidan1](https://discourse.julialang.org/u/scheidan1)\
**Post date:** [July 4, 2024, 2:37pm UTC](https://discourse.julialang.org/t/how-to-minimize-the-variance-of-time-intervals-between-scheduled-slots/116591/3 "2024-07-04T14:37:53Z")

</div>

Thanks a lot @blegat!  
This is clever solution. As soon I have some time I will try to implement it.
