SimPy: Simulation in Python
SimPy: Simulation in Python
A pratical guide
About the Book
The book SimPy: Simulation in Python presents the SimPy library for building discrete-event simulation models in Python.
Throughout its chapters, it introduces the library’s fundamental concepts and its main commands through examples. At the end of each chapter, practical exercises are provided—both solved challenges and additional exercises—so that readers can test their knowledge.
In addition to the library’s fundamentals, the book also covers some advanced topics, such as agent-based simulation, optimization and simulation.
Table of Contents
- See me, Feel me, README
- Preface
- Why use SimPy?
- Pros and cons
- A brief history of SimPy
- Where to find help about SimPy
- How to use this book
- Installing SimPy
- Step 1: Anaconda, the easy way
- Step 2: Installing Pip (for those who did not install Anaconda)
- Step 3: Installing SimPy
- Step 4: Installing an Integrated Development Environment (IDE)
- It All Depends on Python
- Test Your Python Knowledge: the gambler’s ruin problem
- Challenges
- Solution to Challenge 1
- Test Your Knowledge
- First Steps in SimPy: creating entities
- Importing the random and simpy libraries
- Creating a simulation environment
- Creating an arrival generator within the environment
- Creating time intervals with env.timeout(wait_time)
- Running the model for a determined time with env.run(until=simulation_time)
- Concepts from this section
- Challenges
- Solution to Challenges 2 and 3
- Test Your Knowledge
- Creating, occupying and releasing resources
- Creating
- Occupying
- Releasing
- Resource status
- Concepts from this section
- Putting it all together in an example: the M/M/1 queue
- Entity arrival generation
- Performing service at the server
- An alternative representation for occupying and releasing resources
- Concepts from this section
- Challenges
- Solution to Challenges 4, 5 and 6
- Test Your Knowledge
- Attributes and variables: differences in SimPy
- Attributes in object-oriented models
- Concepts from this section
- Challenges
- Solution to Challenges 7 and 8
- Test Your Knowledge
- Environments: controlling the simulation
- Execution control with env.run(until=end_of_simulation)
- Stop by execution of all scheduled processes
- Stop by end of specific process execution with env.run(until=process)
- Step-by-step simulation: peek & step
- Concepts from this section
- Challenges
- Solution to Challenges 9 and 10
- Other types of resources: with priority and preemptive
- Resources with priority: PriorityResource
- Resources that can be interrupted: PreemptiveResource
- Concepts from this section
- Challenges
- Solution to Challenges 11 and 12
- Process interruptions: simpy.Interrupt
- Creating equipment breakdowns
- Interrupting a process without capture by try…except
- Concepts from this section
- Challenges
- Solution to Challenges 13 and 14
- Test Your Knowledge
- Storage and selection of specific objects with Store, FilterStore and PriorityStore
- Building a set of objects with Store
- Selecting a specific object with FilterStore()
- Creating a Store with priority: PriorityStore
- Concepts from this section
- Challenges
- Solution to Challenges 15 and 16
- Test Your Knowledge
- Filling or emptying boxes, tanks, stocks or objects with Container()
- Filling my container yield myContainer.put(quantity)
- Emptying my container: yield myContainer.get(quantity)
- Creating a sensor for the current container level
- Concepts from this section
- Challenges
- Solution to Challenges 17 and 18
- Test Your Knowledge
- Creating batches (or grouping) entities during simulation
- A tactic for batch grouping using Container
- Grouping batches by entity attribute using FilterStore
- Challenges
- Solution to Challenges 19 and 20
- Test Your Knowledge
- Creating, manipulating and triggering events with event()
- Creating an isolated event with event()
- Concepts from this section
- Challenges
- Solution to Challenges 21 and 22
- Test Your Knowledge
- Waiting for multiple events at the same time with AnyOf and AllOf
- Waiting until at least one event finishes with AnyOf
- Waiting for all events with AllOf
- Better understanding the outputs of AllOf and AnyOf commands
- Concepts from this section
- Challenges
- Solution to Challenges 23 and 24
- Test Your Knowledge
- Useful properties of events
- Concepts from this section
- Challenges
- Solution to Challenge 25
- Test Your Knowledge
- Adding callbacks to events
- Every process is an event
- Concepts from this section
- Challenges
- Solution to Challenge 26
- Test Your Knowledge
- Event interruptions
- Interrupting an event with the interrupt method
- Interrupting an event with the fail method
- What are generator functions? (or how SimPy works) - Part I
- Iterator
- Generator functions
- What are generator functions? (or how SimPy works?) - Part II
- SimPy vs. generator functions
- Agent-Based Simulation using SimPy
- How to build an agent simulation model: basic steps
- Agent-based epidemic model: the SIR model
- Modeling the problem in SimPy
- Improving simulation code performance
- How to proceed from here
- Concepts from this section
- Challenges
- Solution to Challenges 27 and 28
- Test Your Knowledge
- Data input and output via spreadsheet
- Communication with the library
Other books by this author
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them