

clearvars
close all

num_initial_opinions = 5000;
num_agents = 100;

InitialOpinion_cell = cell(num_agents,1);
IO_Variable = nan*ones(num_agents,2);

id_initial_condition = 0;

mean_array = 0.01:0.01:1;
aux_counter_1 = 0;

while id_initial_condition < num_initial_opinions

    % disp([ 9  num2str(id_initial_condition) '/' num2str(num_initial_opinions)])
    aux_counter_1 = aux_counter_1 + 1;
    if aux_counter_1>size(mean_array,2)
        aux_counter_1 = 1;
    end

    initial_opinions = rand(num_agents,1);

    des_mean = mean_array(aux_counter_1);
    des_mean  = des_mean + (0.05*rand(1));

    initial_opinions = BiasingFunction(initial_opinions, des_mean);

    % At this point the mean of the initial opinions shoud be the correct one 
    if ~isnan(initial_opinions)

        num_combinations = 100;
        negative_percentage = linspace(0,0.5,num_combinations);

        for id_combination = randi(num_combinations,1) 
            % add some signs 

            initial_opinion_signes = (2*double(rand(num_agents,1) > negative_percentage(id_combination))) - ones(size(initial_opinions));
            signed_initial_opinions = initial_opinions.*initial_opinion_signes;

            var_1 = mean(signed_initial_opinions);
            var_2 = mean(abs(signed_initial_opinions));

            SoFar = size(unique(round(IO_Variable(1:id_initial_condition,:),2), 'rows'),1);
            NewSoFar = size(unique(round([IO_Variable(1:id_initial_condition,:); var_1, var_2],2), 'rows'),1);

            if SoFar == (NewSoFar-1)
                accepted = 1;
            else
                accepted = 0;
            end

            if (id_initial_condition < num_initial_opinions)&&accepted

                id_initial_condition = id_initial_condition + 1;
                InitialOpinion_cell{id_initial_condition} = signed_initial_opinions;
                IO_Variable(id_initial_condition,:) = [var_1, var_2];

            end

            if (id_initial_condition < num_initial_opinions)&&accepted

                id_initial_condition = id_initial_condition + 1;
                signed_initial_opinions = -signed_initial_opinions;
                InitialOpinion_cell{id_initial_condition} = signed_initial_opinions;
                var_1 = mean(signed_initial_opinions);
                var_2 = mean(abs(signed_initial_opinions));
                IO_Variable(id_initial_condition,:) = [var_1, var_2];

                disp([ 9  num2str(id_initial_condition) '/' num2str(num_initial_opinions)])
            end
        end
    end
end

AllInitialOpinions = InitialOpinion_cell;

TotalAgents = num_agents;

Now = datetime('now');
time_stamp = datestr(Now, 'yyyymmddHHMMSS');

save(['InitialOpinions_' num2str(TotalAgents) '_agents.mat'], 'AllInitialOpinions', 'time_stamp')














