# Trying to solve a puzzle

**URL:** <https://discourse.julialang.org/t/trying-to-solve-a-puzzle/61515>\
**Category:** Optimization (Mathematical)\
**Created:** [May 20, 2021, 1:05pm UTC](https://discourse.julialang.org/t/trying-to-solve-a-puzzle/61515 "2021-05-20T13:05:25Z")\
**Posts on this page:** 1\
**Showing post:** 15

<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:** [May 20, 2021, 8:36pm UTC](https://discourse.julialang.org/t/trying-to-solve-a-puzzle/61515/15 "2021-05-20T20:36:26Z")

</div>

Here’s a more compact way to write it:

```nohighlight
using JuMP, Cbc
function main(; houses::Vector{Tuple{Int,Int}}, ratio::Float64)
    M = maximum(h[1] for h in houses)
    N = maximum(h[2] for h in houses)
    model = Model(Cbc.Optimizer)
    @variable(model, cell_tower[1:M, 1:N], Bin)
    @variable(model, is_covered[houses], Bin)
    @objective(model, Min, sum(cell_tower))
    stencil(i, j) = cell_tower[max(i-1,1):min(i+1,M), max(j-1,1):min(j+1,N)]
    @constraint(model, [h in houses], is_covered[h] <= sum(stencil(h[1], h[2])))
    @constraint(model, sum(is_covered) >= ratio * length(houses))
    optimize!(model)
    return value.(cell_tower)
end
houses = [(1, 9), (3, 2), (4, 4), (5, 6), (8, 9), (9, 1)]
main(houses, ratio = 0.7)

```

---

_[View the full topic](https://discourse.julialang.org/t/trying-to-solve-a-puzzle/61515)._
