Created
July 14, 2017 12:14
-
-
Save drvinceknight/36b769b2866064776ba1d9d1f72ee34d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import ciw\n", | |
"import multiprocessing\n", | |
"import numpy as np\n", | |
"\n", | |
"N = ciw.create_network(Arrival_distributions=[['Exponential', 0.2]],\n", | |
" Service_distributions=[['Exponential', 0.1]],\n", | |
" Number_of_servers=[3])\n", | |
" \n", | |
"def run_simulation(network, seed=0, max_time=10000):\n", | |
" ciw.seed(seed)\n", | |
" Q = ciw.Simulation(N)\n", | |
" Q.simulate_until_max_time(max_simulation_time=max_time)\n", | |
" recs = Q.get_all_records()\n", | |
" waits = [r.waiting_time for r in recs if r.arrival_date > 100 and r.arrival_date < 1540]\n", | |
" mean_wait = sum(waits) / len(waits)\n", | |
" return mean_wait" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"max_time = 500\n", | |
"repetitions = 200" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 loop, best of 3: 1.3 s per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit\n", | |
"# In series\n", | |
"\n", | |
"average_waits = []\n", | |
"for trial in range(repetitions):\n", | |
" average_waits.append(run_simulation(network=N, seed=trial, max_time=max_time))\n", | |
"np.mean(average_waits)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 loop, best of 3: 695 ms per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit\n", | |
"# In parallel\n", | |
"\n", | |
"pool = multiprocessing.Pool(multiprocessing.cpu_count())\n", | |
"args = [(N, trial, max_time) for trial in range(repetitions)]\n", | |
"average_waits = pool.starmap(run_simulation, args)\n", | |
"np.mean(average_waits)" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [default]", | |
"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.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment