Skip to content

Commit 9cdcbc2

Browse files
Merge pull request #44 from drvinceknight/progress_bar
Adding tqdm
2 parents ec7203e + 4e0e4fc commit 9cdcbc2

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

ciw/simulation.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
from __future__ import division
22
import os
3-
from random import (random, expovariate, uniform, triangular,
4-
gammavariate, gauss, lognormvariate, weibullvariate)
3+
import tqdm
4+
from random import (expovariate, uniform, triangular, gammavariate,
5+
lognormvariate, weibullvariate)
56
from csv import writer, reader
6-
import copy
7-
from decimal import Decimal, getcontext
7+
from decimal import getcontext
88
from collections import namedtuple
99

10-
import yaml
1110
import numpy.random as nprandom
1211

1312
from .node import Node
1413
from .exactnode import ExactNode, ExactArrivalNode
1514
from .arrival_node import ArrivalNode
1615
from .exit_node import ExitNode
17-
from .server import Server
18-
from .individual import Individual
19-
from .data_record import DataRecord
2016
from .state_tracker import *
2117
from .deadlock_detector import *
2218

23-
2419
Record = namedtuple('Record', 'id_number customer_class node arrival_date waiting_time service_start_date service_time service_end_date time_blocked exit_date destination queue_size_at_arrival queue_size_at_departure')
2520

2621
class Simulation(object):
@@ -236,20 +231,35 @@ def simulate_until_deadlock(self):
236231
time_of_deadlock - self.times_dictionary[state]
237232
for state in self.times_dictionary.keys()}
238233

239-
def simulate_until_max_time(self, max_simulation_time):
234+
def simulate_until_max_time(self, max_simulation_time, progress_bar=False):
240235
"""
241236
Runs the simulation until max_simulation_time is reached.
242237
"""
243238
self.nodes[0].update_next_event_date()
244239
next_active_node = self.find_next_active_node()
245240
current_time = next_active_node.next_event_date
241+
242+
if progress_bar is not False:
243+
self.progress_bar = tqdm.tqdm(total=max_simulation_time)
244+
246245
while current_time < max_simulation_time:
247246
next_active_node.have_event()
248247
for node in self.transitive_nodes:
249248
node.update_next_event_date(current_time)
250249
next_active_node = self.find_next_active_node()
250+
251+
if progress_bar:
252+
remaining_time = max_simulation_time - self.progress_bar.n
253+
time_increment = next_active_node.next_event_date - current_time
254+
self.progress_bar.update(min(time_increment, remaining_time))
255+
251256
current_time = next_active_node.next_event_date
252257

258+
if progress_bar:
259+
remaining_time = max(max_simulation_time - self.progress_bar.n, 0)
260+
self.progress_bar.update(remaining_time)
261+
self.progress_bar.close()
262+
253263
def source(self, c, n, kind):
254264
"""
255265
Returns the location of class c node n's arrival or

ciw/tests/test_simulation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ def test_simulate_until_max_time_method(self):
104104
drl.append((dr.customer_class, dr.service_time))
105105
self.assertEqual(drl, [(1, 10.0), (0, 5.0), (0, 5.0)])
106106

107+
def test_simulate_until_max_time_with_pbar_method(self):
108+
Q = ciw.Simulation(ciw.create_network(
109+
'ciw/tests/testing_parameters/params.yml'))
110+
Q.simulate_until_max_time(150, progress_bar=True)
111+
self.assertEqual(Q.progress_bar.total, 150)
112+
self.assertEqual(Q.progress_bar.n, 150)
113+
114+
115+
116+
107117
def test_simulate_until_deadlock_method(self):
108118
ciw.seed(3)
109119
Q = ciw.Simulation(ciw.create_network(

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
PyYAML
22
networkx
33
hypothesis
4-
numpy
4+
numpy
5+
tqdm

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
packages=['ciw'],
1919
description='A discrete event simulation library for open queueing networks',
2020
long_description=readme + '\n\n' + changes + '\n\n' + authors,
21-
install_requires=["PyYAML", "networkx", "hypothesis", "numpy"],
21+
install_requires=["PyYAML", "networkx", "hypothesis", "numpy", "tqdm"]
2222
)

0 commit comments

Comments
 (0)