{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**SimPy** is a process-based discrete-event simulation framework based on standard Python. Its event dispatcher is based on the time of events and the processes it contains. It provides the modeller with components of a simulation model, including processes, for active components like vehicles or customers, and resources, for passive components like servers or counters. SimPy also provides monitors to aid in understanding the model. \n", "\n", "**Features of SimPy:**\n", "- Processes: SimPy has a built-in concept of processes, which are the active components of a simulation. They can be used to model active entities like customers, vehicles, or messages.\n", "- Events: SimPy uses events to schedule processes. Events are triggered at a specific time and can be used to model interactions between processes.\n", "- Resources: SimPy provides resources, which are used to model passive components like servers, counters, or channels. Resources can be used by processes and can be limited in their capacity.\n", "- Monitors: SimPy provides monitors, which are used to observe and analyze the simulation. Monitors can be used to collect data on processes, resources, and events.\n", "- Randomness: SimPy has built-in support for randomness, which is essential for simulation models. It uses the Mersenne Twister random number generator.\n", "- Easy to Learn: SimPy is built on top of Python and uses a syntax that is easy to learn, even for those without prior experience with simulation or Python.\n", "- Fast: SimPy is fast and can handle large simulations with millions of events.\n", "- Extensive Libraries: SimPy has extensive libraries for various domains like manufacturing, healthcare, and logistics.\n", "- Visualization: SimPy provides tools for visualization, which can be used to visualize the simulation and insights.\n", "\n", "**To install:** \n", "> pip intall simpy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import simpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Example on how to declare your variables in a class" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Start parking at 0\n", "Start driving at 5\n", "Start parking at 7\n", "Start driving at 12\n", "Start parking at 14\n" ] } ], "source": [ "class P:\n", " ''' hold variables in a class '''\n", " parking_duration = 5 # how long is the car parked\n", " trip_duration = 2 # how long is the car driving for\n", " max_sim_time = 15 # how long to run the dimulation for\n", "\n", "class Car(object):\n", " ''' stop and drive the car '''\n", " def __init__(self, env, parking_duration, trip_duration):\n", " # reference to current simulation environment\n", " self.env = env\n", " \n", " # this is how you accept the variables\n", " self.parking_duration = parking_duration\n", " self.trip_duration = trip_duration\n", " \n", " # Start the run process everytime an instance is created.\n", " self.action = env.process(self.park_and_drive())\n", " \n", " def park_and_drive(self):\n", " while True:\n", " print('Start parking at %d' % self.env.now)\n", " yield self.env.timeout(self.parking_duration)\n", "\n", " print('Start driving at %d' % self.env.now)\n", " yield self.env.timeout(self.trip_duration) \n", " \n", "# create an environment \n", "env = simpy.Environment()\n", "\n", "# pass the variables to the Car class\n", "car = Car(env, P.parking_duration, P.trip_duration)\n", "\n", "# start simulation\n", "env.run(until=P.max_sim_time) " ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true } }, "source": [ "

This tutorial was created by HEDARO

" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 4 }