# How can I extract MOI expression from a linear JuMP obj expression

**URL:** <https://discourse.julialang.org/t/how-can-i-extract-moi-expression-from-a-linear-jump-obj-expression/52689>\
**Category:** Optimization (Mathematical)\
**Created:** [January 1, 2021, 9:19am UTC](https://discourse.julialang.org/t/how-can-i-extract-moi-expression-from-a-linear-jump-obj-expression/52689 "2021-01-01T09:19:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahbubar06](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@Mahbubar06](https://discourse.julialang.org/u/Mahbubar06)\
**Post date:** [January 1, 2021, 9:19am UTC](https://discourse.julialang.org/t/how-can-i-extract-moi-expression-from-a-linear-jump-obj-expression/52689/1 "2021-01-01T09:19:52Z")

</div>

Hello,

I am struggling to generate an MOI (MathOptInterface) expression from a linear JuMP expression.

I have a JuMP model as follows.

```julia
m = Model(Ipopt.Optimizer)
@variable(m, 0 <= x[1:3] <= 10)
@constraint(m, sum(x[i] for i in 1:3) >= 0)
@objective(m, Min, sum(x[i] for i in 1:3))

```

The obj JuMP expression is

```julia
obj_expr = JuMP.objective_function(m)
julia> print(obj_expr)
x[1] + x[2] + x[3]

```

I can generate MOI expressions for nonlinear objective or constraints, but don’t know how to do it for linear ones. My goal is to generate another JuMP model with the same objective function. For example,

```julia
nm = Model(Ipopt.Optimizer)
@variable(nm, x[1:3])
func = moi_obj_expr #suppose this is the moi expression
MOI.set(nm, MOI.ObjectiveFunction{typeof(func)}(), func)
MOI.set(nm, MOI.ObjectiveSense(), JuMP.objective_sense(m))

```

Thank you.

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [January 1, 2021, 12:45pm UTC](https://discourse.julialang.org/t/how-can-i-extract-moi-expression-from-a-linear-jump-obj-expression/52689/2 "2021-01-01T12:45:22Z")

</div>

You can go from JuMP to MOI with `moi_function`  
[https://github.com/jump-dev/JuMP.jl/blob/2b0d9100b83985f67bbb673c9e34dbedc9e30da6/src/aff\_expr.jl#L498](https://github.com/jump-dev/JuMP.jl/blob/2b0d9100b83985f67bbb673c9e34dbedc9e30da6/src/aff_expr.jl#L498)  
And the other way around with `jump_function`  
[https://github.com/jump-dev/JuMP.jl/blob/2b0d9100b83985f67bbb673c9e34dbedc9e30da6/src/aff\_expr.jl#L515](https://github.com/jump-dev/JuMP.jl/blob/2b0d9100b83985f67bbb673c9e34dbedc9e30da6/src/aff_expr.jl#L515)

---

<div class="post-metadata">

**Author:** ![Mahbubar06](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@Mahbubar06](https://discourse.julialang.org/u/Mahbubar06)\
**Post date:** [January 1, 2021, 2:34pm UTC](https://discourse.julialang.org/t/how-can-i-extract-moi-expression-from-a-linear-jump-obj-expression/52689/3 "2021-01-01T14:34:51Z")

</div>

@blegat Thank you so much. The problem is solved now.
