Programming Languages for Simulation - polito.it · Programming Languages for Simulation ... simulation: ! SIMULA ! GPSS/H ! ... among blocks and the overall simulation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Special-purpose languages n They are very high level (in terms of
abstraction) programming languages, strictly dedicated to the coding of simulation models
n They natively offer syntactical tools to represent the main elements for the supported modeling approach n Event scheduling -> functions for creation and
SIMULA: Example Simulation Begin Class FittingRoom; Begin Ref (Head) door; Boolean inUse; Procedure request; Begin If inUse Then Wait (door); inUse:= True; End; Procedure leave; Begin inUse:= False; Activate door.First; End; door:- New Head; End; Procedure report (message); Text message; Begin OutFix (Time, 2, 0); OutText (": " & message); OutImage; End;
SIMULA: Example Process Class Person (pname); Text pname; Begin While True Do Begin Hold (Normal (12, 4, u)); report (pname & " is requesting the fitting room"); fittingroom1.request; report (pname & " have entered the fitting room"); Hold (Normal (3, 1, u)); fittingroom1.leave; report (pname & " have left the fitting room"); End; End; Integer u; Ref (FittingRoom) fittingRoom1; fittingRoom1:- New FittingRoom; Activate New Person ("Sam"); Activate New Person ("Sally"); Activate New Person ("Andy"); Hold (100); End;
SIMSCRIPT II.5: Example Preamble '' A simple telephone system model - CACI Products Company '' files: TELPHN1.SRC Normally mode is integer Processes include GENERATOR Every INCOMING.CALL has a CALL.ID Define NUMBER.BUSY and LOST.CALLS as integer variables End ''Preamble
SIMSCRIPT II.5: Example Main Activate a GENERATOR now Start simulation Print 1 line with LOST.CALLS thus 15 phone calls were made and ** were lost due to
busy lines End ''Main Process GENERATOR For I = 1 to 15 do Activate a INCOMING.CALL now Let CALL.ID(INCOMING.CALL) = I Wait uniform.f (2.0, 6.0, 1) minutes Loop End ''GENERATOR
n There are no specific requirements for the coding of models based on the event scheduling approach: usually the native functions and data structures provided by the language are sufficient
n We need to provide functions for the event management and the control of the simulation, but it is code easily reusable (e.g. it may be provided by public domain libraries)
n Each block can be already coded or it can just be and empty shell for user-provided code
n Each tool is usually very specialized: new libraries can increase its functions and capabilities, but not the application field (at least not in fields that are completely different)
n The basic elements of a discrete-event simulator are: n the main cycle for event analysis (Event Loop) n the Future-Event Set n the functions to be executed when each event
happens n To these key elements we can add:
n the procedures for data collection and measurement analysis
n the procedures for the initialization of the simulator n the termination criteria for the simulation
n Before going in the details of the various simulator parts, we need to define some fundamental information for our simulator and how to represent it n The simulation time n The events and their attributes
n Each event must be represented in our program using a composite data type, whose nature will depend on the implementation choice for the Future-Event Set
n Each event will contain at least: n the time at which the event is scheduled n an identifier for the type of event
n The event scheduling time is the key field, since it is needed to decide which is the next event to be executed, i.e. the one with the smallest scheduling time in the FES
n The event type is needed to identify which kind of event must happen, and therefore which specific function should be executed
n Often it is useful to associate attributes to each event
n This permits us to aggregate events according to their general type, to discriminate them later at execution time
n Example n In a queueing system with two different kind of
servers, instead of defining distinct events for the two servers, we define a single service event and we store the information on the involved server in an attribute
n The selected data structure must support in the most efficient way the common operations for the FES n Insertion of a new event n Extraction of the next event n Lookup and deletion of an event
n The management efficiency for the first two operations is fundamental, while lookup and deletion may be required only for advanced cases and their implementation might be not optimized
n Extraction n Since the list is ordered, we must just remove the
first event in the list n Complexity O(1)
n Lookup and Deletion n Lookup has the same complexity of an insertion,
O(N) n If we know in advance that events should be
cancelled, it might be useful to maintain a copy of their reference outside the FES, to be able to cancel them without having to look them up -> complexity O(1)
n The binary heap has insertion and extraction complexity O(log2N), due to the need to rebalance the data structure after some of those events
n Besides the insertion and extraction complexity, we need to consider the overhead due to the fact that we might need to reallocate the array when the heap size increases
n It is possible to define hybrid data structures for the FES, designed to reduce as much as possible the complexity while maintaining the computational efficiency
n A possible efficient solution it to maintain the events in a linear (bilinked) list, adding an “overlay” structure of reference to intermediate positions in the list
n The extraction complexity for such a solution is still O(1), while insertion complexity is now smaller than O(N), since insertion is now aided by the direct access to the intermediate positions of the FES
n We need anyway to consider the increased complexity due to the overlay management
n For this course the usage of linear lists is enough, even if they are not the most efficient solution
n In ‘real world’ situations we must seriously consider the trade-off between the FES management complexity and the gain in efficiency and execution time, so an advanced solution is usually the best choice
n Our scope is to evaluate the system performance before its actual implementation
n The performance indexes we are interested in will vary according to the situation but they are typically represented by numeric quantities measured inside the program
n The key activity of our simulation is to collect samples of those numeric quantities to later proceed to the evaluation of our indexes
n Parts of this course is dedicated to the evaluation of the confidence of the measures done and the methods to control the simulation to have guarantees of such confidence
n In this preliminary phase we want just to recall the various types of quantities that we may need to measure and how such measures can be performed
How to measure averages? n To measure averages requires, as we will see
later, a lot of attentions n In our simulator we have two possibilities:
n to collect all the samples of the quantities we are interested in, saving them outside the simulator (in a file) and leaving the statistical analysis of our quantities to an external dedicated program
n to perform inside the program the needed mathematical operations to produce as outputs only the average quantities
n Due to the length and complexity of our simulations, the latter possibility is the one usually adopted
n We need to measure different types of averages, depending on the involved quantities n Point (discrete-time) averages n Time (continuous-time) averages n Statistical frequencies
User input n To study the system, we want to reproduce its
behavior when a certain number of parameters vary
n Quite obviously, if all parameters were fixed inside the simulation program, we would need a different program for each possible combination of values
n It is therefore normal to plan for the input of parameters n interactively n on the command line n using configuration files n through a combination of the previous methods
n At the end of the Event Loop, before exiting the program, we must inform the users of the simulation results (Duh!)
n If the simulation ended regularly we will print (on the screen and/or on a file) all the measures collected and any information we consider important to report
n If the simulation ended anomalously, we will explicitly report this fact, to avoid the possible usage of meaningless data
n A run of the program corresponds to a single realization of the stochastic process representing the simulated system, i.e. to a single point in the space of the possible results
n This is not enough: to characterize and study the system we want to explore a large portion of the results’ space
n For each one we must define a variation range and the number of values in such a range
n Simulation runs for each combination of values for the parameters
n For each combination, several runs (with different seed and different duration) to reduce the effect of start conditions and the pseudo-random sequences
n Complexity increases exponentially with the number of parameters: don’t exaggerate!
n Representation of the output results n We need to select the best way to present our