function [a,b]=smo(x,y,c,tol,max_passes) num=size(x,2); a=zeros(num,1);b=0; passes=0; e=zeros(num,1); while passes<max_passes num_changed_alphas=0; for i=1:num f2=0; for k=1:num f2=f2+a(k)*y(k)*(x(:,k)'*x(:,i)); end f2=f2+b; e(i)=f2-y(i); if y(i)*e(i)<-tol&&a(i)<c || y(i)*e(i)>tol&&a(i)>0 j=ceil(rand(1,1)*num); while j==i j=ceil(rand(1,1)*num); end ai_old=a(i);aj_old=a(j); f=0; for k=1:num f=f+a(k)*y(k)*(x(:,k)'*x(:,j)); end f=f+b; e(j)=f-y(j); if y(i)~=y(j) L=max(0,a(j)-a(i)); H=min(c,c+a(j)-a(i)); else L=max(0,a(i)+a(j)-c); H=min(c,a(i)+a(j)); end if L==H continue; end eta=2*x(:,i)'*x(:,j)-x(:,i)'*x(:,i)-x(:,j)'*x(:,j); if eta>=0 continue end a(j)=a(j)-y(j)*(e(i)-e(j))/eta; if a(j)>H a(j)=H; elseif a(j)<L a(j)=L; end if a(j)-aj_old<1e-5 continue end a(i)=a(i)+y(i)*y(j)*(aj_old-a(j)); b1=b-e(i)-y(i)*(a(i)-ai_old)*(x(:,i)'*x(:,i))-y(j)*(a(j)-aj_old)*(x(:,i)'*x(:,j)); b2=b-e(j)-y(i)*(a(i)-ai_old)*(x(:,i)'*x(:,j))-y(j)*(a(j)-aj_old)*(x(:,j)'*x(:,j)); if a(i)>0&&a(i)<c b=b1; elseif a(j)>0&&a(j)<c b=b2; else b=(b1+b2)/2; end num_changed_alphas=num_changed_alphas+1; end end if num_changed_alphas==0 passes=passes+1; else passes=0; end end end
function predict=smopredict(a,b,x,y,test) num=max(size(a,1),size(a,2)); f=zeros(1,size(test,2)); for i=1:num f=f+a(i)*y(i)*x(:,i)'*test; end f=f+b; predict=-ones(1,size(test,2)); predict=predict+2*(f>0); end
x=[0.9,0.8,0.7,0.2,0.1,0.1,0.2;0.1,0.2,0.1,0.9,0.7,0.99,0.777]; y=[1,1,1,-1,-1,-1,-1]; [a,b]=smo(x,y,1,1e-3,100); test=[0.9,0.7,0.6,0.1,0.3,0.2,0;0.1,0.2,0.1,0.9,0.8,0.7,0.88]; predict=smopredict(a,b,x,y,test)