# How do you apply ceil/round function inside JuMP constraints, or can you force constraints to evaluate to Ints?

**URL:** <https://discourse.julialang.org/t/how-do-you-apply-ceil-round-function-inside-jump-constraints-or-can-you-force-constraints-to-evaluate-to-ints/26197>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 9, 2019, 6:27pm UTC](https://discourse.julialang.org/t/how-do-you-apply-ceil-round-function-inside-jump-constraints-or-can-you-force-constraints-to-evaluate-to-ints/26197 "2019-07-09T18:27:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![clausherther](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clausherther/32/4872_2.png) [@clausherther](https://discourse.julialang.org/u/clausherther)\
**Post date:** [July 9, 2019, 6:27pm UTC](https://discourse.julialang.org/t/how-do-you-apply-ceil-round-function-inside-jump-constraints-or-can-you-force-constraints-to-evaluate-to-ints/26197/1 "2019-07-09T18:27:56Z")

</div>

Hi, I have this simple model that tries to maximize theoretical demand for a product given some SKU level inventory constraints and a probability distribution for SKU demand. In the first constraint I’d like to compare the output of a multiplication of my variable `d` multiplied by the given probability distribution `p` for a SKU to the inventory constraint for that SKU. However, I’d like to apply either a `ceil` or `round` function to the multiplication since we can’t have partial units. I’ve set this up as an integer problem, but I don’t know how I can either specify integer constraints, or apply a type coercion/ceil/round function inside of the constraint. Sample code:

```julia
using JuMP, GLPK

p = [.1, .15, .2, .25, .2, .1]
inventory = [10.0, 13.0, 22.0, 25.0, 20.0, 10.0];

m = Model(with_optimizer(GLPK.Optimizer))

@variable(m, d >= 0, Int)
@objective(m, Max, d/sum(inventory))

for i = 1:length(p)
    @constraint(m, ceil(d * p[i]) <= inventory[i])
end

```

Error:

```julia
MethodError: no method matching ceil(::GenericAffExpr{Float64,VariableRef})
Closest candidates are:
  ceil(!Matched::Type{BigInt}, !Matched::BigFloat) at mpfr.jl:314
  ceil(!Matched::Missing) at missing.jl:112
  ceil(!Matched::Missing, !Matched::Integer) at missing.jl:112
  ...

```

Is there a better way to ensure that the output of `d * p[i]` gets coerced into an integer value?  
Thanks!

---

<div class="post-metadata">

**Author:** ![clausherther](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clausherther/32/4872_2.png) [@clausherther](https://discourse.julialang.org/u/clausherther)\
**Post date:** [July 9, 2019, 10:57pm UTC](https://discourse.julialang.org/t/how-do-you-apply-ceil-round-function-inside-jump-constraints-or-can-you-force-constraints-to-evaluate-to-ints/26197/2 "2019-07-09T22:57:58Z")

</div>

I was able to reformulate this in a way that I don’t think need the function applied anymore:

This now models each SKU’s demand as an integer and then checks that against the required SKU distribution.

```julia
skus = [1,2,3,4,5,6]
n_skus = length(skus)

sku_probabilities = [.1, .15, .2, .25, .2, .1]
total_demand = 10000
inventory_distribution = [.1, .13, .22, .25, .2, .1]
inventory = [total_demand * p for p in inventory_distribution]

```

```julia

m = Model(with_optimizer(GLPK.Optimizer))

@variable(m, sku_demand[1:n_skus] >= 0, Int)

# we maximize inventory utilization:
@objective(m, Max, sum(sku_demand)/sum(inventory))

for i = 1:length(skus)
    # make sure demand <= inventory
    @constraint(m, sku_demand[i] <= inventory[i] )
    # make sure demand is distributed as required
    @constraint(m, sku_demand[i] <= sum(sku_demand) * sku_probabilities[i] )
end

m

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 10, 2019, 2:14pm UTC](https://discourse.julialang.org/t/how-do-you-apply-ceil-round-function-inside-jump-constraints-or-can-you-force-constraints-to-evaluate-to-ints/26197/3 "2019-07-10T14:14:46Z")

</div>

Another way is to add an auxiliary variable and force that to be integer.

```nohighlight
p = 0.3
model = Model()
@variable(model, d >= 0, Int)
@variable(model, aux, Int)
@constraint(model, d * p == aux)

```

---

<div class="post-metadata">

**Author:** ![clausherther](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/clausherther/32/4872_2.png) [@clausherther](https://discourse.julialang.org/u/clausherther)\
**Post date:** [July 10, 2019, 2:25pm UTC](https://discourse.julialang.org/t/how-do-you-apply-ceil-round-function-inside-jump-constraints-or-can-you-force-constraints-to-evaluate-to-ints/26197/4 "2019-07-10T14:25:57Z")

</div>

Oh that’s handy, thanks!  
So, you’d still also have the original constraint then, but the `aux` constraint forces `d * p[i]` to evaulate to an `int`?

```julia
@constraint(m, d * p[i] <= inventory[i])

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [July 10, 2019, 2:29pm UTC](https://discourse.julialang.org/t/how-do-you-apply-ceil-round-function-inside-jump-constraints-or-can-you-force-constraints-to-evaluate-to-ints/26197/5 "2019-07-10T14:29:15Z")

</div>

Yes, exactly.
