Optimization Expressions

What Are Optimization Expressions?

Optimization expressions are linear combinations of optimization variables.

x = optimvar('x',3,3); % a 3-by-3 variable named 'x'
expr1 = sum(x,1) % add the columns of x, get a row vector
expr2 = sum(x,2) % add the rows of x, get a column vector
expr3 = sum(sum(x.*randn(3))) % add the elements of x multiplied by random numbers
expr4 = randn(3)*x % multiply a random matrix times x
expr5 = sum(sum(x*diag(1:3))) % multiply the columns of x by their column number and sum the result

Optimization expressions also result from many MATLAB® operations on optimization variables, such as transpose or concatenation of variables. For the list of supported operations on optimization expressions, see Supported Operations on Optimization Variables and Expressions.

Optimization modeling functions do not allow you to specify complex, Inf, or NaN values. If you obtain such an expression through operations, the expression cannot be displayed. See Expression Contains Inf or NaN.

Expressions for Objective Functions

An objective function is an expression of size 1-by-1.

y = optimvar('y',5,3);
expr = sum(y,2); % a 5-by-1 vector
expr2 = [1:5]*expr;

The expression expr is not suitable for an objective function because it is a vector. The expression expr2 is suitable for an objective function.

To include an expression as an objective function in a problem, use dot notation, or include the objective when you create the problem.

prob = optimproblem;
prob.Objective = expr2;
% or equivalently
prob = optimproblem('Objective',expr2);

To create an expression in a loop, start with an empty expression as returned by optimexpr.

x = optimvar('x',3,3,'Type','integer','LowerBound',0,'UpperBound',1);
y = optimvar('y',3,3);
expr = optimexpr;
for i = 1:3
    for j = 1:3
        expr = expr + y(j,i) - x(i,j);
    end
end
showexpr(expr)
  y(1, 1) + y(2, 1) + y(3, 1) + y(1, 2) + y(2, 2) + y(3, 2) + y(1, 3) + y(2, 3) + y(3, 3)
- x(1, 1) - x(2, 1) - x(3, 1) - x(1, 2) - x(2, 2) - x(3, 2) - x(1, 3) - x(2, 3) - x(3, 3)

You can create expr without any loops:

x = optimvar('x',3,3,'Type','integer','LowerBound',0,'UpperBound',1);
y = optimvar('y',3,3);
expr = sum(sum(y' - x));
showexpr(expr)
  y(1, 1) + y(2, 1) + y(3, 1) + y(1, 2) + y(2, 2) + y(3, 2) + y(1, 3) + y(2, 3) + y(3, 3)
- x(1, 1) - x(2, 1) - x(3, 1) - x(1, 2) - x(2, 2) - x(3, 2) - x(1, 3) - x(2, 3) - x(3, 3)

Expressions for Constraints

Constraints are any two comparable expressions that include one of these comparison operators: ==, <=, or >=. Comparable expressions have the same size, or one of the expressions must be scalar, meaning of size 1-by-1.

x = optimvar('x',3,2,'Type','integer','LowerBound',0,'UpperBound',1);
y = optimvar('y',2,4);
z = optimvar('z');

constr1 = sum(x,2) >= z;

x is of size 3-by-2, so sum(x,2) is of size 3-by-1. This expression is comparable to z because z is a scalar variable.

constr2 = y <= z;

y is of size 2-by-4. Again, y is comparable to z because z is a scalar variable.

constr3 = (sum(x,1))' <= sum(y,2);

sum(x,1) is of size 1-by-2, so (sum(x,1))' is of size 2-by-1. sum(y,2) is of size 2-by-1, so the two expressions are comparable.

To include constraints in a problem, use dot notation and give each constraint a different name.

prob = optimproblem;
prob.Constraints.constr1 = constr1;
prob.Constraints.constr2 = constr2;
prob.Constraints.constr3 = constr3;

You can also include constraints when you create a problem. For example, suppose that you have 10 pairs of positive variables whose sums are no more than one.

x = optimvar('x',10,2,'LowerBound',0);
prob = optimproblem('Constraints',sum(x,2) <= 1);

To create constraint expressions in a loop, start with an empty constraint expression as returned by optimconstr.

x = optimvar('x',3,2,'Type','integer','LowerBound',0,'UpperBound',1);
y = optimvar('y',2,4);
z = optimvar('z');
const1 = optimconstr(2);
for i = 1:2
    const1(i) = x(1,i) - x(3,i) + 2*z >= 4*(y(i,2) + y(i,3) + 2*y(i,4));
end
showconstr(const1)
(1, 1)

  x(1, 1) - x(3, 1) - 4*y(1, 2) - 4*y(1, 3) - 8*y(1, 4) + 2*z >= 0

(2, 1)

  x(1, 2) - x(3, 2) - 4*y(2, 2) - 4*y(2, 3) - 8*y(2, 4) + 2*z >= 0

You can create const1 without any loops.

x = optimvar('x',3,2,'Type','integer','LowerBound',0,'UpperBound',1);
y = optimvar('y',2,4);
z = optimvar('z');
const1 = x(1,:) - x(3,:) + 2*z >= 4*(y(:,1) + y(:,3) + 2*y(:,4))';
showconstr(const1)
(1, 1)

  x(1, 1) - x(3, 1) + 2*z - 4*y(1, 1) - 4*y(1, 3) - 8*y(1, 4) >= 0

(1, 2)

  x(1, 2) - x(3, 2) + 2*z - 4*y(2, 1) - 4*y(2, 3) - 8*y(2, 4) >= 0

Tip

For best performance, include variable bounds in the variable definitions, not in constraint expressions.

Caution

Each constraint expression in a problem must use the same comparison. For example, the following code leads to an error, because cons1 uses the <= comparison, cons2 uses the >= comparison, and cons1 and cons2 are in the same expression.

prob = optimproblem;
x = optimvar('x',2,'LowerBound',0);
cons1 = x(1) + x(2) <= 10;
cons2 = 3*x(1) + 4*x(2) >= 2;
prob.Constraints = [cons1;cons2]; % This line throws an error

You can avoid this error by using separate expressions for the constraints.

prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;

Optimization Variables Have Handle Behavior

  • OptimizationVariable objects have handle copy behavior. See Handle Object Behavior (MATLAB) and Comparison of Handle and Value Classes (MATLAB). Handle copy behavior means that a copy of an OptimizationVariable points to the original and does not have an independent existence. For example, create a variable x, copy it to y, then set a property of y. Note that x takes on the new property value.

    x = optimvar('x','LowerBound',1);
    y = x;
    y.LowerBound = 0;
    showbounds(x)
        0 <= x

See Also

| | | | | | |

Related Topics

Was this topic helpful?