

clearvars 

num_agents = 100;

disp('Classification Based Model Agent Parameter Creation')

continue_while = 1;

while continue_while

    % *************************************************************************
    % *************************************************************************
    % *************************************************************************
    
    %                        CENTER ASSIGNATIONS
    
    disp([ 9 9 'CENTER ASSIGNATIONS'])
    
    Array_ave = 0:100;
    Array_rad = 0:100;
    
    NumFrac = 1;
    
    % Count the number of acceptable combinations of average, radical, and
    % different agents  
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif>=0
                num_combinations = num_combinations + 1;
            end
        end
    end
    
    Percentages = nan*ones(num_combinations,3);
    
    % Create a matrix with the corresponding percentages of average, radical,
    % and different agents 
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif >= 0
                num_combinations = num_combinations + 1;
                Percentages(num_combinations,:) = [n_ave, n_rad, n_dif];
            end
        end
    end
    
    DesiredPoints = [Percentages; 100/3, 100/3, 100/3];
    
    AgentInteralType = cell(NumFrac*(size(DesiredPoints,1)+2),1);
    
    % Count the number of acceptable combinations of average, radical, and
    % different agents  
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif>=0
                num_combinations = num_combinations + 1;
            end
        end
    end
    
    Percentages = nan*ones(num_combinations,3);
    
    % Create a matrix with the corresponding percentages of average, radical,
    % and different agents 
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif >= 0
                num_combinations = num_combinations + 1;
                Percentages(num_combinations,:) = [n_ave, n_rad, n_dif];
            end
        end
    end
    
    col_counter = 0;
    
    % ????????????????????????????????????????????????????????????????????????
    % ????????????????????????????????????????????????????????????????????????
    % Add the fractions related to desired points
    
    for id_row = 1:size(DesiredPoints,1)
    
        % disp([ 9 num2str(id_row) '/' num2str(size(DesiredPoints,1))])
    
        [AugmentedPer, ~] = Aux1_PercentageWeights(Percentages, DesiredPoints(id_row,:));
    
        % Fractional assignations
        for id_col = 1:NumFrac
        
            LBG = nan*ones(3,num_agents);
             
            for id_agent = 1:num_agents
                random_integer = randi(size(AugmentedPer,1),1);
                LBG(:,id_agent) = transpose(AugmentedPer(random_integer,:));
            end
            
            col_counter = col_counter + 1;
            AgentInteralType{col_counter} = LBG;
        
        end
    end
    
    % ????????????????????????????????????????????????????????????????????????
    % ????????????????????????????????????????????????????????????????????????
    % Add the fractions far from the center
    
    [AugmentedPer, ~] = Aux1_PercentageWeights_from_center(Percentages);
    % Fractional assignations
    for id_col = 1:NumFrac
    
        LBG = nan*ones(3,num_agents);
         
        for id_agent = 1:num_agents
            random_integer = randi(size(AugmentedPer,1),1);
            LBG(:,id_agent) = transpose(AugmentedPer(random_integer,:));
        end
        
        col_counter = col_counter + 1;
        AgentInteralType{col_counter} = LBG;
    
    end
    
    % ????????????????????????????????????????????????????????????????????????
    % ????????????????????????????????????????????????????????????????????????
    % Add the fractions chosen uniformly randomly
    
    AugmentedPer = Percentages;
    for id_col = 1:NumFrac
    
        LBG = nan*ones(3,num_agents);
         
        for id_agent = 1:num_agents
            random_integer = randi(size(AugmentedPer,1),1);
            LBG(:,id_agent) = transpose(AugmentedPer(random_integer,:));
        end
        
        col_counter = col_counter + 1;
        AgentInteralType{col_counter} = LBG;
    
    end
    
    AgentInteralType_1 = AgentInteralType;
    
    % *************************************************************************
    % *************************************************************************
    % *************************************************************************
    
    %                        EXTERNAL ASSIGNATIONS
    
    disp([ 9 9 'EXTERNAL ASSIGNATIONS'])
    
    AgentInteralType = cell(NumFrac*(size(DesiredPoints,1)),1);
    
    Array_ave = 0:100;
    Array_rad = 0:100;
    
    % Count the number of acceptable combinations of average, radical, and
    % different agents  
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif>=0
                if (n_ave<10)||(n_rad<10)||(n_dif<10)
                    num_combinations = num_combinations + 1;
                end
            end
        end
    end
    
    Percentages_ext = nan*ones(num_combinations,3);
    
    % Create a matrix with the corresponding percentages of average, radical,
    % and different agents 
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif >= 0
                if (n_ave<10)||(n_rad<10)||(n_dif<10)
                    num_combinations = num_combinations + 1;
                    Percentages_ext(num_combinations,:) = [n_ave, n_rad, n_dif];
                end
            end
        end
    end
    
    Array_ave = 0:2:100;
    Array_rad = 0:2:100;
    
    % Count the number of acceptable combinations of average, radical, and
    % different agents  
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif>=0
                if (n_ave<20)||(n_rad<20)||(n_dif<20)
                    num_combinations = num_combinations + 1;
                end
            end
        end
    end
    
    Percentages_int = nan*ones(num_combinations,3);
    
    % Create a matrix with the corresponding percentages of average, radical,
    % and different agents 
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif >= 0
                if (n_ave<20)||(n_rad<20)||(n_dif<20)
                    num_combinations = num_combinations + 1;
                    Percentages_int(num_combinations,:) = [n_ave, n_rad, n_dif];
                end
            end
        end
    end
    
    Array_ave = 0:5:100;
    Array_rad = 0:5:100;
    
    % Count the number of acceptable combinations of average, radical, and
    % different agents  
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif>=0
                num_combinations = num_combinations + 1;
            end
        end
    end
    
    Percentages_int_int = nan*ones(num_combinations,3);
    
    % Create a matrix with the corresponding percentages of average, radical,
    % and different agents 
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif >= 0
                num_combinations = num_combinations + 1;
                Percentages_int_int(num_combinations,:) = [n_ave, n_rad, n_dif];
            end
        end
    end
    
    Percentages = [Percentages_ext; Percentages_int; Percentages_int_int];
    
    col_counter = 0;
    
    % ????????????????????????????????????????????????????????????????????????
    % ????????????????????????????????????????????????????????????????????????
    % Add the fractions related to desired points
    
    for id_row = 1:size(DesiredPoints,1)
    
        % disp([ 9 num2str(id_row) '/' num2str(size(DesiredPoints,1))])
    
        [AugmentedPer, ~] = Aux1_PercentageWeights(Percentages, DesiredPoints(id_row,:));
    
        % Fractional assignations
        for id_col = 1:NumFrac
        
            LBG = nan*ones(3,num_agents);
             
            for id_agent = 1:num_agents
                random_integer = randi(size(AugmentedPer,1),1);
                LBG(:,id_agent) = transpose(AugmentedPer(random_integer,:));
            end
            
            col_counter = col_counter + 1;
            AgentInteralType{col_counter} = LBG;
        
        end
    end
    
    AgentInteralType_2 = AgentInteralType;
    
    % *************************************************************************
    % *************************************************************************
    % *************************************************************************
    
    %                        LATERAL ASSIGNATIONS
    
    disp([ 9 9 'LATERAL ASSIGNATIONS'])
    
    Array_ave = 0:100;
    Array_rad = 0:100;
    
    % Count the number of acceptable combinations of average, radical, and
    % different agents  
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif>=0
                if (n_ave<10)
                    num_combinations = num_combinations + 1;
                end
            end
        end
    end
    
    Percentages_ext_lat = nan*ones(num_combinations,3);
    
    % Create a matrix with the corresponding percentages of average, radical,
    % and different agents 
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif >= 0
                if (n_ave<10)
                    num_combinations = num_combinations + 1;
                    Percentages_ext_lat(num_combinations,:) = [n_ave, n_rad, n_dif];
                end
            end
        end
    end
    
    
    DesiredPoints = Percentages_ext_lat;
    NumFrac = 1;
    
    % ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
    % ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
    % ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
    
    Lateral_Levels = [3 6 8 10];
    Lateral_Levels_cell = cell(size(Lateral_Levels));
    
    for id_LL = 1:length(Lateral_Levels)
    
        AgentInteralType = cell(NumFrac*(size(DesiredPoints,1)),1);
        % Count the number of acceptable combinations of average, radical, and
        % different agents  
        num_combinations = 0;
        for n_ave = Array_ave
            for n_rad = Array_rad
                n_dif = 100 - n_ave - n_rad;
                if n_dif>=0
                    if (n_ave<Lateral_Levels(id_LL))
                        num_combinations = num_combinations + 1;
                    end
                end
            end
        end
        
        Percentages = nan*ones(num_combinations,3);
        
        % Create a matrix with the corresponding percentages of average, radical,
        % and different agents 
        num_combinations = 0;
        for n_ave = Array_ave
            for n_rad = Array_rad
                n_dif = 100 - n_ave - n_rad;
                if n_dif >= 0
                    if (n_ave<Lateral_Levels(id_LL))
                        num_combinations = num_combinations + 1;
                        Percentages(num_combinations,:) = [n_ave, n_rad, n_dif];
                    end
                end
            end
        end
        
        col_counter = 0;
        
        % ????????????????????????????????????????????????????????????????????????
        % ????????????????????????????????????????????????????????????????????????
        % Add the fractions related to desired points
        
        for id_row = 1:size(DesiredPoints,1)
        
            % disp([ 9 num2str(id_row) '/' num2str(size(DesiredPoints,1))])
        
            [AugmentedPer, ~] = Aux1_PercentageWeights(Percentages, DesiredPoints(id_row,:));
        
            % Fractional assignations
            for id_col = 1:NumFrac
            
                LBG = nan*ones(3,num_agents);
                 
                for id_agent = 1:num_agents
                    random_integer = randi(size(AugmentedPer,1),1);
                    LBG(:,id_agent) = transpose(AugmentedPer(random_integer,:));
                end
                
                col_counter = col_counter + 1;
                AgentInteralType{col_counter} = LBG;
            
            end
        end
    
        Lateral_Levels_cell{id_LL} = AgentInteralType;
    
    end
    
    % *************************************************************************
    % *************************************************************************
    % *************************************************************************
    
    %                        CORNER ASSIGNATIONS
    
    disp([ 9 9 'CORNER ASSIGNATIONS'])
    
    Array_ave = 0:100;
    Array_rad = 0:100;
    
    % Count the number of acceptable combinations of average, radical, and
    % different agents  
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif>=0
                if (n_ave>90)
                    num_combinations = num_combinations + 1;
                end
            end
        end
    end
    
    Percentages_ext_lat = nan*ones(num_combinations,3);
    
    % Create a matrix with the corresponding percentages of average, radical,
    % and different agents 
    num_combinations = 0;
    for n_ave = Array_ave
        for n_rad = Array_rad
            n_dif = 100 - n_ave - n_rad;
            if n_dif >= 0
                if (n_ave>90)
                    num_combinations = num_combinations + 1;
                    Percentages_ext_lat(num_combinations,:) = [n_ave, n_rad, n_dif];
                end
            end
        end
    end
    
    
    DesiredPoints = Percentages_ext_lat;
    NumFrac = 3;
    
    % ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
    % ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
    % ()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
    
    AgentInteralType = cell(NumFrac*(size(DesiredPoints,1)),1);    
    col_counter = 0;
    
    % ????????????????????????????????????????????????????????????????????????
    % ????????????????????????????????????????????????????????????????????????
    % Add the fractions related to desired points
    
    for id_row = 1:size(DesiredPoints,1)
    
        % disp([ 9 num2str(id_row) '/' num2str(size(DesiredPoints,1))])
    
        [AugmentedPer, ~] = Aux1_PercentageWeights(Percentages_ext_lat, DesiredPoints(id_row,:));
    
        % Fractional assignations
        for id_col = 1:NumFrac
        
            LBG = nan*ones(3,num_agents);
             
            for id_agent = 1:num_agents
                random_integer = randi(size(AugmentedPer,1),1);
                LBG(:,id_agent) = transpose(AugmentedPer(random_integer,:));
            end
            
            col_counter = col_counter + 1;
            AgentInteralType{col_counter} = LBG;
        
        end
    end
    
    AgentInteralType_3 = AgentInteralType;
    
    indices_1 = 1:2:size(AgentInteralType_1,1);
    indices_2 = 1:size(AgentInteralType_2,1);
    
    AgentInteralType_1_aux = cell(size(indices_1,2),1);
    AgentInteralType_2_aux = cell(size(indices_2,2),1);
    
    disp([ 9 9 'SELECTING'])
    
    for id_row = 1:size(indices_1,2)
        AgentInteralType_1_aux{id_row} = AgentInteralType_1{indices_1(id_row)};
    end
    for id_row = 1:size(indices_2,2)
        AgentInteralType_2_aux{id_row} = AgentInteralType_2{indices_2(id_row)};
    end
    
    AgentInteralType = [AgentInteralType_1_aux; AgentInteralType_2_aux; ...
                        Lateral_Levels_cell{1}; Lateral_Levels_cell{2}; ...
                        Lateral_Levels_cell{3}; Lateral_Levels_cell{4}; AgentInteralType_3];
    
    AgentInteralType_rot_1 = cell(size(AgentInteralType));
    AgentInteralType_rot_2 = cell(size(AgentInteralType));
    AgentInteralType_rot_3 = cell(size(AgentInteralType));
    
    disp([ 9 9 'ROTATING'])
    
    for id_row = 1:size(AgentInteralType,1)
        AgentInteralType_rot_1{id_row} = AgentInteralType{id_row};
        AgentInteralType_rot_2{id_row} = circshift(AgentInteralType{id_row},1,1);
        AgentInteralType_rot_3{id_row} = circshift(AgentInteralType{id_row},2,1);
    end
    
    clear AgentInteralType
    
    AgentInteralType = [AgentInteralType_rot_1; AgentInteralType_rot_2; AgentInteralType_rot_3];
    save(['./Data/Initial_Data/InnerFractions_' num2str(num_agents) '_agents_all.mat'], 'AgentInteralType');
    
    
    % >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
    % Shorten the parameter space!! 
    
    % At this point there are way too many initial assignations to have results
    % in a sensible time, so they have to be shortened
    clearvars -except SP
    load(['InnerFractions_' num2str(num_agents) '_agents_all.mat'])
    
    num = size(AgentInteralType,1);
    new_indices = 1:30:num; 
    
    new_AgentInteralType = cell(size(new_indices,2),1);
    
    for id_col = 1:size(new_indices,2)
    
    new_AgentInteralType{id_col} = AgentInteralType{new_indices(id_col)};
    
    end
    
    AgentInteralType = new_AgentInteralType;
    
    AgentInteralType_rot_1 = cell(size(AgentInteralType));
    AgentInteralType_rot_2 = cell(size(AgentInteralType));
    AgentInteralType_rot_3 = cell(size(AgentInteralType));
    
    disp([ 9 9 'ROTATING'])
    
    for id_row = 1:size(AgentInteralType,1)
        AgentInteralType_rot_1{id_row} = AgentInteralType{id_row};
        AgentInteralType_rot_2{id_row} = circshift(AgentInteralType{id_row},1,1);
        AgentInteralType_rot_3{id_row} = circshift(AgentInteralType{id_row},2,1);
    end
    
    clear AgentInteralType
    
    AllAgentParameters = [AgentInteralType_rot_1; AgentInteralType_rot_2; AgentInteralType_rot_3];
    
    
    % Check that all the agent parameters representation are different:
    num_agent_parameters = size(AllAgentParameters,1);
    RepresentationAgentParameters = nan*ones(num_agent_parameters,3); % The mean and variance of the susceptibility
    
    for id_AP = 1:num_agent_parameters
                RepresentationAgentParameters(id_AP,:) = CB_RepresentationAgentParameter(AllAgentParameters(id_AP));
    end
    
    unique_average_agent_parameters = size(unique(RepresentationAgentParameters, 'rows'),1);
    
    disp([ 9 9 9 ' ~ Total agent parameters = ' num2str(num_agent_parameters) ' ~~ Total different agent parameters = ' num2str(unique_average_agent_parameters) ])
    
    if unique_average_agent_parameters == num_agent_parameters
        continue_while = 0;
    end

end

save([  'CB_AgentParameters_' num2str(num_agents) '_agents.mat'], 'AllAgentParameters')



