guohanghui/graph-theory
0
1from graph import Graph2from graph.transshipment_problem import clondike_transshipment_problem, Train, schedule_rail_system3 4 5def test_mining_train():6 """7 Assures that a train can schedule a number of in, out and in/out jobs8 using TSP.9 """10 g = clondike_transshipment_problem()11 assert isinstance(g, Graph)12 13 equipment_deliveries = [14 ("L-1", "L-1-1"),15 ("L-1", "L-1-2"), # origin, destination16 ("L-1", "L-1-3"),17 ("L-1", "L-1-4")18 ]19 20 mineral_deliveries = [21 ("L-1-1", "L-1"),22 ("L-1-2", "L-1"),23 ("L-1-3", "L-1"),24 ("L-1-4", "L-1"),25 ]26 27 access_nodes = {"L-1", "L-1-1", "L-1-2", "L-1-3", "L-1-4"}28 29 train = Train(rail_network=g, start_location="L-1", access=access_nodes)30 31 s1 = train.schedule(equipment_deliveries)32 s2 = train.schedule(mineral_deliveries)33 s3 = train.schedule(equipment_deliveries[:] + mineral_deliveries[:])34 35 s1_expected = [36 ('L-1', 'L-1-1'), ('L-1', 'L-1-2'), ('L-1', 'L-1-3'), ('L-1', 'L-1-4')37 ] # shortest jobs first.!38 39 s2_expected = [40 ('L-1-1', 'L-1'), ('L-1-2', 'L-1'), ('L-1-3', 'L-1'), ('L-1-4', 'L-1')41 ] # shortest job first!42 43 s3_expected = [44 ('L-1', 'L-1-1'), ('L-1-1', 'L-1'), # circuit 145 ('L-1', 'L-1-2'), ('L-1-2', 'L-1'), # circuit 246 ('L-1', 'L-1-3'), ('L-1-3', 'L-1'), # circuit 347 ('L-1', 'L-1-4'), ('L-1-4', 'L-1') # circuit 448 ] # shortest circuit first.49 50 assert s1 == s1_expected51 assert s2 == s2_expected52 assert s3 == s3_expected53 54 55def test_surface_mining_equipment_delivery():56 """57 Assures that equipment from the surface can arrive in the mine58 """59 g = clondike_transshipment_problem()60 61 equipment_deliveries = [62 ("Surface", "L-1-1"),63 ("Surface", "L-1-2"), # origin, destination64 ]65 66 lift_access = {"Surface", "L-1", "L-2"}67 lift = Train(rail_network=g, start_location="Surface", access=lift_access)68 69 L1_access = {"L-1", "L-1-1", "L-1-2", "L-1-3", "L-1-4"}70 level_1_train = Train(rail_network=g, start_location="L-1", access=L1_access)71 72 assert lift_access.intersection(L1_access), "routing not possible!"73 74 schedule_rail_system(rail_network=g, trains=[lift, level_1_train],75 jobs=equipment_deliveries)76 s1 = level_1_train.schedule()77 s2 = lift.schedule()78 79 s1_expected = [('L-1', 'L-1-1'), ('L-1', 'L-1-2')]80 s2_expected = [("Surface", "L-1"), ("Surface", "L-1")]81 82 assert s1 == s1_expected83 assert s2 == s2_expected84 85 86def test_double_direction_delivery():87 """88 Tests a double delivery schedule:89 Lift is delivering equipment into the mine as Job-1, Job-290 Train is delivering gold out of the mine as Job-3, Job-491 The schedules are thereby:92 Lift: [Job-1][go back][Job-2]93 Train: [Job-3][go back][Job-4]94 The combined schedule should thereby be:95 Lift: [Job-1][Job-3][Job-2][Job-4]96 Train: [Job-3][Job-1][Job-4][Job-2]97 which yields zero idle runs.98 """99 g = clondike_transshipment_problem()100 101 equipment_deliveries = [102 ("Surface", "L-1-1"),103 ("Surface", "L-1-2")104 ]105 106 mineral_deliveries = [107 ("L-1-1", "Surface"),108 ("L-1-2", "Surface")109 ]110 lift_access = {"Surface", "L-1", "L-2"}111 lift = Train(rail_network=g, start_location="Surface", access=lift_access)112 113 L1_access = {"L-1", "L-1-1", "L-1-2", "L-1-3", "L-1-4"}114 level_1_train = Train(rail_network=g, start_location="L-1", access=L1_access)115 116 assert lift_access.intersection(L1_access), "routing not possible!"117 118 schedule_rail_system(rail_network=g, trains=[lift, level_1_train],119 jobs=equipment_deliveries + mineral_deliveries)120 s1 = level_1_train.schedule()121 s2 = lift.schedule()122 123 s1_expected = [('L-1', 'L-1-1'), ('L-1-1', 'L-1'), ('L-1', 'L-1-2'), ('L-1-2', 'L-1')]124 s2_expected = [('Surface', 'L-1'), ('L-1', 'Surface'), ('Surface', 'L-1'), ('L-1', 'Surface')]125 126 assert s1 == s1_expected127 assert s2 == s2_expected128 