How do i use optimization toolbox to optimize an objective function with matrix input variables (4x4)
Show older comments
I have an objective function and non linear constraint m file as follows:
objective function
function k = mut(u)
load('matlab1.mat')
k = (exp(u(1)*test__TT*u(2)));
end
non linear constraint
function [c,ceq] = constr(u)
load('matlab1.mat')
% Nonlinear inequality constraints
c = (exp(u(1)*test__TT*u(2)))-B;
ceq = [];
end
% End of function
test_TT input variable is a 4x4 matrix
i am solving using the optimization tool box "fmincon". u0 = [0 0]
when i run the optimization solver, i get an error saying "Supplied objective function must return a scalar value."
basically, my output should be two (4x4) optimal variables for u(1) and u(2).
how to i setup the problem to return solution thats calculates the optimal for each variable matrix pair (element wise).
Answers (1)
Alan Weiss
on 3 Feb 2017
0 votes
You can only optimize scalar objective functions. What does it mean to optimize two or more functions at the same time? Either optimize each function separately, or find a Pareto front for a multiobjective optimization.
In terms of programming efficiency, you should not put a load statement in an objective function or nonlinear constraint function. Instead, load the data into your workspace just once, and then pass parameters using anonymous functions or nested functions as you prefer.
Alan Weiss
MATLAB mathematical toolbox documentation
4 Comments
adewale oduwole
on 6 Feb 2017
Walter Roberson
on 6 Feb 2017
Edited: Walter Roberson
on 6 Feb 2017
You cannot return a 4 x 4 output from your objective function to fmincon or related functions.
Now, if u is the input which is to be found, and a computation on u results in a scalar output of fitness, then you can use it. See https://www.mathworks.com/help/optim/ug/matrix-arguments.html . The output from fmincon will be the matrix of values that optimized the function.
adewale oduwole
on 11 Feb 2017
Walter Roberson
on 11 Feb 2017
Does your objective function take multiple inputs and return a scalar output? If so, then don't worry about it: just pass in a matrix for the initial values of the variable and you will get out a matrix of the same size, and your objective function will be passed the variable in the form of a matrix.
Does your objective function return a non-scalar output? If so then that cannot be handled by MATLAB using fmincon or ga or particleswarm or patternsearch or simulated annealing. It can be handled by gamultiobj() though.
Categories
Find more on Problem-Based Nonlinear Optimization 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!