How can I find indices of an OptimizationExpression array with the value of "Expression with all zero coefficients"?

3 Ansichten (letzte 30 Tage)
Let an optimization expression is given by: expr = optimexpr(5,1); Suppose that only expr(1) and expr(2) are assigned, and we know that expr(3), expr(4), and expr(5) are empty (Expression with all zero coefficients). How can I find indices of an Optimization Expression array with the values of "Expression with all zero coefficients". For instance, a function returns: inds = [0 0 1 1 1].
Note: This is particularly useful when constructing constraints without for loops. We begin with a relatively large OptimizationExpression, perform necessary operations in a vectorized form, and then only non-zero coefficients are used to form constraints/objective function.

Akzeptierte Antwort

ilker golcuk
ilker golcuk am 20 Dez. 2018
The function "evaluate" solves the problem. Evaluating the expression at point 1 (or any other point depending on the problem) implicitly gives the empty cells in the expression.
clear; clc;
% Decision variables
x = optimvar('x',[5,1]);
% Empty expression
expr = optimexpr(size(x));
% Assignments for the expression
expr(1) = x(1);
expr(2) = x(2);
% Detect empty cells in the expression and remove them
inds = expr.evaluate(struct('x',ones(size(x)))) == 0; % Evaluate at point 1.
expr(inds) = [];
% Generate constraints from the expression
% if the empty expressions are not removed, the constraint 0 >= 5 is being generated!!!
cons1 = expr >= 5;

Weitere Antworten (0)

Kategorien

Mehr zu Problem-Based Optimization Setup finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by