image thumbnail
from The Bullwhip effect simulated in Matlab Environment by stelios ploumpis
A simulated script that allow anyone to understand the bullwhip effect through a basic supply chain.

BullWhip_effect.m
%  Bullwhip effect

% Democritus Univercity of Thrace (DUTH)
% Department of Production engineering and Managment
% Professor supervisor : Dr.Alexander Tsigkas
%  CopyRight: Stelios Ploumpis 2013

clc
close all
clear all

disp('                     " The Bullwhip effect simulated in Matlab Enviroment "');
fprintf('\n');
disp('Democritus Univercity of Thrace (DUTH)');
disp('Department of Production engineering and Managment');
disp('Professor supervisor : Dr. Alexander Tsigkas');
disp('CopyRight: Stelios Ploumpis 2013');

fprintf('\n');fprintf('\n');

%-- Wellcoming
disp('Wellcome to the Bear Distribution Game... :-) !!!');

fprintf('\n');fprintf('\n');fprintf('\n');

%Rules
rules

% Show the flow chart of the supply chain model
image=imread('flow_chart.png');
figure, imshow(image)


%-- Weeks 
N=input('Please define the number of weeks you want to play :   ');
fprintf('\n');fprintf('\n');fprintf('\n');


array=input('Please define the random Customer Demand between a certain interval e.g.(type [10 20]) :   ');
fprintf('\n');fprintf('\n');fprintf('\n');

ad=input('Please define the "random" change in Customer Demand that causes the bullwhip effect e.g.(if 5,the interval is going to be [10+5,20+5]) :   ');
fprintf('\n');fprintf('\n');fprintf('\n');

%--------------------- Various Initializations-----------------------%
Orders_C= zeros(1,N);
Orders_R= zeros(1,N);
Orders_W= zeros(1,N);
Orders_D= zeros(1,N);
Orders_F= zeros(1,N);

Total_cost_R_array= zeros(1,N);
Total_cost_W_array= zeros(1,N);
Total_cost_D_array= zeros(1,N);
Total_cost_F_array= zeros(1,N);

Total_stock_R= zeros(1,N);
Total_stock_W= zeros(1,N);
Total_stock_D= zeros(1,N);
Total_stock_F= zeros(1,N);

My_Order_R=zeros(1,N);
My_Order_W=zeros(1,N);
My_Order_D=zeros(1,N);
My_Order_F=zeros(1,N);

Outgoing_Deliv_W=zeros(1,N+10);
Outgoing_Deliv_D=zeros(1,N+10);
Outgoing_Deliv_F=zeros(1,N+10);

Backorder_R=0;
Stock_R=input('What is your initial stock Retailer ?:  ');
fprintf('\n')
Incoming_Deliv_R=0;
Backorder_W=0;
Stock_W=input('What is your initial stock Wholesaler ?:  ');
fprintf('\n')
Incoming_Deliv_W=0;
Backorder_D=0;
Stock_D=input('What is your initial stock Distributor ?:  ');
fprintf('\n')
Incoming_Deliv_D=0;
Backorder_F=0;
Stock_F=input('What is your initial stock Factory ?:  ');
fprintf('\n')
Incoming_Deliv_F=0;
Init_order=input('What is the initial quantity of orders in every department except the Retailer :  ');
fprintf('\n');fprintf('\n');


%--------------------- The bullwhip simulator ----------------------%
for i=1:N
    disp('Number of Current Week : ');
    fprintf('%d',i);
    fprintf('\n');fprintf('\n');
    
    if(i<=5)    %--- After 5 rounds the Customer Demand is going to increase according to the additive value :ad
    Customer_Demand= randi([array(1),array(2)]); %---Random Customer Demand between the interval of 10 and 20
    else
    Customer_Demand= randi([array(1)+ad,array(2)+ad]);  %--- The random change in Customer Demand that causes the bullwhip effect.
    end
    
    %-- Retailer ---%
disp(' RETAILER: ')
    if(i<=2)
        Incoming_Deliv_R=0;
    else
        Incoming_Deliv_R=Outgoing_Deliv_W(i-2);
    end
    
    
    Incoming_Order_R=Customer_Demand; %---- Order from Customer
    
    c_R=(Stock_R+Incoming_Deliv_R)-(Incoming_Order_R+Backorder_R);
    if(c_R<0)
        Outgoing_Deliv_R=Stock_R+Incoming_Deliv_R;
        Backorder_R=abs(c_R);
        Stock_R=0;
    else
        Outgoing_Deliv_R=Incoming_Order_R+Backorder_R;
        Stock_R=c_R;
        Backorder_R=0;
    end
    
    Total_cost_R=Backorder_R*2+Stock_R*1;
    
    Total_cost_R_array(i)=Total_cost_R;
    
    if(Stock_R>0)
    Total_stock_R(i)=Stock_R;
    else
    Total_stock_R(i)=-Backorder_R;
    end
    
    Orders_C(i)=Incoming_Order_R;
    
    disp('Incoming Delivery from provider:  ');
    fprintf('%d',Incoming_Deliv_R);
    fprintf('\n');
    disp('Incoming Order from client: ');
    fprintf('%d',Incoming_Order_R);
    fprintf('\n');
    disp('Outgoing Delivery: ');
    fprintf('%d',Outgoing_Deliv_R);
    fprintf('\n');
    disp('Backorder: ');
    fprintf('%d',Backorder_R);
    fprintf('\n');
    disp('Stock: ');
    fprintf('%d',Stock_R);
    fprintf('\n');
    disp('Total cost: ');
    fprintf('%d',Total_cost_R);
    fprintf('\n');
    My_Order_R(i)=input('Whats your order Retailer ?:  ');
    Orders_R(i)=My_Order_R(i);
    clc
    
    fprintf('\n');fprintf('\n');fprintf('\n');
    
%-- Wholesaler ---%
disp(' WHOLESALER: ')
    if(i<=2)
        Incoming_Deliv_W=0;
    else
        Incoming_Deliv_W= Outgoing_Deliv_D(i-2);
    end
    
    if(i==1)
    Incoming_Order_W=Init_order; 
    
    else
    Incoming_Order_W= My_Order_R(i-1); %---- Order from retailer
    end
    
    c_W=(Stock_W+Incoming_Deliv_W)-(Incoming_Order_W+Backorder_W);
    if(c_W<0)
        Outgoing_Deliv_W(i)=Stock_W+Incoming_Deliv_W;
        Backorder_W=abs(c_W);
        Stock_W=0;
    else
        Outgoing_Deliv_W(i)=Incoming_Order_W+Backorder_W;
        Stock_W=c_W;
        Backorder_W=0;
    end
    
    Total_cost_W=Backorder_W*2+Stock_W*1;
    
    Total_cost_W_array(i)=Total_cost_W;
    
    if(Stock_W>0)
    Total_stock_W(i)=Stock_W;
    else
    Total_stock_W(i)=-Backorder_W;
    end
    
    disp('Incoming Delivery from provider: ');
    fprintf('%d',Incoming_Deliv_W);
    fprintf('\n');
    disp('Incoming Order from client: ');
    fprintf('%d',Incoming_Order_W);
    fprintf('\n');
    
    disp('Outgoing Delivery: ');
    fprintf('%d',Outgoing_Deliv_W(i));
    fprintf('\n');
    
    disp('Backorder: ');
    fprintf('%d',Backorder_W);
    fprintf('\n');
    disp('Stock: ');
    fprintf('%d',Stock_W);
    fprintf('\n');
    disp('Total cost: ');
    fprintf('%d',Total_cost_W);
    fprintf('\n');
    My_Order_W(i)=input('Whats your order wholesaler ?:  ');
    Orders_W(i)=My_Order_W(i);
    clc
    
    fprintf('\n');fprintf('\n');fprintf('\n');
    
    %-- Distributor ---%
disp(' DSTRIBUTOR: ')

    if(i<=2)
        Incoming_Deliv_D=0;
    else
        Incoming_Deliv_D=Outgoing_Deliv_F(i-2);
    end
    
    if(i==1)
        Incoming_Order_D= Init_order;
    else
        Incoming_Order_D= My_Order_W(i-1); %---- Order from Wholesaler
    end  
    
    c_D=(Stock_D+Incoming_Deliv_D)-(Incoming_Order_D+Backorder_D);
    if(c_D<0)
        Outgoing_Deliv_D(i)=Stock_D+Incoming_Deliv_D;
        Backorder_D=abs(c_D);
        Stock_D=0;
    else
        Outgoing_Deliv_D(i)=Incoming_Order_D+Backorder_D;
        Stock_D=c_D;
        Backorder_D=0;
    end
    
    Total_cost_D=Backorder_D*2+Stock_D*1;
    
    Total_cost_D_array(i)=Total_cost_D;
    
    if(Stock_D>0)
    Total_stock_D(i)=Stock_D;
    else
    Total_stock_D(i)=-Backorder_D;
    end
    
    disp('Incoming Delivery from provider:  ');
    fprintf('%d',Incoming_Deliv_D);
    fprintf('\n');
    disp('Incoming Order from client: ');
    fprintf('%d',Incoming_Order_D);
    fprintf('\n');
    disp('Outgoing Delivery: ');
    fprintf('%d',Outgoing_Deliv_D(i));
    fprintf('\n');
    disp('Backorder: ');
    fprintf('%d',Backorder_D);
    fprintf('\n');
    disp('Stock: ');
    fprintf('%d',Stock_D);
    fprintf('\n');
    disp('Total cost: ');
    fprintf('%d',Total_cost_D);
    fprintf('\n');
    My_Order_D(i)=input('Whats your order distributor ?:  ');
    Orders_D(i)=My_Order_D(i);
    clc
    
    fprintf('\n');fprintf('\n');fprintf('\n');
    
    %--- Factory ---%
disp(' FACTORY: ')

    if(i<=3)
        Incoming_Deliv_F=0;
    else
        Incoming_Deliv_F= My_Order_F(i-3);
    end
    
    if(i==1)
       Incoming_Order_F=Init_order;
    else
       Incoming_Order_F= My_Order_D(i-1); %---- Order from Distributor 
    end
    
    c_F=(Stock_F+Incoming_Deliv_F)-(Incoming_Order_F+Backorder_F);
    if(c_F<0)
        Outgoing_Deliv_F(i)=Stock_F+Incoming_Deliv_F;
        Backorder_F=abs(c_F);
        Stock_F=0;
    else
        Outgoing_Deliv_F(i)=Incoming_Order_F+Backorder_F;
        Stock_F=c_F;
        Backorder_F=0;
    end
    
    Total_cost_F=Backorder_F*2+Stock_F*1;
    
    Total_cost_F_array(i)=Total_cost_F;
    
    if(Stock_F>0)
    Total_stock_F(i)=Stock_F;
    else
    Total_stock_F(i)=-Backorder_F;
    end
    
    disp('Incoming Delivery from provider: ');
    fprintf('%d',Incoming_Deliv_F);
    fprintf('\n');
    disp('Incoming Order from client: ');
    fprintf('%d',Incoming_Order_F);
    fprintf('\n');
    disp('Outgoing Delivery: ');
    fprintf('%d',Outgoing_Deliv_F(i));
    fprintf('\n');
    disp('Backorder: ');
    fprintf('%d',Backorder_F);
    fprintf('\n');
    disp('Stock: ');
    fprintf('%d',Stock_F);
    fprintf('\n');
    disp('Total cost: ');
    fprintf('%d',Total_cost_F);
    fprintf('\n');
    My_Order_F(i)=input('Whats your order factory ?:  ');
    Orders_F(i)=My_Order_F(i);
    clc
    
    fprintf('\n');fprintf('\n');fprintf('\n');
    
   %----------------------------------------------------------------%
    
    
end


weeks= 1:N;

% -- Plot of the Total Cost of every department during the week time
figure,
p1=plot(weeks,Total_cost_R_array);
title('Plot of Total Department Cost')
xlabel('Weeks');
ylabel('Total Cost');
set(p1,'Color','b')
hold on;
p2=plot(weeks,Total_cost_W_array);
set(p2,'Color','g')
hold on;
p3=plot(weeks,Total_cost_D_array);
set(p3,'Color','y')
hold on;
p4=plot(weeks,Total_cost_F_array);
set(p4,'Color','r')
legend('Retailer','Wholesaler','Distributor','Factory');

% -- Plot of the Total Stock in every department during the week time
figure,
p1=plot(weeks,Total_stock_R);
title('Plot of Total Stocks')
xlabel('Weeks');
ylabel('Stocks');
set(p1,'Color','b')
hold on;
p2=plot(weeks,Total_stock_W);
set(p2,'Color','g')
hold on;
p3=plot(weeks,Total_stock_D);
set(p3,'Color','y')
hold on;
p4=plot(weeks,Total_stock_F);
set(p4,'Color','r')
legend('Retailer','Wholesaler','Distributor','Factory');

% -- Plot of the Total Orders in every department during the week time
figure,
p1=plot(weeks,Orders_C);
title('Plot of Total Orders')
xlabel('Weeks');
ylabel('Orders');
set(p1,'Color','black')
hold on;
p2=plot(weeks,Orders_R);
set(p2,'Color','b')
hold on;
p3=plot(weeks,Orders_W);
set(p3,'Color','g')
hold on;
p4=plot(weeks,Orders_D);
set(p4,'Color','y')
p4=plot(weeks,Orders_F);
set(p4,'Color','r')
legend('Customer','Retailer','Wholesaler','Distributor','Factory');



%-- The end :-)... !!!





Contact us