function [outperm,coord,dev,fit,addcon] = uniscallpac(prox,inperm)


%UNISCALLPAC carries out a unidimensional scaling of a symmetric proximity
%  matrix using iterative linear programming, with the inclusion of an
%  additive constant (ADDCON) in the model.
%  PROX is the input proximity matrix (with a zero main diagonal and a 
%  dissimilarity interpretation);
%  INPERM is the input beginning permutation (a permutation of the first $n$ integers).
%  OUTPERM is the final permutation of PROX.
%  COORD is the set of coordinates of the unidimensional scaling 
%  in ascending order;
%  DEV is the value of deviance (the normalized $L_{1}$ loss function) for the
%  coordinates and object permutation; and FIT is the matrix being fit to
%  PROX(OUTPERM,OUTPERM) with the given deviance.
%
%  syntax: [outperm,coord,dev,fit,addcon] = uniscallpac(prox,inperm)

tic;
n = size(prox,1);
outperm = inperm;

[fit begindex coord addcon exitflag] = linfitl1ac(prox,inperm);

nchange = 1;

while (nchange == 1)
    
    nchange = 0;
    
          
    for k = 1:(n-1)
        for j = (k+1):n
           
        intrperm = outperm;
        intrperm(k) = outperm(j);
        intrperm(j) = outperm(k);
           
        [fit tryindex coord addcon exitflag] = linfitl1ac(prox,intrperm);
        
       
        if ((tryindex > (begindex + 1.0e-008)) & (exitflag > 0))
            
        
            nchange = 1;
            begindex = tryindex;
            outperm = intrperm;
        end
    end
end
end 

[fit dev coord addcon exitflag] = linfitl1ac(prox,outperm);
toc