Discrete-event simulation is one of the most useful techniques to evaluate quickly and effectively the performance of systems. It enables benchmarking proposed strategies against existing ones in a time- and computing-efficient manner. However, there are several aspects that should be considered when designing and implementing your simulation environment. In this tutorial, a number of best practices when designing and implementing event-driven simulations will be discussed. A use case of routing in optical networks will be used as an example. The implementation of the main simulator components using Java and Python will be described.
2. 2020-03-01 Chalmers University of Technology 2
OUTLINE
• Introduction
• Performance analysis
• Discrete-event simulation
• Routing and resource assignment in optical networks
• Implementation details
• Best practices
• Examples in Java and Python
• Conclusions
• Q&A
3. • Performance analysis
2020-03-01 Chalmers University of Technology 3
DISCRETE-EVENT SIMULATION
Analytic Simulation Prototyping
Realistic
Expensive
Prone to contamination
Set of equations
e.g.: queue theory
Set of events described by random numbers
e.g.: event-driven simulation
Real systems and devices
e.g.: testbed
4. 2020-03-01 Chalmers University of Technology 4
DISCRETE-EVENT SIMULATION
• Environment
• Models the set of elements that the simulation is interested in
analyzing
• Discrete sequence of events in time
• Each event changes (or may change) the state of the
environment
• No changes between two consecutive events
• Next-time event progression: time between events may vary
(e.g., arrival/departure of requests)
• Fixed-increment time progression: time between events is
fixed (e.g., periodic reconfiguration)
5. 2020-03-01 Chalmers University of Technology 5
DISCRETE-EVENT SIMULATION
• Main components:
• State: the environment state at a given point in time
• Clock: the current simulation clock, which is not tied to the wall
clock
• Event list: the list with the next events to be processed/executed
• Events: set of (problem-specific) procedures to be executed
over the environment
Event list
Simulation
clock
6. 2020-03-01 Chalmers University of Technology 6
EVENT-DRIVEN SIMULATION
Examples of environments and their events
1. Bank queue
• Productor-consumer kind of problem
• Clients arrive, cashiers serve the clients
• Queue theory can be used for analytic assessment
2. Public transportation
• Passengers boarding/dropping at stops, vehicle speed change, etc.
3. Virus outbreak
• Contact probability between people, contamination probability, etc.
4. Networks
• Packet-based: packet arrival, enqueue, dequeue, departure
• Circuit-based: call arrival, call departure, etc.
7. 2020-03-01 Chalmers University of Technology 7
ROUTING AND SPEC. ALLOC. IN OPTICAL NETWORKS
• Circuit-switched network
• Discrete resource allocation
• Wavelength division multiplexing
(WDM)
• Single-wavelength channels
• With wavelength conversion (opaque)
• No wavelength continuity constraint
• Without wavelength conversion (transparent)
• Elastic optical network (EON)
• Multiple-slice channels
• Spectrum continuity and contiguity constraint
Link A Link B Link C Link D Link E
S1
S2
S3
S4
Link A Link B Link C Link D Link E
S1
S2
S3
S4
Link A Link B Link C Link D Link E
S1
S2
S3
S4
8. 2020-03-01 Chalmers University of Technology 8
IMPLEMENTATION DETAILS
State
• Can be modeled as a class
• Attributes of the class model the state of the environment
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/core.py#L12
Python
Java
https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/components/Topology.java#L21
9. 2020-03-01 Chalmers University of Technology 9
IMPLEMENTATION DETAILS
Event list
• Should be implemented as an ordered list
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/core.py#L219
Python
Java
https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/Simulator.java#L27
10. 2020-03-01 Chalmers University of Technology 10
IMPLEMENTATION DETAILS
Events
• Create a standard event model
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/events.py#L6
Python
Java
https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/events/Event.java#L11
https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/events/ActiveProcess.java#L12
11. 2020-03-01 Chalmers University of Technology 11
IMPLEMENTATION DETAILS
Events
• Create a standard event model and use it
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/core.py#L318
Python
Java
https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/Simulator.java#L86
12. 2020-03-01 Chalmers University of Technology 12
BEST PRACTICES
Never overwrite files
• Have a base folder for all results of a given batch
• Create a timestamp’d folder for the results of every run
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/run.py#L37 https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/MultiThreadSimulator.java#L94
Python
Java
13. 2020-03-01 Chalmers University of Technology 13
BEST PRACTICES
Have file- or argument-based configuration
• Prevents you from recompiling the code
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/run.py#L137 https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/FileAgent.java#L89
Python
Java
14. 2020-03-01 Chalmers University of Technology 14
BEST PRACTICES
Bonus: make a copy of the configurations for every result
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/run.py#L45 https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/MultiThreadSimulator.java#L104
Python
Java
15. 2020-03-01 Chalmers University of Technology 15
BEST PRACTICES
Bonus 2: make a copy of the current simulator version
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/run.py#L59 https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/MultiThreadSimulator.java#L120
Python
Java
16. 2020-03-01 Chalmers University of Technology 16
BEST PRACTICES
x. Only enqueue events when needed
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/events.py#L17 https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/events/ConnectionManager.java#L104
Python
Java
17. 2020-03-01 Chalmers University of Technology 17
BEST PRACTICES
Run different configurations in threads
• Single environment can/should not be parallelized
• Multiple environments can/should be parallelized
• Use of thread pool is recommended (improves resource efficiency)
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/run.py#L93 https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/MultiThreadSimulator.java#L131
https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/MultiThreadSimulator.java#L152
Python
Java
18. 2020-03-01 Chalmers University of Technology 18
BEST PRACTICES
Save averages and individual results
• Bonus: save everything possible
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/core.py#L325
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/run.py#L113
https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/Simulator.java#L100
https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/065e0726ffe7e7146616d5347b90c7da21bbf851/src/main/java/simulator/cs/anycast/core/Simulator.java#L122
Python
Java
19. 2020-03-01 Chalmers University of Technology 19
BEST PRACTICES
Plot results as you go
• Helps identifying problems before everything runs
https://github.com/carlosnatalino/python-simple-anycast-wdm-simulator/blob/cdb1340234cad0217fc7687abcadb1a3f35c8522/run.py#L103 https://github.com/carlosnatalino/java-anycast-cs-simulator/blob/bbede8fabadcc8c8c96542af469c7334e3ba1a67/src/main/java/simulator/cs/anycast/core/MultiThreadSimulator.java#L174
Python
Java
20. 2020-03-01 Chalmers University of Technology 20
CONCLUSIONS AND LINKS
• Simulations are a good compromise between cost, time and
adherence to reality
• Java and Python are both great options to implement your simulator
• Good trade-off between development complexity and runtime efficiency
• C3SE offers computing power to run your simulations
• Versions used in this presentation:
• Python version: https://github.com/carlosnatalino/python-simple-anycast-
wdm-simulator
• Java version: https://github.com/carlosnatalino/java-anycast-cs-simulator/
• Presentation available at:
https://www.slideshare.net/CarlosNatalinoSilva/discreteevent-simulation-
best-practices-and-implementation-details-in-python-and-java
Editor's Notes
Comment that one event list does not allow for multithreading