# Return Tuple from JuMP

**URL:** <https://discourse.julialang.org/t/return-tuple-from-jump/62026>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [May 28, 2021, 11:33pm UTC](https://discourse.julialang.org/t/return-tuple-from-jump/62026 "2021-05-28T23:33:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjanun](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mjanun](https://discourse.julialang.org/u/mjanun)\
**Post date:** [May 28, 2021, 11:33pm UTC](https://discourse.julialang.org/t/return-tuple-from-jump/62026/1 "2021-05-28T23:33:28Z")

</div>

I have few questions on JuMP. Can we return JuMP results as a named tuple of termination status and variable values? See MWE below. Variables will not always contain values, eg. if model is infeasible, so I don’t know how to create a tuple in that case.

How can I capture the log of the solver (one of the open source ones Clp, Cbc, glpk) and write to a file?

```julia

using JuMP
function solve()
c = rand(10)
d = rand(10)
model = Model(Cbc.Optimizer)
@variable(model, x[1:10] >= 0)
@constraint(model,sum(x[i]*c[i] for i=1:10) >=sum(d))
optimize!(model)
status = termination_status(model) 
if status == MOI.INFEASIBLE
   solution = Nothing
   objective = Nothing
elseif status == MOI.OPTIMAL
    solution = value.(x)
    objective = objective_value(model)
elseif status == MOI.TIME_LIMIT && has_values(model)
    solution = value.(x)
    objective = objective_value(model)
else
    error("The model was not solved correctly.")
end
result = (status, solution, objective)
end

```

When I check result is a tuple, but not a named tuple. so I cannot query, `result.solution` to get values of x

---

<div class="post-metadata">

**Author:** ![asavasci](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@asavasci](https://discourse.julialang.org/u/asavasci)\
**Post date:** [May 29, 2021, 1:20am UTC](https://discourse.julialang.org/t/return-tuple-from-jump/62026/2 "2021-05-29T01:20:28Z")

</div>

You can dump out result as a dictionary  
`result = Dict("status"=>status, "solution" => solution, "obj_value"=>JuMP.objective_value(model))`

and access each key by typing

```julia
result["status"]
result["solution"]
result["obj_value"]

```

---

<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 29, 2021, 1:36am UTC](https://discourse.julialang.org/t/return-tuple-from-jump/62026/3 "2021-05-29T01:36:24Z")

</div>

NamedTuples aren’t specific to JuMP. Here’s the Julia documentation: [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#Core.NamedTuple)

You want something like:

```nohighlight
function solve()
    c = rand(10)
    d = rand(10)
    model = Model(Cbc.Optimizer)
    @variable(model, x[1:10] >= 0)
    @constraint(model, sum(x[i] * c[i] for i in 1:10) >= sum(d))
    optimize!(model)
    status = termination_status(model) 
    if status == MOI.INFEASIBLE
        return (
            status = status,
            solution = fill(NaN, 10),
            objective = NaN,
        )
    elseif status == MOI.OPTIMAL || (status == MOI.TIME_LIMIT && has_values(model))
        return (
            status = status, 
            solution = value.(x), 
            objective = objective_value(model),
        )
    else
        error("The model was not solved correctly.")
    end
end

```

---

<div class="post-metadata">

**Author:** ![mjanun](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@mjanun](https://discourse.julialang.org/u/mjanun)\
**Post date:** [May 29, 2021, 8:21am UTC](https://discourse.julialang.org/t/return-tuple-from-jump/62026/4 "2021-05-29T08:21:32Z")

</div>

Thank you. Is there a function to save the cbc solver output / log and write to a file? I have checked JuMP documentation but it doesn’t not state anything about it
