OptimizationConstraint - Optimization constraints - MATLAB (original) (raw)
Description
An OptimizationConstraint
object contains constraints in terms of OptimizationVariable objects or OptimizationExpression objects. Each constraint uses one of these comparison operators: ==
, <=
, or>=
.
A single statement can represent an array of constraints. For example, you can express the constraints that each row of a matrix variable x
sums to one, as shown in Create Simple Constraints in Loop.
Creation
Create an empty constraint object using optimconstr. Typically, you use a loop to fill the expressions in the object.
If you create an optimization expressions from optimization variables using a comparison operators ==
, <=
, or>=
, then the resulting object is either an OptimizationEquality or an OptimizationInequality. See Version History.
Include constraints in the Constraints
property of an optimization problem by using dot notation.
prob = optimproblem; x = optimvar('x',5,3); rowsum = optimconstr(5); for i = 1:5 rowsum(i) = sum(x(i,:)) == i; end prob.Constraints.rowsum = rowsum;
Properties
This property is read-only.
Optimization variables in the object, specified as a structure of OptimizationVariable objects.
Data Types: struct
Object Functions
evaluate | Evaluate optimization expression or objectives and constraints in problem |
---|---|
infeasibility | Constraint violation at a point |
issatisfied | Constraint satisfaction of an optimization problem at a set of points |
show | Display information about optimization object |
write | Save optimization object description |
Examples
Create a 5-by-3 optimization variable x
.
Create the constraint that each row sums to one by using a loop. Initialize the loop using optimconstr
.
rowsum = optimconstr(5); for i = 1:5 rowsum(i) = sum(x(i,:)) == 1; end
Inspect the rowsum
object.
rowsum = 5×1 Linear OptimizationConstraint array with properties:
IndexNames: {{} {}}
Variables: [1×1 struct] containing 1 OptimizationVariable
See constraint formulation with show.
Show the constraints in rowsum
.
(1, 1)
x(1, 1) + x(1, 2) + x(1, 3) == 1
(2, 1)
x(2, 1) + x(2, 2) + x(2, 3) == 1
(3, 1)
x(3, 1) + x(3, 2) + x(3, 3) == 1
(4, 1)
x(4, 1) + x(4, 2) + x(4, 3) == 1
(5, 1)
x(5, 1) + x(5, 2) + x(5, 3) == 1
Version History
Introduced in R2017b
You can now evaluate optimization expressions and constraints using evaluate and issatisfied for OptimizationConstraint
objects.
The value of a constraint depends on the constraint type. An equation is equivalent to an ==
constraint. For expressions L
and R
:
Constraint Type | Value |
---|---|
L <= R | L – R |
L >= R | R – L |
L == R | abs(L – R) |
For details, see the evaluate andissatisfied reference pages.
When you use a comparison operator <=
,>=
, or ==
on an optimization expression, the result is no longer an OptimizationConstraint
object. Instead, the equality comparison ==
returns an OptimizationEquality object, and an inequality comparison<=
or >=
returns an OptimizationInequality object. You can use these new objects for defining constraints in an OptimizationProblem object, exactly as you would previously forOptimizationConstraint
objects. Furthermore, you can useOptimizationEquality
objects to define equations for anEquationProblem object.
The new objects make it easier to distinguish between expressions that are suitable for an EquationProblem
and those that are suitable only for an OptimizationProblem
. You can use existingOptimizationConstraint
objects that represent equality constraints in an EquationProblem
object. Furthermore, when you use an OptimizationEquality
or anOptimizationInequality
as a constraint in anOptimizationProblem
, the software converts the constraint to an OptimizationConstraint
object.