How can I using fmincon on both vector variables and matrix variables
Show older comments
I have a optimization problem with two variables: x is a vector and U is a matrix. I want to solving it by fmincon. My question is, how I can write the fmincon. More exactly, how to write the objective function and constraints? Or I have to transform U into a vector variable.
Any suggestion is appreciated. Thanks!
8 Comments
Torsten
on 16 Feb 2022
You have to use a vector X that comprises x and U, e.g.
U=U(:);
X = [x,U]
Walter Roberson
on 16 Feb 2022
Is U effectively a constant input for the optimization? Or are the values in it being optimized too? Everything being optimized has to be packed into one variable.
Jingyu Liu
on 16 Feb 2022
Jingyu Liu
on 16 Feb 2022
Walter Roberson
on 16 Feb 2022
When you write upper or lower bounds, or linear inequalities, or linear equalities, you do not need to refer to the variable that holds the "current" optimization parameters.
When you write nonlinear constraints, you most often use a function instead of an anonymous function. Inside the function you can take the inputs and unpack them into variables. For example,
function [c, ceq] = nonlcon(X)
x = X(1:20);
U = reshape(X(21:end), 5, 20); %U is 5 x 20
%now use x and U to compute c and ceq
end
I am not clear as to whether you are assigning values to U that are to be used consistently during the call to fmincon, or if fmincon is responsible for changing the U values as part of the optimization? fmincon does not handle integer constaints, so if they are decision variables to be optimized by fmincon then you have a problem.
If U is to be fixed for any one fmincon run, then you should be using a different approach entirely: http://www.mathworks.com/help/matlab/math/parameterizing-functions.html
Jingyu Liu
on 17 Feb 2022
Joshua Scott
on 15 Sep 2023
@Walter Roberson, What if you have both scalars and a matrix that need to be optimized?
Would I: Vectorize the matrix, reshape within the function getting called by fmincon, and reshape after fmincon?
Or am I missing a step?
Thank you for the guidance!
Walter Roberson
on 15 Sep 2023
function [c, ceq] = nonlcon(X) %example
x = X(1);
U = reshape(X(2:end), 5, 20); %U is 5 x 20
%now use x and U to compute c and ceq
end
And yes, after the optimization when you have the packed vector, you would to the same extraction to get the final values.
Accepted Answer
More Answers (0)
Categories
Find more on Choose a Solver in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!