function [sol, pert, f, prob, mycount, myabsum, mygckl, mygacv,part1,part2,mA] = solve(mylambda, mysigma)
%function [mycount, myabsum, mygckl, mygacv] = solve(mylambda, mysigma)
%count is number of nonzeros of [1:n], [n+1,2n], and [(d-1)*n,d*n]
%absum is sum of absolute value of [1:n], [n+1,2n], and [(d-1)*n,d*n]

global B;
global y;
global x;
global n dim m;

sigmaz = 0.001;
tol = 10^(-6);

pert = zeros(n,1);
newy = ones(n,1);
mycount = zeros(1,dim);
myabsum = zeros(1,dim);


for i=1:n
  rand('state', i+n);
  u = rand(1,1);
  if u < 0.5
     pert(i,1) = sigmaz;
     newy(i,1) = y(i,1) + sigmaz;
  else
     pert(i,1) = -sigmaz;
     newy(i,1) = y(i,1) - sigmaz;
  end
end

display('************lambda******')
mylambda
display('************sigma********')
mysigma
display('*******n and m******')



z0 = zeros(2*m, 1);
b1 = zeros(2*m,1);
b2 = 10^16*ones(2*m,1);
vlb = [-10^16*ones(m,1);zeros(m,1)];
vub = b2;


B = zeros(n,m);
for k=1: dim
   for i = 1: n
       for j = 1:n
           B(i, (k-1)*n+j) = exp(-(x(i,k)-x(j,k))^2/(2*mysigma^2));
       end
   end
end


p = -B'*(ones(n,1)+y)/(2*n);
q = mylambda*ones(m,1);
c = [p;q];


newp =  -B'*(ones(n,1)+newy)/(2*n);
newc = [newp;q];


A = sparse([-eye(m)       eye(m);
             eye(m)       eye(m)]);


sol  = nlp(z0, 'fobj', 'fcon', c, A, b1, b2);
newsol = nlp(z0, 'fobj', 'fcon', newc, A, b1, b2);


for i = 1:dim
   myabsum(1,i) = sum(abs(sol((i-1)*n+1:i*n,1)));
end


for i = 1:dim
   mycount(1,i) = 0;
   for j = 1:n
       if abs(sol((i-1)*n+j)) >  tol;
            [i,j]
            mycount(1,i) = mycount(1,i) +1; 
       end
   end
end


f = B*sol(1:m,1);
newf = B*newsol(1:m,1);


tmp = 3*x(i,1)^2-4*x(i,2);
truep = exp(tmp)./(1+exp(tmp));
truemu = 2*truep-1;

prob = exp(f)./(1+exp(f));
mu = 2*prob-1;


var = 4*prob.*(1-prob);


mygckl = sum(-truemu.*f + 2*log(1+exp(f))-f)/n;
trH = sum(pert.*(newf-f));
trW = n *sigmaz^2 - 0.5*sum(pert.*var.*(newf-f));
obs = sum(ones(n,1)-y.*mu) *trH/(n*trW);

mygacv = sum(-y.*f + 2*log(1+exp(f))-f)/n + obs;

part1 = sum(-y.*f + 2*log(1+exp(f))-f)/n;
part2 = sum(ones(n,1)-y.*mu)/n * sum(pert.*(newf-f))/(n*sigmaz^2-0.5*sum(pert.*var.*(newf-f)));


mA = sum(-y.*f + 2*log(1+exp(f))-f)/n + sum(pert.*(newf-f))/(n*sigmaz^2-sum(0.5*pert.*var.*(newf-f)))*sum(ones(n,1)-y.*mu)/n;






