def __init__(self, code=0, name=""): self.code = code if name == "": self.name = "Node" + str(code) else: self.name = name # graph features self.out_links = [] self.in_links = [] # demand features self.s_interval = stochastic.Stochastic() # parameters of "not stochastic" demand self.direct_in = [] self.direct_out = [] self.back_in = [] self.back_out = [] # result parameters self.pass_out = [] self.pass_in = []
def generate_demand(self, duration): self.in_psgs = [] self.out_psgs = [] self.transfer_psgs = [] self.transfered_psgs = [] # define the arrival moments for ln in self.lines: ln.transfer_node = self ln.define_schedule(duration) # generate origin passengers st = 0 s_int = stochastic.Stochastic(law=2, scale=60.0/self.origin_pass_number) while st < duration: st += s_int.get_value() psg = passenger.Passenger() psg.t_appear = st psg.to_transfer = False psg.next_line = random.choice(self.lines) # passengers part isn't considered self.in_psgs.append(psg) # generate out and transfer passengers for ln in self.lines: for tm in ln.arrivals: pn = int(ln.out_psgs_number.get_value()) if pn < 0: pn = 0 for _ in range(pn): p = passenger.Passenger() p.t_appear = tm p.to_transfer = random.random() < self.transfer_prob if p.to_transfer: line_to_transfer = random.choice(self.lines) while line_to_transfer is ln: line_to_transfer = random.choice(self.lines) p.next_line = line_to_transfer self.transfer_psgs.append(p) # print tm, ln.name, p.next_line.name # else: # print tm, ln.name, "end" self.out_psgs.append(p)
def gen_demand(self, duration, is_stochastic=True): """" Generates demand for trips in the network duration - duration of the simulation period, min. """ self.demand = [] if is_stochastic: for source in self.nodes: time = 0 while time <= duration: interval = round(source.s_interval.get_value(), 1) time += interval # generating a new passenger new_passenger = passenger.Passenger() new_passenger.m_appearance = time new_passenger.origin_node = source # defining the target node - random choice rule (can't be the same as origin node) target = random.choice(self.nodes) while target == source: target = random.choice(self.nodes) new_passenger.destination_nodes = self.define_destinations( source, target) # print [new_passenger.origin_node.code] + [nd.code for nd in new_passenger.destination_nodes] new_passenger.m_boarding = [ 0 for _ in range(new_passenger.transits_number + 1) ] new_passenger.m_disembarkation = [ 0 for _ in range(new_passenger.transits_number + 1) ] new_passenger.current_destination_node = new_passenger.destination_nodes[ 0] # adding the passenger to the origin node collection source.pass_out.append(new_passenger) self.demand.append(new_passenger) else: # for nd in self.nodes: time = 0 for din in nd.direct_in: if din > 0: st = stochastic.Stochastic(law=2, scale=60.0 / din) t = st.get_value() while t <= 60: t += st.get_value() new_passenger = passenger.Passenger() new_passenger.m_appearance = time + t new_passenger.origin_node = nd destination_node = random.choice(self.nodes) find_next = True while find_next: if destination_node == nd: destination_node = random.choice( self.nodes) find_next = True continue idx = time // 60 if time // 60 < len( nd.direct_in) else len(nd.direct_in) - 1 # print "sum:", sum(dnd.direct_out[idx] for dnd in self.nodes) if sum(dnd.direct_out[idx] for dnd in self.nodes) - nd.direct_out[idx] >= 1: if destination_node.direct_out[idx] > 0: destination_node.direct_out[idx] -= 1 find_next = False else: destination_node = random.choice( self.nodes) find_next = True else: find_next = False new_passenger.destination_nodes = self.define_destinations( nd, destination_node) new_passenger.current_destination_node = new_passenger.destination_nodes[ 0] nd.pass_out.append(new_passenger) self.demand.append(new_passenger) # print new_passenger.origin_node.code, new_passenger.destination_node.code time += 60 time = 0 for bin in nd.back_in: if bin > 0: st = stochastic.Stochastic(law=2, scale=60.0 / bin) t = st.get_value() while t <= 60: t += st.get_value() new_passenger = passenger.Passenger() new_passenger.m_appearance = time + t new_passenger.origin_node = nd destination_node = random.choice(self.nodes) find_next = True while find_next: if destination_node == nd: destination_node = random.choice( self.nodes) find_next = True continue idx = time // 60 if time // 60 < len( nd.back_in) else len(nd.back_in) - 1 # print "sum:", sum(dnd.direct_out[idx] for dnd in self.nodes) if sum(dnd.back_out[idx] for dnd in self.nodes) - nd.back_out[idx] >= 1: if destination_node.back_out[idx] > 0: destination_node.back_out[idx] -= 1 find_next = False else: destination_node = random.choice( self.nodes) find_next = True else: find_next = False new_passenger.destination_nodes = self.define_destinations( nd, destination_node) new_passenger.current_destination_node = new_passenger.destination_nodes[ 0] nd.pass_out.append(new_passenger) self.demand.append(new_passenger) time += 60
from stochastic import stochastic from transportnet import net # generation of a random transport network # given: num_nodes - number of nodes in a net # num_links - number of links in a net # link_weight - stochastic variable # # num_nodes max(num_links) # 1 0 0*1 # 2 2 1*2 # 3 6 2*3 # 4 12 3*4 # 5 ? 4*5(?) # ... ... # n n*(n-1) # forming the network # n = net.Net() # stochastic variable of the links weight sw = stochastic.Stochastic(1, 1, 0.2) # stochastic variable of the number of line stops sn = stochastic.Stochastic(0, 3, 4) # generating the links n.generate(6, 30, sw) # generating the lines n.gen_lines(5, sn) # print out simulation results n.print_characteristics()
for veh_number in range(1, 9 + 2, 2): for mean_int in range(1, 9 + 2, 2): for stops_number in range(2, 10 + 2, 2): for mean_dist in range(1, 3 + 1, 1): for capacity in range(10, 70 + 30, 30): # runs of the experiment twt = [] mean_twt = 0 for runs in range(100): # forming the network n = net.Net() for i in range(2, stops_number + 1): n.add_link( i - 1, i, stochastic.Stochastic(law=1, location=mean_dist, scale=0.2 * mean_dist).get_value()) # define the route path l = line.Line(n, range(1, stops_number + 1)) # defining the route fleet # vehicles = [] # for i in range(veh_number): # vehicles.append(vehicle.Vehicle(capacity)) l.add_vehicles([ vehicle.Vehicle(capacity) for _ in range(veh_number) ]) # add the line to the network n.lines.append(l) # generating the mean intervals for passengers arrivals to the network (line) nodes for node in n.nodes:
from transfernode import node from transfernode import line from stochastic import stochastic from genetics import ga wislicka = node.Node() line125 = line.Line(14, 20, stochastic.Stochastic(1, 2.50, 1.02), 0.0149, "125") line129 = line.Line(10, 15, stochastic.Stochastic(1, 3.30, 1.65), 0.0301, "129") line132 = line.Line(13, 20, stochastic.Stochastic(1, 2.08, 1.41), 0.0302, "132") line138 = line.Line(1, 20, stochastic.Stochastic(1, 3.47, 2.03), 0.0542, "138") line152 = line.Line(7, 20, stochastic.Stochastic(1, 3.77, 2.00), 0.0471, "152") line159 = line.Line(4, 15, stochastic.Stochastic(1, 8.83, 2.25), 0.0696, "159") line172 = line.Line(1, 20, stochastic.Stochastic(1, 4.85, 1.60), 0.0836, "172") line192 = line.Line(19, 20, stochastic.Stochastic(1, 3.43, 1.45), 0.0282, "192") line429 = line.Line(4, 15, stochastic.Stochastic(1, 1.83, 1.34), 0.0235, "429") line501 = line.Line(12, 15, stochastic.Stochastic(1, 3.47, 1.61), 0.1442, "501") line502 = line.Line(1, 10, stochastic.Stochastic(1, 4.75, 2.03), 0.2569, "502") line511 = line.Line(5, 15, stochastic.Stochastic(1, 5.11, 2.05), 0.1113, "511") line572 = line.Line(12, 20, stochastic.Stochastic(1, 2.96, 1.49), 0.1060, "572") wislicka.origin_pass_number = 402 #[pas./h] wislicka.lines = [ line125, line129, line132, line138, line152, line159, line172, line192, line429, line501, line502, line511, line572 ]
# # n.add_link(18, 19, s_weight.get_value()) # # n.add_link(20, 21, s_weight.get_value()) # # n.add_link(21, 10, s_weight.get_value()) # # n.add_link(10, 22, s_weight.get_value()) # # n.add_link(22, 23, s_weight.get_value()) # # n.add_link(23, 5, s_weight.get_value()) # # n.add_link(5, 24, s_weight.get_value()) # # n.add_link(25, 5, s_weight.get_value()) # # n.add_link(5, 26, s_weight.get_value()) # # n.add_link(26, 27, s_weight.get_value()) # # n.add_link(27, 17, s_weight.get_value()) # # n.add_link(17, 28, s_weight.get_value()) # # n.add_link(28, 29, s_weight.get_value()) # # n.add_link(29, 30, s_weight.get_value()) s_mean_interval = stochastic.Stochastic(law=0, location=1, scale=4) for nd in n.nodes: nd.s_interval = stochastic.Stochastic(law=2, scale=s_mean_interval.get_value()) # # define a set of public transport lines # bochnia_lines = [line.Line(n, [27, 28, 39, 18, 23, 26, 15, 31, 32, # 31, 15, 26, 23, 18, 35, 25, 27], [27, 32]), # line.Line(n, [27, 28, 39, 18, 9, 11, 7, 8, 10, 4, 5, 6, # 5, 4, 10, 8, 7, 11, 9, 18, 35, 25, 27], [27, 6]), # line.Line(n, [18, 35, 20, 21, 22, 19, 14, 12, 13, # 12, 14, 19, 22, 21, 20, 35, 18], [18, 13]), # line.Line(n, [27, 28, 39, 18, 23, 24, 17, 16, 40, 41, 30, 42, 43, 2, 3, 33, 34, 29, # 34, 1, 2, 36, 37, 38, 37, 36, 43, 42, 30, 41, 40, 16, 17, 24, 23, 18, 35, 25, 27], [27, 29])] # # conf #2 # # lines = [line.Line(n, [1, 3, 4, 6, 9, 12, 14]), # # line.Line(n, [2, 4, 5, 8, 11, 13]),
for ln in lns_back_in: ps_back_in.append([int(el) for el in ln.split('\t')]) # matrix of passengers' arrivals in main direction ps_direct_out = [] for ln in lns_direct_out: ps_direct_out.append([int(el) for el in ln.split('\t')]) # matrix of passengers' arrivals in back direction ps_back_out = [] for ln in lns_back_out: ps_back_out.append([int(el) for el in ln.split('\t')]) # simulate the average demand interval for the nodes for node in n.nodes: node.s_interval = stochastic.Stochastic(law=2, scale=stochastic.Stochastic(law=1, location=2, scale=0.5).get_value()) # set a trace of the line #129 line129 = line.Line(n, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) # add vehicles operating at the line #129 line129.add_vehicles([vehicle.Vehicle(veh_capacity) for __ in range(veh_num)]) line129.velocity = velocity line129.intermediate_stop_duration = 1.0*stop_dur/60 # [min] # add tne line #129 to the net n.lines.append(line129) # simulate the line operation n.simulate(duration=18*60) # print out characteristics of the net # n.print_characteristics() # n.print_od_matrix() wait_coef += 1.0 * n.total_wait_time / n.duration