hessian for inequality constraint in fmincon - matlab

I am trying to help fmincon to converge faster by supplying gradient vector and Hessian matrix. I am using interior-point algorithm and I realize that in such case, I have to supply Hessian using a call to another function which is assigned to HessFcn of my OPTIOINS. I have only inequality constraint (C). it is in quadratic form.
gbee_r_i,p_in,nel,nhp are known matrices or variables.
I define constraint as below:
function [ c,ceq,DC,DCeq] = cond5( x,gbee_r_i,p_in,nel,nhp)
nn=0;
bb=0;
for i=1:nel
for j=1:nhp
nn=nn+1;
bb(nn,:)=1*x'*(gbee_r_i(:,:,j,i))'*p_in*gbee_r_i(:,:,j,i)*x;
rr(:,nn)=(gbee_r_i(:,:,j,i))'*p_in*gbee_r_i(:,:,j,i)*x;
end
end
DCeq=[];
DC=rr;
ceq=[];
c=bb;
end
Define options like this:
options = optimoptions(#fmincon,...
'GradObj','on','GradConstr','on','Hessian','user-supplied',...
HessFcn',#(x)hessinterior(x,lambda,gbee_r_i,p_in,nel,nhp),'Display',...
'iter','Algorithm','interior-point','maxFunEvals',20000000000000,'MaxIter',5000,'TolFun',1e-3);
`#(x)hessinterior(x,lambda,gbee_r_i,p_in,nel,nhp)
is HessFcn and is defined as below.
function [ h ] = hessinterior(x,lambda,gbee_r_i,p_in,nel,nhp)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
nn=0;
h=0;
for i=1:nel
for j=1:nhp
nn=nn+1;
h=h+lambda.ineqnonlin(nn)*(gbee_r_i(:,:,j,i))'*p_in*gbee_r_i(:,:,j,i);
end
end
end
after running the program this error is shown
Error using #(x)hessinterior(x,lambda,gbee_r_i,p_in,nel,nhp)
Too many input arguments.
Error in C:\Program Files\MATLAB\R2013a\toolbox\optim\optim\private\computeHessian.p>computeHessian
(line 36)
Error in C:\Program Files\MATLAB\R2013a\toolbox\optim\optim\barrier.p>barrier (line 300)
Error in fmincon (line 900)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...

You need to make a function that is defined such as
hess = #(x,lambda) cond5( x,lambda,gbee_r_i,p_in,nel,nh)
Then define your options as (You need to define it below the above
options = optimoptions(#fmincon,...
'GradObj','on','GradConstr','on','Hessian','user-supplied',...
HessFcn',hess,'Display','iter','Algorithm','interior-point','maxFunEvals',20000000000000,'MaxIter',5000,'TolFun',1e-3);

Related

Why not enough input arguments in nlcon in MATLAB?

I am trying to solve an optimization problem in MATLAB with the function fmincon, but there appears a problem with the nonlinear constraints, as MATLAB tells that there are not enough input arguments.
This is my nlcon funtion:
function [c,ceq] = nlcon(w_md2,std)
c =[];
ceq = w_md2'*std-1;
end
that's the execution of fmincon:
covMat = cov(mon_ret) ;
[corMat, std] = corrcov(covMat);
w0 = repmat(n1, port_size, 1);
md2function = #(w_md2) md2(covMat, w_md2);
nonlincon = #nlcon;
w_md_2 = fmincon(md2function, w0, [], [], Aeq, Beq, lbnds, ubnds, nonlincon)
and that`s the error:
Not enough input arguments.
Error in nlcon (line 3)
ceq = w_md2'*std-1;
Error in fmincon (line 639)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in main (line 60)
w_md_2 = fmincon(md2function, w0, ...
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
Would be really glad if someone could help as I am at total newby to MATLAB.
Best regards
The documentation says that the function passed as argument nonlincon should take one input argument. That means that it is called with only one input argument, and std remains undefined.
You probably want to do
nonlincon = #(x)nlcon(x,std);
That way you create an anonymous function that takes one input argument, and calls your function with the value of std you computed earlier.

changing ObjectiveLimit option in fminunc in matlab

I have a noisy picture that I want to denoise, with specific energy function, my function have 3 free variable which I can change them until the energy function converge to the minimum value,I found values of these 3 variable by testing different value and run the program, but I want to know how can I find them by a good optimization algorithm. as far as I know I can use fminunc function in matlab but it gives me an error:
fminunc stopped because the objective function value is less than or equal to the default value of the objective function limit.
h,beta,v is the 3 variable which initialize by h=0.8,beta=4,v=0.9
here is the enrgy formula
and also my code:
load Input_images.mat;
X=noisyImg();
Z=X;
[n,m]=size(Z);
c=1;
for c=1:5
c
for i=1:n
for j=1:m
[neighbours] = neighbour(i,j,n,m,Z);
sumneighpos=sum(Z(i,j)*neighbours(1,:)');
inputnoisypos=(Z(i,j)*X(i,j));
noisyzpos=h*Z(i,j);
sumpos=noisyzpos-(beta*sumneighpos)-(v*inputnoisypos);
sumneg=-sumpos;
if sumneg<sumpos
Z(i,j)=-Z(i,j);
end
end
end
end
code for the optimization part.
f = #(q,w) h*Z(i,j)-q*sumneighpos-w*inputnoisypos;
fun = #(x) f(x(1),x(2));
x0 = [0.9; 4];
options = optimoptions('fminunc','Algorithm','quasi-newton');
[x, fval, exitflag, output] = fminunc(fun,x0,options);
here I have 2 question how can I fix the error and second one, is there any better algorithm?

Matlab Fmincon "too many output arguments"

I am solving a very simple constrained optimization problem. At this point, I have only entered a constraint that makes the (L-2) vector norm equal 1 and later I hope to add non-negativity constraints.
Fmincon is giving me a "Too many output arguments" on the my constraint. I don't understand why.
Objective function: A simple Quadratic form. Actually a variance covariance Matrix, I am entering as a pre-calculated global variable.
function [y, grady] = quadobj(x)
global Q
y = x*Q*x';
if nargout > 1
grady = 2*Q*x;
end
Equality Constraint: that vector L2 norm should be 1.
function outeq = confuneq2(x)
% Nonlinear equality constraints
outeq = x*x'-1;
end
Fmincon.
x0 = [0.7,0.1, -0.69];
options = optimoptions(#fmincon,'Algorithm','sqp');
[x,fval] = fmincon(#quadobj,x0,[],[],[],[],[],[],...
#confuneq2,options);
But it's not working. I am getting the following error.
Error using confuneq2
Too many output arguments.
Error in fmincon (line 632)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue
Please help!
Confusingly, the problem is that your function has too few output arguments. If you look at the error, it is telling you that MATLAB is trying to call your function with two output arguments but you've programmed it to take only one. Thus it errors because it has called your function with too many output arguments.
All the examples in the docs have two outputs so try create your function this way:
function [out, outeq] = confuneq2(x)
out = x*x'-1;
outeq = [];
end

How to use fmincon to optimize two control vectors of a function

I have a function of 2 different vector. These are the control vector (decision variables) of the function. I want to use fmincon to optimize this function and also get the both control vector results separately.
I have tried to use handle ,#, but I got an error.
The function is:
function f = myFS(x,sv) % x is a vector (5,1)
f = norm(x)^2-sigma*(sv(1)+sv(2));
end
%% I tried to write fmincone to consider both control vectors (x and sv)
[Xtemp(:,h2),Fval, fiasco] = fmincon(#(x,sv)myFS(x,sv)...
,xstart,[],[],[],[],VLB,VUB,#(x,sv)myCon(sv),options);
Here is the error I get:
Error using myFS (line 12) Not enough input arguments.
Error in fmincon (line 564)
initVals.f =
feval(funfcn{3},X,varargin{:});
Error in main_Econstraint (line 58) [Xtemp(:,h2),Fval, fiasco] =
fmincon('myFS',xstart,[],[],[],[],VLB,VUB,#(x,sv)myCon(sv),options);
Thanks
fmincon expects your function to be of a single variable, there is no getting around that, but see:
http://se.mathworks.com/help/optim/ug/passing-extra-parameters.html
for example, if both x, cv are variables of the optimization you can combine them and then split them in the actual objective
for example
x_cv = vertcat(x, cv) and then x = x_cv(1:5); cv = x_cv(6:end)'
if cv is not a variable of the optimization, then 'freeze it' as the link above suggests

MatLab fmincon constrained optimization "Not enough input arguments."

I did this
function f=objfun(w)
a=0.5
w0=[0.1;0.2;0.3];
f=(a^2)/2 + w(1)+ w(2)+ w(3);
[w,fval]=fmincon('objfun',w0,[],[],[],[],[],[],'constraint')
But I got this error message.
Error using objfun (line 3)
Not enough input arguments.
What problem is it talking about?
I learned fmincon from
http://www.math.colostate.edu/~gerhard/classes/331/lab/fmincon.html
and it tells me that codes like this
function f=objfun(x)
f=x(1)^4-x(1)^2+x(2)^2-2*x(1)+x(2);
will be the first lines to do constrained optimization.
What has gone wrong?
I believe you need to pass a function handle to fmincon. From the docs http://www.mathworks.com/help/optim/ug/fmincon.html
x = fmincon(#myfun,x0,A,b)
where myfun is a MATLABĀ® function such as
function f = myfun(x)
f = ... % Compute function value at x
Try passing a function handle to fmincon. I assume that constraints is your non linear constraint function, it should be a function handle as well. I also assume that you are not calling fmincon from inside your objective function. If so then I think you will have some thing like this:
objfun.m
function f = objfun(w)
a=0.5;
f=(a^2)/2 + w(1)+ w(2)+ w(3);
return
end
main.m
w0=[0.1;0.2;0.3];
[w,fval]=fmincon(#objfun,w0,[],[],[],[],[],[],#constraint)