Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__pycache__/
__pycache__/
*.png
102 changes: 74 additions & 28 deletions velo_rent.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,98 @@
# download modsim.py if necessary
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from os.path import basename, exists
from multiprocessing import Pool

def download(url):
filename = basename(url)
if not exists(filename):
from urllib.request import urlretrieve
local, _ = urlretrieve(url, filename)
print('Downloaded ' + local)

download('https://github.com/AllenDowney/ModSimPy/raw/master/modsim.py')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# import functions from modsim

from modsim import *
import matplotlib.pyplot as plt
def State(**variables):
"""Contains the values of state variables."""
return pd.Series(variables, name="state")


def flip(p=0.5):
"""Flips a coin with the given probability.

p: float 0-1

returns: boolean (True or False)
"""
return np.random.random() < p


def TimeSeries(*args, **kwargs):
"""Make a pd.Series object to represent a time series."""
if args or kwargs:
# underride(kwargs, dtype=float)
series = pd.Series(*args, **kwargs)
else:
series = pd.Series([], dtype=float)

series.index.name = "Time"
if "name" not in kwargs:
series.name = "Quantity"
return series


bikeshare = State(mailly=10, moulin=2)

def velo_a_mailly():

def velo_a_moulin():
# print('Moving a bike to moulin')
bikeshare.mailly -= 1
bikeshare.moulin += 1

def velo_a_moulin():

def velo_a_mailly():
# print('Moving a bike to mailly')
bikeshare.moulin -= 1
bikeshare.mailly += 1


def step(p1, p2):
if flip(p1):
velo_a_mailly()

if flip(p2):
velo_a_moulin()

results = TimeSeries()

results[0] = bikeshare.mailly
def run_simulation(num_steps, p1, p2):
results = TimeSeries()
results[0] = bikeshare.mailly
for i in range(num_steps):
step(p1, p2)
results[i + 1] = bikeshare.mailly
return results


def run_simulations_in_parallel(params_list):
with Pool(3) as p:
result = p.starmap_async(run_simulation, params_list)
return result.get()


params_list = [(10000, 0.5, 0.47), (10000, 0.5, 0.33), (10000, 0.47, 0.6)]
res1, res2, res3 = run_simulations_in_parallel(params_list)


fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
ax1.plot(res1)
ax1.set_title("Velos à Mailly")
ax1.set_xlabel("Temps")
ax1.set_ylabel("nombre")

ax2.plot(res2)
ax2.set_title("Velos à Mailly")
ax2.set_xlabel("Temps")
ax2.set_ylabel("nombre")

for i in range(60*14*30):
# print(f"step {i}")
step(0.5, 0.4)
results[i+1] = bikeshare.mailly
ax3.plot(res3)
ax3.set_title("Velos à Mailly")
ax3.set_xlabel("Temps")
ax3.set_ylabel("nombre")

fig, ax = plt.subplots()
ax.plot(results)
ax.set_title("Velos à Mailly")
ax.set_xlabel("Temps")
ax.set_ylabel("nombre")
plt.show()
plt.savefig("mailly.png")