def test_custom_pdf_method(self): seed(45) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) self.assertEqual(Q.custom_pdf([0.1, 0.4, 1.0], [9.5, 10.7, 14.6]), 10.7) self.assertEqual(Q.custom_pdf([0.1, 0.4, 1.0], [9.5, 10.7, 14.6]), 14.6) self.assertEqual(Q.custom_pdf([0.1, 0.4, 1.0], [9.5, 10.7, 14.6]), 14.6) self.assertEqual(Q.custom_pdf([0.1, 0.4, 1.0], [9.5, 10.7, 14.6]), 10.7) self.assertEqual(Q.custom_pdf([0.1, 0.4, 1.0], [9.5, 10.7, 14.6]), 14.6) self.assertEqual(Q.custom_pdf([0.1, 0.4, 1.0], [9.5, 10.7, 14.6]), 14.6) self.assertEqual(Q.custom_pdf([0.1, 0.4, 1.0], [9.5, 10.7, 14.6]), 9.5) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_custom_dist/')) self.assertEqual(Q.service_times[1][0](), 5.0) self.assertEqual(Q.service_times[1][0](), 6.0) self.assertEqual(Q.service_times[1][0](), 6.0) self.assertEqual(Q.service_times[1][1](), 5.0) self.assertEqual(Q.service_times[1][1](), 5.0) self.assertEqual(Q.service_times[1][1](), 6.0) self.assertEqual(Q.service_times[2][1](), 1.3) self.assertEqual(Q.service_times[2][1](), 1.3) self.assertEqual(Q.service_times[2][1](), 2.1) self.assertEqual(Q.service_times[2][1](), 1.9) self.assertEqual(Q.service_times[2][1](), 1.5) self.assertEqual(Q.service_times[2][1](), 2.1) self.assertEqual(Q.service_times[2][1](), 1.9)
def test_detect_deadlock_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) nodes = ['A', 'B', 'C', 'D', 'E'] connections = [('A', 'D'), ('A', 'B'), ('B', 'E'), ('C', 'B'), ('E', 'C')] for nd in nodes: Q.digraph.add_node(nd) for cnctn in connections: Q.digraph.add_edge(cnctn[0], cnctn[1]) self.assertEqual(Q.detect_deadlock(), True) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) nodes = ['A', 'B', 'C', 'D'] connections = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')] for nd in nodes: Q.digraph.add_node(nd) for cnctn in connections: Q.digraph.add_edge(cnctn[0], cnctn[1]) self.assertEqual(Q.detect_deadlock(), False) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) nodes = ['A', 'B'] for nd in nodes: Q.digraph.add_node(nd) self.assertEqual(Q.detect_deadlock(), False) connections = [('A', 'A')] for cnctn in connections: Q.digraph.add_edge(cnctn[0], cnctn[1]) self.assertEqual(Q.detect_deadlock(), True)
def test_simulate_until_max_time_method(self): seed(3) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) Q.simulate_until_max_time() L = Q.get_all_individuals() self.assertEqual(round(L[300].data_records.values()[0][0].service_start_date, 8), 6.04730086) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_dynamic_classes/')) Q.simulate_until_max_time() L = Q.get_all_individuals() drl = [] for dr in L[0].data_records[1]: drl.append((dr.customer_class, dr.service_time)) self.assertEqual(drl, [(1, 10.0), (1, 10.0), (0, 5.0), (1, 10.0)])
def test_find_next_active_node_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) i = 0 for node in Q.nodes[:-1]: node.next_event_date = i i += 1 self.assertEqual(str(Q.find_next_active_node()), 'Arrival Node') Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) i = 10 for node in Q.nodes[:-1]: node.next_event_date = i i -= 1 self.assertEqual(str(Q.find_next_active_node()), 'Node 4')
def test_init_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) self.assertEqual(Q.lmbda, [[3.0, 7.0, 4.0, 1.0], [2.0, 3.0, 6.0, 4.0], [2.0, 1.0, 2.0, 0.5]]) self.assertEqual(Q.mu, [[['Exponential', 7.0], ['Exponential', 7.0], ['Gamma', 0.4, 0.6], ['Deterministic', 0.5]], [['Exponential', 7.0], ['Triangular', 0.1, 0.8, 0.85], ['Exponential', 8.0], ['Exponential', 5.0]], [['Deterministic', 0.3], ['Deterministic', 0.2], ['Exponential', 8.0], ['Exponential', 9.0]]]) self.assertEqual(Q.c, [9, 10, 8, 8]) self.assertEqual(Q.transition_matrix, [[[0.1, 0.2, 0.1, 0.4], [0.2, 0.2, 0.0, 0.1], [0.0, 0.8, 0.1, 0.1], [0.4, 0.1, 0.1, 0.0]], [[0.6, 0.0, 0.0, 0.2], [0.1, 0.1, 0.2, 0.2], [0.9, 0.0, 0.0, 0.0], [0.2, 0.1, 0.1, 0.1]], [[0.0, 0.0, 0.4, 0.3], [0.1, 0.1, 0.1, 0.1], [0.1, 0.3, 0.2, 0.2], [0.0, 0.0, 0.0, 0.3]]]) self.assertEqual([str(obs) for obs in Q.nodes], ['Arrival Node', 'Node 1', 'Node 2', 'Node 3', 'Node 4', 'Exit Node']) self.assertEqual(Q.max_simulation_time, 2500) self.assertEqual(Q.class_change_matrix, 'NA') self.assertEqual(Q.schedules, [False, False, False, False]) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_dynamic_classes/')) self.assertEqual(Q.class_change_matrix, [[[0.7, 0.3], [0.2, 0.8]], [[1.0, 0.0], [0.0, 1.0]]]) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_server_schedule/')) self.assertEqual(Q.schedules, [True, False])
def test_init_method(self): seed(5) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = asq.ArrivalNode(Q) self.assertEqual(round(N.next_event_date, 5), 0.00440) self.assertEqual(N.number_of_individuals, 0) dates_dict = {1: {0: 0.2110410999, 1: 0.1415614623, 2: 0.3923690877}, 2: {0: 0.1218825551, 1: 0.0044003133, 2: 0.2442775601}, 3: {0: 0.0819463473, 1: 0.4135097542, 2: 0.7256307839}, 4: {0: 0.1738823223, 1: 0.3988184145, 2: 0.2987813628}} self.assertEqual({nd:{obs:round(N.next_event_dates_dict[nd][obs], 10) for obs in N.next_event_dates_dict[nd]} for nd in N.next_event_dates_dict}, dates_dict)
def test_initialise_next_event_dates_dict_method(self): seed(6) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = asq.ArrivalNode(Q) dates_dict_1 = {1: {0: 0.4362282541, 1: 0.2672232406, 2: 0.3864256273}, 2: {0: 0.1636952311, 1: 0.0714709565, 2: 0.8065738414}, 3: {0: 0.4088480190, 1: 0.0514323248, 2: 0.8132038176}, 4: {0: 1.1573751438, 1: 0.4649276714, 2: 0.8176876727}} dates_dict_2 = {1: {0: 0.0325870775, 1: 0.8054262558, 2: 0.8168179515}, 2: {0: 0.0841671381, 1: 0.0328245299, 2: 0.2196023847}, 3: {0: 0.2519089068, 1: 0.0573597814, 2: 1.5117882121}, 4: {0: 0.8881158889, 1: 0.0560592622, 2: 2.1307650868}} self.assertEqual({nd:{obs:round(N.next_event_dates_dict[nd][obs], 10) for obs in N.next_event_dates_dict[nd]} for nd in N.next_event_dates_dict}, dates_dict_1) N.initialise_next_event_dates_dict() self.assertEqual({nd:{obs:round(N.next_event_dates_dict[nd][obs], 10) for obs in N.next_event_dates_dict[nd]} for nd in N.next_event_dates_dict}, dates_dict_2)
def test_init_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) self.assertEqual(Q.lmbda, [[3.0, 7.0, 4.0, 1.0], [2.0, 3.0, 6.0, 4.0], [2.0, 1.0, 2.0, 0.5]]) self.assertEqual(Q.mu, [[['Exponential', 7.0], ['Exponential', 7.0], ['Gamma', 0.4, 0.6], ['Deterministic', 0.5]], [['Exponential', 7.0], ['Triangular', 0.1, 0.8, 0.85], ['Exponential', 8.0], ['Exponential', 5.0]], [['Deterministic', 0.3], ['Deterministic', 0.2], ['Exponential', 8.0], ['Exponential', 9.0]]]) self.assertEqual(Q.c, [9, 10, 8, 8]) self.assertEqual(Q.transition_matrix, [[[0.1, 0.2, 0.1, 0.4], [0.2, 0.2, 0.0, 0.1], [0.0, 0.8, 0.1, 0.1], [0.4, 0.1, 0.1, 0.0]], [[0.6, 0.0, 0.0, 0.2], [0.1, 0.1, 0.2, 0.2], [0.9, 0.0, 0.0, 0.0], [0.2, 0.1, 0.1, 0.1]], [[0.0, 0.0, 0.4, 0.3], [0.1, 0.1, 0.1, 0.1], [0.1, 0.3, 0.2, 0.2], [0.0, 0.0, 0.0, 0.3]]]) self.assertEqual([str(obs) for obs in Q.nodes], ['Arrival Node', 'Node 1', 'Node 2', 'Node 3', 'Node 4', 'Exit Node']) self.assertEqual(Q.max_simulation_time, 2500) self.assertEqual(Q.class_change_matrix, 'NA') self.assertEqual(Q.schedules, [False, False, False, False]) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_dynamic_classes/')) self.assertEqual(Q.class_change_matrix, [[[0.7, 0.3], [0.2, 0.8]], [[1.0, 0.0], [0.0, 1.0]]]) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_server_schedule/')) self.assertEqual(Q.schedules, [True, False]) Q = asq.Simulation(Arrival_rates = {'Class 2': [2.0, 1.0, 2.0, 0.5], 'Class 1': [2.0, 3.0, 6.0, 4.0], 'Class 0': [3.0, 7.0, 4.0, 1.0]}, Number_of_nodes = 4, detect_deadlock = False, Simulation_time = 2500, Number_of_servers = [9, 10, 8, 8], Queue_capacities = [20, 'Inf', 30, 'Inf'], Number_of_classes = 3, Service_rates = {'Class 2': [['Deterministic', 0.3], ['Deterministic', 0.2], ['Exponential', 8.0], ['Exponential', 9.0]], 'Class 1': [['Exponential', 7.0], ['Triangular', 0.1, 0.8, 0.85], ['Exponential', 8.0], ['Exponential', 5.0]], 'Class 0': [['Exponential', 7.0], ['Exponential', 7.0], ['Gamma', 0.4, 0.6], ['Deterministic', 0.5]]}, Transition_matrices = {'Class 2': [[0.0, 0.0, 0.4, 0.3], [0.1, 0.1, 0.1, 0.1], [0.1, 0.3, 0.2, 0.2], [0.0, 0.0, 0.0, 0.3]], 'Class 1': [[0.6, 0.0, 0.0, 0.2], [0.1, 0.1, 0.2, 0.2], [0.9, 0.0, 0.0, 0.0], [0.2, 0.1, 0.1, 0.1]], 'Class 0': [[0.1, 0.2, 0.1, 0.4], [0.2, 0.2, 0.0, 0.1], [0.0, 0.8, 0.1, 0.1], [0.4, 0.1, 0.1, 0.0]]}) self.assertEqual(Q.parameters, {'Arrival_rates': {'Class 2': [2.0, 1.0, 2.0, 0.5], 'Class 1': [2.0, 3.0, 6.0, 4.0], 'Class 0': [3.0, 7.0, 4.0, 1.0]}, 'Number_of_nodes': 4, 'Simulation_time': 2500, 'detect_deadlock': False, 'Number_of_servers': [9, 10, 8, 8], 'Queue_capacities': [20, 'Inf', 30, 'Inf'], 'Number_of_classes': 3, 'Service_rates': {'Class 2': [['Deterministic', 0.3], ['Deterministic', 0.2], ['Exponential', 8.0], ['Exponential', 9.0]], 'Class 1': [['Exponential', 7.0], ['Triangular', 0.1, 0.8, 0.85], ['Exponential', 8.0], ['Exponential', 5.0]], 'Class 0': [['Exponential', 7.0], ['Exponential', 7.0], ['Gamma', 0.4, 0.6], ['Deterministic', 0.5]]}, 'Transition_matrices': {'Class 2': [[0.0, 0.0, 0.4, 0.3], [0.1, 0.1, 0.1, 0.1], [0.1, 0.3, 0.2, 0.2], [0.0, 0.0, 0.0, 0.3]], 'Class 1': [[0.6, 0.0, 0.0, 0.2], [0.1, 0.1, 0.2, 0.2], [0.9, 0.0, 0.0, 0.0], [0.2, 0.1, 0.1, 0.1]], 'Class 0': [[0.1, 0.2, 0.1, 0.4], [0.2, 0.2, 0.0, 0.1], [0.0, 0.8, 0.1, 0.1], [0.4, 0.1, 0.1, 0.0]]}})
def test_init_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = Q.transitive_nodes[1] s = asq.Server(N, 3) self.assertEqual(s.id_number, 3) self.assertEqual(s.node, N) self.assertEqual(s.node.id_number, 2) self.assertEqual(s.cust, False) self.assertEqual(s.busy, False) self.assertEqual(s.offduty, False)
def test_find_next_event_date_method(self): seed(1) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = asq.ArrivalNode(Q) self.assertEqual(round(N.next_event_date, 5), 0.00105) N.find_next_event_date() self.assertEqual(round(N.next_event_date, 5), 0.00105) self.assertEqual(N.next_node, 1) self.assertEqual(N.next_class, 1) N.have_event() self.assertEqual(round(N.next_event_date, 5), 0.00518) self.assertEqual(N.next_node, 3) self.assertEqual(N.next_class, 1)
def test_init_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = asq.Node(1, Q) self.assertEqual(N.mu, [['Exponential', 7.0], ['Exponential', 7.0], ['Deterministic', 0.3]]) self.assertEqual(N.c, 9) self.assertEqual(N.transition_row, [[0.1, 0.2, 0.1, 0.4], [0.6, 0.0, 0.0, 0.2], [0.0, 0.0, 0.4, 0.3]]) self.assertEqual(N.next_event_date, 'Inf') self.assertEqual(N.individuals, []) self.assertEqual(N.id_number, 1) self.assertEqual([[round(obs, 1) for obs in row] for row in N.cum_transition_row], [[0.1, 0.3, 0.4, 0.8], [0.6, 0.6, 0.6, 0.8], [0.0, 0.0, 0.4, 0.7]]) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_dynamic_classes/')) N1 = Q.transitive_nodes[0] self.assertEqual(N1.class_change_for_node, [[0.7, 0.3], [0.2, 0.8]]) N2 = Q.transitive_nodes[1] self.assertEqual(N2.class_change_for_node, [[1.0, 0.0], [0.0, 1.0]]) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_server_schedule/')) N = Q.transitive_nodes[0] self.assertEqual(N.scheduled_servers, True) self.assertEqual(N.cyclelength, 100) self.assertEqual(N.c, 1) self.assertEqual(N.masterschedule, [30, 60, 90, 100, 130, 160, 190, 200, 230, 260, 290]) self.assertEqual(N.next_event_date, 30)
def test_have_event_method(self): seed(1) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = asq.ArrivalNode(Q) self.assertEqual([str(obj) for obj in Q.transitive_nodes[0].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[1].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[2].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[3].individuals], []) self.assertEqual(round(N.next_event_date, 5), 0.00105) self.assertEqual(N.next_node, 1) N.have_event() self.assertEqual([str(obj) for obj in Q.transitive_nodes[0].individuals], ['Individual 1']) self.assertEqual([str(obj) for obj in Q.transitive_nodes[1].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[2].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[3].individuals], []) self.assertEqual(round(N.next_event_date, 5), 0.00518) self.assertEqual(N.next_node, 3) seed(12) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = asq.ArrivalNode(Q) self.assertEqual([str(obj) for obj in Q.transitive_nodes[0].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[1].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[2].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[3].individuals], []) self.assertEqual(round(N.next_event_date, 5), 0.01938) self.assertEqual(N.next_node, 3) N.have_event() self.assertEqual([str(obj) for obj in Q.transitive_nodes[0].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[1].individuals], []) self.assertEqual([str(obj) for obj in Q.transitive_nodes[2].individuals], ['Individual 1']) self.assertEqual([str(obj) for obj in Q.transitive_nodes[3].individuals], []) self.assertEqual(round(N.next_event_date, 5), 0.02021) self.assertEqual(N.next_node, 2)
def test_change_shift_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_server_schedule/')) N = Q.transitive_nodes[0] N.next_event_date = 30 self.assertEqual([str(obs) for obs in N.servers], ['Server 1 at Node 1']) self.assertEqual([obs.busy for obs in N.servers], [False]) self.assertEqual([obs.offduty for obs in N.servers], [False]) self.assertEqual(N.c, 1) N.change_shift() self.assertEqual([str(obs) for obs in N.servers], ['Server 2 at Node 1', 'Server 3 at Node 1']) self.assertEqual([obs.busy for obs in N.servers], [False, False]) self.assertEqual([obs.offduty for obs in N.servers], [False, False]) self.assertEqual(N.c, 2) N.servers[0].busy = True N.next_event_date = 90 N.change_shift() self.assertEqual([str(obs) for obs in N.servers], ['Server 2 at Node 1', 'Server 4 at Node 1', 'Server 5 at Node 1', 'Server 6 at Node 1']) self.assertEqual([obs.busy for obs in N.servers], [True, False, False, False]) self.assertEqual([obs.offduty for obs in N.servers], [True, False, False, False]) self.assertEqual(N.c, 3)
def test_find_cdf_class_changes_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_dynamic_classes/')) N1 = Q.transitive_nodes[0] self.assertEqual(N1.find_cdf_class_changes(), [[0.7, 1.0], [0.2, 1.0]])
def test_repr_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N1 = asq.Node(1, Q) N2 = asq.Node(2, Q) self.assertEqual(str(N1), 'Node 1') self.assertEqual(str(N2), 'Node 2')
def test_simulate_until_deadlock_method(self): seed(3) Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_deadlock_sim/')) times = Q.simulate_until_deadlock() self.assertEqual(round(times[((0, 0), (0, 0))], 8), 9.09939457)
def average_waiting_time_per_month(data_by_month): """ Finds the expected wait if a customer arrives in a given month """ return [sum([obs[4] for obs in row if obs[1]==float(4)])/len([obs[4] for obs in row if obs[1]==float(4)]) for row in data_by_month], [sum([obs[4] for obs in row if obs[1]!=float(4)])/len([obs[4] for obs in row if obs[1]!=float(4)]) for row in data_by_month] multiple_runs_demand_cls_4 = [] multiple_runs_demand_cls_other = [] multiple_runs_wait_cls_4 = [] multiple_runs_wait_cls_other = [] for i in range(num_runs): P = asq.load_parameters(directory) Q = asq.Simulation(P) Q.simulate_until_max_time() max_sim_time = Q.max_simulation_time bins = [obs*(365.25/12) for obs in range(int(ceil(max_sim_time / (365.25/12)))+1)] Q.write_records_to_file(directory, i) data = read_in_data(directory, i) data_by_month = bin_by_month(data, bins) count_cls_4, count_other_cls = count_demand_N2(data_by_month) multiple_runs_demand_cls_4.append(count_cls_4) multiple_runs_demand_cls_other.append(count_other_cls) expected_wait_by_month_cls_4, expected_wait_by_month_cls_other = average_waiting_time_per_month(data_by_month) multiple_runs_wait_cls_4.append(expected_wait_by_month_cls_4) multiple_runs_wait_cls_other.append(expected_wait_by_month_cls_other) write_results_to_file(multiple_runs_demand_cls_4, 'demand_new', directory) write_results_to_file(multiple_runs_demand_cls_other, 'demand_follow_up', directory)
def test_repr_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = asq.ArrivalNode(Q) self.assertEqual(str(N), 'Arrival Node')
def test_find_cum_transition_row_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = asq.Node(1, Q) self.assertEqual([[round(obs, 1) for obs in row] for row in N.find_cum_transition_row()], [[0.1, 0.3, 0.4, 0.8], [0.6, 0.6, 0.6, 0.8], [0.0, 0.0, 0.4, 0.7]])
def test_repr_method(self): Q = asq.Simulation(asq.load_parameters('tests/datafortesting/logs_test_for_simulation/')) N = Q.transitive_nodes[0] s = asq.Server(N, 4) self.assertEqual(str(s), 'Server 4 at Node 1')