OptimizationConstraint

An OptimizationConstraint object contains constraints in terms of OptimizationVariable objects or OptimizationExpression objects. Each constraint uses one of these comparison operators: == , = .

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.

Tip

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 == , = , 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

IndexNames — Index names
'' (default) | cell array of strings | cell array of character vectors

Index names, specified as a cell array of strings or character vectors. For information on using index names, see Named Index for Optimization Variables.

Data Types: cell

Variables — Optimization variables in object
structure of OptimizationVariable objects

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 Simple Constraints in Loop

Create a 5-by-3 optimization variable x .

x = optimvar('x',5,3);

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
rowsum = 5x1 Linear OptimizationConstraint array with properties: IndexNames: <>> Variables: [1x1 struct] containing 1 OptimizationVariable See constraint formulation with show.

Show the constraints in rowsum .

show(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

R2024b: OptimizationConstraint supports evaluate and issatisfied

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 TypeValue
L L – R
L >= R R – L
L == R abs(L – R)

For details, see the evaluate and issatisfied reference pages.

R2019b: OptimizationConstraint split into OptimizationEquality and OptimizationInequality

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 = returns an OptimizationInequality object. You can use these new objects for defining constraints in an OptimizationProblem object, exactly as you would previously for OptimizationConstraint objects. Furthermore, you can use OptimizationEquality objects to define equations for an EquationProblem 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 existing OptimizationConstraint objects that represent equality constraints in an EquationProblem object. Furthermore, when you use an OptimizationEquality or an OptimizationInequality as a constraint in an OptimizationProblem , the software converts the constraint to an OptimizationConstraint object.