def __init__(self, gate_num, duration, arrive_interval, check_interval): self.simulation = Simulation(duration) self.waitline = SQueue() # 等待队列 self.duration = duration self.gates = [0] * gate_num # 通道列表,0表示空闲 self.total_wait_time = 0 self.total_used_time = 0 self.car_num = 0 self.arrive_interval = arrive_interval # 到达时间的间隔 self.check_interval = check_interval # 检查时间的范围
def __init__(self, gate_num, duration, arrive_interval, check_interval): self.simulation = Simulation(duration) self.waitline = SQueue() self.duration = duration self.gates = [0] * gate_num self.total_wait_time = 0 self.total_used_time = 0 self.car_num = 0 self.arrive_interval = arrive_interval self.check_interval = check_interval
def __init__(self, gate_num, duration, arrive_interval, check_interval): self.simulation = Simulation(duration) #模拟事件的驱动程序 self.waitline = SQueue() #等待的车辆队列 self.duration = duration self.gates = [0] * gate_num #收费口,使用中为1,空闲为0 self.total_wait_time = 0 self.total_used_time = 0 self.car_num = 0.0 self.arrive_interval = arrive_interval #车辆到达间隔,模拟的使用其范围内的随机数 self.check_interval = check_interval #也是随机数的范围
def levelorder(t, proc): qu = SQueue() qu.enqueue(t) while not qu.is_empty(): t = qu.dequeue() if t is None: continue if t.left: qu.enqueue(t.left) if t.right: qu.enqueue(t.right) proc(t.data)
def BFS(self, v, visited): queue = SQueue(self.Nv) self.visit(v) visited[v] = True queue.addQ(v) while not queue.is_empty(): pre = queue.deleteQ() # 遍历pre的邻接点 next_ = self.G[pre].firstEdge while next_: if not visited[next_.adjV]: self.visit(next_.adjV) visited[next_.adjV] = True queue.addQ(next_.adjV) next_ = next_.next_
def maze_solver_queue(maze, start, end): # width-first search if start == end: print('Path finds.') return qu = SQueue() mark(maze, start) qu.enqueue(start) while not qu.is_empty(): pos = qu.dequeue() for i in range(4): nextp = (pos[0] + dirs[i][0], pos[1] + dirs[i][1]) if passable(maze, nextp): if nextp == end: print('Path finds.') return mark(maze, nextp) qu.enqueue(nextp) print('No path.')
def maze_solver_queue(maze, start, end): if start == end: print("Path finds.") return None qu = SQueue() mark(maze, start) qu.enqueue(start) while not qu.is_empty(): pos = qu.dequeue() for i in range(4): nextp = (pos[0] + dirs[i][0], pos[1] + dirs[i][1]) if passable(maze, nextp): if nextp == end: print("Path find.") return None mark(maze, nextp) qu.enqueue(nextp) print("No path.")
class Customs: def __init__(self, gate_num, duration, arrive_interval, check_interval): self.simulation = Simulation(duration) self.waitline = SQueue() self.duration = duration self.gates = [0] * gate_num self.total_wait_time = 0 self.total_used_time = 0 self.car_num = 0 self.arrive_interval = arrive_interval self.check_interval = check_interval def wait_time_acc(self, n): self.total_wait_time += n def total_time_acc(self, n): self.total_used_time += n def car_count_1(self): self.car_num += 1 def add_event(self, event): self.simulation.add_event(event) def cur_time(self): return self.simulation.cur_time() def enqueue(self, car): self.waitline.enqueue(car) def has_queued_car(self): return not self.waitline.is_empty() def next_car(self): return self.waitline.dequeue() def find_gate(self): for i in range(len(self.gates)): if self.gates[i] == 0: self.gates[i] = 1 return i return None def free_gate(self, i): if self.gates[i] == 1: self.gates[i] = 0 else: raise ValueError('Clear gate error.') def simulate(self): Arrive(0, self) # generate the initial car self.simulation.run() self.statistics() def statistics(self): print('Simulate ' + str(self.duration) + ' minutes, for ' + str(len(self.gates)) + ' gates') print(self.car_num, 'cars pass the customs') print('Average waiting time:', self.total_wait_time / self.car_num) print('Average passing time:', self.total_used_time / self.car_num) i = 0 while not self.waitline.is_empty(): self.waitline.dequeue() i += 1 print(i, 'cars are in waiting line.')
class CustomsApplication: def __init__(self, gate_num, duration, arrive_interval, check_interval): self.simulation = Simulation(duration) #模拟事件的驱动程序 self.waitline = SQueue() #等待的车辆队列 self.duration = duration self.gates = [0] * gate_num #收费口,使用中为1,空闲为0 self.total_wait_time = 0 self.total_used_time = 0 self.car_num = 0.0 self.arrive_interval = arrive_interval #车辆到达间隔,模拟的使用其范围内的随机数 self.check_interval = check_interval #也是随机数的范围 def wait_time_acc(self, n): self.total_wait_time += n def total_time_acc(self, n): self.total_used_time += n def car_count_1(self): self.car_num += 1 def add_event(self, event): self.simulation.add_event(event) def cur_time(self): self.simulation.cur_time() def enqueue(self, car): self.waitline.enqueue(car) def has_queued_car(self): return not self.waitline.is_empty() def next_car(self): return self.waitline.dequeue() def find_gate(self): for i in range(len(self.gates)): if self.gates[i] == 0: self.gates[i] = 1 return i return None def free_gate(self, i): if self.gates[i] == 1: self.gates[i] = 0 else: raise ValueError("Clear gate error") def simulateRun(self): Arrive(0, self) #Arrive是可以自己产生产生事件的事件类,模拟车辆到达 self.simulation.run() #模拟事件驱动程序启动 self.statistics() #模拟程序运行完毕后,输出统计数据 def statistics(self): print("Simulate " + str(self.duration) + " minute , for " + str(len(self.gates)) + " gates") print(self.car_num, " cars pass the customs") print("Average waiting time:", self.total_wait_time / self.car_num) print("Average passing time:", self.total_used_time / self.car_num) i = 0 while not self.waitline.is_empty(): self.waitline.dequeue() i += 1 print(i, " cars are in waiting line.")
class Customs: def __init__(self, gate_num, duration, arrive_interval, check_interval): self.simulation = Simulation(duration) self.waitline = SQueue() # 等待队列 self.duration = duration self.gates = [0] * gate_num # 通道列表,0表示空闲 self.total_wait_time = 0 self.total_used_time = 0 self.car_num = 0 self.arrive_interval = arrive_interval # 到达时间的间隔 self.check_interval = check_interval # 检查时间的范围 def wait_time_acc(self, n): self.total_wait_time += n def total_time_acc(self, n): self.total_used_time += n def car_count_1(self): self.car_num += 1 def add_event(self, event): self.simulation.add_event(event) def cur_time(self): return self.simulation.cur_time() def enqueue(self, car): self.waitline.enqueue(car) def has_queued_car(self): return not self.waitline.is_empty() def next_car(self): return self.waitline.dequeue() def find_gate(self): for i in range(len(self.gates)): if self.gates[i] == 0: self.gates[i] = 1 return i return None def free_gate(self, i): if self.gates[i] == 1: self.gates[i] = 0 else: raise ValueError("free gate error") # 实施模拟 def simulate(self): Arrive(0, self) # 汽车到达事件,初始化生成一辆车 self.simulation.run() self.statistics() # 输出统计数据 def statistics(self): print("Simulation " + str(self.duration) + " minutes, for " + str(len(self.gates)) + " gates") print(self.car_num, " cars pass the customs") print("Average waiting time: ", self.total_wait_time / self.car_num) print("Average passing time: ", self.total_used_time / self.car_num) i = 0 while not self.waitline.is_empty(): self.waitline.dequeue() i += 1 print(i, " cars are in waiting line")