I am simulating a simple bus network (as shown in the picture) using event simulation. There are two buses running (starting at 1 and 4) and the travel time between nodes is 10 units. For now, I used the switch-cases to manage the event. However, if I need to expand the network, it might be very confusing to manage all the events with switch-cases technique.
Is there any solution to improve this code?
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <time.h>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;
double Clock, Travel_time_bw_block;
class Event
{
public:
enum EvtType {arrival_1, arrival_2, arrival_3, arrival_4, arrival_5, arrival_6};
Event(EvtType type = arrival_1, int bus_no = 0, double etime = 0.0):
_type(type),_bus_no(bus_no), _etime(etime) {}
EvtType get_type() const
{
return _type;
}
double get_bus_no() const
{
return _bus_no;
}
double get_time() const
{
return _etime;
}
protected:
EvtType _type;
int _bus_no;
double _etime;
};
struct EventLess
{
bool operator()(const Event& lhs, const Event& rhs) const
{
return (lhs.get_time() > rhs.get_time());
}
};
priority_queue <Event, vector<Event>, EventLess> FutureEventList;
void Initialization()
{
// initialize global variables
Clock = 0.0;
Travel_time_bw_block = 10.0;
Event evt1(Event::arrival_1, 1, 0.0);
Event evt2(Event::arrival_4, 2, 0.0);
FutureEventList.push(evt1);
FutureEventList.push(evt2);
}
int main(){
int Simulation_Time = 200;
Initialization();
while(Clock < Simulation_Time){
Event evt = FutureEventList.top();
FutureEventList.pop();
Clock = evt.get_time();
switch(evt.get_type()){
case Event::arrival_1:
{
cout << "bus " << evt.get_bus_no() << " arriving block 1 at: " << Clock << endl;
Event enter_1 = Event(Event::arrival_2, evt.get_bus_no(), Clock + Travel_time_bw_block);
FutureEventList.push(enter_1);
break;
}
case Event::arrival_2:
{
cout << "bus " << evt.get_bus_no() << " arriving block 2 at: " << Clock << endl;
Event enter_2 = Event(Event::arrival_3, evt.get_bus_no(), Clock + Travel_time_bw_block);
FutureEventList.push(enter_2);
break;
}
case Event::arrival_3:
{
cout << "bus " << evt.get_bus_no() << " arriving block 3 at: " << Clock << endl;
Event enter_3 = Event(Event::arrival_4, evt.get_bus_no(), Clock + Travel_time_bw_block);
FutureEventList.push(enter_3);
break;
}
case Event::arrival_4:
{
cout << "bus " << evt.get_bus_no() << " arriving block 4 at: " << Clock << endl;
Event enter_4 = Event(Event::arrival_5, evt.get_bus_no(), Clock + Travel_time_bw_block);
FutureEventList.push(enter_4);
break;
}
case Event::arrival_5:
{
cout << "bus " << evt.get_bus_no() << " arriving block 5 at: " << Clock << endl;
Event enter_5 = Event(Event::arrival_6, evt.get_bus_no(), Clock + Travel_time_bw_block);
FutureEventList.push(enter_5);
break;
}
case Event::arrival_6:
{
cout << "bus " << evt.get_bus_no() << " arriving block 6 at: " << Clock << endl;
Event enter_6 = Event(Event::arrival_1, evt.get_bus_no(), Clock + Travel_time_bw_block);
FutureEventList.push(enter_6);
break;
}
}
}
}