Main Content

OptimizationInequality

Inequality constraints

Description

An OptimizationInequality object contains an inequality constraint in terms of OptimizationVariable objects or OptimizationExpression objects. An inequality constraint uses the comparison operator <= or >=.

A single statement can represent an array of inequalities. For example, you can express the inequalities that each row of a matrix variable x sums to no more than one in this single statement:

constrsum = sum(x,2) <= 1

Use OptimizationInequality objects as constraints in an OptimizationProblem.

Tip

For the full workflow, see Problem-Based Optimization Workflow.

Creation

Create an inequality using optimization expressions with the comparison operator <= or >=.

Include inequalities in the Constraints property of an optimization problem by using dot notation.

prob = optimproblem;
x = optimvar('x',4,6);
SumLessThanOne = sum(x,2) <= 1;
prob.Constraints.SumLessThanOne = SumLessThanOne;

You can also create an empty optimization inequality by using optimineq or optimconstr. Typically, you then set the inequalities in a loop. For an example, see Create Inequalities in Loop. However, for the most efficient problem formulation, avoid setting inequalities in loops. See Create Efficient Optimization Problems.

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

evaluateEvaluate optimization expression or objectives and constraints in problem
infeasibilityConstraint violation at a point
issatisfiedConstraint satisfaction of an optimization problem at a set of points
showDisplay information about optimization object
writeSave optimization object description

Examples

collapse all

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

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

Create the inequalities that each row of x sums to no more than one.

constrsum = sum(x,2) <= 1
constrsum = 
  4x1 Linear OptimizationInequality array with properties:

    IndexNames: {{}  {}}
     Variables: [1x1 struct] containing 1 OptimizationVariable

  See inequality formulation with show.

View the inequalities.

show(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 inequalities in an optimization problem, set a Constraints property to constrsum by using dot notation.

prob = optimproblem;
prob.Constraints.constrsum = constrsum
prob = 
  OptimizationProblem with properties:

       Description: ''
    ObjectiveSense: 'minimize'
         Variables: [1x1 struct] containing 1 OptimizationVariable
         Objective: [0x0 OptimizationExpression]
       Constraints: [1x1 struct] containing 1 OptimizationConstraint

  See problem formulation with show.

Create the constraint that a two-element variable x must lie in the intersections of a number of disks whose centers and radii are in the arrays centers and radii.

x = optimvar('x',1,2);
centers = [1 -2;3 -4;-2 3];
radii = [6 7 8];
constr = optimineq(length(radii));
for i = 1:length(constr)
    constr(i) = sum((x - centers(i,:)).^2) <= radii(i)^2;
end

View the inequality expressions.

show(constr)
  arg_LHS <= arg_RHS

  where:

        arg1 = zeros(3, 1);
        arg1(1) = sum((x - extraParams{1}).^2);
        arg1(2) = sum((x - extraParams{2}).^2);
        arg1(3) = sum((x - extraParams{3}).^2);
        arg_LHS = arg1(:);
        arg1 = zeros(3, 1);
        arg1(1) = 36;
        arg1(2) = 49;
        arg1(3) = 64;
        arg_RHS = arg1(:);

    extraParams{1}:

     1    -2


  extraParams{2}:

     3    -4


  extraParams{3}:

    -2     3

Instead of using a loop, you can create the same constraints by using matrix operations on the variables.

constr2 = sum(([x;x;x] - centers).^2,2) <= radii'.^2;

Creating inequalities in a loop can be more time consuming than creating inequalities by using matrix operations.

Check the value of an inequality constraint and satisfaction of an equality constraint at a point specified by a structure.

x = optimvar("x");
y = optimvar("y");
ineq = x^2 + y^2/4 <= 2;
eq = 4*x^2 + y^2 == 5;
x0.x = 1;
x0.y = 1;
evaluate(ineq,x0) % Value = L - R, where L =  x^2 + y^2/4 and R = 2
ans = 
-0.7500
issatisfied(eq,x0) % Checks whether 4*x^2 + y^2 = 5
ans = logical
   1

Version History

Introduced in R2019b

expand all