diff --git a/.gitignore b/.gitignore index ba0430d2..979f738d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -__pycache__/ \ No newline at end of file +__pycache__/ +*.png \ No newline at end of file diff --git a/velo_rent.py b/velo_rent.py index d06d3be6..364a779c 100644 --- a/velo_rent.py +++ b/velo_rent.py @@ -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() \ No newline at end of file +plt.savefig("mailly.png")