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?
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 (also see here) 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.
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 Bigfloats is tested on the Convex side though.
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.