Define a Non linear objective function with 2 decision variables

2 views (last 30 days)
I have a matrix X (932x1404) and I want to find 2 matrices W(932x2) , H(2X1404) in order to minimize the error abs(X-WH) over W and H. It is similar to a non negative matrix factorization problem but I want to add specific constraints. The matrix X actually is a black and white image with values 0-255 and I want the W to have similar values (to represent the centroids of each class) and the matrix H to have values from 0 to 1 but with the sum of the values in each column to equal 1 (something like weights). So the problem I want to define in matlab is the following:
min_W,H (|X-W*H|) (o.f.)
under the constraints
Wik>=0 (c1)
Wik<=255 (c2)
Hkj>=0 (c3)
Hkj <= 1 (c4 , not neccessary I guess due ot the following constraint)
sum_k Hkj =1 for each column j of Matrix H (c5)
where i is the ith row and k the kth column of matrix W
and for matrix H the k denotes the kth row and j the jth column
the sum_k denotes the sum over all rows entries for a specific column j. for example for the first column we sum all the rows and so on.
Could you please let me know how I can formulate such a problem in matlab? I watched various tutorials for optimization toolbox but I have not figured it out yet.
Thank you in advance for your help
  2 Comments
Matt J
Matt J on 28 Oct 2019
Is it important that it be min(abs(error)) or would you be just as happy with least squares?
Charis Nt
Charis Nt on 28 Oct 2019
Edited: Charis Nt on 28 Oct 2019
for the moment any norm that could be used as 'termination criterio' or a norm in which we willl minimize would be fine.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 28 Oct 2019
Edited: Matt J on 28 Oct 2019
If you have R2109a or higher, the general set-up could be done like this. For simplicity, I demonstrate using a least squares objective.
W=optimvar('W',932,2,'LowerBound',0,'UpperBound',255);
H=optimvar('H',2,1404,'LowerBound',0,'UpperBound',1);
prob=optimproblem;
prob.Objective=sum(W(:))+sum(H(:)); %dummy objective - not used
prob.Constraints.sumH=sum(H,1)==1;
idxH=1:numel(H); idxW=numel(H)+1:numel(W)+numel(H);
p=rmfield(prob2struct(prob),{'f','f0','solver'});
p.fun=@myObjective(z,X,idxH,idxW);
p.x0([idxW;idxH])=[W0(:);H0(:)];
solution=mapSolution(prob, fmincon(p))
function err=myObjective(z,X,idxH,idxW)
W=z(idxW);
H=z(idxH);
err=norm(X-W*H,'fro').^2;
end
  2 Comments
Charis Nt
Charis Nt on 28 Oct 2019
Thank you for your answer
I will try to run it and let you know. However I am using matlab 2018b, do you think that it will be an issue?
Matt J
Matt J on 28 Oct 2019
Edited: Matt J on 28 Oct 2019
Yes, you won't be able to use varindex in R2018b. I've rewritten the code to not use it.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!