% This script produces fig. 1

clear
clc

mu_ag = [15 10 5 1]; % sstandard of each agent
sigma=2; % uncertainty

Position(1,:) = [ 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ]; % positions 


Last_position = 20; % the worst position in the race
Mot_mat = cell(1,length(mu_ag));
 
for ag = 1 : length(mu_ag)     
     mu = mu_ag(ag);   
     for t = 1: length(Position)        
           Exp_eff = Position(t) - 2;
           if Exp_eff < 1
               Exp_eff = 1;
           end
           Exp_Noeff = Position(t) + 2; 
           if Exp_Noeff > 20
               Exp_Noeff = 20;
           end          
           Exp_eff_T = Last_position-Exp_eff;
           Exp_Noeff_T = Last_position-Exp_Noeff;
           Pres_T = Last_position - Position(t);
           mu_T = Last_position-mu;
           
           V_eff = 1/(1+exp(-(Exp_eff_T-mu_T)/sigma));
           V_Noeff = 1/(1+exp(-(Exp_Noeff_T-mu_T)/sigma));
           V_pres = 1/(1+exp(-(Pres_T-mu_T)/sigma));
           Mot(t) = V_eff-V_Noeff;
           V_eff_vect(t) = V_eff;
           V_Noeff_vect(t) = V_Noeff;
           Mot_Valence(t) = abs(V_eff-V_pres) - abs(V_Noeff-V_pres);
     end

     Mot_mat{1,ag} = Mot;     
     Mot_mat{2,ag} = V_eff_vect;
     Mot_mat{3,ag} = V_Noeff_vect;
     Mot_mat{4,ag} = Mot_Valence;
end   
set(0, 'DefaultAxesFontSize', 18, 'defaultlineLineWidth', 3)
     figure
     for ag = 1: length(mu_ag) 
         subplot(3,length(mu_ag),length(mu_ag)*2+ag)
         plot(Mot_mat{4,ag})  
         title([ 'agent ' num2str(ag) ': \mu =' num2str(mu_ag(ag)) ', \sigma = 2'])
         xlabel ('Current position')
         ylabel ('Motivational valence')
         xticks([1 6 11 16 20])
         xticklabels({'20','15','10','5','1'})
         ylim([-0.3 0.3])
         xlim([1 20])
               
         
         subplot(3,length(mu_ag),length(mu_ag)+ag)
         plot(Mot_mat{1,ag})  
         title([ 'agent ' num2str(ag) ': \mu =' num2str(mu_ag(ag)) ', \sigma = 2'])
         xlabel ('Current position')
         ylabel ('Motivation')
         xticks([1 6 11 16 20])
         xticklabels({'20','15','10','5','1'})
         ylim([0 0.8])
         xlim([1 20])
         
         subplot(3,length(mu_ag),ag)
         plot(Mot_mat{2,ag},'r')
         hold on
         plot(Mot_mat{3,ag},'g','LineStyle','--')
         title([ 'agent ' num2str(ag) ': \mu =' num2str(mu_ag(ag)) ', \sigma = 2']) 
         xlabel ('Current position')
         ylabel ('V_E & V_N_O_E')
         xticks([1 6 11 16 20])
         xticklabels({'20','15','10','5','1'})
         ylim([0 1])
         xlim([1 20])
     end