def _simulate( self) -> Generator[Tuple[dict, List[object], bool], object, None]: """This is the generator to wrap each episode process.""" self._streamit_episode += 1 streamit.episode(self._streamit_episode) while True: # Ask business engine to do thing for this tick, such as generating and pushing events. # We do not push events now. streamit.tick(self._tick) self._business_engine.step(self._tick) while True: # Keep processing events, until no more events in this tick. pending_events = self._event_buffer.execute(self._tick) if len(pending_events) == 0: # We have processed all the event of current tick, lets go for next tick. break # Insert snapshot before each action. self._business_engine.frame.take_snapshot(self.frame_index) # Append source event id to decision events, to support sequential action in joint mode. decision_events = [event.payload for event in pending_events] decision_events = decision_events[0] if self._decision_mode == DecisionMode.Sequential \ else decision_events # Yield current state first, and waiting for action. actions = yield self._business_engine.get_metrics( ), decision_events, False # archive decision events. self._decision_events.append(decision_events) if actions is None: # Make business engine easy to work. actions = [] elif not isinstance(actions, Iterable): actions = [actions] if self._decision_mode == DecisionMode.Sequential: # Generate a new atom event first. action_event = self._event_buffer.gen_action_event( self._tick, actions) # NOTE: decision event always be a CascadeEvent # We just append the action into sub event of first pending cascade event. event = pending_events[0] assert isinstance(event, CascadeEvent) event.state = EventState.EXECUTING event.add_immediate_event(action_event, is_head=True) else: # For joint mode, we will assign actions from beginning to end. # Then mark others pending events to finished if not sequential action mode. for i, pending_event in enumerate(pending_events): if i >= len(actions): if self._decision_mode == DecisionMode.Joint: # Ignore following pending events that have no action matched. pending_event.state = EventState.FINISHED else: # Set the state as executing, so event buffer will not pop them again. # Then insert the action to it. action = actions[i] pending_event.state = EventState.EXECUTING action_event = self._event_buffer.gen_action_event( self._tick, action) assert isinstance(pending_event, CascadeEvent) pending_event.add_immediate_event(action_event, is_head=True) # Check the end tick of the simulation to decide if we should end the simulation. is_end_tick = self._business_engine.post_step(self._tick) if is_end_tick: break self._tick += 1 # Make sure we have no missing data. if (self._tick + 1) % self._snapshot_resolution != 0: self._business_engine.frame.take_snapshot(self.frame_index) # The end. yield self._business_engine.get_metrics(), None, True
from maro.streamit import streamit with streamit: # experiment name with open(os.path.join(args.dir, "config.yml"), "r") as fp: config = yaml.safe_load(fp) # streamit.info(args.scenario, args.topology, args.durations, args.episodes) streamit.complex("config", config) for episode in range(args.episodes): epoch_folder = f"epoch_{episode}" epoch_full_path = os.path.join(args.ssdir, epoch_folder) # ensure epoch folder exist if os.path.exists(epoch_full_path): streamit.episode(episode) # import for each category port_number = import_port_details(streamit, epoch_full_path) vessel_number = import_vessel_details(streamit, epoch_full_path) import_metrics(streamit, epoch_full_path, port_number, vessel_number) # NOTE: we only have one attention file for now, so hard coded here streamit.episode(0) import_attention(streamit, os.path.join(args.dir, "atts_1"))
""" # opts['enable-dump-snapshot'] = '' # Initialize an environment with a specific scenario, related topology. env = Env(scenario="cim", topology="global_trade.22p_l0.1", start_tick=start_tick, durations=durations, options=opts) # Query environment summary, which includes business instances, intra-instance attributes, etc. print(env.summary) for ep in range(2): # Tell streamit we are in a new episode. streamit.episode(ep) # Gym-like step function. metrics, decision_event, is_done = env.step(None) while not is_done: past_week_ticks = [ x for x in range(max(decision_event.tick - 7, 0), decision_event.tick) ] decision_port_idx = decision_event.port_idx intr_port_infos = ["booking", "empty", "shortage"] # Query the decision port booking, empty container inventory, shortage information in the past week past_week_info = env.snapshot_list["ports"][ past_week_ticks:decision_port_idx:intr_port_infos]