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.
-
exception
psamm.lpsolver.lp.
SolverError
¶ Error wrapping solver specific errors.
-
class
psamm.lpsolver.lp.
VariableSet
¶ A tuple used to represent sets of variables.
-
class
psamm.lpsolver.lp.
Product
¶ A tuple used to represent a variable product.
-
class
psamm.lpsolver.lp.
RangedProperty
(fget=None, fset=None, fdel=None, fmin=None, fmax=None, doc=None)¶ Numeric property with minimum and maximum values.
The value attribute is used to get/set the actual value of the propery. The min/max attributes are used to get the bounds. The range is not automatically enforced when the value is set.
-
psamm.lpsolver.lp.
ranged_property
(min=None, max=None)¶ Decorator for creating ranged property with fixed bounds.
-
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 theVariableSet
.>>> 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 returnRelation
instances.>>> rel = Expression({'x': 2}) >= Expression({'y': 3}) >>> str(rel) '2*x - 3*y >= 0'
Warning
Chained relations cannot be converted to multiple relations, e.g.
4 <= e <= 10
will fail to produce the intended relations!-
offset
¶ Value of the offset
-
variables
()¶ Return immutable view of variables in expression.
-
values
()¶ Return immutable view of (variable, value)-pairs in expression.
-
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.
-
-
class
psamm.lpsolver.lp.
RelationSense
¶ An enumeration.
-
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.
-
sense
¶ Type of relation (equality or inequality)
Can be one of Equal, Greater or Less, or one of the strict relations, StrictlyGreater or StrictlyLess.
-
expression
¶ Left-hand side expression
-
-
class
psamm.lpsolver.lp.
ObjectiveSense
¶ Enumeration of objective sense values
-
Minimize
= -1¶ Minimize objective function
-
Maximize
= 1¶ Maximize objective function
-
-
class
psamm.lpsolver.lp.
VariableType
¶ Enumeration of variable types
-
Continuous
= 'C'¶ Continuous variable type
-
Integer
= 'I'¶ Integer variable type
-
Binary
= 'B'¶ Binary variable type (0 or 1)
-
-
class
psamm.lpsolver.lp.
Solver
¶ Factory for LP Problem instances
-
class
psamm.lpsolver.lp.
Constraint
¶ Represents a constraint within an LP Problem
-
delete
()¶ Remove constraint from Problem instance
-
-
class
psamm.lpsolver.lp.
VariableNamespace
(problem, **kwargs)¶ Namespace for defining variables.
Namespaces are always unique even if two namespaces have the same name. Variables defined within a namespace will never clash with a global variable name or a name defined in another namespace. Namespaces should be created using the
Problem.namespace()
.>>> v = prob.namespace(name='v') >>> v.define([1, 2, 5], lower=0, upper=10) >>> prob.set_objective(v[1] + 3*v[2] - 5 * v[5])
-
define
(names, **kwargs)¶ Define variables within the namespace.
This is similar to
Problem.define()
except that names must be given as an iterable. This method accepts the same keyword arguments asProblem.define()
.
-
set
(names)¶ Return a variable set of the given names in the namespace.
>>> v = prob.namespace(name='v') >>> v.define([1, 2, 5], lower=0, upper=10) >>> prob.add_linear_constraints(v.set([1, 2]) >= 4)
-
sum
(names)¶ Return the sum of the given names in the namespace.
>>> v = prob.namespace(name='v') >>> v.define([1, 2, 5], lower=0, upper=10) >>> prob.set_objective(v.sum([2, 5])) # v[2] + v[5]
-
expr
(items)¶ Return the sum of each name multiplied by a coefficient.
>>> v = prob.namespace(name='v') >>> v.define(['a', 'b', 'c'], lower=0, upper=10) >>> prob.set_objective(v.expr([('a', 2), ('b', 1)]))
-
value
(name)¶ Return value of given variable in namespace.
>>> v = prob.namespace(name='v') >>> v.define([1, 2, 5], lower=0, upper=10) >>> prob.solve() >>> print(v.value(2))
-
-
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.
-
define
(*names, **kwargs)¶ Define a variable in the problem.
Variables must be defined before they can be accessed by var() or set(). This function takes keyword arguments lower and upper to define the bounds of the variable (default: -inf to inf). The keyword argument types can be used to select the type of the variable (Continuous (default), Binary or Integer). Setting any variables different than Continuous will turn the problem into an MILP problem. Raises ValueError if a name is already defined.
-
has_variable
(name)¶ Check whether a variable is defined in the problem.
-
namespace
(names=None, **kwargs)¶ Return namespace for this problem.
If names is given it should be an iterable of names to define in the namespace. Other keyword arguments can be specified which will be used to define the names given as well as being used as default parameters for names that are defined later.
>>> v = prob.namespace(name='v') >>> v.define([1, 2, 5], lower=0, upper=10) >>> prob.set_objective(v[1] + 3*v[2] - 5 * v[5])
-
var
(name)¶ Return variable as an
Expression
.
-
expr
(values, offset=0)¶ Return the given dictionary of values as an
Expression
.
-
set
(names)¶ Return variables as a set expression.
This returns an
Expression
containing aVariableSet
.
-
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 ofConstraints
.
-
set_objective
(expression)¶ Set objective of the problem to the given
Expression
.
-
set_linear_objective
(expression)¶ Set objective of the problem.
Deprecated since version 0.19: Use
set_objective()
instead.
-
set_objective_sense
(sense)¶ Set type of problem (minimize or maximize)
-
solve
(sense=None)¶ Solve problem and return result.
Raises
SolverError
if the solution is not optimal.
-
solve_unchecked
(sense=None)¶ Solve problem and return result.
The user must manually check the status of the result to determine whether an optimal solution was found. A
SolverError
may still be raised if the underlying solver raises an exception.
-
result
¶ Result of solved problem
-
-
exception
psamm.lpsolver.lp.
InvalidResultError
(msg=None)¶ Raised when a result that has been invalidated is accessed
-
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.-
success
¶ Whether solution was optimal
-
status
¶ String indicating the status of the problem result
-
unbounded
¶ Whether solution is unbounded
-
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.
-