Discrete-event Simulation Lab Spec (Java)Aims
To provide practice at de ning and using abstract classes
To build a class that implements a simple interface
To contruct a simple class library that can be re-used in many di erent applications
To introduce the concept of discrete-event simulation
To experiment with static class variablesThe problem
Discrete-event simulation is a way of modelling the evolution of systems over time. It is a very general and powerful technique that is used extensively to study behavioural and performance aspects of real-world systems, e.g. manufacturing systems, computer networks, queueing systems, transportation etc. You will have the opportunity to study these issues later in the degree course.In this exercise you a required to build a small package of classes that collectively will manage a discrete-event simulation. The classes have been carefully designed to provide a high degree of exibility (in terms of re-use) whilst at the same time being small and simple. There are many ways to build such a simulation library in Java. The one described here has been designed speci cally to exercise certain features of the Java language; it has not been engineered to minimise execution time, for example.Discrete-event Simulation
Discrete-event simulation (DES) is based on the scheduling of events in time order. Associated with every simulation is a (possibly empty) set of variables, which represent the state of the simulated system, together with other variables that may be required to control the simulation. These are typically implemented as instance variables and/or class variables in some Java class. Invoking an event will typically change one or more of the these variables and may also schedule one or more new events to happen at future time(s). At the heart of every DES is a method that traverses a list of events in time order, invoking each event in turn. Note that all times are virtual: the simulated passing of time has nothing to do with the passing of real time as the simulation executes. Time is just a Java double! A good analogy is a standard pocket diary: the entries in the diary (lunch at 12-00, meeting at 2-00, opera at 5-00, etc.) are the events and the diary itself is the event list. The diary is "processed" in time order and one event may schedule another, for example in the meeting at 2-00 you may schedule the next meeting, e.g. for the same time the following week. The "state" in this case is essentially the state of "your life"(!) For example, event "My birthday today" will increase your age by one year and "Hairdresser at 9-00" may very well change the length, and possibly colour, of your hair!

解决方案 »

  1.   

    Requirements
    An important part of the exercise is to come up with a library of classes that can be used to manage a wide range of discrete-event simulations. The basic requirements are:It should be possible to build many different types of event, although each event should share important characteristics, e.g. the ability to be scheduled at a specified timeEvents should be able to take an arbitrary set of parameters, in addition to required parameters, e.g. the time at which the event should happen.The simulation class library should not depend in any way on the system being modelled, i.e. it should be possible to simulate any system, at least in principle.
    What to Do
    You're going to build a package called simulation which should contain the classes Simulation and Event as described below:Class Simulation
    This should contain two (private) instance variables: one a double that represents virtual
    time, in arbitrary time units, and the other the event list. You should provide a method, e.g. now(), that returns the value of current time.To manage the event list you need two additional methods: one that adds an event to the list (void schedule( Event e ), say) and one that controls the processing of the event list in time order (void simulate(), say). The latter should repeatedly perform the following actions:Remove the next event from the front of the event list
    Update the time to the invocation time of that event
    Invoke the eventA method is invoked by calling the event's invoke() method (see below).
      

  2.   

    The Event List
    The event list can be implemented in many ways { in this exercise we recommend you use a predefined class called TreeSet that implements ordered sets; the name comes from the fact that it uses trees to implement sets, although this is of no concern to you. TreeSet is defined in class java.util and can be imported by typing:import java.util.TreeSet;at the top of the file. The TreeSets class contains two methods that will prove useful when defining the Simulation class: void add( Object x ) that adds an object to a TreeSet and Object first() that returns the rst object in the TreeSet according to a specified ordering.TreeSets can contain any type of object provided they can be ordered in a meaningful way. This is enforced by requiring the objects to implement the interface Comparable. This is defined in java.lang and contains a single method definition int compareTo(Object x) that returns -1 if the object is \smaller than" x, 0 of it is 'equal to" x and 1 if it is "larger than" x. The ordering on events is based on their time of occurrence: one event is "smaller than" another if it is to be invoked earlier. Here, the objects will be events, so the Event class (below) must be defined to implement Comparable.Note that when you remove an object from a TreeSet the result is an Object, i.e. the most general object type. This means that when you remove an event from the list it too will be identified as being of type Object even though you know it's of type Event! You therefore have to cast the Object into an Event, for example:Event e = (Event) diary.first() ;where diary here is the name of the event list.Termination
    All that remains is to determine when to terminate the simulate() loop above. We need to be able to control termination in a completely general way. We do this by making class Simulation an abstract class, with an abstract boolean method stop() that must be defined for each simulation program, i.e. for each subclass of Simulation. In this way the user of the Simulation class can choose any termination criteria, e.g. based on time (execute for 100 simulated days) or on the simulation state (simulate the manufacturing of 5000 components). Of course, it may be that the event list becomes empty before the stop method delivers true - you need to check this separately and terminate the simulate() loop accordingly. Note also that updating the current time may cause the stop method to deliver true, when previously it delivered false. You should check this before invoking each event.Class Event
    The Event superclass should be defined to be abstract with an abstract void method invoke() that must be defined in each subclass. This is the method called by the simulate method above when the event reaches the front of the event list. The time at which the event should be invoked needs to be recorded in an instance variable visible to the simulation package but not elsewhere. You want to ensure that the current simulated time can only be accessed from outside the package by calling the method now().The Event constructor should take two arguments. The first (a double) is the interval of time that must pass before the event is to be invoked, i.e. it is not an absolute time. The second argument (a Simulation) should be the "current simulation". This needs to be passed to all events so that they have access to both the current time and the event list, in case they need to schedule one or more future events. The Event constructor should set the invoke time and current simulation instance variables appropriately and add itself to the event list associated with that simulation. The Event class should implement the interface Comparable so that events can be added to the TreeSet representing the event list. This means that it must also contain a definition of the compareTo method described above. in order to define the ordering on events.
    Application Programs
    With the simulation package complete, you can now build discrete-event simulations by importing its various classes. Every simulation has the following general structure:A separate class for each event { each of these will be a subclass of the abstract superclass Event but may take additional parameters, for instance the current simulation state.
    A subclass of the Simulation class, mySim for example, with the stop() method suitably de ned. To kick-start a simulation you need to schedule one or more initial events and then invoke the simulate() method in the superclass.
    An optional set of state/control variables|note that some simulations may not require this. These variables should be instance variables in some class, e.g. State; later you will be invited to experiment by making them static class variables instead. An instance of the state class should be created when the simulation is initialised and passed to each
    event (i.e. each Event subclass) as an additional parameter.You can arrange it so that the Simulation subclass contains the `main' method which sets the simulation running by creating an instance of the simulation class, e.g. mySim. Note that you could put the main method in a separate class, but for this exercise you should include it here. This looks a little odd at rst since the main method creates an instance of a class in which it is de ned! This is perfectly legal, however, as it is a static method.Experiments
    Simple Fixed Schedule
    Schedule a fixed number of instances of a Print event that prints a message when it is invoked. The Print event should take an integer parameter, in addition to the time parameter, that should be displayed as part of the message. For example, given the following schedule (note that this here refers to the "current simulation" ):new Print( 1, 7.2, this ) ;
    new Print( 2, 11.6, this ) ;
    new Print( 3, 2.9, this ) ;and de ning the stop method so that it always returns false, the program should print the following:Event 3 invoked at time 2.9
    Event 1 invoked at time 7.2
    Event 2 invoked at time 11.6You can experiment with the stop method, e.g. making it always deliver false, making it deliver true some time before the last Print event is invoked, making it deliver true before the rst Print event has been invoked etc. Note: no state/control variables are required in this example.In nite Ticks
    Build a simulation class Ticks that initially schedules an instance of a Tick event at time 1. The Tick event should print the current time and then schedule a new Tick event at time t+1 where t is the current time. The program should stop after a speci ed time - a parameter of the Ticks constructor, which should be given as a command line argument. As an example, given a stopping time of 10.0 your program should produce the following sequence of messages:Tick at: 1.0
    Tick at: 2.0
    Tick at: 3.0
    Tick at: 4.0
    Tick at: 5.0
    Tick at: 6.0
    Tick at: 7.0
    Tick at: 8.0
    Tick at: 9.0
      

  3.   

    Simple Queueing System
    Schedule an initial instance of an Arrival event. The Arrival event should change the state of a queue, as represented by a single integer state variable representing the population of the queue. This queue length should be an instance variable in a separate class, e.g. State. The arrival event should also schedule the next arrival event, similar to the Tick event above; it should also schedule a Departure event if the arriving customer has joined an empty queue and therefore moved straight into service. A Departure event decrements the queue population and checks whether there is another customer in the queue. If so, it must schedule that customer's Departure event accordingly, as it will by now have hit the front of the queue. Each event should print a message, reporting the event type, the time of the event and the current population of the queue.You need to make assumptions about the service times and inter-arrival times. To make the simulation more interesting you should use a random number generator to generate the inter-arrival times. Java has a prede ned class Random that can be used to do this. To load this typeimport java.util.Random ;at the top of your simulation class le. The constructor for class Random takes a seed value which is used to initialise the generator. The seed is of type long | a double-precision variant of the int type. If two generators are initialised with the same seed they will produce identical sequences of random numbers; this is useful when testing your program. If gen is the name of the random number generator object then method gen.nextDouble() can be used to generate a random double between 0 and 1. This can be easily scaled, but here we will use the numbers directly as inter-arrival times. The obvious place to put the random number generator is in the same (State) class that stores the state variable for the queue length.The service times should be assumed to be xed at 0.25. In this way the inter-arrival times are twice as large as the service times, on average.The constructor for the queue simulation should take the stopping time and random number seed as parameters. You should provide these as command line arguments to your main program class SSQSim. For example, given a stopping time of 4.0 and a seed of 1987281099, execution of the simulation should produce the following trace:Arrival at: 0.7777800058166073, new population = 1
    Arrival at: 0.8532981371697105, new population = 2
    Departure at: 1.0277800058166073, new population = 1
    Departure at: 1.2777800058166073, new population = 0
    Arrival at: 1.717518578826721, new population = 1
    Arrival at: 1.939347130653938, new population = 2
    Departure at: 1.967518578826721, new population = 1
    Arrival at: 1.9765137857679331, new population = 2
    Departure at: 2.217518578826721, new population = 1
    Departure at: 2.467518578826721, new population = 0
    Arrival at: 2.828433719186867, new population = 1
    Departure at: 3.078433719186867, new population = 0
    Arrival at: 3.157263945340226, new population = 1
    Arrival at: 3.1874794285156836, new population = 2
    Departure at: 3.407263945340226, new population = 1
    Arrival at: 3.4427516133419704, new population = 2
    Arrival at: 3.5976721621551273, new population = 3
    Departure at: 3.657263945340226, new population = 2
    Departure at: 3.907263945340226, new population = 1
    SIMULATION COMPLETEYou should be able to reproduce this exactly!
      

  4.   

    LZ,分这么少,就不要出这么多题了吧? 像是国外学校的Assignment啊