Documentation

# OptimizationConstraint

Optimization constraints

## Description

An `OptimizationConstraint` object contains constraint expressions in terms of `OptimizationVariable` objects. Each constraint expression uses one of these comparison operators: `==`, `<=`, or `>=`.

A single expression can represent an array of constraints. For example, you can express the constraints that each row of a matrix variable `x` sums to one in this single expression.

`constrsum = sum(x,2) == 1`

## Creation

Create constraints using optimization expressions with one of these comparison operators: `==`, `<=`, or `>=`.

Include constraints in the `Constraints` property by using dot notation.

```prob = optimproblem; x = optimvar(x,4,6); SumToOne = sum(x,2) == 1; prob.Constraints.SumToOne = SumToOne;```

You can also create an empty optimization constraint by using `optimconstr`. Typically, you then fill the expression in a loop. For examples, see the `optimconstr` function reference page.

## 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`

## Object Functions

 `infeasibility` Constraint violation at a point `showconstr` Display optimization constraint `writeconstr` Save optimization constraint description

## Examples

collapse all

Create a 4-by-6 optimization variable matrix named x.

`x = optimvar('x',4,6);`

Create the constraints that each row of x sums to one.

`constrsum = sum(x,2) == 1`
```constrsum = 4x1 Linear OptimizationConstraint array with properties: IndexNames: {{} {}} Variables: [1x1 struct] containing 1 OptimizationVariable See constraint formulation with showconstr. ```

View the constraints.

`showconstr(constrsum)`
```(1, 1) x(1, 1) + x(1, 2) + x(1, 3) + x(1, 4) + x(1, 5) + x(1, 6) == 1 (2, 1) x(2, 1) + x(2, 2) + x(2, 3) + x(2, 4) + x(2, 5) + x(2, 6) == 1 (3, 1) x(3, 1) + x(3, 2) + x(3, 3) + x(3, 4) + x(3, 5) + x(3, 6) == 1 (4, 1) x(4, 1) + x(4, 2) + x(4, 3) + x(4, 4) + x(4, 5) + x(4, 6) == 1 ```

To include the constraints in an optimization problem, use dot notation.

```prob = optimproblem; prob.Constraints.constrsum = constrsum;```

Watch now