function marginwrapper cd /afs/cs.cmu.edu/project/lemur-1/lkontor/Archive/CURRENT/matlab_work/string_proc/combinatorics addpcurdir cd ../../fsa/ global ALF ALF = 'ab'; global fname REPFID fname = 'runningreport.txt'; REPFID = fopen(fname,'w'); myprint('Exploring the margin of PT languages\n'); TESTREC = []; good = 1; while good % generate rand pt-language goodM = 0; while ~goodM fprintf('generating random pt-language...\n'); %M = randpt(ALF,0,2,1,15); M = randpt(ALF,1,4,1,15); % have we observed M before? newM = 1; for i=1:length(TESTREC) newM = ~empty_fsa(symdiff_dfsa(M,TESTREC(i))); if ~newM, break; end end S = simple_paths(M); L = 0; for i=1:length(S) L=max(L,length(S{i})); end %goodM = (newM & (L<=10)); % just to keep length reasonable %** goodM = (newM & (L<=8)) % just to keep length reasonable end TESTREC = [TESTREC; M]; myprint('----------------------------------------------------------\n'); myprint(fsa2str(M)); % generate sample of length 0 to L [X,Y] = reglang_posneg_dense(0,L,M); H = gram_matrix(X,@ptkermx,Y); alpha = mysvm(H,Y); %[nsv, alpha, b0] = svc_gram(H,Y) ii = find(alpha>1e-5); % display features & weights print_xa(X(ii),alpha(ii)); rho = min(abs(H)*alpha); % margin R = sqrt(max(diag(H))); % radius myprint(sprintf('[margin=%1.9f] \n',R/rho)); nQ = length(M.Q); %gam = R/rho; gam = rho; %hold on %plot(nQ,rho,'o'); %drawnow end fclose(REPFID); return function print_uw(u,w) for i=1:length(u) myprint(sprintf('%3d. u=[%s] w = %1.5f \n',i,u{i},w(i))); end return function print_xa(x,a) for i=1:length(x) myprint(sprintf('%3d. x=[%s] alpha = %1.5f \n',i,x{i},a(i))); end return function str = fsa2str(DFSA) str = ''; DS = deadstate_dfsa(DFSA); str = [str sprintf('Alphabet: "%s"\n',DFSA.ALF)]; str = [str sprintf('The states Q: %s\n',num2str(DFSA.Q))]; str = [str sprintf('Start state: %d\n',DFSA.q0)]; str = [str sprintf('Accepting states: [%s]\n',num2str(sort(DFSA.F)))]; str = [str sprintf('Dead states: %s\n',num2str(DS))]; for q = setdiff(DFSA.Q,DS) % the new, more convenient to view way [rr,ss] = get_ddelta(DFSA,q); % don't show dead state transitions %x = find(~ismember(rr,DS)); % DO show deadstate transitions x = find(rr); rr=rr(x); ss=ss(x); ru = unique(rr); if ~isempty(ru) for r=ru x=find(r==rr); S=ss(x); str = [str sprintf('(%3d,%5s) -> %3d \n',q,S,r)]; end end end return function myprint(str) global fname REPFID fprintf(str); fprintf(REPFID,str); fclose(REPFID); REPFID = fopen(fname,'a'); return function s = sample_from_sp(R,lam) % larger lam => longer strings s = ''; for i=1:length(R) A = R{i}; if length(A)==1 s(end+1) = A; else while 1 a = randel(A); if a == '@' if rand > lam break end else s(end+1) = a; end end end end s = strrep(s,'@',''); return