psamm.lpsolver.lp – Linear programming problems

Base objects for representation of LP problems.

A linear programming problem is built from a number of constraints and an objective function. The objective function is a linear expression represented by Expression. The constraints are represented by Relation, created from a linear expression and a relation sense (equals, greater, less).

Expressions are built from variables defined in the Problem instance. In addition, an expression can contain a VariableSet instead of a single variable. This allows many similar expressions to be represented by one Expression instance which means that the LP problem can be constructed faster.

class psamm.lpsolver.lp.Constraint

Represents a constraint within an LP Problem

delete()

Remove constraint from Problem instance

class psamm.lpsolver.lp.Expression(variables={}, offset=0)

Represents a linear expression

The variables can be any hashable objects. If one or more variables are instead VariableSets, this will be taken to represent a set of expressions separately using a different element of the VariableSet.

>>> e = Expression({'x': 2, 'y': 3})
>>> str(e)
'2*x + 3*y'

In order to provide a more natural syntax for creating Relations the binary relation operators have been overloaded to return Relation instances.

>>> rel = Expression({'x': 2}) >= Expression({'y': 3})
>>> str(rel)
'2*x - 3*y >= 0'
__add__(other)

Add expression with a number or another expression

__eq__(other)

Return equality relation (equation): self == other

This method is overloaded so that relations can be formed using a natural syntax.

__ge__(other)

Return greater-than relation (inequality): self >= other

This method is overloaded so that relations can be formed using a natural syntax.

__gt__(other)

Return strictly greater-than relation (inequality): self > other

This method is overloaded so that relations can be formed using a natural syntax.

__le__(other)

Return less-than relation (inequality): self <= other

This method is overloaded so that relations can be formed using a natural syntax.

__lt__(other)

Return strictly less-than relation (inequality): self < other

This method is overloaded so that relations can be formed using a natural syntax.

__radd__(other)

Add expression with a number or another expression

__unicode__()

Return string representation of expression

offset

Value of the offset

value_sets()

Iterator of expression sets

This will yield an iterator of (variable, value)-pairs for each expression in the expression set (each equivalent to values()). If none of the variables is a set variable then a single iterator will be yielded.

values()

Return immutable view of (variable, value)-pairs in expression.

variables()

Return immutable view of variables in expression.

exception psamm.lpsolver.lp.InvalidResultError(msg=None)

Raised when a result that has been invalidated is accessed

class psamm.lpsolver.lp.ObjectiveSense

Enumeration of objective sense values

class psamm.lpsolver.lp.Problem

Representation of LP Problem instance

Variable names in the problem can be any hashable object. It is the responsibility of the solver interface to translate the object into a unique string if required by the underlying LP solver.

add_linear_constraints(*relations)

Add constraints to the problem.

Each constraint is given as a Relation, and the expression in that relation can be a set expression. Returns a sequence of Constraints.

define(*names, **kwargs)

Define a variable in the problem

expr(values, offset=0)

Return the given dictionary of values as an Expression.

has_variable(name)

Check whether a variable is defined in the problem.

result

Result of solved problem

set(names)

Return variables as a set expression.

This returns an Expression containing a VariableSet.

set_linear_objective(expression)

Set objective of the problem to the given Expression.

set_objective(expression)

Set objective of the problem to the given Expression.

set_objective_sense(sense)

Set type of problem (minimize or maximize)

solve(sense=None)

Solve problem and return result

var(name)

Return variable as an Expression.

class psamm.lpsolver.lp.Product

A tuple used to represent a variable product.

class psamm.lpsolver.lp.Relation(sense, expression)

Represents a binary relation (equation or inequality)

Relations can be equalities or inequalities. All relations of this type can be represented as a left-hand side expression and the type of relation. In this representation, the right-hand side is always zero.

__unicode__()

Convert relation to string representation

expression

Left-hand side expression

sense

Type of relation (equality or inequality)

Can be one of Equal, Greater or Less, or one of the strict relations, StrictlyGreater or StrictlyLess.

class psamm.lpsolver.lp.Result

Result of solving an LP problem

The result is tied to the solver instance and is valid at least until the problem is solved again. If the problem has been solved again an InvalidResultError may be raised.

__bool__()

Whether solution was optimal

__nonzero__()

Whether solution was optimal

get_value(expression)

Get value of variable or expression in result

Expression can be an object defined as a name in the problem, in which case the corresponding value is simply returned. If expression is an actual Expression object, it will be evaluated using the values from the result.

status

String indicating the status of the problem result

success

Whether solution was optimal

unbounded

Whether solution is unbounded

class psamm.lpsolver.lp.Solver

Factory for LP Problem instances

create_problem()

Create a new Problem instance

class psamm.lpsolver.lp.VariableSet

A tuple used to represent sets of variables.

class psamm.lpsolver.lp.VariableType

Enumeration of variable types