def run(self, file: TextIO) -> Dict[str, Any]: """Run the simulation on the events stored in <initial_events>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. """ # Initialize statistics stats = { 'num_customers': 0, 'total_time': 0, 'max_wait': -1 } max_waits = dict() initial_event_list = create_event_list(file) for event in initial_event_list: self._events.add(event) if isinstance(event, CustomerArrival): stats['num_customers'] += 1 max_waits[event.customer] = event.timestamp while not self._events.is_empty(): event = self._events.remove() if isinstance(event, CheckoutCompleted): max_waits[event.customer] = event.timestamp - \ max_waits[event.customer] spawns = event.do(self._store) for spawn in spawns: self._events.add(spawn) stats['total_time'] = event.timestamp stats['max_wait'] = max_waits[max(max_waits, key=max_waits.get)] return stats
def run(self, event_file): """Run the simulation on the events stored in <event_file>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. @type self: GroceryStoreSimulation @type event_file: str A filename referring to a raw list of events. Precondition: the event file is a valid list of events. @rtype: dict[str, object] """ # Initialize statistics stats = { 'num_customers': 0, 'total_time': 0, 'max_wait': -1 } initial_events = create_event_list(event_file) # TODO: Process all of the events, collecting statistics along the way. return stats
def run(self, initial_events): """Run the simulation on the list of events in <initial_events>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. @type self: Simulation @type initial_events: list[Event] An initial list of events. @rtype: dict[str, object] """ # Add all initial events to the event queue. for event in initial_events: self._events.add(event) while self._events.is_empty() is False: executed_event = self._events.remove() result_events = executed_event.do(self._dispatcher, self._monitor) # this warning can be ignored if result_events is not None: for result_event in result_events: self._events.add(result_event) # Until there are no more events, remove an event # from the event queue and do it. Add any returned # events to the event queue. return self._monitor.report() if __name__ == "__main__": events = create_event_list("events.txt") sim = Simulation() final_stats = sim.run(events) print(final_stats)
def run(self, event_file): """Run the simulation on the events stored in <event_file>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. @type self: GroceryStoreSimulation @type event_file: str A filename referring to a raw list of events. Precondition: the event file is a valid list of events. @type initial_events: list[event] @rtype: dict[str, object] """ # Initialize statistics stats = { 'num_customers': 0, #check 'total_time': 0, 'max_wait': -1 } initial_events = create_event_list(event_file) #counted number of customers from file directly # TODO: Process all of the events, collecting statistics along the way. #first, add event from initial_events to pq then sort using sort method for index in range (len(initial_events)): self._events.add(initial_events[index]) #second, pass sorted events which is inside PQ to store while not self._events.is_empty(): #trigger the do function which checks if new events spawn in the store #new events are returned #setup simulation clock, advance clock to first event current_event = self._events.remove() #when there is only one event left, equate the time if self._events.is_empty(): stats['total_time'] = current_event.timestamp #the event is triggered returned_tuple = current_event.do(self._store) if returned_tuple[1] == 'int': stats['max_wait'] = returned_tuple[0] elif returned_tuple[1] == 'one event': self._events.add(returned_tuple[0]) elif returned_tuple[1] == 'event list': for item in returned_tuple[0]: self._events.add(item) return stats
def run(self, event_file): """Run the simulation on the events stored in <event_file>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. @type self: GroceryStoreSimulation @type event_file: str A filename referring to a raw list of events. Precondition: the event file is a valid list of events. @rtype: dict[str, int] """ # Initialize statistics stats = { 'num_customers': 0, 'total_time': 0, 'max_wait': -1 } initial_events = create_event_list(event_file) for i in range(len(initial_events)): self._events.add(initial_events[i]) if type(initial_events[i]) == JoinLine: stats['num_customers'] += 1 # THE FOLLOWING CODE MAY SHOW SOME WARNINGS. # (IF NOT, PLEASE IGNORE THIS COMMENT) # PROFESSOR LIU TOLD ME THAT THESE WARNINGS ARE PROBLEMS # WITH PyCharm ITSELF. SINCE WE ARE NOT ALLOWED TO CHANGE # THE INTERFACE, PROF SAID WE CAN JUST IGNORE THEM. THANK YOU. while not self._events.is_empty(): event = self._events.remove() spawned_events = event.do(self._store) stats['total_time'] = event.timestamp # Calculate max_wait if type(event) == FinishCheckOut: end_time = event.timestamp customer_name = event.name for u in range(len(initial_events)): if type(initial_events[u]) == JoinLine: temp = initial_events[u] if customer_name == temp.name: start_time = temp.timestamp waited_time = end_time - start_time if waited_time > stats['max_wait']: stats['max_wait'] = waited_time if spawned_events is not None: for j in range(len(spawned_events)): self._events.add(spawned_events[j]) return stats
def run(self, event_file): """Run the simulation on the events stored in <event_file>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. @type self: GroceryStoreSimulation @type event_file: str A filename referring to a raw list of events. Precondition: the event file is a valid list of events. @rtype: dict[str, object] """ # Initialize statistics stats = { 'num_customers': 0, 'total_time': 0, 'max_wait': -1 } initial_events = create_event_list(event_file) # TODO: Process all of the events, collecting statistics along the way. for event in initial_events: self._events.add(event) while not self._events.is_empty(): current_event = self._events.remove() new_events = current_event.do(self._store) stats['total_time'] = current_event.timestamp # collects the total time if new_events is None: pass elif len(new_events) > 0: for x in new_events: self._events.add(x) # find out how many Customers have checked out # through store's finished_customers dict stats['num_customers'] = len(self._store.finished_customers) # find out max_wait from store's finished_customers for customer in self._store.finished_customers: if self._store.finished_customers[customer].get_wait_time() > \ stats['max_wait']: stats['max_wait'] = self._store.finished_customers[ customer].get_wait_time() return stats
def run(self, file: TextIO) -> Dict[str, Any]: """Run the simulation on the events stored in <initial_events>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. """ # This initializes statistics. stats = {'num_customers': 0, 'total_time': 0, 'max_wait': -1} # This fills self._events from file. event_list = create_event_list(file) while len(event_list) != 0: self._events.add(event_list.pop()) # This keeps track of customer start and end times. This is used to # calculate the maximum wait time. customers = {} while not self._events.is_empty(): event = self._events.remove() # This checks to see the event type. if isinstance(event, CustomerArrival): # This checks to see if the customer is in the customers # dictionary since the "max_wait" calculation is based on the # customer arriving at the wait area. if event.customer not in customers: customers[event.customer] = [event.timestamp, -1] stats['num_customers'] += 1 elif isinstance(event, CheckoutCompleted): customers[event.customer][1] = event.timestamp new_events = event.do(self._store) # This adds the new events. while not len(new_events) == 0: self._events.add(new_events.pop()) stats['total_time'] = event.timestamp # This calculates the maximum wait time. for key in customers: wait_time = customers[key][1] - customers[key][0] if wait_time > stats['max_wait']: stats['max_wait'] = wait_time return stats
def run(self, event_file): """Run the simulation on the events stored in <event_file>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. @type self: GroceryStoreSimulation @type event_file: str A filename referring to a raw list of events. Precondition: the event file is a valid list of events. @rtype: dict[str, object] """ # Initialize statistics stats = { 'num_customers': 0, 'total_time': 0, 'max_wait': -1 } initial_events = create_event_list(event_file) for item in initial_events: self._events.add(item) while self._events.is_empty() == False: processEvent= self._events.remove() #change the stats acrroding to the eventbeing popped out from list if(processEvent.status=="Finish"): waitTime=processEvent.getWaitTime() if waitTime > stats['max_wait']: stats['max_wait'] = waitTime if(processEvent.status=="Begin"): stats['num_customers']+=1 if(self._events.is_empty() and processEvent.status=="Finish"): stats['total_time']=processEvent.timestamp newEvents=processEvent.do(self._store) if(newEvents!=None): for event in newEvents: self._events.add(event) return stats
def run(self, event_file): """Run the simulation on the events stored in <event_file>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. @type self: GroceryStoreSimulation @type event_file: str A filename referring to a raw list of events. Precondition: the event file is a valid list of events. @rtype: dict[str, object] """ # Initialize statistics stats = { 'num_customers': 0, 'total_time': 0, 'max_wait': -1 } initial_events = create_event_list(event_file) # TODO: Process all of the events, collecting statistics along the way. #add events to self._events for event in initial_events: self._events.add(event) while not self._events.is_empty(): event = self._events.remove() if isinstance(event,EventJoin): new_events = EventJoin.do() elif isinstance(event,EventClose): new_events = EventClose.do() elif isinstance(event,EventBegin): new_events = EventBegin.do() elif isinstance(event,EventFinish): new_events = EventFinish.do() for item in new_events: self._events.add(new_events) return stats
def run(self, event_file): """Run the simulation on the events stored in <event_file>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. @type self: GroceryStoreSimulation @type event_file: str A filename referring to a raw list of events. Precondition: the event file is a valid list of events. @rtype: dict[str, object] """ stats = {'num_customers': 0, 'total_time': 0, 'max_wait': -1} initial_events = create_event_list(event_file) max_times = [] for event in initial_events: self._events.add(event) if type(event) is JoinLine: self._store.costumers.add(event.cos) else: event.line = self._store.lines[event.line] # To avoid creating another public attribute for the class # CloseLine, I intentionally stored the index of the line that # will close in a " wrong " attribute (in event.line) from the # create_event_list function, and here I fix it. while not self._events.is_empty(): next_event = self._events.remove() new_events = next_event.do(self._store) if type(next_event) is FinishCheckingOut: max_times.append(next_event.cos.total_time_waited) for event in new_events: self._events.add(event) stats['total_time'] = next_event.timestamp stats['num_customers'] = len(self._store.costumers) stats['max_wait'] = max(max_times) return stats
def run(self, file: TextIO) -> Dict[str, Any]: """Run the simulation on the events stored in <initial_events>. Return a dictionary containing statistics of the simulation, according to the specifications in the assignment handout. """ # Initialize statistics stats = { 'num_customers': 0, 'total_time': 0, 'max_wait': -1 } # TODO: Calculate and return the correct statistics. file_list = create_event_list(file) for i in file_list: self._events.add(i) dict_name = {} max_wait = 0 while not self._events.is_empty(): i = self._events.remove() if isinstance(i, CustomerArrival): if i.customer.name not in dict_name: dict_name[i.customer.name] = i.timestamp stats['num_customers'] += 1 if isinstance(i, CheckoutCompleted): dict_name[i.customer.name] \ = i.timestamp - dict_name[i.customer.name] for event in i.do(self._store): self._events.add(event) for j in dict_name: if dict_name[j] > max_wait: max_wait = dict_name[j] time_last = i.timestamp stats['total_time'] = time_last stats["max_wait"] = max_wait return stats
@type self: Simulation @type initial_events: list[Event] An initial list of events. @rtype: dict[str, object] """ # TODO # Add all initial events to the event queue. # Until there are no more events, remove an event # from the event queue and do it. Add any returned # events to the event queue. for i in initial_events: self._events.add(i) while not self._events.is_empty(): event = self._events.remove() new_events = event.do(self._dispatcher, self._monitor) for new_event in new_events: self._events.add(new_event) return self._monitor.report() if __name__ == "__main__": events = create_event_list("events.txt") sim = Simulation() final_stats = sim.run(events) print(final_stats)
17---Arnold---request---(6,6) 17---Arnold---request---(6,6) ---------------------------------------- """ rider_str = 'Riders:\n' riders = sim._monitor._activities[RIDER] for events in riders.values(): for event in events: rider_str += '\t{}\n'.format(event) driver_str = 'Drivers:\n' drivers = sim._monitor._activities[DRIVER] for events in drivers.values(): for event in events: driver_str += '\t{}\n'.format(event) return (20*'--') + '\n' + rider_str + \ (20*"--") + '\n' + driver_str + \ (20*"--") if __name__ == "__main__": filename = 'events.txt' events = create_event_list(filename) filename1 = 'events_small.txt' #events = create_event_list(filename1) sim = Simulation() final_stats = sim.run(events) print(final_stats) print(sim)
>>> final_stats = sim.run(events) >>> print(final_stats['driver_total_distance']) 14.0 >>> print(final_stats['rider_wait_time']) 11.0 >>> print(final_stats['driver_ride_distance']) 10.0 """ # Add all initial events to the event queue. for e in initial_events: self._events.add(e) # Until there are no more events, remove an event # from the event queue and do it. Add any returned # events to the event queue. while not self._events.is_empty(): r_event = self._events.remove().do(self._dispatcher, self._monitor) if r_event: for e in r_event: self._events.add(e) return self._monitor.report() if __name__ == "__main__": events = create_event_list("events.txt") sim = Simulation() final_stats = sim.run(events) print(final_stats)