[SciPy-Dev] Proposed enhancement: pure Python implementationof LP simplex method (two-phase)

Enzo Michelangeli enzomich@gmail....
Fri Jul 30 05:54:40 CDT 2010


Pauli,

Thanks for uploading lp2.py: I hadn't noticed it and I uploaded a revised 
lp.py with the changes you required except for the object stuff, on which I 
had a question (namely: should the NamedTuple be based on 
http://docs.python.org/library/collections.html#collections.namedtuple ?). 
Anyway, feel free to remove lp.py .

Regarding the warning, you may safely take it away altogether: the "print" 
was a diagnostic leftover that I had overlooked (the "unsolvable" status is 
anyway returned to the caller).

Enzo

From: "Pauli Virtanen" <pav@iki.fi>
Sent: Friday, July 30, 2010 4:49 PM

> Fri, 30 Jul 2010 09:48:49 +0800, Enzo Michelangeli wrote:
> [clip]
>>>> http://projects.scipy.org/scipy/ticket/1252
>>>
>>> I think your "test case" could easily be turned into a unit test, which
>>> would make the submission more complete.
>>
>> Done.
>
> Seems to work.
>
> Some API nitpicks,
>
> 1)
>
> Return a solution object rather than a tuple:
>
> class NamedTuple(tuple):
>    def __new__(cls, values, names):
>        self = tuple.__new__(cls, values)
>        for value, name in zip(values, names):
>            setattr(self, name, value)
>        return self
>
> class Solution(NamedTuple):
>    _fields = []
>    def __new__(cls, *a, **kw):
>        values = list(a)
>        for name in cls._fields[len(values):]:
>            values.append(kw.pop(name))
>        if len(values) != len(cls._fields) or kw:
>            raise ValueError("Invalid arguments")
>        return NamedTuple.__new__(cls, values, cls._fields)
>
>    def __repr__(self):
>        return "%s%s" % (self.__class__.__name__,
>                         NamedTuple.__repr__(self))
>
> class LPSolution(Solution):
>    """
>    Solution to a linear programming problem
>
>    Attributes
>    ----------
>    x
>        The optimal solution
>    min
>        The optimal value
>    is_bounded
>        True if the solution is bounded; False if unbounded
>    solvable
>        True if the problem is solvable; False if unsolvable
>    basis
>        Indices of the basis of the solution.
>    """
>    _fields = ['x', 'min', 'is_bounded', 'solvable', 'basis']
>
> def lp(...):
>    ...
>    return LPSolution(optx, zmin, is_bounded, sol, basis)
>
>
> We could (and probably should) replace *all* cases in Scipy where a tuple
> is currently returned by this sort of pattern. It's backwards compatible,
> since the returned object is still some sort of a tuple.
>
> 2)
>
> Don't print to stdout.
>
> Use warnings.warn if you need to warn about something.
>
> 3)
>
> Call
>
> c = np.asarray(c)
> A = np.asarray(A)
> b = np.asarray(b)
>
> in the beginning -- good for convenience.




More information about the SciPy-Dev mailing list