Skip to content

Instantly share code, notes, and snippets.

@geraintpalmer
Created February 14, 2017 11:54
Show Gist options
  • Save geraintpalmer/f385068520a4e0246762c8ad41e997ce to your computer and use it in GitHub Desktop.
Save geraintpalmer/f385068520a4e0246762c8ad41e997ce to your computer and use it in GitHub Desktop.
# Create a new Simulation class that inherits from the ciw.Simulation
# class. Has all the attributes and methods of the original class, but
# we can add a new method, "simulate_until_max_customers".
class Simulation(ciw.Simulation):
def simulate_until_max_customers(self, max_customers):
"""
Runs the simulation until max_customers have finished reached the Exit Node.
"""
self.nodes[0].update_next_event_date()
next_active_node = self.find_next_active_node()
current_time = next_active_node.next_event_date
while len(self.nodes[-1].all_individuals) < max_customers:
next_active_node.have_event()
for node in self.transitive_nodes:
node.update_next_event_date(current_time)
next_active_node = self.find_next_active_node()
current_time = next_active_node.next_event_date
# Define a parameters dictionary (M/M/1 queue)
params = {
'Arrival_distributions': [['Exponential', 5.0]],
'Service_distributions': [['Exponential', 10.0]],
'Transition_matrices': [[0.0]],
'Number_of_servers': [1]
}
# Run the simulation, using the new Simulation class rather
# that the ciw.Simulation class. We can now use the new
# "simulate_until_max_customers" method instead.
N = ciw.create_network(params)
Q = Simulation(N)
Q.simulate_until_max_customers(100)
# We should now see that the number of records should be
# equal to the max number of customers we specified.
recs = Q2.get_all_records()
# Note that we are stopping the simulation once max_customers have reached
# the exit node. This means that more customers may still be travelling around
# the network by the end of the simulation. Their records will also be counted,
# but only max_customers have reached the Exit Node.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment