首页
编程语言
数据库
网络开发
Algorithm算法
移动开发
系统相关
金融统计
人工智能
其他
首页
>
> 详细
辅导Programming、讲解Application留学生、Java程序调试、辅导Java语言 辅导Python编程|讲解留学生Process
Programming Project #2
Beers-Bars-Drinkers
Discrete Event-Driven Simulation
An Application of Priority Queues
Due Date: Monday March 18, 2019Project #2 Software Gurus Bar Slide 2
Discrete Event-Driven Simulation
One of the most common applications of priority queues is in the
creation of simulations.
A discrete event-driven simulation is one popular simulation technique.
Objects in the simulation model represent objects in the real
world and are programmed to react as much as possible as the
real objects would react.
A priority queue is used to store a representation of “events” waiting
to happen.
This queue is ordered based on the time the event should occur,
so the smallest element will be the next event to be modeled
(Min Heap).
As an event occurs, it can spawn other events.
These subsequent events are placed into the queue as well.
Execution continues until all events have occurred or until a
preset time for simulation is exceeded.Project #2 Software Gurus Bar Slide 3
Programming Project: Discrete Event-Driven Simulation
This project introduces Object-Oriented Frameworks
and features Inheritance and Polymorphism
A framework is like the skeleton of an application
It provides the structure for a solution, but none of the applicationspecific
behavior.
Our framework for simulations describes the features common to all
discrete event-driven simulations, namely:
the concept of a priority queue of pending events, and
a loop that repeatedly removes the next event from the queue and
executes it.
But note that the framework has no knowledge of the specific
simulation being constructed.Project #2 Software Gurus Bar Slide 4
The Software Gurus Bar
Imagine that you are thinking of opening a bar in San Mateo!
You need to decide how large the bar should be, how many seats you
should have, and so on.
If you plan too small, customers will be turned away when there is
insufficient space, and you will lose potential profits.
On the other hand, if you plan too large, most of the seats will be
unused, and you will be paying useless rent on the space and hence
losing profits.
So you need to choose approximately the right number, but how do
you decide?Project #2 Software Gurus Bar Slide 5
The Software Gurus Bar
To create a simulation, you first examine similar operations in
comparable locations and form a model that includes, among other
factors,
1) an estimation of the number of customers you can expect to arrive
in any period of time,
2) the length of time they will take to decide on an order, and
3) the length of time they will stay after having been served.
Based on this, you can design a simulation.The Software Gurus Bar
“Where everybody knows your name!”Project #2 Software Gurus Bar Slide 7
The Software Gurus Bar
To see how we might create a simulation of our Software Gurus Bar,
consider a typical scenario:
A group of customers arrive at the bar.
From our measurements of similar bars, we derive a probability that
indicates how frequently this occurs.
For example, suppose that we assume that groups will consist of
from one to five people, selected uniformly over that range.
In actual simulation, the distribution would seldom be uniform.
For example, groups of size two, three and four might predominate,
with groups of one and five being less frequent.
We will start with uniform distributions and later you will modify
appropriately.Project #2 Software Gurus Bar Slide 8
The Software Gurus Bar
These groups will arrive at time spaced 2 to 5 minutes apart, again
selected uniformly.
Once they arrive, a group will either be seated or, seeing that there
are no seats available, leave.
If seated, they will take from 2 to 10 minutes to order and then will
remain from 30 to 60 minutes in the bar.
We know that every customer will order from three kinds of beer:
§ local beer, import beer, or special beer.
The bar makes a profit of
$2 on each local beer,
$3 on each imported beer and
$4 on each special beer.Project #2 Software Gurus Bar Slide 9
Object of Simulation – the Bar
The primary object in the simulation is the bar itself.
It might seem odd to provide “behavior” for this inanimate object, but
we can think of the bar as a useful abstraction for the servers and
managers who work in the bar.
The bar manages two data items:
the number of available seats and
the amount of profit generated.Project #2 Software Gurus Bar Slide 10
Object of Simulation – the Bar
The behavior of the bar can be described by the following list:
When a customer group arrives, the size of the group is compared
to the number of available seats.
If insufficient seats are available, the group leaves.
Otherwise, the group is seated and the number of seats is
decreased.
When a customer orders and is served, the amount of profit is
updated.
When a customer group leaves, the seats are released for another
customer group.
A partial description of the SoftwareGurusBar class is given below:Event
- time:int;
+ ?constructor? Event(t:int);
+ processEvent( );
+ compareTo(o:Object):int;
SimulationFramework
- currentTime: int;
- eventQueue: MinPQ
+ scheduleEvent(newE: Event);
+ run ( );
OrderEvent
- groupSize:int;
+ <
>
Event(t:int, gSize:int);
+ processEvent( );
Plus other application Classes...
LeaveEvent
- groupSize:int;
+ <
>
Event(t:int, gSize:int);
+ processEvent( );
ArriveEvent
- groupSize:int;
+ <
>
Event(t:int, gSize:int);
+ processEvent( );
Project #2 Software Gurus Bar Slide 11Project #2 Software Gurus Bar Slide 12
class SoftwareGurusBar
# include
# include
# include
# include "event.h"
class randomInteger {
public:
unsigned int operator ( ) (unsigned int);
} randomizer;
unsigned int randomInteger::operator ( ) (unsigned int max)
{
// rand return random integer
// convert to unsigned to make positive
// take remainder to put in range
unsigned int rval = rand( );
return rval % max;
}
unsigned int randBetween(int low, int high) {
return low + randomizer(high - low);
}Project #2 Software Gurus Bar Slide 13
class SoftwareGurusBar (cont.)
class SoftwareGurusBar {
public:
// try with 50 chairs, then try with 40, 60, ...
// in order to find out “optimal” profit prospects
SoftwareGurusBar( )
: freeChairs(50), profit(0.0) { }
bool canSeat (unsigned int numberOfPeople); // slide 14
void order(unsigned int beerType); // slide 15
void leave(unsigned int numberOfPeople); // slide 15
unsigned int freeChairs;
double profit;
};
SoftwareGurusBar theBar;
SimulationFramework theSimulation;Project #2 Software Gurus Bar Slide 14
class SoftwareGurusBar (cont.)
bool SoftwareGurusBar::canSeat (unsigned int numberOfPeople)
{
// if sufficient room, then seat customers
cout << "Time: " << theSimulation.currentTime;
cout << " Group of " << numberOfPeople << " customers arrives";
if (numberOfPeople < freeChairs) {
cout << " Group is seated" << endl;
freeChairs -= numberOfPeople;
return true;
}
else {
cout << " No room, group leaves" << endl;
return false;
}
}Project #2 Software Gurus Bar Slide 15
class SoftwareGurusBar (cont.)
void SoftwareGurusBar::order (unsigned int beerType)
{
// serve beer
cout << "Time: " << theSimulation.currentTime;
cout << " serviced order for " << beerType << endl;
// update profit based on this beerType
// add your code here ...
}
void SoftwareGurusBar::leave (unsigned int numberOfPeople)
{
// people leave, free up chairs
cout << "Time: " << theSimulation.currentTime;
cout << " group of size " << numberOfPeople << " leaves" << endl;
freeChairs += numberOfPeople;
}Project #2 Software Gurus Bar Slide 16
class SoftwareGurusBar (cont.)
void main( ) {
// load priority queue with initial Arrive Events then run simulation
unsigned int t = 0;
// load 4 hours (240 minutes) of Arrive Events
while (t < 240) {
// new group every 2-5 minutes
t += randBetween(2,5);
// group size ranges from 1 to 5
theSimulation.scheduleEvent(new ArriveEvent(t, randBetween(1,5)));
}
// then run simulation and print profits
theSimulation.run( );
cout << "Total profits " << theBar.profit << endl;
}Project #2 Software Gurus Bar Slide 17
class ArriveEvent
class ArriveEvent : public Event {
public: ArriveEvent (unsigned int time, unsigned int gs)
: Event(time), groupSize(gs) { }
virtual void processEvent ( );
protected:
unsigned int groupSize;
};
void ArriveEvent::processEvent( )
{
if (theBar.canSeat(groupSize))
// place an order within 2 & 10 minutes
theSimulation.scheduleEvent (
new OrderEvent(theSimulation.currentTime + randBetween(2,10),
groupSize));
}Project #2 Software Gurus Bar Slide 18
class OrderEvent
class OrderEvent : public Event {
public: OrderEvent (unsigned int time, unsigned int gs)
: Event(time), groupSize(gs) { }
virtual void processEvent ( );
protected:
unsigned int groupSize;
};
void orderEvent::processEvent( )
{
// each member of the group orders a beer (type 1,2,3)
for (int i = 0; i < groupSize; i++)
theBar.order(randBetween(1,3));
int t = theSimulation.currentTime + randBetween(15,35);
// schedule a LeaveEvent for this group of drinkers
// all the group leaves together
// add your code here ...
};Project #2 Software Gurus Bar Slide 19
class LeaveEvent
class LeaveEvent : public Event {
public:
LeaveEvent (unsigned int time, unsigned int gs)
: Event(time), groupSize(gs) { }
virtual void processEvent ( );
protected:
unsigned int groupSize;
};
void leaveEvent::processEvent ( )
{
theBar.leave(groupSize);
}Project #2 Software Gurus Bar Slide 20
The Software Gurus Bar
The method randBetween(low, high) returns a random integer
between the two given endpoints low & high.
The methods canSeat, order, and leave manipulate the profits and
the number of chairs.
The execution of the simulation is controled by the simulation
framework and the inner classes ArriveEvent, OrderEvent, and
LeaveEvent – which are described next.Project #2 Software Gurus Bar Slide 21
Frameworks: Reusable Subsystems
A framework is reusable software that implements a generic solution
to a generalized problem.
It provides common facilities applicable to different application
programs.
Principle: Applications that do different, but related, things tend to
have quite similar designsProject #2 Software Gurus Bar Slide 22
Frameworks to Promote Reuse
A framework is intrinsically incomplete
Certain classes or methods are used by the framework, but are
missing (slots)
Some functionality is optional
§ Allowance is made for developer to provide it (hooks)
Developers use the services that the framework provides
§ Taken together the services are called the Application Program
Interface (API)Project #2 Software Gurus Bar Slide 23
Object-Oriented Frameworks
In the object oriented paradigm, a framework is composed of a library
of classes.
The API is defined by the set of all public methods of these
classes.
Some of the classes will normally be abstractProject #2 Software Gurus Bar Slide 24
Frameworks
A framework is like the skeleton of an application
It provides the structure for a solution, but none of the applicationspecific
behavior.
Our framework for simulations describes the features common to all
discrete event-driven simulations, namely:
the concept of a priority queue of pending events, and
a loop that repeatedly removes the next event from the queue and
executes it.
But note that the framework has no knowledge of the specific
simulation being constructed.Project #2 Software Gurus Bar Slide 25
Frameworks
To create a working application, certain key classes provided by the
framework are used to create subclasses. These subclasses can provide
the application-specific behavior.
In our simulation framework, the key class is Event. Three new types
of events are created, corresponding to groups of drinkers
arriving at the bar,
drinkers ordering for beer, and
drinkers leaving the bar.
Other frameworks you may have already seen are Java’s AWT & Swing.
To create a new application, you use inheritance to subclass from
Frame (or JFrame), adding application specific behavior.
Frameworks can be created for any task that is repeated with a wide
range of variations but that has a core of common functionality.Project #2 Software Gurus Bar Slide 26
A Framework for Simulation
Rather than simply code a simulation of this one problem, we will
generalize the problem and first produce a generic framework for
simulations. At the heart of a simulation is the concept of event.
An event will be represented by an instance of class Event. The only
value held by the class will be the time the event is to occur. The
method processEvent will be invoked to “execute” the event when the
appropriate time is reached.
The simulation queue will need to maintain a collection of various types
of events. Each form of event will be represented by different derived
classes of class Event. Not all events will have the same type, although
they will all be derived from class Event.Project #2 Software Gurus Bar Slide 27
A Framework for Simulation
We are now ready to define the class SimulationFramework, which
provides the basic structure for simulation activities.
The class SimulationFramework provides three functions.
§ The first is used to insert a new event into the priority queue,
§ the second runs the simulation,
§ and the third returns the simulation time, which is being held in
a private data field.Project #2 Software Gurus Bar Slide 28
class SimulationFramework (event.h)
class SimulationFramework {
public:
SimulationFramework ( ) : eventQueue( ), currentTime(0) { }
void scheduleEvent (Event * newEvent)
{
// insert newEvent into eventQueue (Priority Queue)
// Priority Queue is based on MinHeap using newEvent’s time
eventQueue.insert (newEvent);
}
void run( );
unsigned int currentTime;
protected:
priority_queue
, eventComparison> eventQueue;
};Project #2 Software Gurus Bar Slide 29
class SimulationFramework (event.h)
void simulation::run( )
{
// execute events until event queue becomes empty
while (! eventQueue.empty( )) {
// copy & remove min-priority element (min time) from eventQueue
Event * nextEvent = eventQueue.peak( );
eventQueue.deleteMin( );
// update simulation’s current time
currentTime = nextEvent->time;
// process nextEvent
nextEvent->processEvent( ); // what do you see here???
// cleanup nextEvent object
delete nextEvent;
}
}Project #2 Software Gurus Bar Slide 30
Priority Queue API
Requirement: Generic items are Comparable.
Records with keys (priorities) that can be compared
public class MinPQ
>
MinPQ( ) create an empty priority queue
MinPQ(Key[ ] a) create a priority queue with given keys
void insert(Key v) insert a key into the priority queue
Key delMin( ) return and remove the element with the smallest key
boolean isEmpty( ) is the priority queue empty?
Key min( ) return the element with the smallest key
int size( ) number of entries in the priority queue
Key must be Comparable Project #2 Software Gurus Bar Slide 31
class Event & class eventComparison (event.h)
class Event {
public:
// constructor requires time of event
Event (unsigned int t) : time(t) { }
// time is a public data field
unsigned int time;
// execute event by invoking this method
virtual void processEvent( ) { }
};
class eventComparison {
public:
bool operator ( ) (Event * left, Event * right)
{
return left->time > right->time;
}
};Project #2 Software Gurus Bar Slide 32
What is left for you to do?
The following tasks are left for you to complete this project:
Implement the Priority Queue ADT based on the MinHeap Data Structure
Understand the given code and complete the “tiny” sections of code marked
with (left for you)
Get it to run
Modify the simulation so it uses the weighted discrete random numbergenerated
function described on the next slide.
§ Select reasonable numbers of weights for the type of beer ordered by
the drinkers and for the sizes of the groups of drinkers
§ Compare a run of the resulting program to a run of the given program
(based on uniform distribution)
? Chalenge: Applying good object-oriented design principles, add a fourth
event type corresponding to re-ordering more beer
Entire group remains in bar for another round with a diminishing probability
Make sure this does not infinitely spawn re-order events
Warning: please don’t leave things till last minute!Project #2 Software Gurus Bar Slide 33
Weighted Probability
One alternative to the use of uniform distribution is the idea of a weighted
discrete probability. Suppose that we observe a real bar and note that 15% of
the time they will order local beer, 60% of the time they will order imported
beer, and 25% of the time they will order special beer. This is certainly a far
different distribution from the uniform distribution we used above (33% of
each type of beer). In order to simulate this new behavior, we can add a new
method to our class random.
Add a method named weightedProbability to the class
SimulationFramework. This method will take as argument a vector of
unsigned integer values. For example, to generate the preceding
distribution, you will pass to the weightedProbability method a vector
of three elements containing 15, 60, and 25.
The method first sums the values in the array, resulting in a maximum
value. In this case, the value would be 100. A random number between 1 and
this maximum value is then generated.
The method then decides in which category the number belongs. This can
be discovered by “scanning” through the values. In our example, if the
number is less than 15, the method should return 0; if less than or equal to
75 (15 + 60), return 1; otherwise return 2.Project #2 Software Gurus Bar Slide 34
Cheers...
Making your way in the world today takes everything you've got.
Taking a break from all your worries, sure would help a lot.
Wouldn't you like to get away?
Sometimes you want to go
Where everybody knows your name,
and they're always glad you came.
You wanna be where you can see, our troubles are all the same
You wanna be where everybody knows Your name.
You wanna go where people know, people are all the same,
You wanna go where everybody knows your name.
联系我们
QQ:99515681
邮箱:99515681@qq.com
工作时间:8:00-21:00
微信:codinghelp
热点文章
更多
辅导 comm2000 creating socia...
2026-01-08
讲解 isen1000 – introductio...
2026-01-08
讲解 cme213 radix sort讲解 c...
2026-01-08
辅导 csc370 database讲解 迭代
2026-01-08
讲解 ca2401 a list of colleg...
2026-01-08
讲解 nfe2140 midi scale play...
2026-01-08
讲解 ca2401 the universal li...
2026-01-08
辅导 engg7302 advanced compu...
2026-01-08
辅导 comp331/557 – class te...
2026-01-08
讲解 soft2412 comp9412 exam辅...
2026-01-08
讲解 scenario # 1 honesty讲解...
2026-01-08
讲解 002499 accounting infor...
2026-01-08
讲解 comp9313 2021t3 project...
2026-01-08
讲解 stat1201 analysis of sc...
2026-01-08
辅导 stat5611: statistical m...
2026-01-08
辅导 mth2010-mth2015 - multi...
2026-01-08
辅导 eeet2387 switched mode ...
2026-01-08
讲解 an online payment servi...
2026-01-08
讲解 textfilter辅导 r语言
2026-01-08
讲解 rutgers ece 434 linux o...
2026-01-08
热点标签
mktg2509
csci 2600
38170
lng302
csse3010
phas3226
77938
arch1162
engn4536/engn6536
acx5903
comp151101
phl245
cse12
comp9312
stat3016/6016
phas0038
comp2140
6qqmb312
xjco3011
rest0005
ematm0051
5qqmn219
lubs5062m
eee8155
cege0100
eap033
artd1109
mat246
etc3430
ecmm462
mis102
inft6800
ddes9903
comp6521
comp9517
comp3331/9331
comp4337
comp6008
comp9414
bu.231.790.81
man00150m
csb352h
math1041
eengm4100
isys1002
08
6057cem
mktg3504
mthm036
mtrx1701
mth3241
eeee3086
cmp-7038b
cmp-7000a
ints4010
econ2151
infs5710
fins5516
fin3309
fins5510
gsoe9340
math2007
math2036
soee5010
mark3088
infs3605
elec9714
comp2271
ma214
comp2211
infs3604
600426
sit254
acct3091
bbt405
msin0116
com107/com113
mark5826
sit120
comp9021
eco2101
eeen40700
cs253
ece3114
ecmm447
chns3000
math377
itd102
comp9444
comp(2041|9044)
econ0060
econ7230
mgt001371
ecs-323
cs6250
mgdi60012
mdia2012
comm221001
comm5000
ma1008
engl642
econ241
com333
math367
mis201
nbs-7041x
meek16104
econ2003
comm1190
mbas902
comp-1027
dpst1091
comp7315
eppd1033
m06
ee3025
msci231
bb113/bbs1063
fc709
comp3425
comp9417
econ42915
cb9101
math1102e
chme0017
fc307
mkt60104
5522usst
litr1-uc6201.200
ee1102
cosc2803
math39512
omp9727
int2067/int5051
bsb151
mgt253
fc021
babs2202
mis2002s
phya21
18-213
cege0012
mdia1002
math38032
mech5125
07
cisc102
mgx3110
cs240
11175
fin3020s
eco3420
ictten622
comp9727
cpt111
de114102d
mgm320h5s
bafi1019
math21112
efim20036
mn-3503
fins5568
110.807
bcpm000028
info6030
bma0092
bcpm0054
math20212
ce335
cs365
cenv6141
ftec5580
math2010
ec3450
comm1170
ecmt1010
csci-ua.0480-003
econ12-200
ib3960
ectb60h3f
cs247—assignment
tk3163
ics3u
ib3j80
comp20008
comp9334
eppd1063
acct2343
cct109
isys1055/3412
math350-real
math2014
eec180
stat141b
econ2101
msinm014/msing014/msing014b
fit2004
comp643
bu1002
cm2030
联系我们
- QQ: 99515681 微信:codinghelp
© 2024
www.7daixie.com
站长地图
程序辅导网!