How to optimize the rows of a vector given by a self-built function?

Hi,
I have built a function that takes a large number of scalars (x,y,z,w,...) and gives back a long vector Q = buildQ(x,y,z,w,...).
Now I would like to independently optimize (maximize) each line of Q over all (x,y,z,w,...) given some constraints on them (ub and lb).
I would like to use fmincon to do it, however I don't know how to write the objective functions (Q lines) in the form needed to use it, for what I understand the objective function in fmincon should be a function handle that gives back a scalar.
For example I tried writing Q =@(x,y,z,w,...) buildQ(x,y,z,w,...) and then optimize in a loop, but then I can't index the lines like Q(i), or Q(i)(x,y,z,w,...) etc (matlabs gives an error message saying I can't do this). I also tried writing e.x. Q(1) =@(x,y,z,w,...) buildQ(x,y,z,w,...)(1) and it doesn't work for the same reason.
Is there a smart way to do this??
This sounds like it should be an easy task however I'm confused on how to implement it
If anyone can help, it would be great, thanks

Answers (2)

This sounds like it should be an easy task
Not to me. Is Q(1), Q(2), ...,Q(N) each a function of a different, non-overlapping, independent set of variables?
If so, just feed the scalar objective f(x,y,z..)= sum(Q) to fmincon.
If not, then it is not clear what it means to 'optimize' them. How can you simultaneously minimize a set of quantities that cannot be independently controlled?

4 Comments

It's the same variables, but what I want to do are independent optmizations on each line of Q.
E.x. (x1,y1,z1,w1....) maximize Q(1), (x2,y2,z2,w2....) maximize Q(2), etc.
It's the same variables
Isn't x1 a different variable from x2, and similarly for y1 and y2, etc..? If so, my suggestion to minimize sum(Q) (or maximize -sum(Q)) still stands. You will have M*N unknowns where M*N=numel([x1,y1,z1,w1,...,xN,yN,wN,zN])
No no they are really the same variables (x,y,z,w...) for all the entries in the vector. And anyway I need to obtain the max attainable value for each line separately, so unfortunately I can't just sum them.
Well, unless you can modify your buildQ to restrict the computation to only a single Q(i) (see my other answer) then you must settle for the brute force way,
for i=1:N
unknowns = fmincon(@(unknowns) myObjective(unknowns,i), unknowns0, ______)
end
function negQi=myObjective(unknowns,i)
unknowns=num2cell(unknowns);
Q=buildQ(unknowns{:});
negQi=-Q(i);
end

Sign in to comment.

Matt J
Matt J on 29 May 2021
Edited: Matt J on 29 May 2021
but what I want to do are independent optmizations on each line of Q.
Ideally, you would modify buildQ to let you restrict computation to only a single Q(i) Then you could do N independent optimizations easily in a loop. That would probably be the fastest way, since each Q(i) is a function of a relatively low dimensional space.

1 Comment

Yes, maybe that it's the best thing to do in the end. I thought there existed an easier way to to this that I didn't know about, but I guess not
Thanks a lot!

Sign in to comment.

Asked:

on 29 May 2021

Edited:

on 29 May 2021

Community Treasure Hunt

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

Start Hunting!