clc;
clear;
lower_bound = 1; % SMALLEST NUMBER OF LOCATION POSITION
upper_bound = 100; % HIGHEST NUMBER OF LOCATION POSITION

empty_locations = 21; % NUMBER OF EMPTY LOCATIONS
no_locations = 100; % NUMBER OF LOCATIONS
no_items = 79; % NUMBER OF ITEMS TO BE LOCATED

dist = zeros(no_locations,no_locations);
for i = 1:no_locations 
    for j = 1:no_locations
        dist(i,j) = 10;   %sqrt((i-j).^2 + (i - j).^2);
    end
end

for i=1:no_locations
    dist(i,i) = 0;
end
dist;


%T = readtable('Matlab_vstup_sekce.xlsx','Range','A2:C17')
T = readtable('data_matlab_v3.xlsx','Range','H1:O101');
col1 = T.Sekce; % First column of data
col2 = T.Kolize1; % Second column of data
col3 = T.Kolize2;
col4 = T.Kolize3;
col5 = T.Kolize4;
col6 = T.Kolize5;
col7 = T.prazdne;
for i = 1:size(col1)
    pozice1 = col1(i);
    pozice2 = col2(i);
    pozice3 = col3(i);
    pozice4 = col4(i);
    pozice5 = col5(i);
    pozice6 = col6(i);
    if pozice1 > 0 && pozice2 > 0
        dist(pozice1,pozice2) = 1;
        dist(pozice2,pozice1) = 1;
    end
    if pozice1 > 0 && pozice3 > 0
        dist(pozice1,pozice3) = 1;
        dist(pozice3,pozice1) = 1;
    end
    if pozice1 > 0 && pozice3 > 0
        dist(pozice1,pozice3) = 1;
        dist(pozice3,pozice1) = 1;
    end
    if pozice1 > 0 && pozice4 > 0
        dist(pozice1,pozice4) = 1;
        dist(pozice4,pozice1) = 1;
    end
    if pozice1 > 0 && pozice5 > 0
        dist(pozice1,pozice5) = 1;
        dist(pozice5,pozice1) = 1;
    end
end

for i=1:empty_locations 
    a = col7(i)
    dist(a,:) = 0;
    dist(:,a) = 0;
end

dist
%x = rand(1, no_locations); % random points in plane and their distances
%y = rand(1, no_locations);
%dist = zeros(no_locations,no_locations);
%for i = 1:no_locations 
%    for j = 1:no_locations
%        dist(i,j) = sqrt((x(i)-x(j)).^2 + (y(i) - y(j)).^2);
%    end
%end
%scatter(x,y); % figure of all points
%hold on;



frequency = zeros(no_items, 2);
frequency(:,1) = [ 8,9,8,5,7,4,4,6,11,6,6,11,6,5,4,5,10,6,5,10,8,16,20,9,6,8,6,6,6,3,4,5,5,4,7,10,6,3,6,6,0,0,0,0,0,6,11,6,5,5,6,9,9,5,10,6,10,7,5,5,5,3,6,7,6,6,8,11,12,15,13,6,7,3,3,3,4,5,5 ]; % ADD FREQUENCIES HERE [x,y,z...]
%frequency(:,1) = randi(100, no_items, 1); % first column frequency
frequency(:,2) = [ 97,95,93,91,89,87,85,83,81,77,75,73,71,69,67,65,63,61,57,55,53,51,49,47,45,43,41,37,35,33,31,29,27,25,23,19,17,15,13,11,9,7,5,3,1,98,96,94,92,90,88,86,84,82,78,76,74,72,70,68,66,64,62,58,56,54,52,50,48,46,44,42,38,36,34,32,30,28,26, ]; % second column item id
empty_full = zeros(no_locations,1); % all locations start as empty

[~,inx]=sort(frequency(:,1), 'descend'); % sort based on highest frequencies
frequency = frequency(inx,:);

%first two items
maximum = max(max(dist(lower_bound:upper_bound, lower_bound:upper_bound)));
[row_max, column_max] = find(dist(lower_bound:upper_bound, lower_bound:upper_bound)==maximum);
empty_full(row_max(1,1)+lower_bound-1) = frequency(1,2);
empty_full(column_max(1,1)+lower_bound-1) = frequency(2,2);

%remaining items
for i=3:no_items
    % choose only occupied rows, in each column find minimum 
    % (=distance of each location to the closest occupied location) 
    % and then choose maximum (=most distant location to occupied
    % locations)
    occupied = find(empty_full>0);
    occupied_rows = dist(occupied,:);
    [minimums,min_row] = min(occupied_rows(:,lower_bound:upper_bound));
    [maximum, column_max] = max(minimums);
    empty_full(column_max(1,1)+lower_bound-1) = frequency(i,2);
end
occupied = find(empty_full>0);

%for i=1:no_locations
   % disp("lokace");
   % disp(i);
    %disp("identifikator zbozi");
    %disp(empty_full(i));
    %disp(" ")
   

empty_full
%scatter(x(occupied),y(occupied),'red','filled'); % add selected locations to the figure in red