load lincoln_eighth_circuit_distance.dat

prox = lincoln_eighth_circuit_distance

tic;

n = 14;

best_vaf = 0.0;
store_vaf = zeros(100,1);

for i = 1:100
    
    [outpermone,outpermtwo,coordone,coordtwo,fitone,fittwo,addconone,...
        addcontwo,vaf] = ms_biscalqa(prox,targlin(n),targlin(n), ...
        randperm(n),randperm(n),3,1);
    
    store_vaf(i) = vaf;
    
    if (vaf > best_vaf)
        
        best_vaf = vaf;
        best_outpermone = outpermone;
        best_outpermtwo = outpermtwo;
        best_coordone = coordone;
        best_coordtwo = coordtwo;
        best_fitone = fitone;
        best_fittwo = fittwo;
        best_addconone = addconone;
        best_addcontwo = addcontwo;
        
    end
    
end

sorted_vafs = sort(store_vaf');

sorted_vafs

        best_vaf
        best_outpermone 
        best_outpermtwo
        best_coordone
        best_coordtwo
        
        best_addconone
        best_addcontwo 
        
        axes = zeros(n,2);
        
        for i = 1:n
            
            axes(best_outpermone(i),1) = best_coordone(i);
            axes(best_outpermtwo(i),2) = best_coordtwo(i);
        end
        
        figure(1)
        
        plot(axes(1:n,1),axes(1:n,2),'ko')
        
        hold on
        
objectlabels = char('Ur','Da','Pa','Sh','Su','De','Ta','Sp','Pe','Me','Bl','Pu','Cl','Mo');
        
        text(axes(1:n,1),axes(1:n,2),objectlabels,'fontsize',10,'verticalalignment','bottom')
        
        
toc;

cityblock_coordinates = axes;



tic;

opts = statset('Maxiter',1000);

best_vaf = 0.0;
store_vaf = zeros(100,1);

for k = 1:100

[coords,stress] = mdscale(prox,2,'Criterion','metricstress', 'Start',...
    'random','Replicates',1,'Options',opts);

n = size(coords,1);

distance_matrix = zeros(n,n);

for i = 1:n
    for j = 1:n
        
        distance_matrix(i,j) = sqrt(((coords(i,1) - coords(j,1))^2) + ...
            ((coords(i,2) - coords(j,2))^2));
    end
end

ave_diff = (sum(sum(prox - distance_matrix)))/(n*(n-1));

ave_proximity = (sum(sum(prox)))/(n*(n-1));

prox_vec = squareform(prox);

distance_vec = squareform(distance_matrix);

r = corrcoef(prox_vec',distance_vec');
vaf = r(1,2)^2;

store_vaf(k) = vaf;

if(vaf > best_vaf)
    
    best_vaf = vaf;
    best_coords = coords;
    
end
end

store_vaf;

sorted_vafs = sort(store_vaf');

sorted_vafs
best_vaf
best_coords

figure(2)

axis equal

plot(best_coords(:,1),best_coords(:,2),'ko')

hold on


text(best_coords(:,1),best_coords(:,2),objectlabels,'fontsize',10,'verticalalignment','bottom')

toc;

euclidean_coordinates = [best_coords(:,1),best_coords(:,2)];

[d,z,transform] = procrustes(cityblock_coordinates,euclidean_coordinates);

figure(3)

plot(cityblock_coordinates(:,1),cityblock_coordinates(:,2),'rx',...
    euclidean_coordinates(:,1),euclidean_coordinates(:,2),'b.',...
    z(:,1),z(:,2),'ko')

hold on

text(cityblock_coordinates(:,1),cityblock_coordinates(:,2),objectlabels,'fontsize',8,'verticalalignment','bottom')

text(z(:,1),z(:,2),objectlabels,'fontsize',8,'verticalalignment','bottom')

transform(1).b

transform(1).T

transform(1).c

