%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%	DLSSVM optimization method described in
%	"Object Tracking via Dual Linear Structured SVM and Explicit Feature Map", 
%   Jifeng Ning, Jimei Yang, Shaojie Jiang, Lei Zhang and Ming-Hsuan Yang, 
%   CVPR,Las Vegas, 4266-4274, 2016.
%	
%	Copyright (C) 2016 Jifeng Ning, Shaojie Jiang
%	e-mail:shaojiejiang@126.com, jf ning@sina.com
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [w0, patterns]=dlssvmOptimization(patterns,params, w0)
% parameters:
%   patterns: all training set
%   params
%   w0: initial discriminative classifer
% returns:
%   w0: updated classifer
P=5;
% start=cputime;
for p=1:P
    n=size(patterns,2);
    if p==1
        idPat=n;
    else
        idPat = n - floor((p-1)*n/P);
    end

    patterns=updateWorkingSet(patterns,w0, idPat,1);
    [w0, patterns]=updateOneAlpha(patterns,w0,params,idPat);
    
    % maintian the budgets for support vectors 
    svSize=getSVSize(patterns);
    while svSize > params.nBudget
        [w0, patterns]=svBudgetMaintain(w0, patterns);
        svSize=getSVSize(patterns);
    end
    
    for q=1:10
        n=size(patterns,2);
        idPat = n - floor(n*(q-1)/10);
        [w0, patterns]=updateOneAlpha(patterns,w0,params,idPat);
    end
end