Main Content

OptimizationConstraint

Optimization constraints

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 Compatibility Considerations.

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

expand all

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

This property is read-only.

Optimization variables in the object, specified as a structure of OptimizationVariable objects.

Data Types: struct

Object Functions

infeasibilityConstraint violation at a point
showDisplay information about optimization object
writeSave optimization object description

Examples

collapse all

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

expand all