def run_change_machine_state(self): while True: yield simpy.AnyOf(self.env, [x.finished_control for x in self.task_list.control_tasks]) task = [tsk for tsk in self.task_list.control_tasks if tsk.finished_control.processed][0] command = task.finished_control.value if self.road_conditions.tor60.processed and int(task.finished_control.value[1]) == 4: command = 'TOR60' if int(task.finished_control.value[1]) <= self.road_conditions.get_state(): self.machine.on_event(self.env.timeout(0, value=command)) self.user_memory.update_machine_state(self.machine.get_state_index()) # task.update_awareness_param() self.task_list.run_machine_triggered_tasks(command=command) else: print("WARNING: USER TAKE WRONG DECISION ON ", str(int(self.env.now / 60)) + ':' + str(int(self.env.now % 60)).zfill(2)) previous_state = str(self.machine.state) self.machine.on_event(self.env.timeout(0, value='Error')) self.task_list.run_machine_triggered_tasks('Error') yield self.env.timeout(2) if previous_state == 'TOR10' or previous_state == 'TOR60': self.machine.on_event(self.env.timeout(0, value='L0_switch')) else: self.machine.on_event(self.env.timeout(0, value=previous_state + '_switch')) # print('switch_callback' + callback + ' ' + str(self.env.now)) task.finished_control = self.env.event()
def sim(self): obs = self.get_obs() while (not self.env.done): yield self.simpy_env.timeout( ACTION_WAIT_TIME) # Forces small gap between get_obs and act action, agent_info = self.policy(obs) self.action_event = self.simpy_env.process( self.take_action(action)) try: yield simpy.AnyOf(self.simpy_env, [self.action_event, self.env.done_event]) except simpy.Interrupt: pass reward = self.get_reward() self.observations.append(self.env.observation_space.flatten(obs)) self.actions.append(self.env.action_space.flatten(action)) self.rewards.append(reward) self.agent_infos.append(agent_info) self.env_infos.append({}) obs = self.get_obs() self.offset_t_sojourn.append( self.env.observation_space.flatten(obs)[-1])
def reset_and_sim(self, policies): self.simpy_env = simpy.Environment() self.done = False self.fire_extinguish_events = [simpy.Event(self.simpy_env) for i in range(self.n_fires)] fire_levels = [] for i, n in enumerate(self.num_fires_of_each_size): fire_levels += [i+1] * n if self.fire_locations is not None: self.fires = [ Fire(self, self.simpy_env, i, fire_levels[i], fl) for i, fl in enumerate(self.fire_locations) ] else: # we want to randomize fire_locations = ( 2.*np.random.random_sample((self.n_fires,2)) - 1.).tolist() self.fires = [ Fire(self, self.simpy_env, i, fire_levels[i], fl) for i, fl in enumerate(fire_locations) ] if self.start_positions is not None: self.env_agents = [ UAV(self, self.simpy_env, i, sp, sp, self.discount, policies[i]) for i,sp in enumerate(self.start_positions) ] else: # we want to randomize start_positions = ( 2.*np.random.random_sample((self.n_agents,2)) - 1.).tolist() self.env_agents = [ UAV(self, self.simpy_env, i, sp, sp, self.discount, policies[i]) for i,sp in enumerate(start_positions) ] # Process all UAVs for uav in self.env_agents: self.simpy_env.process( uav.sim() ) # Process all fires for fire in self.fires: self.simpy_env.process( fire.sim() ) done = simpy.AllOf(self.simpy_env, self.fire_extinguish_events) self.simpy_env.run(until = simpy.AnyOf(self.simpy_env, [done, self.simpy_env.timeout(MAX_SIMTIME)]) ) self.done = True if(True): for uav in self.env_agents: try: uav.action_event.interrupt() except RuntimeError: pass self.simpy_env.run(until = self.simpy_env.now*(1.0001)) assert sum([uav.get_reward() for uav in self.env_agents]) == 0, 'There were unaccounted for rewards' # Collect observations, actions, etc.. and return them observations = [ u.observations for u in self.env_agents] actions = [ u.actions for u in self.env_agents] rewards = [ u.rewards for u in self.env_agents] agent_infos = [ u.agent_infos for u in self.env_agents] env_infos = [ u.env_infos for u in self.env_agents] offset_t_sojourn = [ u.offset_t_sojourn for u in self.env_agents ] return observations, actions, rewards, agent_infos, env_infos, offset_t_sojourn
def step(self, actions): # Takes an action set, outputs next observations, accumulated reward, done (boolean), info # Convention is: # If an agent is to act on this event, pass an observation and accumulated reward, # otherwise, pass None # "obs" variable will look like: [ [None], [None], [o3_t], [None], [o5_t] ] # "rewards" will look like: [ None , None , r3_r , None , r5_t ] # The action returned by the (decentralized) policy will look like # [ None , None , a3_t , None , a5_t ] for i, a in enumerate(actions): if a is not None: # need to bound action action = max(min(a[0], MAX_STOP_TIME), MIN_STOP_TIME) event = simpy.Event(self.simpy_env) self.agent_event_list[i] = event self.simpy_env.process(self.env_agents[i].change_traffic( event, action)) self.simpy_env.run( until=simpy.AnyOf(self.simpy_env, self.agent_event_list + [self.max_simtime_event])) whotriggered = who_triggered(self.agent_event_list) # Get next_obs, rewards # Check if max_simtime_reached try: self.max_simtime_event.ok done = True obs = [e.get_obs() for e in self.env_agents] rewards = [e.get_reward() for e in self.env_agents] except (AttributeError): done = False obs = [ self.env_agents[i].get_obs() if w else [None] for i, w in enumerate(whotriggered) ] rewards = [ self.env_agents[i].get_reward() if w else None for i, w in enumerate(whotriggered) ] return obs, rewards, done, {}
def worker_manager(self, env, batches): try: while True: if options['to_stop']: return self.working = True while True: if options['to_stop']: return self.assigned_batch = scheduler(env, batches, self) if numpy.size(self.assigned_batch) == 0: batch_events = [ x.batch.process for x in batches if x.batch.arrive_time > env.now ] if numpy.size(batch_events) == 0: return print('--- WORKER ---' ) if options['verbose'] > 4 else None print( "worker", self.index, 'will resume at', numpy.min([ x.batch.arrive_time for x in batches if x.batch.arrive_time > env.now ])) if options['verbose'] > 4 else None yield simpy.AnyOf(env, batch_events) else: break print('--- WORKER ---') if options['verbose'] > 4 else None print("batch", self.assigned_batch, "with deadline", self.assigned_batch.batch.deadline, "assigned") \ if options['verbose'] > 4 else None self.assigned_batch.reserve_job() yield env.timeout( numpy.random.exponential(options['average_service_time'])) print('--- WORKER ---') if options['verbose'] > 4 else None print('worker', self.index, 'finished a job at', env.now, 'for batch', self.assigned_batch.batch.index ) if options['verbose'] > 4 else None self.assigned_batch.success_job(env) self.assigned_batch = numpy.nan except simpy.Interrupt as i: print('--- WORKER ---') if options['verbose'] > 4 else None print('job interrupted', env.now, 'because:', i.cause) if options['verbose'] > 2 else None if not self.assigned_batch == []: self.assigned_batch.failure_job()
def process_queue(self): retries = 0 while self.tx_queue: frame = self.tx_queue[0] # persistent process with exponential backoff k = 1 while True: wait_time = self.node.sim.random.randrange(k) * 5e-3 yield self.node.timeout(wait_time) if self.node.phy.cca(): break k = k * 2 self.node.phy.send_pdu(frame) # wait for ack if this is a unicast frame if frame.dst != BROADCAST_ADDR: self.ack_event = self.node.create_event() self.ack_event.wait_for = frame duration = frame.nbits / self.node.phy.bitrate + 1e-3 yield simpy.AnyOf(self.node.sim.env, [ self.node.timeout(duration), self.ack_event, ]) if self.ack_event.triggered: retries = 0 self.tx_queue.popleft() self.stat.total_tx_unicast += 1 else: retries += 1 backoff_time = self.node.sim.random.randrange( 2**retries) * 5e-3 yield self.node.timeout(backoff_time) self.stat.total_retransmit += 1 else: retries = 0 self.tx_queue.popleft() self.stat.total_tx_broadcast += 1 self.ack_event = None
def step(self, actions): # Takes an action set, outputs next observations, accumulated reward, done (boolean), info # Convention is: # If an agent is to act on this event, pass an observation and accumulated reward, # otherwise, pass None # "obs" variable will look like: [ [None], [None], [o3_t], [None], [o5_t] ] # "rewards" will look like: [ None , None , r3_r , None , r5_t ] # The action returned by the (decentralized) policy will look like # [ None , None , a3_t , None , a5_t ] for i, a in enumerate(actions): if a is not None: if a >= 5: # Agents wants to hold self.env_agents[i].change_goal(hold_current=True) else: self.env_agents[i].change_goal(new_goal=a) self.simpy_env.run(until=simpy.AnyOf( self.simpy_env, self.uav_events + self.fire_events + [self.max_simtime_event])) agents_to_act = [False] * self.n_agents # check if any fires triggered fires_extinguished = who_triggered(self.fire_events) for i in fires_extinguished: agents_to_act = [True] * self.n_agents self.fires[i].extinguish() done = False if (not any([f.status for f in self.fires])): done = True # check if any single agent triggered uavs_to_act = who_triggered(self.uav_events) for i in uavs_to_act: agents_to_act[i] = True # Get next_obs, rewards try: # Check if max_simtime_reached self.max_simtime_event.ok done = True except (AttributeError): pass if (done): obs = [e.get_obs() for e in self.env_agents] rewards = [e.get_reward() for e in self.env_agents] else: obs = [ self.env_agents[i].get_obs() if w else [None] for i, w in enumerate(agents_to_act) ] rewards = [ self.env_agents[i].get_reward() if w else None for i, w in enumerate(agents_to_act) ] if (PRINTING): print('Obs: ', obs) if (PRINTING): print('Reward: ', rewards) return obs, rewards, done, {}
def passengerRun(self, env, passengerId): entryFloorObj = floors_stat[self.entryFloor] carList = [i for i in entryFloorObj.carsAtFloor] def carAtList_generator(): return [ car.carAt_reactivate for i, car in entryFloorObj.carsAtFloor.items() ] def carArrivedList_generator(): return [ car.carArrived for i, car in entryFloorObj.carsAtFloor.items() ] yield simpy.AnyOf(env, carAtList_generator()) # wait and look if nobody were here before you: yield env.timeout(0.0001) while passengerId in entryFloorObj.queue: for car_id in carList: allocatedCar = cars_stat[car_id] # car have to be at floor 0 and not overfilled: if (allocatedCar.carUsage.count < allocatedCar.carCapacity and entryFloorObj.carsAtFloor[car_id].carAt): history.append( '%6d passenger %d try to entry car %d Doors are %s' % (env.now, passengerId, car_id, allocatedCar.doorOpenedMonit.is_alive)) # passengers try entry car but in order of ascending id: self.req = allocatedCar.carUsage.request( priority=self.id) # passenger wait for entry or for car doors closing: yield self.req | allocatedCar.doorOpenedMonit history.append( '%6d passenger %d waited to entry car %d with priority %d' ' Doors are %s' % (env.now, passengerId, car_id, self.id, allocatedCar.doorOpenedMonit.is_alive)) # if not entry car: if not self.req.triggered: history.append( '%6d passenger %d not leave queue and not entry car %d' % (env.now, passengerId, car_id)) allocatedCar.carUsage.put_queue.remove(self.req) else: entryFloorObj.queue.pop( entryFloorObj.queue.index(passengerId)) allocatedCar.passengersInCar.append(passengerId) allocatedCar.passengersInCarDest.append( self.destFloor) passengers_stat[passengerId].depTime = env.now history.append( '%6d passenger %d leave queue and entry car %d LobbyQueue %s' % (env.now, passengerId, car_id, str(entryFloorObj.queue))) yield env.process( self.passengerUnloading(env, passengerId)) allocatedCar.carUsage.release(self.req) self.runEnd = True break else: pass if not self.runEnd: history.append('%6d passenger %d wait for any car' % ( env.now, passengerId, )) yield simpy.AnyOf(env, carArrivedList_generator())