{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "########################################################################\n", "########################################################################\n", "#\n", "# Queue.py\n", "# Simulate a queue where people arrive randomly with an average time of\n", "# X seconds between people and are dealt with in Y seconds on average\n", "# Use a loop with a 1 second step in the simulation\n", "#\n", "# Print the maximum wait that any one of 1000 simulated people had\n", "# Note this solution uses numpy to generate distrubibutions, you can\n", "# also \"math\" functions and \"random\" functions (import math,random)\n", "#\n", "########################################################################\n", "########################################################################\n", "\n", "X = 10 # starting value, can be adjusted\n", "Y = 7 # starting value, can be adjusted\n", "nPerson = 1000 # number of people to simulate, stop when all dealt with\n", "\n", "import numpy as np\n", "\n", "def timeToNext(beta):\n", " \"\"\"Return time to next person, input is beta = 1/(average arrival rate)\"\"\"\n", " # functional form of time to next person if ac is the average arrival time\n", " # is p(t) = 1/beta * exp(- t / beta )\n", " # where beta = beta\n", " # np.random.exponential uses scale = beta\n", " # https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.random.exponential.html\n", " ranExp = np.random.exponential(scale = beta, size=1)\n", " return int(ranExp) # note forcing integer times here to simplify\n", "\n", "def addAPerson(t,q):\n", " \"\"\" Function to add a person to the queue with average time between people = X\"\"\"\n", " # t = time added\n", " # q = your queue\n", " pass # write your function here\n", "\n", "def removeAPerson(t,q):\n", " \"\"\"Function to remove a person how has been dealt with, return time they waited\"\"\"\n", " # t = time removed\n", " # q = your queue\n", " return 0 #your function here, return time waited\n", "\n", "def arriveTimesArray(n,dt):\n", " \"\"\"Return a numpy array of integers, which are the times each person will arrive\"\"\" \n", " return 0 # return the array here\n", "\n", "def timerLoop(X,Y):\n", " \"\"\"Run a loop to add and remove people as required\"\"\"\n", " arriveTimes = arriveTimesArray(nPerson,X)\n", " # queue\n", " q = []\n", " # waied times\n", " wait = []\n", " ## loop over the times from 0 -> ... in steps of 1s \n", " ## until there are no people to arrive and noone in the queue\n", " ## store the time each person waited in the \"wait\" array as an integer\n", " #\n", " # np.max is the maximum of an array, np.mean is the average\n", " print(\"Max time waited was {}s average {}s\".format(np.max(wait),np.mean(wait)))\n", "\n", "timerLoop(X,Y)\n", "print(\"\\nOnce this works try rerunning with different arrival times and processing times\\ntimerLoop(10,7) # default values\")\n", "\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }