讲解:Beers-Bars-Drinkers、c/c++、Software、c/c++SQL|R

Programming Project #2Beers-Bars-DrinkersDiscrete Event-Driven SimulationAn Application of Priority QueuesDue Date: Monday March 18, 2019 Project #2 Software Gurus Bar Slide 2Discrete Event-Driven Simulation One of the most common applications of priority queues is in thecreation of simulations. A discrete event-driven simulation is one popular simulation technique. Objects in the simulation model represent objects in the realworld and are programmed to react as much as possible as thereal objects would react. A priority queue is used to store a representation of “events” waitingto 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 apreset time for simulation is exceeded.Project #2 Software Gurus Bar Slide 3Programming Project: Discrete Event-Driven SimulationThis project introduces Object-Oriented Frameworksand features Inheritance and Polymorphism A framework is like the skeleton of an application It provides the structure for a solution, but none of the applicationspecificbehavior. Our framework for simulations describes the features common to alldiscrete 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 andexecutes it. But note that the framework has no knowledge of the specificsimulation being constructed.Project #2 Software Gurus Bar Slide 4The 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 youshould have, and so on. If you plan too small, customers will be turned away when there isinsufficient space, and you will lose potential profits. On the other hand, if you plan too large, most of the seats will beunused, and you will be paying useless rent on the space and hencelosing profits. So you need to choose approximately the right number, but how doyou decide?Project #2 Software Gurus Bar Slide 5The Software Gurus Bar To create a simulation, you first examine similar operations incomparable locations and form a model that includes, among otherfactors,1) an estimation of the number of customers you can expect to arrivein any period of time,2) the length of time they will take to decide on an order, and3) 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 7The 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 thatindicates how frequently this occurs. For example, suppose that we assume that groups will consist offrom 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 modifyappropriately.Project #2 Software Gurus Bar Slide 8The Software Gurus Bar These groups will arrive at time spaced 2 to 5 minutes apart, againselected uniformly.Once they arrive, a group will either be seated or, seeing that thereare no seats available, leave.If seated, they will take from 2 to 10 minutes to order and then willremain 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 9Object 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, butwe can think of the bar as a useful abstraction for the servers andmanagers 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 10Object 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 comparedto the number of available seats.If insufficient seats are available, the group leaves.Otherwise, the group is seated and the number of seats isdecreased. When a customer orders and is served, the amount of profit isupdated. When a customer group leaves, the seats are released for anothercustomer 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 12class SoftwareGurusBar# include # include # include # include event.hclass 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 rangeunsigned int rval = rand( );return rval % max;}unsigned int randBetween(int low, int high) {return low + randomizer(high - low);}Project #2 Software Gurus Bar Slide 13class SoftwareGurusBar (cont.)class SoftwareGurusBar {public:// try with 50 chairs, then try with 40, 60, ...// in order to find out “optimal” profit prospectsSoftwareGurusBar( ): freeChairs(50), profit(0.0) { }bool canSeat (unsigned int numberOfPeople); // slide 14void order(unsigned int beerType); // slide 15void leave(unsigned int numberOfPeople); // slide 15unsigned int freeChairs;double profit;};SoftwareGurusBar theBar;SimulationFramework theSimulation;Project #2 Software Gurus Bar Slide 14class SoftwareGurusBar (cont.)bool SoftwareGurusBar::canSeat (unsigned int numberOfPeople){// if sufficient room, then seat customerscout cout if (numberOfPeople cout freeChairs -= numberOfPeople;return true;}else {cout return false;}}Project #2 Software Gurus Bar Slide 15class SoftwareGurusBar (cont.)void SoftwareGurusBar::order (unsigned int beerType){// serve beercout cout // update profit based on this beerType// add your code here ...}void SoftwareGurusBar::leave (unsigned int numberOfPeople){// people leave, free up chairscout cout freeChairs += numberOfPeople;}Project #2 Software Gurus Bar Slide 16class SoftwareGurusBar (cont.)void main( ) {// load priority queue with initial Arrive Events then run simulationunsigned int t = 0;// load 4 hours (240 minutes) of Arrive Eventswhile (t // new group every 2-5 minutest += randBetween(2,5);// group size ranges from 1 to 5theSimulation.scheduleEvent(new ArriveEvent(t, randBetween(1,5)));}// then run simulation and print profitstheSimulation.run( );cout }Project #2 Software Gurus Bar Slide 17class ArriveEventclass 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 minutestheSimulation.scheduleEvent (new OrderEvent(theSimulation.currentTime + randBetween(2,10),groupSize));}Project #2 Software Gurus Bar Slide 18class OrderEventclass 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 theBar.order(randBetween(1,3));int t = theSimulation.currentTime + randBetween(15,35);// schedBeers-Bars-Drinkers作业代做、代写c/c++程序语言作业、代做Software作业、c/c++编程作业ule a LeaveEvent for this group of drinkers// all the group leaves together// add your code here ...};Project #2 Software Gurus Bar Slide 19class LeaveEventclass 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 20The Software Gurus Bar The method randBetween(low, high) returns a random integerbetween the two given endpoints low & high. The methods canSeat, order, and leave manipulate the profits andthe number of chairs. The execution of the simulation is controled by the simulationframework and the inner classes ArriveEvent, OrderEvent, andLeaveEvent – which are described next.Project #2 Software Gurus Bar Slide 21Frameworks: Reusable Subsystems A framework is reusable software that implements a generic solutionto a generalized problem. It provides common facilities applicable to different applicationprograms. Principle: Applications that do different, but related, things tend tohave quite similar designsProject #2 Software Gurus Bar Slide 22Frameworks to Promote Reuse A framework is intrinsically incomplete Certain classes or methods are used by the framework, but aremissing (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 ProgramInterface (API)Project #2 Software Gurus Bar Slide 23Object-Oriented Frameworks In the object oriented paradigm, a framework is composed of a libraryof classes. The API is defined by the set of all public methods of theseclasses. Some of the classes will normally be abstractProject #2 Software Gurus Bar Slide 24Frameworks A framework is like the skeleton of an application It provides the structure for a solution, but none of the applicationspecificbehavior. Our framework for simulations describes the features common to alldiscrete 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 andexecutes it. But note that the framework has no knowledge of the specificsimulation being constructed.Project #2 Software Gurus Bar Slide 25Frameworks To create a working application, certain key classes provided by theframework are used to create subclasses. These subclasses can providethe application-specific behavior.In our simulation framework, the key class is Event. Three new typesof 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 fromFrame (or JFrame), adding application specific behavior. Frameworks can be created for any task that is repeated with a widerange of variations but that has a core of common functionality.Project #2 Software Gurus Bar Slide 26A Framework for Simulation Rather than simply code a simulation of this one problem, we willgeneralize the problem and first produce a generic framework forsimulations. At the heart of a simulation is the concept of event. An event will be represented by an instance of class Event. The onlyvalue held by the class will be the time the event is to occur. Themethod processEvent will be invoked to “execute” the event when theappropriate time is reached. The simulation queue will need to maintain a collection of various typesof events. Each form of event will be represented by different derivedclasses of class Event. Not all events will have the same type, althoughthey will all be derived from class Event.Project #2 Software Gurus Bar Slide 27A Framework for Simulation We are now ready to define the class SimulationFramework, whichprovides 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 ina private data field.Project #2 Software Gurus Bar Slide 28class 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 timeeventQueue.insert (newEvent);}void run( );unsigned int currentTime;protected:priority_queue , eventComparison> eventQueue;};Project #2 Software Gurus Bar Slide 29class SimulationFramework (event.h)void simulation::run( ){// execute events until event queue becomes emptywhile (! eventQueue.empty( )) {// copy & remove min-priority element (min time) from eventQueueEvent * nextEvent = eventQueue.peak( );eventQueue.deleteMin( );// update simulation’s current timecurrentTime = nextEvent->time;// process nextEventnextEvent->processEvent( ); // what do you see here???// cleanup nextEvent objectdelete nextEvent;}}Project #2 Software Gurus Bar Slide 30Priority Queue APIRequirement: Generic items are Comparable.Records with keys (priorities) that can be comparedpublic class MinPQ >MinPQ( ) create an empty priority queueMinPQ(Key[ ] a) create a priority queue with given keysvoid insert(Key v) insert a key into the priority queueKey delMin( ) return and remove the element with the smallest keyboolean isEmpty( ) is the priority queue empty?Key min( ) return the element with the smallest keyint size( ) number of entries in the priority queueKey must be Comparable Project #2 Software Gurus Bar Slide 31class Event & class eventComparison (event.h)class Event {public:// constructor requires time of eventEvent (unsigned int t) : time(t) { }// time is a public data fieldunsigned int time;// execute event by invoking this methodvirtual void processEvent( ) { }};class eventComparison {public:bool operator ( ) (Event * left, Event * right){return left->time > right->time;}};Project #2 Software Gurus Bar Slide 32What 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 markedwith (left for you) Get it to run Modify the simulation so it uses the weighted discrete random numbergeneratedfunction described on the next slide.§ Select reasonable numbers of weights for the type of beer ordered bythe 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 fourthevent 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 33Weighted Probability One alternative to the use of uniform distribution is the idea of a weighteddiscrete probability. Suppose that we observe a real bar and note that 15% ofthe time they will order local beer, 60% of the time they will order importedbeer, and 25% of the time they will order special beer. This is certainly a fardifferent distribution from the uniform distribution we used above (33% ofeach type of beer). In order to simulate this new behavior, we can add a newmethod to our class random. Add a method named weightedProbability to the classSimulationFramework. This method will take as argument a vector ofunsigned integer values. For example, to generate the precedingdistribution, you will pass to the weightedProbability method a vectorof three elements containing 15, 60, and 25. The method first sums the values in the array, resulting in a maximumvalue. In this case, the value would be 100. A random number between 1 andthis maximum value is then generated. The method then decides in which category the number belongs. This canbe discovered by “scanning” through the values. In our example, if thenumber is less than 15, the method should return 0; if less than or equal to75 (15 + 60), return 1; otherwise return 2.Project #2 Software Gurus Bar Slide 34Cheers...Making your way in the world today takes everything youve got.Taking a break from all your worries, sure would help a lot.Wouldnt you like to get away?Sometimes you want to goWhere everybody knows your name,and theyre always glad you came.You wanna be where you can see, our troubles are all the sameYou 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.转自:http://www.7daixie.com/2019031537636536.html

你可能感兴趣的:(讲解:Beers-Bars-Drinkers、c/c++、Software、c/c++SQL|R)