# Advice about refactoring LinearFractional.jl for JuMP 0.19 and Julia 0.7

**URL:** <https://discourse.julialang.org/t/advice-about-refactoring-linearfractional-jl-for-jump-0-19-and-julia-0-7/12966>\
**Category:** Optimization (Mathematical)\
**Created:** [August 7, 2018, 12:01am UTC](https://discourse.julialang.org/t/advice-about-refactoring-linearfractional-jl-for-jump-0-19-and-julia-0-7/12966 "2018-08-07T00:01:16Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [August 8, 2018, 9:17am UTC](https://discourse.julialang.org/t/advice-about-refactoring-linearfractional-jl-for-jump-0-19-and-julia-0-7/12966/2 "2018-08-08T09:17:31Z")

</div>

The easiest way to extend JuMP is by writing new methods for [parse|build|add][variable|constraint]. The JuMPExtension approach might be overkill for some extensions but it may be needed, it is the approach used by [StructJuMP](https://github.com/StructJuMP/StructJuMP.jl/pull/60).

In your case, it seems you could multiply the constant by `t` in the `addconstraint` call. To define a new method for the `addconstraint` call you need new types. Here you `AbstractConstraint` will be classical ones so you need a new type for the model.  
You could define

```julia
struct LinearFractionalModel <: JuMP.AbstractModel
    model::JuMP.Model
    t::JuMP.VariableRef
end

```

then define all methods defined in `JuMPExtension` for `LinearFractionalModel`. However here instead of creating `MyVariableRef` ans `MyConstraintRef` you just redirect all calls to the inner `model` field except `addconstraint` for which you modify the function by modifying the constant term by `t` before redirecting it to the inner `model` field.

Let me know if that’s clear or if you have any question 🙂

**Note** : this should be documented [here](http://www.juliaopt.org/JuMP.jl/latest/extensions.html), your post help us see what are the use cases and how the doc could be structured 🙂

**EDIT** : An alternative would be to do this at the MOI level by writing an MOI layer. You would write a

```julia
struct LinearFractionalModel <: MOI.ModelLike
    model::MOI.ModelLike
    t::MOI.VariableIndex
end

```

then you redirect all calls to the inner `model` except `MOI.addconstraint!` in which you modify the constraint and

```julia
MOI.set(model::LinearFractionalModel, ::ObjectiveFunction{MOI.ScalarNonlinearFunction}, f::ScalarNonlinearFunction)

```

in which you error if the nonlinear function is a fraction and get the numerator and denominator then pass an

```julia
MOI.set(model.model, ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}, new_obj)

```

You would then need to modify the results, e.g. `MOI.VariablePrimal`, to make the transformation transparent, e.g. by dividing `y` by `t` to get `x` because the variable primal values returned by the inner `model` field would be the values of `y` and not `x`.  
The user will then be able to do `@NLobjective model (x+1) / (x - 2)`.  
However, that approach is currently not feasible as the nonlinear interface does not currently permits you to look into the expression graph (you need it to grab the numerator, denominator, …) but that will be the case when the nonlinear interface will be rewritten with [Cassette](https://github.com/jrevels/Cassette.jl).

---

_[View the full topic](https://discourse.julialang.org/t/advice-about-refactoring-linearfractional-jl-for-jump-0-19-and-julia-0-7/12966)._
