Escolar Documentos
Profissional Documentos
Cultura Documentos
Jeffrey A. Joines
Stephen D. Roberts
78
An Introduction to Object-Oriented Simulation in C++ 79
The class definition specifies the object’s properties, the An OOS models the behavior of interacting objects over
attributes and behaviors. The attributes define all the time. However before we can consider a simulation, we
singular properties of the object while the behaviors de- needs to understand how objects interact or communi-
fine how the object interacts within the environment with cate with each other. The interaction among objects is
other objects. performed by communication called “message passing.”
Attributes are considered the data members of an ob- One object sends a message to another and the receiving
ject. In the case of our Exponential random vari- object then responds. An object may simply publish a
able, its mean (given by the identifier mu) would be a message which may be responded to by one of several
real number attribute. objects. For example in a bank simulation, a customer
arrives at a bank and may be served by any of several
double mu;
tellers. In a O-O context, the customer publishes their
Other attributes would be similarly defined. arrival and waits for service by a teller. There are sev-
The behaviors (sometimes referred to as methods) of eral ways in transmitting messages in an object-oriented
an object represent actions the object can perform or program and it depends on the programming language.
take. For example, if the exponential random variable
needed to obtain a sample, the following member func- 2.2.1 Direct Reference
tion can be used:
double sample(){
Perhaps the simplest form of message passing is direct
return –mu * log( 1.0 – randomNumber() );} reference to the object’s attributes or data members. For
example, if the interarrivalTime object needed to
where the randomNumber() function yields a uniform have a mean of 5.5, then the simplest means to commu-
random variable between 0 and 1. By representing be- nicate this message is through direct assignment.
havior with functions, the object can react to parameters
interarrivalTime.mu = 5.5;
passed in the function argument as well as change vari-
able values within the function. This message causes the object to receive the value and
set its variable mu. This is a forced message because the
2.1.2 Classes and Instances object has no choice but to perform the action.
Notice, the word “class” not “object” is used in defining 2.2.2 Data Methods or Functions
the object which can be confusing since it would seem
that we are defining objects. Lets consider the more Rather than forcing a value upon an object, a value could
complete definition based on our prior discussion of en- be communicated to the object and then let it determine
capsulation and properties (ignore the “public” for now), how to deal with the value. For example, if a new
the Exponential class is defined as follows. “member function” or data method to the Exponential
class Exponential{ class called setMu() was added as follows.
public:
void setMu( double initMu ){
double mu;
mu = initMu; }
double sample(){ return –mu * log( 1.0 –
randomNumber() );}
};
Now the object is sent the setMu message with a
message value of 5.5 which “communicates” our interest
Rather than defining an object directly, a class is defined in changing the mean. The interarrivalTime ob-
where the class provides a “pattern” for creating objects ject receives the message and changes its internal value
and defines the “type.” By defining a class (of objects), of mu.
rather than a single object, the opportunity exists to use
the class to create many objects (i.e., re-use existing interarrivalTime.setMu(5.5);
code). Furthermore, as seen later, the class is a descrip-
tion of a pattern for constructing objects which can be Although this example really does the same thing as the
easily extended. direct reference, there are important distinctions. First,
in our function call we simply “passed” the value of 5.5
Now that the class is defined, objects can be created
to the object. Second, we didn’t tell the object how to
directly from this class. These created objects are called
change the attribute mu. The object has a function writ-
“instances” of a class. For example, serviceTime is
ten by the designer of the Exponential class that
an instance of the Exponential class.
causes the mean parameter to change. Notice, the user
Exponential interarrivalTime, serviceTime; of the function does not need to know how the function
80 Joines and Roberts
inside the class works. In fact, the class designer could example, if we wanted the exponential to accept an inte-
change the internal name of mu to expMean within the ger specification of its mean.
class, and all exiting user code would remain the same. Exponential( int startMu ){
This encapsulation of the data is extremely important in mu = startMu;}
OOS. Also, the same message can be made to respond
Now, exponential objects with either a double or an int
to several different message value types often referred to
as arguments can be specified (actually C++ will make
as “polymorphism.”
appropriate conversions among its built-in types, but this
example illustrates the way a user could provide conver-
2.2.3 Pointers
sions among user-defined classes). The following creates
two objects using different argument types.
Another way to communicate is indirectly through
pointers which are simply addresses of the location of an Exponential arrival(9.3), inspect(6);
object. For example, a pointer to the interarrival- Users can also define a special member function
Time object can be created as follows and the setMu called a destructor that acts when the object is destroyed.
message can be sent via the pointer. For example, a destructor for the exponential class has
Exponential * rnPtr = &interarrivalTime; the following form.
rnPtr->setMu(5.5);
~Exponential(){
Pointers have the advantage of not needing to know the // print out how often used? }
particular object ahead of time, but only the address of Only one destructor can be defined since a destructor has
the object. Thus, if we change the pointer to point to the no arguments.
serviceTime object, the format of the message re-
mains the same. With a more complex message, use of 2.3.2 Visibility of Properties
pointers becomes very convenient.
RNPtr->setMu(3.5); It should be clear that a user of a class does not really
need to know the internal workings of the class. For ex-
2.3 How Are Objects Formed? ample, they do not need to know what algorithm is used
to obtain the sample (they may want to know for their
In our example, the exponential object has no ability to own assurance). Furthermore, the designer of the class
be created with different means. Instead, the object’s may not want the user of the class to know everything
mean was changed to a specific value. Although an ob- about the class. Thus, the class designer has the option of
ject can be instantiated from a class without special in- causing properties of the class to become invisible to
structions, often we want the creation to accomplish users of the class and to provide a public interface to
certain objectives. Likewise, we also might want to do those hidden properties. The two most frequently used
something special when an object is destroyed. labels are “public” and “private.” Properties within a
class that are public can be accessed directly by a user
2.3.1 Constructors and Destructors while those that are private are available only to the de-
signer. For example, the variable containing the mean is
Special member functions can be defined that act when made private within the class to prevent improper use
an object is created and destroyed which are called con- (i.e., direct manipulation). Our class would then look like
structors and destructors, respectively. The constructor the following.
is recognized by having no return type and having the class Exponential{
same name as the class. For example, the following private:
could be a constructor for the exponential object. double mu;
public:
Exponential( double initialMu ) { Exponential(double initialMu ){mu=initialMu;}
mu = initialMu; } double sample();
void setMu( double changeMu ){mu = changeMu;}
This function accepts the invocation argument and sets double getMu( ) { return mu; }
the internal mean to it. An object whose initial mean is };
4.3 can be specified upon creation as follows. Now mu cannot be changed directly by a user. Thus the
Exponential serviceTime(4.3); direct reference to mu, as done earlier, will fail. Now
In C++, functions can be “overloaded” so that they differ communication to the exponential objects must be per-
only in their formal arguments (i.e., “polymorphism”). formed through member functions. The designer of the
Therefore, a class can have multiple constructors. For class can now protect the class data members from un-
wanted changes while the user of the class is unaffected.
An Introduction to Object-Oriented Simulation in C++ 81
2.4 How Are Objects Formed From Others? parent-child relationship. In our example, the exponen-
tial can be considered a kind of random variable. It
One of the fundamental benefits of an O-O design is the would be useful for the Exponential to be a child of
ability to make other objects out of existing ones. We RandomNumber and thus inherit all the random vari-
have already seen how to design a class of objects using able properties. Hence, what could be done to the ran-
the built-in types from C++. Suppose the following ran- dom variable could also be done with the exponential.
dom number class has been defined which generates No additional setSeed() is required since the one in
uniformly distributed numbers between 0 and 1. the random class can be used.
class RandomNumber{ For example, sometimes a sample from an exponen-
long seed; tial is needed while other times a basic uniform genera-
public
RandomNumber( long seed = -1);
tor is required. Suppose the following two objects and
void setSeed(long sd){seed=sd;} pointer are defined:
virtual double sample();
RandomNumber uni;
};
Exponential exp(5.5);
In this definition, the constructor argument can be speci- RandomNumber * pRN = &uni;
fied or left blank to default to their initial values (i.e., -1 If at an activity in our simulation, a sample from a ran-
means use the next seed). The public member function dom variable is needed, the following message is sent to
sample() is used to obtain a sample and we will assume obtain an activity time.
that the seed will be updated appropriately with each call pRN -> sample();
to sample(). The “virtual” keyword will be discussed
later. However, because Exponential is also a RandomNum-
There are two ways this random number generator ber, the pointer pRN could be assigned to either an Ex-
could be used with our Exponential class. The first ponential or a RandomNumber and the same mes-
method is called composition, in which a random num- sage applies. In the composition example, two separate
ber object is included within the exponential class. The activities would be required (i.e., one which used an ex-
second method of using the random number generator is ponential and another one which used a uniform).
through inheritance which makes the exponential class pRN = &exp;
a kind of random number. Inheritance is one of the ma-
In an true O-O language with inheritance, the message
jor features that distinguish a “object-based” language
would be sent to the proper object and the sampling
from a true “object-oriented” one.
would be from the correct sampling function. In O-O
terms, determining which variate to sample at run-time is
2.4.1 Composition called “run-time” binding and is performed by specify-
ing the sample() to be “virtual” in the parent class.
First, consider the case of composition where we simply
To specify that Exponential inherits from Ran-
compose the new class from the existing class:
domNumber, the header for the class definition would
class Exponential{ be modified as:
private:
double mu; class Exponential: public RandomNumber{...
RandomNumber rn;
public: showing that RandomNumber is the parent and its visi-
void setSeed(long sd){RN.setSeed(sd);} bility is “public”.
… //Public Properties
}; Under inheritance, the child class inherits the public
(and protected) properties of the parent. Now these
Notice that the Exponential is defined simply to properties are directly available to the child class and the
“have” a RandomNumber. In O-O parlance, the rela- class type resolves any conflicts. C++ also permits mul-
tionship between the Exponential and the Random- tiple inheritance, meaning a child can inherit from sev-
Number rn is called a “has-a” relationship. The data eral parents.
member rn is used in the sample() function of the ex-
ponential. Notice, a setSeed() needs to be defined in 3 OBJECT-ORIENTED VS. OBJECT-BASED
order to access the one in the random variable.
Because many simulation languages offer pre-specified
2.4.2 Inheritance functionality produced in another language, the user
cannot access the internal function of the language. In-
The second kind of relationship among classes is called stead, only the vendor can modify the internal function-
an “is-a” relationship and is based on inheritance or a
82 Joines and Roberts
ality. Also, users have only limited opportunity to extend user-defined robot class can be added to a language that
an existing language feature. Some simulation languages contains standard resources without compromising any
allow for certain programming-like expressions or aspect of the existing simulation language, and the robot
statements, which are inherently limited. Most lan- may be used as a more complex resource. There are two
guages allow the insertion of procedural routines written basic mechanisms in C++ that allow OOS to provide for
in other general-purpose programming languages. None extensibility: inheritance and genericity.
of this is fully satisfactory because, at best, any proce-
dure written cannot use and change the behavior of a 3.2.1 Inheritance
pre-existing object class. Also, any new object classes
defined by a user in general programming language do Inheritance allows classes to exploit similarity through
not co-exist directly with vendor code. specialization of parent classes (i.e., child classes inherit
the properties of the parent and extend them). All event
3.1 Object-Based Extension types have an associated eventTime and eventType
and the appropriate data methods to specify these prop-
erties. Therefore, specific event types would inherit these
The object-based approach only allows extensibility in
properties and provide additional ones (see Figure 1).
the form of composition (i.e., new objects can only be
created out of existing objects). The simple Event ob- getEventTime
Event
Event Time
ject will demonstrate the limitations of extensibility only setEventTime Event Type
processEvent
entities into the network and end of service events. No- Figure 1: Inheritance Hierarchy
tice that depending on the type of event, the appropriate
event handling function is called. This is an example use For example, NodeEvent, which provides events that
of composition. occur at nodes (e.g., end of service at an activity), pro-
vides a pointer to the Node of interest and the Entity
class Event{
private: which caused the event. The processEvent() is
double eventTime, eventType; declared virtual so that the appropriate processEvent
Source *source; is fired when the event is pulled off the calendar. The
Activity *activity; //… More properties
public: Event’s processEvent() is a pure virtual function
void processEvent(){ meaning any child classes must re-define it. The No-
select EventType{ deEvent’s invokes the nodes executeLeaving()
case ArrivalEvent:
source->newArrival(Entity); break; (another virtual function in the node hierarchy).
case EndofService: //Event’s processEvent
activity->endofService(Entity) break;}} void virtual processEvent() = 0
//… Additional Properties
}; // ProcessEvent’s processEvent
void virtual processEvent(){
If the user wants to add additional events (e.g., a monitor processPtr->executeProcess(entityPtr);}
event), it would require the designer to add an appropri-
ate data member, data methods, and then provide an ad- //NodeEvent’s processEvent
ditional case statement. Therefore, the designer has the void virtual processEvent(){
nodePtr->executeLeaving(entityPtr);}
impossible problem in anticipating every kind of event. //ExecuteLeaving -virtual function in Node
3.2 Object-Oriented Extension Now the designer does not have to anticipate every type
of event. Users have the ability to define their own
An object-oriented simulation deals directly with the events provided they inherit from an existing event class
limitation of extensibility by permitting full data ab- and provide an appropriate processEvent() function.
straction. Data abstraction means that new data types
with their own behavior can be added arbitrarily to the 3.2.2 Parameterized Types
programming language. When a new data type is added,
it can assume just as important a role as any implicit data Even with inheritance, many O-O languages can still be
types and can extend existing types. For example, a new limiting in terms of extensibility. C++ provides an addi-
An Introduction to Object-Oriented Simulation in C++ 83
tional method of extensibility called genericity or pa- simulation platform, another means of encapsulation is
rameterized types (i.e. templates). Parameterized types to place higher level complex interactions into “frame-
are special forms of composition that exploit commonal- works.” For our purposes, frameworks are used to de-
ity of function. For example, most simulations would scribe those collections of classes that provide a set of
declare a source object which is used to place entities specific modeling facilities. The frameworks may consist
into the network. In an OOS environment, the user may of one or more class hierarchies. These collections make
want TVs or Orders to arrive rather than generic entities. the use and reuse of simulation modeling features more
The user can create several different source nodes by intuitive and provide for greater extensibility.
inheriting from the base Source class as seen in Figure 2. Special simulation languages and packages may be
Each of the new classes defines a new type of object to
created from these object classes. For more information,
be created (i.e., TV, Order) and the “virtual function”
see Joines and Roberts (1996). YANSL is an acronym
executeLeaving.
for “Yet Another Network Simulation Language” and is
Source
getInterArrivalTime
Entity
just one instance of the kind of simulation capability that
setInterArrivalTime executeLeaving
InterArrival can be developed within an OOS environment.
TVSource OrderSource
getTV TV getOrder Order 4.1 Basic Concepts and Objects in YANSL
executeLeaving setOrder executeLeaving
setTV
The delay and assign nodes are both departure and 4.1.2 Modeling with YANSL
destination nodes, so they inherit from both the departure
and destination node classes. Departure nodes may need When modeling with YANSL, the modeler views the
a branching choice and called BranchingDepar- model as a network of elemental queuing processes
tureNodes. An activity is a “kind of” delay but in- (graphical symbols could be used). Building the simula-
cludes resource requirements. The properties of the tion model requires the modeler to select from the pre-
YANSL nodes allow transactions to be created at source defined set of node types and integrate these into a net-
nodes, wait at queue nodes, receive attribute assignment work. Transactions flow through the network and have
at assign nodes, be delayed at activity nodes, and exit the the same interpretation they have in the other simulation
network at sink nodes. languages. Transactions may require resources to serve
Resources may service transactions at activity nodes. them at activities and thus may need to queue to await
resource availability. Resources may be fixed or mobile
The resource framework for YANSL, shown in Figure 5
in YANSL, and one or more resources may be required
allows resources to be identified as individuals, as mem-
at an activity. Unlike some network languages, re-
ber of alternative groupings, or as members of teams.
sources in YANSL are active entities, like transactions,
SimulationElement Choices and may be used to model a wide variety of real-world
items.
ResourceBase RequirementsAlternative
ResourceDecisionC ResourceSelelectionC 4.2 The TV Inspection and Repair Problem
Resource<ResDC> ResourceTeam ResourceGroup<ResSelC> As a portion of their production process, TV sets are sent
to a final inspection station. Some TVs fail inspection
Figure 5: Resource Framework and are sent for repair. After repair, the TVs are re-
When there is a choice of resource service at an activ- turned for re-inspection. Transactions are used to repre-
ity, then a resource selection method is used. The ability sent the TVs. The resources needed are the inspector
to request a resource service at run-time without speci- and the repairman. The network is composed of a source
fying it explicitly is another example of polymorphism. node which describes how the TVs arrive, a queue for
Choices available in YANSL, shown in Figure 6, possible wait at the inspect activity, the inspect activity
extend those in the modeling frameworks. and its requirement for the inspector, a sink where good
TVs leave, a queue for possible wait at the repair activ-
Choices
ity, and the repair activity. Transactions branch from the
source to the inspect queue, are served at the inspect
BranchingChoice QueueRankingChoice
activity, branch to either the sink or to the repair queue,
Probabilistic Deterministic FIFO LIFO
are served at the repair activity and return to the inspect
queue. The data used in the simulation is that the inter-
ResourceDecisionChoice
ResoureSelectionChoice arrival time of TVs, the inspect service time, and repair
service time are exponentially distributed with a mean of
ORDER LONGIDLE PRIORITY LONGESTWAIT
E 5, 3.5, and 8 minutes respectively, and the probability
Figure 6: YANSL Choice Hierarchy that a TV is good after being inspected is .85.
The choices available add broad flexibility to the deci- 4.3 A YANSL Model
sion-making functions in the simulation, without needing
different classes for each different function. Instead, The YANSL network has all the graphical and intuitive
classes are parameterized with these choice classes and appeal of any network based simulation language. A
the choices consist of several methods. Specifically in graphical user interface could be built to provide “con-
YANSL, they allow for the selection of alternative venient” modeling features. Whatever the modeling
branches from a departure node, selection among alter- system used, the ultimate computer readable representa-
native resources in requirements at an Activity, as tion of the model would appear as follows:
well as provide the decision making ability for resources
#include "simulation.h"
to choose what to do next, and ranking choices among
transactions at an Queue. The choices are used to rep- main(){
resent the time-dependent and changing decisions that // SIMULATION INFORMATION
need to be modeled Simulation tvSimulation(1); //1 replication
An Introduction to Object-Oriented Simulation in C++ 85