# Rational{Int64} variables in JuMP?

**URL:** <https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048>\
**Category:** Optimization (Mathematical)\
**Created:** [June 25, 2020, 4:03pm UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048 "2020-06-25T16:03:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeneralPoxter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/generalpoxter/32/17695_2.png) [@GeneralPoxter](https://discourse.julialang.org/u/GeneralPoxter)\
**Post date:** [June 25, 2020, 4:03pm UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048/1 "2020-06-25T16:03:46Z")

</div>

I’ve been trying to make an array of rationals using the @variable macro in JuMP, but I can’t seem to find out how to constrain the array to only rational values.

Does JuMP allow you to build variables with Rational type?  
If so, how?  
And if not, is there an alternative?

---

<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:** [June 25, 2020, 4:29pm UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048/2 "2020-06-25T16:29:03Z")

</div>

No. JuMP only supports `Float64` variables (although you an constrain them to take binary or integer values).

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [June 25, 2020, 5:00pm UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048/3 "2020-06-25T17:00:18Z")

</div>

By “variable with Rational type”, do you mean “variable `x` that should only take rational values” or “I want to pass data to the solver in `Rational` type”?

For the former, it is neither supported by JuMP nor by solvers in general.  
The only exception are a handful of LP solvers, e.g., [SoPlex](https://soplex.zib.de) (also see [here](https://soplex.zib.de/doc-5.0.0/html/EXACT)) that can return _exact_ solutions (using Rational arithmetic) to linear programming and quadratic programming problems. That functionality, however, is not available from Julia.  
As @odow already hinted at, the only way to effectively enforce that a variable `x` takes rational values is through the use of integer variables. Either declare `x` as integer and divide it by a constant, or introduce two integer variables `p, q` and add the (non-linear and non-convex) constraint `x = p / q`.

For the latter, [MathOptInterface.jl](https://github.com/jump-dev/MathOptInterface.jl) and [Convex.jl](https://github.com/jump-dev/Convex.jl) both support arbitrary data types when building a model.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 25, 2020, 5:08pm UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048/4 "2020-06-25T17:08:39Z")

</div>

> [@mtanneau](#):
>
> The only exception are a handful of LP solvers, e.g., [SoPlex](https://soplex.zib.de) (also see [here](https://soplex.zib.de/doc-5.0.0/html/EXACT)) that can return _exact_ solutions (using Rational arithmetic) to linear programming and quadratic programming problems. That functionality, however, is not available from Julia.

Just to add in the particular case of doing stuff like going from hreps to vreps of a polyhedron, CDDLib can solve the problem exactly and is wrapped ([GitHub - JuliaPolyhedra/CDDLib.jl: cdd wrapper module for Julia. cdd is a library for polyhedra manipulation such as double description and Fourier-Motzkin elimination](https://github.com/JuliaPolyhedra/CDDLib.jl)) and can be used via Polyhedra.jl. I just mention it since it’s the only case I know of that you can do easily from Julia.

> [@mtanneau](#):
>
> For the latter, [MathOptInterface.jl](https://github.com/jump-dev/MathOptInterface.jl) and [Convex.jl](https://github.com/jump-dev/Convex.jl) both support arbitrary data types when building a model.

Convex can use arbitrary numeric types in theory, but I wouldn’t be surprised if something goes wrong with rationals since it’s not tested and they aren’t very robust (eg with SDPs `sqrt(2)` scalings are used in the MOI codebase). Using `Bigfloat`s is tested on the Convex side though.

---

<div class="post-metadata">

**Author:** ![GeneralPoxter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/generalpoxter/32/17695_2.png) [@GeneralPoxter](https://discourse.julialang.org/u/GeneralPoxter)\
**Post date:** [June 25, 2020, 5:52pm UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048/5 "2020-06-25T17:52:01Z")

</div>

Do you know what’s the best solver to use when working with nonlinear constraints and integer variables?

---

<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:** [June 25, 2020, 8:50pm UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048/6 "2020-06-25T20:50:29Z")

</div>

Try: [GitHub - lanl-ansi/Juniper.jl: A JuMP-based Nonlinear Integer Program Solver](https://github.com/lanl-ansi/Juniper.jl)

A better question to ask is why you require rational variables. Mixed-integer nonlinear programs are notoriously difficult to solve.

If you can use a common demoninator (e.g., `1000`), then the problem is easy. Having an arbitrary denominator is going to be very difficult.

---

<div class="post-metadata">

**Author:** ![GeneralPoxter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/generalpoxter/32/17695_2.png) [@GeneralPoxter](https://discourse.julialang.org/u/GeneralPoxter)\
**Post date:** [June 25, 2020, 8:54pm UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048/7 "2020-06-25T20:54:29Z")

</div>

Thanks, unfortunately, the solution to the system I am solving must be presented as an exact rational fraction.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 26, 2020, 9:51am UTC](https://discourse.julialang.org/t/rational-int64-variables-in-jump/42048/8 "2020-06-26T09:51:36Z")

</div>

_Technically_ `Float64`s are rational numbers, too 😉

In case you are looking for “nice” rational numbers (with small denominators etc), you can try solving using floating point to get solution, from which you learn which constraints bind, then just solve the resulting simplified linear system using rationals.
