# Fast way to access results in JuMP

**URL:** <https://discourse.julialang.org/t/fast-way-to-access-results-in-jump/88397>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [October 7, 2022, 12:04pm UTC](https://discourse.julialang.org/t/fast-way-to-access-results-in-jump/88397 "2022-10-07T12:04:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TBuConst](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@TBuConst](https://discourse.julialang.org/u/TBuConst)\
**Post date:** [October 7, 2022, 12:04pm UTC](https://discourse.julialang.org/t/fast-way-to-access-results-in-jump/88397/1 "2022-10-07T12:04:19Z")

</div>

Hello everyone,

say, I have a (pretty useless - just to illustrate the problem) MIP model in JuMP

```julia
using JuMP
using Cbc

model = Model(Cbc.Optimizer)

@variable(model, x[1:100000, 1:10, 1:2], Bin)
@constraint(model, c[i=1:100000], sum(x[i,:,:]) == 1)

```

The model solves. What I want to do now is to find out, which items in `x` are 1. My (most likely wrong) approach is to loop through the values of `x`:

```julia
for i in 1:100000
    for j in 1:10
        for k in 1:2
            if value(model[:x][i,j,k]) == 1
                println("Yippie!")
            end
        end
    end
end

```

While this works in principle, it takes forever. In fact, it takes much much longer to extract the result from the model than to solve it (8 seconds for solving, more than 20 minutes for result extraction). Therefore I think, that I’m doing something fundamentally wrong. What is the best way to find the value with a 1 in `x` after the model has been solved?

Thanks for your help and kind regards,

Thomas

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 7, 2022, 5:38pm UTC](https://discourse.julialang.org/t/fast-way-to-access-results-in-jump/88397/2 "2022-10-07T17:38:02Z")

</div>

Ideally, `JuMP.jl` should offer some way to query values from a list of variables simultaneously. I have discussed this with @odow in the past but I do not remember if it was ever implemented (or it was only implemented for deletion, that was my problem at the time).

However, I also have the feeling that you are iterating the matrix in the worst order (inner to outer instead of outer to inner), you can try reversing the nesting of the loops (so the innermost loop is `i` and the outermost is `k`), or use `axes` (or any other function that gives your all the indexes, like `CartesianIndices`). This should you some time if the matrix is large and page faults are frequent.

Finally, `println` is slow, I believe your example is not representative of your real problem, but printing a line (and not to a temporary buffer but directly to the output) is probably even slower than retrieving the value.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [October 8, 2022, 12:57am UTC](https://discourse.julialang.org/t/fast-way-to-access-results-in-jump/88397/3 "2022-10-08T00:57:06Z")

</div>

This performance [issue](https://github.com/jump-dev/Cbc.jl/issues/204) was fixed recently but didn’t get released. I just tagged Cbc.jl 1.0.2 which contains the fix.

---

<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:** [October 8, 2022, 4:44am UTC](https://discourse.julialang.org/t/fast-way-to-access-results-in-jump/88397/4 "2022-10-08T04:44:16Z")

</div>

Yes, this was my fault. I intended to make a release, [Prep for v1.0.2 · jump-dev/Cbc.jl@a54ea84 · GitHub](https://github.com/jump-dev/Cbc.jl/commit/a54ea84495bfaa03b42019840dcf4f2a3f1c5a03), but it looks like I forgot to actually do it.

---

<div class="post-metadata">

**Author:** ![TBuConst](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@TBuConst](https://discourse.julialang.org/u/TBuConst)\
**Post date:** [October 8, 2022, 12:50pm UTC](https://discourse.julialang.org/t/fast-way-to-access-results-in-jump/88397/5 "2022-10-08T12:50:15Z")

</div>

@miles.lubin : Thanks a lot! It works with Cbc.jl 1.0.2.
