Skip to content

Instantly share code, notes, and snippets.

@karthikkondagalla
Created August 27, 2017 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthikkondagalla/6d1d03fceb690846f291f55d98bf7631 to your computer and use it in GitHub Desktop.
Save karthikkondagalla/6d1d03fceb690846f291f55d98bf7631 to your computer and use it in GitHub Desktop.
Grocery Store Simulation Model in C++
#include "grocery.h"
int main()
{
int seed = time(0);
srand(seed);
event e;
L l;
Q q;
stat s;
int i=1,clock = 0;
bool f1,f2;
init_vars(e, s);
while(clock <= SIM_TIME)
{
f1 = (!l.empty());
f2 = (!q.empty());
clock = update_clock(e , f1 , f2);
if(e.nextArr == clock)
{
Arrival(e , i , l);
i++;
}
if(e.nextCheckout == clock)
{
Checkout(e , l , q);
}
if(e.nextDept == clock)
{
Departure(e , q , s);
}
}
print_fin_stat(s);
return(0);
}
/******************************************************
Function: RNG
Arguments: int low, int high
Description: Generates random numbers between two values.
Return: Integer
*******************************************************/
int RNG(int low, int high)
{
int value=(rand()%(high-low+1))+low;
return (value);
}
/******************************************************
Function: Arrival
Arguments: event& e, const int& id, L& l
Description: This routine is called when a new customer arrives into the store. It creates an object of type cust for the new customer
Return: None
*******************************************************/
void Arrival(event& e, const int& id, L& l)
{
cust newCustomer;
newCustomer.id = id;
newCustomer.arrTime = e.nextArr;
newCustomer.waitTime = 0;
newCustomer.pickTime = pick_time(e.nextArr);
l.push_back(newCustomer);
e.nextArr = arr_time(e.nextArr);
LI list_item = min_element(l.begin(), l.end(), cmp);
if((*list_item).pickTime < e.nextCheckout)
{
e.nextCheckout = (*list_item).pickTime;
}
}
/******************************************************
Function: init_vars
Arguments: event& e, stat& s
Description: This routine initializes the values in structures e and s
Return: None
*******************************************************/
void init_vars(event& e, stat& s)
{
e.nextArr = 0;
e.nextCheckout = SIM_TIME+1;
e.nextDept = SIM_TIME+1;
s.numDept = s.totShop = s.totWait = s.totServ = 0;
}
/******************************************************
Function: Departure
Arguments: event& e, Q& q, stat& s
Description: This routine is called when a customer departs from the store.
Return: None
*******************************************************/
void Departure ( event& e, Q& q, stat& s )
{
cust temp = q.front();
q.pop();
update_fin_stat(s, temp, e.nextDept);
if(!q.empty())
{
q.front().waitTime = e.nextDept-q.front().pickTime;
e.nextDept = dept_time(e.nextDept);
}
else
{
e.nextDept = SIM_TIME + 1;
}
}
/******************************************************
Function: Checkout
Arguments: event& e, L& l, Q& q
Description: This routine is called when a customer enters in the checkout line.
It removes the object for that customer from the list l and inserts it in the queue q.
Return: None
*******************************************************/
void Checkout (event& e, L& l, Q& q)
{
LI list_item = min_element(l.begin(), l.end(), cmp);
if(q.empty()) e.nextDept = dept_time(e.nextCheckout);
q.push(*list_item);
l.erase(list_item);
if(!l.empty())
{
list_item = min_element(l.begin(), l.end(), cmp);
e.nextCheckout = (*list_item).pickTime;
}
else
{
e.nextCheckout = SIM_TIME + 1;
}
}
/******************************************************
Function: update_fin_stat
Arguments: stat& s, const cust& c, const int& clock
Description: It calculates the final stats
Return: None
*******************************************************/
void update_fin_stat ( stat& s, const cust& c, const int& clock )
{
s.numDept++;
s.totShop += c.pickTime - c.arrTime;
s.totWait += c.waitTime;
s.totServ += (clock - (c.pickTime + c.waitTime));
if((s.numDept % SAMPLE_INT) == 0)
{
cout <<"clock = "<<clock<<" id = "<<c.id<<" Shop-time = " << (c.pickTime-c.arrTime)<<" Wait-time = "<<c.waitTime<< "Serv-time = " << (clock-(c.pickTime+c.waitTime)) << endl;
}
}
/******************************************************
Function: print_fin_stat
Arguments: const stat& s
Description: It prints the final stats
Return: None
*******************************************************/
void print_fin_stat ( const stat& s )
{
double avg_wait = double(s.totWait) / s.numDept;
double avg_serv = double(s.totServ) / s.numDept;
double avg_shop = double(s.totShop) / s.numDept;
cout<<endl<<"Average Wait time:"<< avg_wait<<"\t";
cout << "Average shop time :"<<avg_shop<<"\t";
cout << "Average serve time :"<<avg_serv<<"\t";
}
/******************************************************
Function: update_clock
Arguments: const event& e, const bool& f1, const bool& f2
Description: This routine updates the simulation clock
Return: Integer
*******************************************************/
int update_clock ( const event& e, const bool& f1, const bool& f2 )
{
int min = SIM_TIME + 1;
if(e.nextArr < min) min = e.nextArr;
if(f1 == true)
{
if(min > e.nextCheckout)
{
min = e.nextCheckout;
}
}
if(f2 == true)
{
if(min > e.nextDept) min = e.nextDept;
}
return min;
}
/******************************************************
Function: cmp
Arguments: const cust& c1, const cust& c2
Description: Reads and processes tokens
Return: True or False i.e boolean value
*******************************************************/
bool cmp ( const cust& c1, const cust& c2 )
{
return (c1.pickTime < c2.pickTime);
}
/******************************************************
Function: arr_time
Arguments: const int& clock
Description: Calculates the arrival time
Return: Integer
*******************************************************/
int arr_time ( const int& clock )
{
return(clock+RNG(MIN_INT_ARR, MAX_INT_ARR));
}
/******************************************************
Function: pick_time
Arguments: const int& clock
Description: Calculates pick up time.
Return: Integer
*******************************************************/
int pick_time ( const int& clock )
{
return(clock+RNG(MIN_PICK, MAX_PICK));
}
/******************************************************
Function: dept_time
Arguments: const int& clock
Description: Calculates the departing time.
Return: Integer
*******************************************************/
int dept_time ( const int& clock )
{
return(clock+RNG(MIN_SERV, MAX_SERV));
}
#ifndef H_TEST9
#define H_TEST9
#include "/home/cs689/common/689.h"
#endif
#define MIN_INT_ARR 1
#define MAX_INT_ARR 5
#define MIN_PICK 1
#define MAX_PICK 20
#define MIN_SERV 1
#define MAX_SERV 4
#define SIM_TIME 6*30*24*60
#define SAMPLE_INT 2000
typedef struct
{
int id;
int arrTime;
int pickTime;
int waitTime;
}cust;
typedef struct
{
int nextArr;
int nextCheckout;
int nextDept;
}event;
typedef struct
{
int numDept;
int totShop;
int totWait;
int totServ;
}stat;
typedef list <cust> L;
typedef L :: iterator LI;
typedef queue< cust > Q ;
int r(int low, int high);
int arr_time ( const int& clock );
int pick_time ( const int& clock );
int dept_time ( const int& clock );
int update_clock ( const event& e, const bool& f1, const bool& f2 );
bool cmp ( const cust& c1, const cust& c2 ) ;
void init_vars(event& e, stat& s);
void update_fin_stat ( stat& s, const cust& c, const int& clock );
void Departure ( event& e, Q& q, stat& s );
void Arrival(event& e, const int& id, L& l);
void Checkout (event& e, L& l, Q& q);
void print_fin_stat ( const stat& s );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment