Beispiel #1
0
 def __init__(self, name, agent, all_locations):
     """
     Creates a new reward feature.
     :param Agent agent: the PsychSim agent capable of retrieving the feature's value given a state.
     :param str name: the label for this reward feature.
     :param list[str] all_locations: all the world's locations.
     """
     super().__init__(name)
     self.agent = agent
     self.world = agent.world
     self.all_locations = all_locations
     self.location_feat = get_location_key(agent)
Beispiel #2
0
 def __init__(self, name, agent, all_locations, inverse=True, max_frequency=1):
     """
     Creates a new reward feature.
     :param Agent agent: the PsychSim agent capable of retrieving the feature's value given a state.
     :param str name: the label for this reward feature.
     :param list[str] all_locations: all the world's locations.
     :param bool inverse: whether to take the inverse frequency, i.e., `time - freq`.
     :param int max_frequency: the maximum frequency that the agent can achieve (either for any or all locations).
     """
     super().__init__(name, 1. / max_frequency)  # use max frequency as normalization factor to keep var in [0,1]
     self.agent = agent
     self.world = agent.world
     self.inverse = inverse
     self.all_locations = all_locations
     self.location_feat = get_location_key(agent)
     self.time_feat = get_mission_seconds_key()
Beispiel #3
0
def plot_trajectories(agents,
                      trajectories,
                      locations,
                      neighbors,
                      output_img,
                      coordinates=None,
                      state=None,
                      title='Trajectories',
                      draw_victims=True,
                      show=False):
    """
    Plots the given set of trajectories over a graph representation of the environment.
    :param Agent or list[Agent] agents: the agent or agents (one per trajectory) whose trajectories we want to plot.
    :param list[list[tuple[World, Distribution]]] trajectories: the set of trajectories to save, containing
    several sequences of state-action pairs.
    :param list[str] locations: the list of possible world locations.
    :param dict[str,dict[int, str]] neighbors: the locations' neighbors in each direction.
    :param str output_img: the path to the file in which to save the plot.
    :param dict[str, tuple[float,float]] coordinates: the coordinates of each world location.
    :param VectorDistributionSet state: the state used to fetch the graph information to be plotted.
    :param str title: the plot's title.
    :param bool draw_victims: whether to include the number of victims of each color in each location of the graph.
    :param bool show: whether to show the plot to the screen.
    :return:
    """
    multiagent = True
    if isinstance(agents, Agent):
        multiagent = False
        agents = [agents] * len(trajectories)

    # get base world plot
    g, g_plot = _plot(agents[0].world, locations, neighbors, output_img,
                      coordinates, state, title, draw_victims)

    if len(trajectories) == 0 or len(trajectories[0]) == 0:
        return

    # draw each trajectory
    t_colors = distinct_colors(len(trajectories))
    for i, trajectory in enumerate(trajectories):
        loc_feat = get_location_key(agents[i])
        state = copy.deepcopy(trajectory[0][0].state)
        state.select(True)  # select most likely state
        source = trajectory[0][0].getFeature(loc_feat, state, True)
        for t in range(1, len(trajectory)):
            state = copy.deepcopy(trajectory[t][0].state)
            state.select(True)
            target = trajectory[t][0].getFeature(loc_feat, state, True)
            label = (agents[i].name if multiagent else
                     'T{:02d}'.format(i)) if t == len(trajectory) - 1 else None
            g.add_edge(source,
                       target,
                       color=color_to_html_format(t_colors[i]),
                       arrow_size=EDGE_ARROW_SIZE,
                       arrow_width=EDGE_ARROW_WIDTH,
                       label_size=EDGE_LABEL_SIZE,
                       label_color=EDGE_LABEL_COLOR,
                       width=TRAJ_EDGE_WIDTH,
                       label=label)
            source = target

    g_plot.redraw()
    _draw_title(title, g_plot.surface, g_plot.width, g_plot.height)
    _save_plot(g_plot, output_img)
    if show:
        g_plot.show()