Round off decision variable

Hey, is there a way to round off decision variables? I initialized the following variable, which is given in hours:

n = 4
model = Model()
@variable(model, start_n[1:n] >= 0, Int)

In one of my cost functions, however, I need the value in days, so I have to divide the value by 24 hours and then round down to a whole number.

I think you could create a second decision variable, also constrained to be integer, like so:

@variable(model, hours, Int)
@variable(model, days, Int)
@constraint(model, days / 24 >= hours - 1)

If your objective minimizes days * cost then the solver will push days down to the rounded value. I’m not entirely sure about the rounding direction but fiddling with a +1 or -1 should work eventually

1 Like

Assuming start_n is the number of hours, do:

using JuMP
n = 10
model = Model()
@variable(model, start_n[1:n] >= 0, Int)
@variable(model, 0 <= hours[1:n] <= 23, Int)
@variable(model, 0 <= days[1:n], Int)
@constraint(model, [i in 1:n], start_n[i] == 24 * days[i] + hours[i])

Edit: opened a pr to improve the docs: [docs] add modulo to tips_and_tricks.jl by odow · Pull Request #3641 · jump-dev/JuMP.jl · GitHub

2 Likes