function [find, vaf, outperm, addcon] = ms_unicirac(prox, inperm, kblock)
% UNICIRAC finds and fits a circular
% unidimensional scale using iterative projection to
% a symmetric proximity matrix in the $L_{2}$-norm based on a
% permutation identified through the use of iterative
% quadratic assignment.
%
% syntax: [find, vaf, outperm, addcon] = unicirac(prox, inperm, kblock)
%
% PROX is the input proximity matrix ($n \times n$ with a
% zero main diagonal and a dissimilarity interpretation);
% INPERM is a given starting permutation (assumed to be around the
% circle) of the first $n$ integers;
% FIND is the least-squares optimal matrix (with
% variance-accounted-for of VAF) to PROX having a circular
% anti-Robinson form for the row and column
% object ordering given by the ending permutation OUTPERM.
% The spacings among the objects are given by the diagonal entries
% in FIND (and the extreme (1,n) entry in FIND). KBLOCK
% defines the block size in the use of the iterative quadratic
% assignment routine. The additive constant for the model is
% given by ADDCON.
%tic;
n = size(prox,1);
dumone = zeros(1,1);
targcircular = zeros(n,n);
for i = 1:n-1
for j = (i+1):n
targcircular(i,j) = min(abs(i-j),n-abs(i-j));
targcircular(j,i) = targcircular(i,j);
end
end
[outperm,rawindex,index] = ms_order(prox,targcircular,inperm,kblock);
[fit,vaf,addcon] = ms_cirfitac(prox,outperm,dumone);
if (any(outperm-inperm) ~= 0)
inperm = outperm;
[outperm,rawindex,index] = ms_order(prox,fit,inperm,kblock);
[fit,vaf,addcon] = ms_cirfitac(prox,outperm,dumone);
end
find = fit;
%toc;