Code covered by the BSD License  

Highlights from
Unbounded Resolution for Function Approximation

image thumbnail

Unbounded Resolution for Function Approximation

by

 

A continuous function is optimized for varying outputs and increasing parameterization dimension

[PointsOut]=Fmin_ConditionNum(CovIn,N,Opt)
function [PointsOut]=Fmin_ConditionNum(CovIn,N,Opt)
% minimize the condition number of my popultion covarience
% for use with DimIncreaseSearchOptimal_fmin_Hist_8

% additional Opt parameters used
% EigGain, how many points to add to increase the worst eigenvalue by one
% PSOPopIn, Initial size of particle swarm
% PSOPop, Running size of swarm
% PSOVelDecay, rate of velocity decay; >0, <1, .3 should be a good value
% PSOGAttract, Conversion of space difference into velocity, 0.01 is a good starting point
% PSOLAttract, Conversion of space difference into velocity, 0.01 is a good starting point
% PSOGenz=50, Number of steps the particles take

% preliminaries/ reference values
% CondNum_In=cond(CovIn)
% temp_eig=eig(CovIn); %this is giving some imaginary numbers
[dummy,S,V]=svd(CovIn);
temp_eig=min(diag(S));
% max(temp_eig)
% min(temp_eig)
% EigDiff=max(temp_eig)-min(temp_eig)
temp_com=combnk(1:N,2);

% Initialize population, generate extra and pick best
% NumPnts=max(Opt.NewPointBatch,ceil(Opt.EigGain*EigDiff))
NumPnts=max(Opt.NewPointBatch,ceil(Opt.EigGain*(Opt.MinEig-min(temp_eig))));
PSOPnts=randn(NumPnts*N,max(round(NumPnts*N*Opt.PSOPopInFactor),Opt.PSOPopIn))*Opt.h/2;
% add a check for points outside the h radius

% evaluate initial population
CondNumList=CondFun(PSOPnts);
[CondNumList,Rank]=sort(CondNumList,'ascend');
% PSOPnts=PSOPnts(:,Rank(1:Opt.PSOPop));
% CondNumList=CondNumList(1:Opt.PSOPop);
% PSOVel=zeros(size(PSOPnts));
Pnt_in=PSOPnts(:,Rank(1));
% [PSOBest,b,c]=fminunc(@(Pnts) cond(CovIn+CovFun(Pnts)),Pnt_in,...
%     optimset('display','final-detailed','LargeScale','off'));
[PSOBest,b,c]=fminunc(@(Pnts) -min(eig(CovIn+CovFun(Pnts))),Pnt_in,...
    optimset('display','off','LargeScale','off'));
% CondNum_Out=cond(CovIn+CovFun(PSOBest))
% max(eig(CovIn+CovFun(PSOBest)))
% min(eig(CovIn+CovFun(PSOBest)))
[dummy,S,V]=svd(CovIn+CovFun(PSOBest));
disp(min(diag(S)))

PointsOut=reshape(PSOBest,NumPnts,N);

% Nested functions used, 
% BEWARE: variables not in inputs or outputs are in function workspace
function [CovOut]=CovFun(PSOPnt)
    Pnts_shaped=reshape(PSOPnt,NumPnts,N);
w=FindWeights(Opt.KernalWeight,Pnts_shaped,zeros(1,N),Opt.h);
Pop_now=[Opt.BasisScale_0*ones(sum(w~=0),1),...
    bsxfun(@minus,Pnts_shaped(w~=0,:),zeros(1,N))/(Opt.h*Opt.BasisScale_1),...
    ((bsxfun(@minus,Pnts_shaped(w~=0,:),zeros(1,N))/Opt.h).^2+Opt.BasisOff_2)/Opt.BasisScale_2,...
    (Pnts_shaped(w~=0,temp_com(:,1)).*Pnts_shaped(w~=0,temp_com(:,2))/(Opt.h^2)+Opt.BasisOff_2)/Opt.BasisScale_2];
W=diag(w(w~=0));
CovOut=(Pop_now.'*(W.'*W)*Pop_now);
end

function [CondNums]=CondFun(Pnts)
CondNums=zeros(1,size(Pnts,2));
for alice=1:size(Pnts,2)
    CondNums(alice)=cond(CovIn+CovFun(Pnts(:,alice)));
end
end

end

Contact us