Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
icecream.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for random_shuffle()
30#include <iostream> // for cout
31#include <queue> // for queue
32#include <vector> // for vector
33
34#include <examples.h>
35
36
37// Execution event in a descrete event driven simulation.
38class event {
39public:
40 // Construct sets time of event.
41 event (unsigned int t) : time (t)
42 { }
43
44 // Execute event by invoking this method.
45 virtual void processEvent () = 0;
46
47 const unsigned int time;
48};
49
51 bool operator() (const event * left, const event * right) const {
52 return left->time > right->time;
53 }
54};
55
56
57// Framework for discrete event-driven simulations.
59public:
61 {}
62 void run ();
63 void scheduleEvent (event * newEvent) {
64 eventQueue.push (newEvent);
65 }
66 unsigned int time;
67protected:
68 std::priority_queue<event*,
69 std::vector<event *, std::allocator<event*> >,
71};
72
73
75
76 while (! eventQueue.empty ()) {
77
78 event * nextEvent = eventQueue.top ();
79 eventQueue.pop ();
80 time = nextEvent->time;
81 nextEvent->processEvent ();
82 delete nextEvent;
83 }
84}
85
86
87// Ice cream store simulation.
89public:
91 { }
92 bool canSeat (unsigned int numberOfPeople);
93 void order (unsigned int numberOfScoops);
94 void leave (unsigned int numberOfPeople);
95 // Data fields.
96 unsigned int freeChairs;
97 double profit;
99
100
101class arriveEvent : public event {
102public:
103 arriveEvent (unsigned int t, unsigned int groupSize)
104 : event (t), size (groupSize)
105 { }
106 virtual void processEvent ();
107private:
108 unsigned int size;
109};
110
111
112class orderEvent : public event {
113public:
114 orderEvent (unsigned int t, unsigned int groupSize)
115 : event (t), size (groupSize)
116 { }
117 virtual void processEvent ();
118private:
119 unsigned int size;
120};
121
122
123class leaveEvent : public event
124{
125public:
126 leaveEvent (unsigned int t, unsigned int groupSize)
127 : event (t), size (groupSize)
128 { }
129 virtual void processEvent ();
130private:
131 unsigned int size;
132};
133
134
135// returns a random integer between 0 and n with n <= 32767 <= INT_MAX
136int irand (int n)
137{
138 static int seq[] = {
139 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf
140 };
141
142 std::random_shuffle (seq, seq + sizeof seq / sizeof *seq);
143
144 const int rnd =
145 ((seq [0] << 11) | (seq [1] << 8) | (seq [2] << 4)) + seq [3];
146
147 return rnd % n;
148}
149
150
152
153 if (theSimulation.canSeat (size))
155 (new orderEvent (time + 1 + irand (4), size));
156}
157
158
160
161 // Each person orders some number of scoops.
162 for (unsigned int i = 0; i < size; i++)
163 theSimulation.order (1 + irand (4));
164
165 // Then we schedule the leave event.
167 (new leaveEvent (time + 1 + irand (10), size));
168}
169
170
172
173 theSimulation.leave (size);
174}
175
176
177// If sufficient room then seat customers.
178bool storeSimulation::canSeat (unsigned int numberOfPeople) {
179
180 std::cout << "Time: " << time;
181 std::cout << " group of " << numberOfPeople << " customers arrives";
182
183 if (numberOfPeople < freeChairs) {
184 std::cout << " is seated\n";
185 freeChairs -= numberOfPeople;
186 return true;
187 }
188 else {
189 std::cout << " no room, they leave\n";
190 return false;
191 }
192}
193
194
195// Service icecream, compute profits.
196void storeSimulation::order (unsigned int numberOfScoops) {
197
198 std::cout << "Time: " << time << " serviced order for "
199 << numberOfScoops << '\n';
200 profit += 0.35 * numberOfScoops;
201}
202
203
204// People leave, free up chairs.
205void storeSimulation::leave (unsigned int numberOfPeople) {
206
207 std::cout << "Time: " << time << " group of size "
208 << numberOfPeople << " leaves\n";
209 freeChairs += numberOfPeople;
210}
211
212
213int main () {
214
215 std::cout << "Ice Cream Store simulation from Chapter 9\n";
216
217 // Load queue with some number of initial events.
218 for (unsigned t = 0; t < 20; t += irand (6)) {
219
220 std::cout << "pumping queue with event " << t << '\n';
222 }
223
224 // Run the simulation.
226
227 std::cout << "Total profits " << theSimulation.profit
228 << "\nEnd of ice cream store simulation\n";
229
230 return 0;
231}
#define i
Definition 17.names.cpp:126
#define t
Definition 17.names.cpp:136
#define n
Definition 17.names.cpp:130
#define left
#define right
arriveEvent(unsigned int t, unsigned int groupSize)
Definition icecream.cpp:103
virtual void processEvent()
Definition icecream.cpp:151
event(unsigned int t)
Definition icecream.cpp:41
const unsigned int time
Definition icecream.cpp:47
virtual void processEvent()=0
leaveEvent(unsigned int t, unsigned int groupSize)
Definition icecream.cpp:126
virtual void processEvent()
Definition icecream.cpp:171
orderEvent(unsigned int t, unsigned int groupSize)
Definition icecream.cpp:114
virtual void processEvent()
Definition icecream.cpp:159
unsigned int time
Definition icecream.cpp:66
std::priority_queue< event *, std::vector< event *, std::allocator< event * > >, eventComparator > eventQueue
Definition icecream.cpp:70
void run()
Definition icecream.cpp:74
void scheduleEvent(event *newEvent)
Definition icecream.cpp:63
unsigned int freeChairs
Definition icecream.cpp:96
bool canSeat(unsigned int numberOfPeople)
Definition icecream.cpp:178
void order(unsigned int numberOfScoops)
Definition icecream.cpp:196
void leave(unsigned int numberOfPeople)
Definition icecream.cpp:205
storeSimulation theSimulation
int irand(int n)
Definition icecream.cpp:136
int main()
Definition icecream.cpp:213
bool operator()(const event *left, const event *right) const
Definition icecream.cpp:51
A local time-of-day.
TOML_NODISCARD_CTOR time() noexcept=default
Default constructor. Does not initialize the members.