Info

This question is closed. Reopen it to edit or answer.

How can I impose the following optimization function with fmincon?

1 view (last 30 days)
Here the dimensions of the W(w), Epsilon(e), Gamma(g) and b are L*1, n*1, n*1 and L*1. The delta function can be treated as returning 1 at all cases.
I have attached the code I have implemented and I'm not sure whether i'm doing it correct. It would be of great help if some one could assist me with this problem.
w = ones(nnz(tr_ind),2) - 0.5;
lb = [(ones(nnz(tr_ind),1)+-inf) zeros(nnz(tr_ind),1)];
ub = [];
[w,fval] = fmincon('objfunc',[w(1:l,1),w(:,2)],[],[],[],[],lb,ub,'constraints',options);
function [f] = objfunc(x)
load('g.mat');
l = 127;
w = x(1:l,1);
e = x(:,2);
lambda = 1;
f = sum(w)^2 + lambda*(g'*e);
end
function [c,ceq] = constraints(x)
load('b.mat');
load('y.mat');
ceq = [];
y = p;
[n,nc,l] = size(b);
w = x(1:l,1);
e = x(:,2);
classes = [10 12 13 14 15 16 17 19];
margin = zeros(n,1);
for i = 1:n
d = zeros(nc,1);
mat = zeros(l,nc);
for j = 1:nc
mat(:,j) = b(i,j,:);
d(j,1) = w'*mat(:,j);
end
d = d - d(classes == y(i));
[s1,~] = sort(d);
margin(i,1) = s1(2);
end
f = ones(n,1) - e - margin;
if nnz(f > 0) > 0
f = 1;
else
f = -1;
end
c = f;
end
Thank You

Answers (1)

Alan Weiss
Alan Weiss on 4 Aug 2014
I have a few suggestions:
  • Don't load data in your objective and constraint functions. This can slow the solver tremendously. See suggestions for including extra parameters in the documentation.
  • You seem to have discontinuous constraints. fmincon does not work with discontinuous functions.
if nnz(f > 0) > 0
f = 1;
else
f = -1;
end
c = f;
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

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

Start Hunting!