WEB BASED SIMULATION USING JAVA

advertisement

WEB BASED SIMULATION USING JAVA, Pidd & Cassel

WEB BASED SIMULATION USING JAVA

M

ICHAEL

P

IDD AND

R

ICARDO

A.

C

ASSEL

Department of Management Science

Lancaster University

Lancaster LA1 4YX

UK

A BSTRACT

The emergence of Java as a general purpose programming language provides yet another vehicle for writing discrete event simulation programs. The resulting programs may be straightforward, much like programs written in C++, or might make use of Internet technology to support multi-threaded and distributed processing. This paper discusses the suitability of Java for this purpose and shows how it can be used to develop discrete event simulations in a variety of ways to implement different simulation paradigms, or world-views.

Page 1 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel

I

NTRODUCTION

Java or JAPL?

In many people's minds, Java and the world-wide-web are almost synonymous.

However, it is important to realise that, in some ways, Java is JAPL (Just Another

Programming Language). Many features of Java are little different from those that are found in other contemporary programming languages (Deitel & Deitel, 1997). There are assignment statements, logical branches, input:output functions and a scheme that allows user-defined types. Indeed, much of the syntax of Java is based on that of C++, which in turn stems from C, a language with its own antecedents. Similarly, its approach to object orientation seems to come from that employed in SmallTalk, which itself came from Simula (Dahl and Nygaard, 1966), a simulation language based on Algol that promoted much of what is now known as object orientation.

Hence, rather as a Portuguese speaker can read Spanish because both descend from

Latin, so most people who know a modern programming language can make some sense of a Java program. However, as with the two Iberian languages, there are some important differences. Why then has Java become so popular? There seem to be three reasons for this, over and above its familiar syntax.

Portability

Java’s portability is due to the fact that it is neither a compiled nor an interpreted language in a pure sense. When a Java source program is compiled, a new file with the suffix class is created. This class file contains an intermediate binary code which is not executable machine code, and which will be the same on whatever platforms it is compiled. This file is then interpreted by a platform-specific interpreter or JVM

(Java Virtual Machine), that will know exactly what to do with the code according to the platform in which it is installed. This feature is attractive to those who wish to use the Internet, because their code can be sited in one computer and be downloaded to and run in another, completely different, computer.

Multi-threading

Though simple computer programs are written to run as a single, continuous process, larger ones may be multi-threaded. A thread is a lightweight process that can be run

(relatively) independently of others. Thus, if a program has multiple threads, then this

Page 2 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel allows the program execution to be interrupted and re-started, it also allows the different threads to run on parallel processors or computers should that be desirable.

Operating systems such as Unix and Windows NT are multi-threaded and provide support for multi-threaded programs. C++ provides some support for multi-threading, but this is really a clumsy add-on to the language. Multi-threading is built-in to the basic concept of Java as a language.

For web-based applications, this multi-threading is important, since a single application may involve the co-operative operation of several computers. This might be a client/server style interaction, in which clients request services from the server, or it might be distributed on a more equal basis. When this multi-threading is linked with the portability of Java discussed earlier, then this presents an opportunity for computer simulations to be distributed around the web, should this be required.

A discrete event simulation lends itself very well to multi-threading, since it is usually simulating a system that is truly parallel. Consider, for example, a simulation of a baggage handling system at an airport. The entities of the system might be aircraft (taking off and landing), passengers (depositing and collecting bags), the conveyor systems and the carousels. At any time in the simulation, different aircraft will be doing different things and a simulation needs to reflect this parallel activity.

As will be discussed later, this parallelism is simulated by various devious means for applications based on a single processor computer. However, in principle at least, there is no reason why the operations of each entity should not be modelled as interacting threads.

There is, however, a danger with multi-threaded programs since the control of the threads is usually handed over to the operating system. The latter may have its own priorities as it runs the threads and the precise control sequence cannot always be guaranteed. This is important when an application is developed under one operating system and run under another. Hence, Java should probably not be used for missioncritical applications.

Class libraries

Java is a small core language with extensive class libraries written in the core language. Thus, the JVM needs only to cope with the small core of the language, but applications can be developed using the class libraries. In turn, the in-built class

Page 3 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel libraries can be extended and refined to develop application-specific packages, such as for discrete simulation. All versions of Java come with in-built class libraries that include facilities such as the following that are useful for discrete simulation.

 awt , which provides classes for developing graphical user interfaces that are independent of the operating system in use.

 net , which contains classes that support network applications

 util , a set of utility functions, including random number generation

 rmi , with classes for remote method invocation, used when an application is distributed across several computers.

It should, however, be noted that many of these classes are somewhat crude because they need to run on any computer with a JVM. In particular, awt produces GUIs that are OK, but only just. It is also possible to use the Java Foundation Classes (JFC) which include swings as the basis of rather better GUIs. For truly portable and fully flexible GUIs, it may be necessary to write classes that are specific to the hardware and software in use.

D

ISCRETE SIMULATION WORLD

-

VIEWS

There are many different ways of organising the detailed inner workings of a discrete event computer simulation program (see Pidd (1998) for full details). However all the various approaches share a common basic architecture as shown in figure 1. This reveals that there are three main components (identified by Fishman (1973)) hidden away behind the user interface.

The first is the simulation executive, which is sometimes known as the control program. Its task is to ensure that the logic of the simulation application is executed in the correct dynamic sequence. Thus, for example, it is important for an aircraft to land before the passengers and their bags are disgorged into the airport. This is usually achieved via the executive’s maintenance of a simulation clock and an event list. The simulation clock is a counter that records the current simulated time. It should never move backwards, instead, as time passes in the simulation, the value of this simulation clock increases. As the entities in the simulation change state, they do so at system events . One such event might be, for example, an aircraft reaching its air-bridge so that the passengers may disembark. On a real flight, the captain radios ahead to tell the airport when the flight is due to arrive so that they can prepare for this. In the

Page 4 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel simulation, when a flight starts, the executive receives an instruction to note that the event flight ends is due at some time in the future. The executive maintains a list of these future events, known as the event list . If the list is maintained in a chronological sequence (earliest at the top) then all the executive has to do is check the top event notice to see when the next event is due. In this way, the executive sequences and schedules the events in which the entities engage during the simulation. The executive can be written by the system provider and should cope with any discrete event simulation application that is organised to communicate properly with it.

Insert figure 1 about here

The second element below the user interface is the simulation library , and this is also the responsibility of the system provider rather than the analyst developing a particular application. This contains useful general-purpose routines and utilities.

Examples include functions for the following.

Generating random variates such as those for Normal, Poisson and Weibull distributions as well as random numbers. The algorithms for these are well known and are discussed in detail in Law and Kelton (1991) amongst others.

Managing aspects of the input:output which are not the responsibility of the GUI.

If output is to be purely text based, then all the input:output routines may reside here.

Tracing and debugging support, needed for programs of any complexity. Ideally, these should relate to discrete event simulation and not just to error checking in the language being used.

Some languages make it simple for a simulation analyst to extend a library so as to make it useful for a whole class of applications.

The third element in figure 1 is the application logic . This is the responsibility of the simulation analyst who is developing a model of a particular system. The logic encodes the ways in which the simulation entities combine to act out the distinctive behaviour of the system being simulated. It includes the events that the simulation executive must control via its event list. What are referred to as simulation worldviews in the title of this section are different ways in which the application logic can

Page 5 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel be presented to the executive. Whichever way is chosen, the end result of the simulation must be the same - otherwise the model would be invalid.

The harassed booking clerks

The best way to understand simulation worldviews approach is via an example. The harassed booking clerks work in a theatre booking office and have just two tasks to perform. They attend to people who arrive in person and queue for service. They also answer the phone when it rings. There is a call queuing system in operation and, for simplicity, we can assume that callers do not ring off, but wait (and wait) until their call is eventually answered. We can also assume, again to keep things simple, that personal enquirers are also infinitely patient. Suppose that the theatre manager wishes to know how many booking clerks to employ.

Event-based simulation

Perhaps the earliest world-view to be suggested for discrete event simulation is what was, confusingly, known as an event-based approach. In this, the simulation logic is atomised into a set of independent event routines. The most widely used early simulation language was SIMSCRIPT (Markowitz et al (1963)) and its first versions required analysts to program in this event-based style.

An event-based approach simulation model of the theatre booking office would consist of the following event routines.

1.

Personal enquirer arrives: if a clerk is idle, a service begins and its end must be scheduled via the event list. Otherwise, the arrival joins a queue. The next personal arrival event is scheduled on the event list.

2.

Phone call arrives: if a clerk is idle, a phone conversation begins and its end must be scheduled via the event list. Otherwise the call joins a queue. The next phone call event is scheduled on the event list.

3.

End of personal service: if a personal queue exists, a new service starts and its end must be scheduled via the event list; or, if a phone queue exists, a new phone conversation starts and its end must be scheduled via the event list. Otherwise, the clerk becomes idle.

4.

End of phone conversation: if a personal queue exists, a new service starts and its end must be scheduled via the event list; or, if a phone queue exists, a new phone

Page 6 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel conversation starts and its end must be scheduled via the event list. Otherwise, the clerk becomes idle.

Thus, the clerk has three states: idle, serving a personal enquirer or talking on the phone.

As should be clear from the description above, each event leads to the scheduling of another event. For example, when phone call n arrives, we must then determine when phone call n+1 is due and note this on the event list.

Event

Personal enquirer arrives

End personal service (clerk 3)

End phone conversation (clerk 1)

Phone call due

End personal service (clerk 2)

Table 1: Event list, event-based simulation

Time due

51

51

53

55

60

If the simulation clock has reached, say time 50, then the event list might be as shown in table 1 if there were 3 clerks. The simulation executive controls the progress of the simulation by looking at the event list to find the time of the next event (51 in this case). It then executes the event routine(s) due at that time - which will result in more entries on the event list unless the event is one that terminates the simulation. When all events due at this current time (51) are complete, the executive repeats itself.

Three-phase simulation

Tocher (1963) suggested an improvement on event-based simulation. An event-based executive has a two-phase operation.

1.

Check when the next event is due and moves the simulation clock to that new time

(51 in table 1).

2.

Force the execution of the events now due at this clock time (next personal enquirer arrives and end personal service (clerk 3)).

Tocher added an extra phase from his realisation that there are two types of activity in a discrete simulation model. The first type he called a B, short for Bound, since it was bound to happen at some time. Such activity can be directly scheduled on the event list, and an example would be the end of a personal service once one has started. The other type he called a C, short for Conditional or Co-operative activity. These cannot be directly scheduled since they depend on the current state of the simulation.

Page 7 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel

Examples might be the start of a personal service, since this depends on two states: there must be an idle clerk and there must be at least one personal enquirer waiting for service. The harassed booking clerk example could be atomised into Bs and Cs as shown in table 2.

Type Name Conditions Actions

C

C

Begin personal service

Begin phone conversation

Clerk idle & personal queue exists

Clerk idle and phone call waiting in queue

None B

B

B

Personal enquirer arrives

Phone call arrives

End of personal service

None

None

B End of phone call

None

Table 2: Bs and Cs for harassed booking clerk

Take customer from head of queue, engage server, schedule end of personal service.

Take call from head of queue, engage server, schedule end of phone conversation.

Place enquirer in queue, schedule next arrival.

Place call in queue, schedule next call.

Clerk to idle, customer leaves.

Clerk to idle, caller hangs up.

At first sight this seems more complicated than the event-based approach, but the payoff comes when complex models are programmed. In the event-based approach, each event routine must include the logic to follow through all possible logical consequence of that event. Hence, when a phone call ends we must consider whether the clerk is free and if so, whether there are callers or enquirers waiting and, if so, what to do. If, to use the cynical jargon of modern management we empower the clerks (that is, we give them many more tasks to do), then the logical interaction of these tasks must be handled within each event routine.

In a three-phase simulation, only the Bs are scheduled on the event list and there are four in this example, just as there were four event routines. In advance, we cannot say which of the Cs will need to be executed as a result of the Bs. Thus, the repeated three phases of the executive are as follows.

1.

Find time of next event, move clock to this time.

2.

Execute all Bs that are scheduled for this time.

3.

Try each C in turn, only doing their actions if the conditions are met. For example, only start a personal service if a clerk is idle and a customer is waiting.

Page 8 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel

Suppose that the clerks are unfortunate enough to be empowered (given more work), then we need to enhance our simulation model. In a three-phase approach, we just ad extra Cs and Bs. We need not worry too much about their interactions if we are careful in designing the Cs. Hence for complicated simulations, a three-phase approach is better than an event-based one.

Process-based simulation

This approach was first built into Simula (Dahl & Nygaard, 1966) and then surfaced in SIMSCRIPT II.5 (Russell, 1987). Its building block is a process, which is a chronologically sequenced set of events and activities through which an entity will pass. It assumes that entities are organised into classes, each which has its own process or processes. Thus, the process of a personal enquirer is to arrive, wait for service if the clerks are busy, be served when a clerk is free and they are next in line, then to leave when the service is complete. Thus, a process-based simulation consists of a set of process descriptions for each class of entity in the simulation, and the simulation proceeds as the processes interact. Presented in these terms, a processbased approach is intuitive and is therefore rather appealing.

However, underneath the mask of processes there is still the need to schedule things for the future and to commit and release resources. Hence, most process-based simulations also employ a three-phase approach, though one that is subtly rather different from Tocher's. To understand this, suppose that the harassed booking clerk model has two processes (for personal enquirers and for phone callers) and that they compete for the resource that we think of as clerks. It is still necessary to schedule the next arrivals and the times at which the service or phone conversation will end. This is often done via two lists. The first is often known as the future events list and it roughly corresponds to the event list in an event-based simulation. The second is usually called the current events list.

When a new entity of either type arrives, it must create a new instance of its own type (the next arrival). On creation, this new instance is passivated , which means that an entry is added to the future events list which will serve as an instruction to activate the entity when it is due to arrive. When its arrival time comes, it is activated and if its service can begin it is passivated once more by adding a notice to the future events list noting when the service will be complete. Otherwise it is passivated until

Page 9 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel its turn comes and sits on the current event list. The executive's three-phase cycle is as follows.

1.

Find the next activation time for entities on the future events list and move the clock to this time.

2.

Take all the records of those entities due to be activated at this time from this events list and move them to the current events list.

3.

Work through the current events list entity by entity, activating each and moving it through its process until it can move no further, then passivate it or, if its work is done, delete it.

There are two problems with this approach. The first is that the process descriptions can become very complicated in complex simulations and their interactions even more so. This can make debugging a nightmare. The second is that there is a risk of deadlock if one passivated entity is waiting for another passivated entity. This needs great care in complicated simulations.

U

SING

J

AVA FOR THREE

-

PHASE SIMULATIONS

It is very easy to develop a simple discrete event simulation library (a simulation package in Java terms). As in all object-oriented work, the key is to consider the classes that will be needed by someone who may wish to extend the package for their own use. In the case of discrete simulation, the three components of figure 1 must obviously be present. Thus, there is a need for a class to represent the simulation executive, a class to represent the application logic and a class to represent the library of utilities that support the simulation. A simple three-phase Java application is available from the following URL: http://www.lancs.ac.uk/staff/smamp/MPCSMS4.html

The simulation executive

As made clear earlier, the executive must sequence and schedule the activities of the entities whose behaviour will be simulated. This is done using an event list on which notices are kept in a chronological sequence. This list grows as events are scheduled and shrinks as they are executed at the appropriate times. The Java.utils

class that comes with the JDK includes a vector class. This is a growable array whose contents may be instances of any legitimate Java class. In addition the executive needs a few new methods to ensure that the chronology of the event list is managed.

Page 10 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel

In addition, the executive class should keep track of the current simulation clock time and should also know the intended duration of the simulation. When an entity is scheduled for some future event, a notice to that effect is added to the event list vector. In all simulation world-views, the executive continues a repeated cycle until the simulation is over. As is made clear earlier, the first phase of the cycle is to find the time of the next event - which is simple if the list is kept in chronological sequence. It may be convenient to keep the list in a non-chronological sequence and to search through it for the next event time, in which case, an efficient search procedure is needed.

In Java, a simple simulation executive might have the following state variables. public class Executive { private static long RunDuration = 0; // Simulation duration private static long Clock = 0; // Current clock time private static long PrevClock = 0; // Previous clock time private static Vector EventList; // Event list private static GEntity CurrEnt = null; // Currently active entity private static boolean TraceOn; // To trace or not?

Of these, only the GEntity class will need explanation and this is discussed below.

The variable CurrEnt is used to represent the current entity - that is, the entity whose event is currently being processed. If there is no such entity, then it takes a null value.

Application logic

To represent application logic, we need a way to represent simulation entities and their events, activities and processes. Entities may be represented by an abstract general entity class, GEntity . Because it is an abstract class, it will have no actual instances (no objects a direct members) but will be used to create whatever actual entity classes are needed in a particular simulation. A simple version of the state variables for a GEntity class might be as follows. public abstract class GEntity { public String Name; protected long NextTime = 0; protected long PrevTime = 0; protected boolean Avail = true; protected int NextEvent = 0;

// Identifier

// Time of next event

// Time of last event

// Available or not?

// Next event if known

Thus, each entity will have a name to identify it, two times to represent the time of its next and last events, a boolean field to indicate if it is scheduled for an event (if not, the next event time is meaningless) and some way to indicate the event in which it must engage. There are many ways to implement the next event and the simplest is shown above. This represents the event by an integer and the set of events open to an

Page 11 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel entity is then managed as a list. The value of NextEvent is then used to indicate which event should be executed. In effect, this is a form of pointer.

The event list of table 1, now represented as a vector of GEntity details might look as follows when the simulation has reached time 50. This assumes that three entity classes are defined and that these (Personal enquirer, Phone call and clerk) all descend from the GEntity class

Name NextTime PrevTime Avail

Personal enquirer clerk clerk

Phone call clerk

51

51

53

55

60

45

48

43

46

45

False

False

False

False

False

Table 2: A simple event list using the Java.util.vector

class

NextEvent

1

1

2

1

1

Thus, the events arrive and end serve , belonging to members of the Personal enquirer and clerk classes, are due for execution at time 51.

(arrival)

(end serve)

(end talk)

(arrive)

(end serve)

Utilities

Finally we need to provide a set of classes to provide the utility support needed in the simulation. For example, the simulation of the theatre booking office will need some way of managing queues. Again, the Java.util.vector

class comes in useful, since this can be used as the basis of a class to manage FIFO queues and can also be used for queues with more complex priorities.

In addition, the simulation will need some way of taking pseudo-random samples from appropriate probability distributions. The Java.util.random

class, that includes a pseudo-random number generator and also sampling routines for Normal distributions, can be extended for this purpose. It would, however, be inadvisable to use these sampling routines for serious work since the pseudo-random number generator does not meet the criteria set out by L’Ecuyer (1994). For serious work, it would be a good idea to develop a sampling package from scratch.

Finally, some form of trace and debugging support is desirable. One way of doing this would be to develop a Trace class that, if the Executive 's TraceOn variable is True, writes information to a trace file as and when needed. This can be checked after the run to see if the simulation's dynamic logic is correct. In addition, some form of graphical or animated output could be added.

Page 12 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel

S

UMMARY

It should be clear from the above that any programmer who is competent in Java will find that it is straightforward to develop a Java-based discrete event simulation library that is useful for real work. However, this article only scratches the surface and Java has many other possibilities for extending discrete event simulation. For example,

Pidd and Cassel (1998a, b)) and Page et al (1997) discuss how the in-built support provided by Java for remote method invocation may be used to support distributed and client/server simulations. Both regard Internet and intranet protocols as a suitable way of supporting these developments. These present new ways of developing parallel and distributed simulations that do not need expensive, special purpose computer systems.

Based on developments in computer supported co-operative work, Wilcox and

Pooley (1998) discuss how Java can enable simulation users to jointly run simulation models even though they are separated in space and time. There is clearly much more to come.

R

EFERENCES

Dahl O. & Nygaard K. (1966) SIMULA - an Algol-based simulation language . Comm

ACM 9, 671-678.

Deitel H.M. and Deitel P.J. (1997) Java: how to program.

Prentice Hall, NJ

Fishman G.S. (1973) Concepts and methods of discrete event digital simulation . John

Wiley & Sons, New York.

L’Ecuyer P. (1994) Uniform random number generation . In Balci O.(1994) (Ed)

Annals of operations research, Vol 23: Simulation and modeling. J.C. Balzer,

Basel.

Law A.M. and Kelton W.D. (1991) Simulation modeling and analysis (2 nd ed).

McGraw-Hill, New York.

Markowitz H.M., Hausner B. & Karr H.W. (1963) SIMSCRIPT: a simulation programming language . RAND Corporation, RM-3310-pr.

Page E.H., Moose R.L. Jnr and Griffin S.P. (1997) Web-based simulation in SimJava using remote method invocation. In Proceedings of the 1997 Winter

Simulation Conference

. ed S. Andradôttir, K.J. Healy, D.H. Withers & B.L.

Nelson, 468-474, Atlanta, GA, 7-10 December.

Pidd M. (1998) Computer simulation in management science (4 th

ed). John Wiley &

Sons Ltd, Chichester.

Pidd M. and Cassel R.A. (1998a) Three phase simulation in Java. In Proceedings of the 1998 Winter Simulation Conference . 13-16 December.

Page 13 of 14

WEB BASED SIMULATION USING JAVA, Pidd & Cassel

Pidd M. and Cassel R.A. (1998b) Using Java to develop discrete event simulations.

Submitted to Jnl Opl Res Soc.

Russell (1987) SIMSCRIPT II.5 programming language . CACI, La Jolla, CA, USA.

Tocher K.D. (1963) The art of simulation . English Universities Press, London.

A

CKNOWLEDGEMENT

Ricardo A. Cassel's research is funded by the Brazilian Government through CAPES -

Fundacao Coordenacao de Aperfeicoamento de Pessoal de Nivel Superior.

Page 14 of 14

Download