Scipy Linear Programming Rational Numbers

2019-09-05 06:38发布

问题:

scipy.optimize.linprog seems to be a wrapper around a C/Fortran library that always uses floating point numbers regardless of the input type.

from scipy import optimize as opt
from fractions import Fraction
import numpy as np

lp = opt.linprog

def frac(arr):
    "convert array or number to rational form"
    if isinstance(arr, int) or isinstance(arr, float):
        return Fraction(arr)
    else:
        return list(map(frac, arr))

A_ub = frac([
    [ 1, 2, 3 ],
    [ 4, 5, 6]
])

A_ub = np.array(A_ub, dtype=object)

b_ub = frac( [ 1, 2 ] )
b_ub = np.array(b_ub, dtype=object)

c = frac( [ -1, -2, -3 ])
c = np.array(c, dtype=object)

result = lp(A_ub=A_ub, b_ub=b_ub, c=c)
print result.x.dtype

prints float64

Is there a similar library with a different solver in numpy/ scipy that is able to manipulate arbitrary python objects and use their methods +, -, *, / instead of coercing everything to floating point?