Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
DESim Example
#include <iostream> #include <string> #include <vector> #include <queue> #include <unordered_map> using namespace std; // define the time data type #define TimeType int // define constants used in our simulation #define TEST_SIZE 15 #define NUM_NAMES 6 string firstNames[NUM_NAMES] = {"Alice","Ambroise","Bert","Denise","Larry","Emily"}; string lastNames[NUM_NAMES] = {"Ghosh","Jones","Lee","Smith","Zhang","Zuckerman"}; #define SIM_LENGTH 100 #define MAX_DURATION 20 #define NUM_TELLERS 2 // define event types typedef enum {ARRIVAL, DEPARTURE} EventType; // define an event struct to store events typedef struct { string customerName; TimeType eventTime; TimeType duration; TimeType waitTime; EventType event; } EventStruct; // a standard way of printing events void printEvent(const EventStruct & event) { cout << event.customerName; cout << ", Time: " << event.eventTime; cout << ", Duration: " << event.duration; cout << ", Event type: "; if (event.event==ARRIVAL) cout << "ARRIVAL"; else{ cout << "DEPARTURE"; cout << "Wait Time: " << event.waitTime; } } // how to compare event times for the priority queue struct compareEventTime { bool operator()(const EventStruct& lhs, const EventStruct& rhs) const { return lhs.eventTime > rhs.eventTime; } }; // our Discrete Event Simulation class class DESim { private: // event queue -- priority queue with events queued by event time priority_queue<EventStruct,vector<EventStruct>,compareEventTime> eventQueue; // wait queue -- priority queue with waiting customers queued by arrival time queue<EventStruct> bankQueue; TimeType currentTime = 0; bool debugOn = false; int tellersAvailable = NUM_TELLERS; public: void addEvent(const EventStruct & event); void addBankQueueCustomer(const EventStruct & event); void setDebugOn(bool value) {debugOn = value;}; void printTellerNumberChange(); void runSim(); }; // end DESim // add a priority queue event void DESim::addEvent(const EventStruct & event) { if (debugOn) {cout << "[" << currentTime <<"] Adding event: "; printEvent(event); cout << endl;} eventQueue.push(event); } // add a bank queue customer (not really an event) void DESim::addBankQueueCustomer(const EventStruct & event) { if (debugOn) {cout << "[" << currentTime <<"] Adding bank queue customer: "; printEvent(event); cout << endl;} bankQueue.push(event); } // indicate that the number of available tellers has changed void DESim::printTellerNumberChange() { if (debugOn) {cout << "[" << currentTime <<"] Current number of tellers available: " << tellersAvailable << endl;} } // run the simulation void DESim::runSim() { while (!eventQueue.empty()) { EventStruct nextEvent = eventQueue.top(); // get next event in priority queue currentTime = nextEvent.eventTime; eventQueue.pop(); switch (nextEvent.event) { case ARRIVAL: if (tellersAvailable) { // enter departure event in eventQueue nextEvent.eventTime = currentTime + nextEvent.duration; nextEvent.event = DEPARTURE; nextEvent.waitTime = 0; addEvent(nextEvent); tellersAvailable--; printTellerNumberChange(); } else { // no tellers available, put customer in bank queue addBankQueueCustomer(nextEvent); } break; case DEPARTURE: if(!bankQueue.empty()) { EventStruct nextCustomer = bankQueue.front(); bankQueue.pop(); nextCustomer.eventTime = currentTime + nextEvent.duration; nextCustomer.event = DEPARTURE; nextCustomer.waitTime = currentTime - addEvent(nextCustomer); } else { tellersAvailable++; printTellerNumberChange(); } break; default: cout << "ERROR: Should never get here! " << endl; } } } int main() { cout << "*** Discrete Event Simulation Example for ECE 2574 ***\n\n"; DESim mySim; mySim.setDebugOn(true); EventStruct myEvent; srand(123321); cout << "Adding " << TEST_SIZE << " events.\n\n"; for (int i=0; i<TEST_SIZE; i++) { myEvent.customerName = firstNames[rand()%NUM_NAMES] + string(" ") + lastNames[rand()%NUM_NAMES]; myEvent.eventTime = rand()%SIM_LENGTH+1; myEvent.duration = rand()%MAX_DURATION+1; myEvent.event = ARRIVAL; mySim.addEvent(myEvent); } cout << "\nRunning simulation...\n"; mySim.runSim(); return 0; }
run
|
edit
|
history
|
help
0
range based for loop temporary object lifetime issue example
Division by zero exception example
Argument passing by using reference and value
the usual name hiding rules do apply with using directives
regimeketopdfb
Access namespace by unqualified manner
le regime keto aviss
Math1
BucketSort
Test bitfields with unnamed union