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

Pauli Virtanen pav at iki.fi
Fri Jul 30 04:49:31 EDT 2010


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.


-- 
Pauli Virtanen




More information about the SciPy-Dev mailing list