LinearProgrammingProblem¶
- class LinearProgrammingProblem(*args)¶
Linear optimization problem.
Warning
This class is experimental and likely to be modified in future releases. To use it, import the
openturns.experimentalsubmodule.This defines a linear problem as:
where
are the problem bounds,
is the cost vector,
is a constant term,
is the constraint matrix term, and
are the constraints bounds terms.
- Parameters:
- costsequence of float
The cost vector
.
- bounds
Interval Bound constraints
- A
MatrixorSparseMatrix Linear inequality constraints matrix
. The sparse form avoids storing the zero coefficients of a large sparse problem.
- cb
Interval Linear inequality constraints bounds terms
- constantfloat, optional
The constant term
. Default is 0.
Methods
Linearize(problem, location)Transform a general optimization problem to a linear problem.
Accessor to bounds.
Accessor to the object's name.
Accessor to the constant term of the objective.
Accessor to input dimension.
Accessor to the equality constraints.
Accessor to the Hessian matrix.
Accessor to the inequality constraints.
Accessor to level function.
Accessor to level value.
Accessor to the linear constraint bounds.
Accessor to the linear constraint term.
Accessor to the linear cost.
getName()Accessor to the object's name.
Accessor to objective function.
Accessor to level function.
Accessor to the linear constraint term in sparse form.
Accessor to the variables type.
Test whether bounds had been specified.
Test whether equality constraints are defined.
Test whether inequality constraints are defined.
Test whether level function had been specified.
Test whether objective function is a scalar or vector function.
hasName()Test if the object is named.
Test whether a least-square problem is defined.
Check if the problem is continuous.
isLinear()Check if the problem is linear.
isMinimization([marginalIndex])Test whether this is a minimization or maximization problem.
Check if the problem is quadratic.
setBounds(bounds)Accessor to bounds.
setConstant(constant)Accessor to the constant term of the objective.
setEqualityConstraint(equalityConstraint)Set the equality constraint function.
setInequalityConstraint(inequalityConstraint)Set the inequality constraint function.
setLevelFunction(levelFunction)Accessor to level function.
setLevelValue(levelValue)Accessor to level value.
setLinearConstraint(*args)Accessor to the linear constraint term.
setLinearCost(cost)Accessor to the linear cost.
setMinimization(minimization[, marginalIndex])Tell whether this is a minimization or maximization problem.
setName(name)Accessor to the object's name.
setObjective(objective)Accessor to objective function.
setResidualFunction(residualFunction)Accessor to level function.
setVariablesType(variableType)Accessor to the variables type.
Notes
The constraint matrix is stored in compressed sparse column form (see
SparseMatrix). Passing a denseMatrixconverts it once and drops its zero coefficients; passing aSparseMatrixstores it as is. A large sparse problem should therefore be built from a sparse matrix, as a dense one would needbytes for
constraints and
variables.
getLinearConstraintCoefficients()rebuilds the dense matrix, whilegetSparseLinearConstraintCoefficients()returns the sparse coefficients without densifying them.Examples
Define a mixed integer linear optimization problem with objective:
with inequality constraints:
and bound constraints:
>>> import openturns as ot >>> import openturns.experimental as otexp >>> cost = [1.1, 1.0] >>> bounds = ot.Interval([0.0, 1.0], [4.0, 1e30], [True, True], [True, False]) >>> A = ot.Matrix([[0.0, 1.0], [1.0, 2.0], [3.0, 2.0]]) >>> lcb = ot.Interval([-1e9, 5.0, 6.0], [7.0, 15.0, 1e9], [False, True, True], [True, True, False]) >>> problem = otexp.LinearProgrammingProblem(cost, bounds, A, lcb) >>> problem.setVariablesType([ot.OptimizationProblemImplementation.INTEGER, ot.OptimizationProblemImplementation.CONTINUOUS])
The same problem can be defined from the sparse constraint matrix, which only stores the non-zero coefficients. The triplets are given by their row index, their column index and their value; duplicate coordinates are summed:
>>> A = otexp.SparseMatrix(3, 2, [0, 1, 1, 2, 2], [1, 0, 1, 0, 1], [1.0, 1.0, 2.0, 3.0, 2.0]) >>> print(A.asDenseMatrix()) [[ 0 1 ] [ 1 2 ] [ 3 2 ]] >>> problem = otexp.LinearProgrammingProblem(cost, bounds, A, lcb) >>> print(problem.getSparseLinearConstraintCoefficients().getNbNonZeros()) 5
- __init__(*args)¶
- static Linearize(problem, location)¶
Transform a general optimization problem to a linear problem.
Objective and inequality constraints coefficients are computed from their gradients at the specified location. The constant term of the linearized objective is set so that it matches the original objective at the location.
- Parameters:
- problem
OptimizationProblem The problem to linearize
- locationsequence of float
The linearization location
- problem
- Returns:
- linearProblem
LinearProgrammingProblem The linear problem
- linearProblem
- getClassName()¶
Accessor to the object’s name.
- Returns:
- class_namestr
The object class name (object.__class__.__name__).
- getConstant()¶
Accessor to the constant term of the objective.
- Returns:
- constantfloat
Constant term.
- getDimension()¶
Accessor to input dimension.
- Returns:
- dimensionint
Input dimension of objective function.
- getEqualityConstraint()¶
Accessor to the equality constraints.
The equality constraints are derived from the linear constraints: a row whose lower and upper bounds are equal,
, defines the equality constraint
.
- Returns:
- equality
Function Linear function
built from the equality rows. Its input dimension is the problem dimension and its output dimension is the number of equality rows. When there is no equality row, the returned linear function has zero output dimension.
- equality
- getHessian()¶
Accessor to the Hessian matrix.
Only relevant for the quadratic problem type.
- Returns:
- hessian
SymmetricMatrix Hessian matrix of the quadratic objective.
- hessian
- getInequalityConstraint()¶
Accessor to the inequality constraints.
The inequality constraints are derived from the linear constraints: each finite bound of a row that is not an equality constraint gives one component, with the convention
. The row
gives the components
and
.
- Returns:
- inequality
Function Linear function
built from the inequality rows. Its input dimension is the problem dimension and its output dimension is the number of finite bounds of the rows that are not equality constraints. When there is no such row, the returned linear function has zero output dimension.
- inequality
- getLevelValue()¶
Accessor to level value.
- Returns:
- valuefloat
Level value.
- getLinearConstraintBounds()¶
Accessor to the linear constraint bounds.
Only relevant for the linear problem type.
- Returns:
- LU
Interval Linear inequality constraint bounds.
- LU
- getLinearConstraintCoefficients()¶
Accessor to the linear constraint term.
Only relevant for the linear problem type.
- Returns:
- A
Matrix Linear inequality constraint term.
- A
Notes
A sparse problem materializes the dense matrix on demand. The experimental
LinearProgrammingProblemprovidesgetSparseLinearConstraintCoefficients()to avoid the densification.
- getLinearCost()¶
Accessor to the linear cost.
Only relevant for the linear problem type.
- Returns:
- cost
Point Linear cost term.
- cost
- getName()¶
Accessor to the object’s name.
- Returns:
- namestr
The name of the object.
- getSparseLinearConstraintCoefficients()¶
Accessor to the linear constraint term in sparse form.
The constraint matrix is returned in compressed sparse column form, without being densified.
- Returns:
- A
SparseMatrix Linear inequality constraint term.
- A
- getVariablesType()¶
Accessor to the variables type.
- Returns:
- variablesType
Indices Types of the variables.
- variablesType
Notes
Possible values for each variable are ot.OptimizationProblemImplementation.CONTINUOUS, ot.OptimizationProblemImplementation.INTEGER and ot.OptimizationProblemImplementation.`BINARY`.
- hasBounds()¶
Test whether bounds had been specified.
- Returns:
- valuebool
True if bounds had been set for this problem, False otherwise.
- hasEqualityConstraint()¶
Test whether equality constraints are defined.
- Returns:
- hasEqualityConstraintbool
True if at least one row of the linear constraints has equal lower and upper bounds.
- hasInequalityConstraint()¶
Test whether inequality constraints are defined.
- Returns:
- hasInequalityConstraintbool
True if at least one row of the linear constraints has a finite bound and is not an equality constraint.
- hasLevelFunction()¶
Test whether level function had been specified.
- Returns:
- valuebool
True if level function had been set for this problem, False otherwise.
- hasMultipleObjective()¶
Test whether objective function is a scalar or vector function.
- Returns:
- valuebool
False if objective function is scalar, True otherwise.
- hasName()¶
Test if the object is named.
- Returns:
- hasNamebool
True if the name is not empty.
- hasResidualFunction()¶
Test whether a least-square problem is defined.
- Returns:
- valuebool
True if this is a least-squares problem, False otherwise.
- isContinuous()¶
Check if the problem is continuous.
- Returns:
- isContinuousbool
Returns True if all variables are continuous.
- isLinear()¶
Check if the problem is linear.
- Returns:
- isLinearbool
Returns True if the problem is of linear type.
- isMinimization(marginalIndex=0)¶
Test whether this is a minimization or maximization problem.
- Parameters:
- marginal_indexint, default=0
Index of the output marginal (for multi-objective only)
- Returns:
- valuebool
True if this is a minimization problem (default), False otherwise.
- isQuadratic()¶
Check if the problem is quadratic.
- Returns:
- isQuadraticbool
Returns True if the problem is of quadratic type.
- setConstant(constant)¶
Accessor to the constant term of the objective.
- Parameters:
- constantfloat
Constant term.
- setEqualityConstraint(equalityConstraint)¶
Set the equality constraint function.
The function must be linear: either a
LinearFunction, or aFunctionwhoseisLinear()method returns True. It is converted to the linear constraints, where
is the transpose of its gradient at the origin and
for a function
such that
is the expected equality constraint. Existing inequality constraints are preserved.
- Parameters:
- equalityConstraint
Function Linear equality constraint
. An empty function clears the equality constraints.
- equalityConstraint
- setInequalityConstraint(inequalityConstraint)¶
Set the inequality constraint function.
The function must be linear: either a
LinearFunction, or aFunctionwhoseisLinear()method returns True. It is converted to the linear constraints, where
is the transpose of its gradient at the origin and
for a function
such that
is the expected inequality constraint. Existing equality constraints are preserved.
- Parameters:
- inequalityConstraint
Function Linear inequality constraint
. An empty function clears the inequality constraints.
- inequalityConstraint
- setLevelFunction(levelFunction)¶
Accessor to level function.
- Parameters:
- levelFunction
Function Level function.
- levelFunction
- setLevelValue(levelValue)¶
Accessor to level value.
- Parameters:
- levelValuefloat
Level value.
- setLinearConstraint(*args)¶
Accessor to the linear constraint term.
Each row
of the constraint matrix defines
, where
and
are the bounds of the constraint interval. Use an infinite bound for a one-sided constraint and equal lower and upper bounds for an equality constraint.
- Parameters:
- A
MatrixorSparseMatrix Linear constraint matrix.
- cb
Interval Linear constraint bounds.
- A
- setMinimization(minimization, marginalIndex=0)¶
Tell whether this is a minimization or maximization problem.
- Parameters:
- minimizationbool
True if this is a minimization problem, False otherwise.
- marginal_indexint, default=0
Index of the output marginal (for multi-objective only)
- setName(name)¶
Accessor to the object’s name.
- Parameters:
- namestr
The name of the object.
- setObjective(objective)¶
Accessor to objective function.
- Parameters:
- objectiveFunction
Function Objective function.
- objectiveFunction
Notes
Constraints and bounds are cleared if the objective has a different input dimension in order to keep the problem valid at all time.
- setResidualFunction(residualFunction)¶
Accessor to level function.
- Parameters:
- levelFunction
Function Level function.
- levelFunction
- setVariablesType(variableType)¶
Accessor to the variables type.
- Parameters:
- variablesType
Indices Types of the variables.
- variablesType
Notes
Possible values for each variable are ot.OptimizationProblemImplementation.CONTINUOUS, ot.OptimizationProblemImplementation.INTEGER and ot.OptimizationProblemImplementation.BINARY.
OpenTURNS