Beispiel #1
0
def action_distrubtion(episode, ref_episode):

    actions_per_sub = actions_model.get_action_per_sub(episode)
    actions_per_sub.append(actions_model.get_action_per_sub(ref_episode)[0])
    y_max = None

    if len(actions_per_sub[0]["y"]) == 0:
        figure_subs = go.Figure(
            layout=layout_no_data("No Actions on subs for this Agent"))
    else:
        figure_subs = go.Figure(layout=layout_def, data=actions_per_sub)
        y_max = max(map(max_or_zero, [trace.y
                                      for trace in actions_per_sub])) + 1

    actions_per_lines = actions_model.get_action_per_line(episode)
    actions_per_lines.append(actions_model.get_action_per_line(ref_episode)[0])

    if len(actions_per_lines[0]["y"]) == 0:
        figure_lines = go.Figure(
            layout=layout_no_data("No Actions on lines for this Agent"))
    else:
        figure_lines = go.Figure(layout=layout_def, data=actions_per_lines)
        if y_max is None:
            y_max = max(
                map(max_or_zero, [trace.y for trace in actions_per_lines])) + 1
        if max(map(max_or_zero, [trace.y
                                 for trace in actions_per_lines])) > y_max:
            y_max = max(
                map(max_or_zero, [trace.y for trace in actions_per_lines])) + 1

    actions_redisp = actions_model.get_action_redispatch(episode)
    actions_redisp.append(actions_model.get_action_redispatch(ref_episode)[0])

    if len(actions_redisp[0]["y"]) == 0:
        figure_redisp = go.Figure(
            layout=layout_no_data("No redispatching actions for this Agent"))
    else:
        figure_redisp = go.Figure(layout=layout_def, data=actions_redisp)
        if y_max is None:
            y_max = max(map(max_or_zero, [trace.y
                                          for trace in actions_redisp])) + 1
        if max(map(max_or_zero, [trace.y
                                 for trace in actions_redisp])) > y_max:
            y_max = max(map(max_or_zero, [trace.y
                                          for trace in actions_redisp])) + 1

    if y_max:
        figure_subs.update_yaxes(range=[0, y_max])
        figure_lines.update_yaxes(range=[0, y_max])
        figure_redisp.update_yaxes(range=[0, y_max])

    return ActionsDistribution(on_subs=figure_subs,
                               on_lines=figure_lines,
                               redisp=figure_redisp)
Beispiel #2
0
def update_layout(predicate, msg):
    if predicate:
        figure_layout = layout_no_data(msg)
    else:
        figure_layout = layout_def

    return figure_layout
Beispiel #3
0
def action_distrubtion(episode):

    actions_per_sub = actions_model.get_action_per_sub(episode)

    if len(actions_per_sub[0]["y"]) == 0:
        figure_subs = go.Figure(
            layout=layout_no_data("No Actions on subs for this Agent"))
    else:
        figure_subs = go.Figure(layout=layout_def, data=actions_per_sub)

    actions_per_lines = actions_model.get_action_per_line(episode)

    if len(actions_per_lines[0]["y"]) == 0:
        figure_lines = go.Figure(
            layout=layout_no_data("No Actions on lines for this Agent"))
    else:
        figure_lines = go.Figure(layout=layout_def, data=actions_per_lines)
    return ActionsDistribution(on_subs=figure_subs, on_lines=figure_lines)
Beispiel #4
0
    def update_interactive_graph(slider_value, active_tab, agent_study,
                                 scenario, agent_ref):
        episode = make_episode(
            agent_study if active_tab == "tab-0" else agent_ref, scenario)

        try:
            act = episode.actions[slider_value]
        except Grid2OpException as ex:
            return (
                dcc.Graph(figure=go.Figure(layout=layout_no_data(
                    "The agent is game over at this time step."))),
                "",
            )
        if any(act.get_types()):
            act_as_str = str(act)
        else:
            act_as_str = "NO ACTION"
        return (
            dcc.Graph(figure=make_network_agent_study(episode,
                                                      timestep=slider_value)),
            act_as_str,
        )
Beispiel #5
0
def indicator_line(scenario, study_agent, ref_agent):
    episode = make_episode(study_agent, scenario)
    ref_episode = make_episode(ref_agent, scenario)
    figures_distribution = action_distrubtion(episode, ref_episode)

    modified_lines = actions_model.get_modified_lines(episode)
    line_values = [None] * episode.n_lines
    for line in modified_lines.index:
        line_values[np.where(episode.line_names == line)[0][0]] = line
    network_graph = make_network(episode).plot_info(
        observation=episode.observations[0],
        line_values=line_values,
    )
    nb_actions = episode.action_data_table[[
        'action_line', 'action_subs', 'action_redisp'
    ]].sum()

    if nb_actions.sum() == 0:
        pie_figure = go.Figure(
            layout=layout_no_data("No Actions for this Agent"))
    else:
        pie_figure = go.Figure(layout=layout_def,
                               data=[
                                   go.Pie(labels=[
                                       "Actions on Lines",
                                       "Actions on Substations",
                                       "Redispatching Actions"
                                   ],
                                          values=[
                                              nb_actions["action_line"],
                                              nb_actions["action_subs"],
                                              nb_actions["action_redisp"]
                                          ])
                               ])

    return html.Div(
        className="lineBlock card",
        children=[
            html.H4("Indicators"),
            html.Div(
                className="card-body row",
                children=[
                    html.Div(
                        className="col-2",
                        children=[
                            html.H5("Agent to study Selection"),
                            dcc.Dropdown(id='agent_log_selector',
                                         options=[{
                                             'label': agent,
                                             'value': agent
                                         } for agent in agents],
                                         value=study_agent,
                                         placeholder="Agent log"),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.
                                    P(id="indicator_score_output",
                                      className=
                                      "border-bottom h3 mb-0 text-right",
                                      children=round(
                                          episode.meta["cumulative_reward"])),
                                    html.P(className="text-muted",
                                           children="Score")
                                ]),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(
                                        id="indicator_nb_maintenances",
                                        className=
                                        "border-bottom h3 mb-0 text-right",
                                        children='{}/{}'.format(
                                            episode.meta['nb_timestep_played'],
                                            episode.
                                            meta['chronics_max_timestep'])),
                                    html.P(className="text-muted",
                                           children="Agent's Survival")
                                ]),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(id="indicator_nb_overflow",
                                           className=
                                           "border-bottom h3 mb-0 text-right",
                                           children=episode.
                                           total_overflow_ts["value"].sum()),
                                    html.P(className="text-muted",
                                           children="Number of Overflow")
                                ]),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(id="indicator_nb_action",
                                           className=
                                           "border-bottom h3 mb-0 text-right",
                                           children=episode.action_data_table[[
                                               'action_line', 'action_subs'
                                           ]].sum(axis=1).sum()),
                                    html.P(className="text-muted ",
                                           children="Number of Action")
                                ]),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(id="indicator_nb_maintenances",
                                           className=
                                           "border-bottom h3 mb-0 text-right",
                                           children=episode.nb_maintenances),
                                    html.P(className="text-muted",
                                           children="Number of Maintenances")
                                ]),
                        ]),
                    html.Div(className="col-3",
                             children=[
                                 html.H6(className="text-center",
                                         children="Type Action Repartition"),
                                 dcc.Graph(id="agent_study_pie_chart",
                                           figure=pie_figure)
                             ]),
                    html.Div(className="col-7",
                             children=[
                                 html.H6(
                                     className="text-center",
                                     children="//WIP// Impacted grid assets"),
                                 dcc.Graph(id="network_actions",
                                           figure=network_graph)
                             ]),
                    html.Div(className="col-12",
                             children=[
                                 html.H2(className="text-center",
                                         children="Actions Distributions")
                             ]),
                    html.Div(className="col-4",
                             children=[
                                 html.H6(className="text-center",
                                         children="On Substations"),
                                 dcc.Graph(
                                     id="distribution_substation_action_chart",
                                     figure=figures_distribution.on_subs)
                             ]),
                    html.Div(className="col-4",
                             children=[
                                 html.H6(className="text-center",
                                         children="On Lines"),
                                 dcc.Graph(
                                     id="distribution_line_action_chart",
                                     figure=figures_distribution.on_lines)
                             ]),
                    html.Div(className="col-4",
                             children=[
                                 html.H6(className="text-center",
                                         children="Redispatching"),
                                 dcc.Graph(
                                     id="distribution_redisp_action_chart",
                                     figure=figures_distribution.redisp)
                             ]),
                ]),
        ])
Beispiel #6
0
def indicator_line(scenario, study_agent, ref_agent):
    episode = make_episode(study_agent, scenario)
    ref_episode = make_episode(ref_agent, scenario)
    figures_distribution = action_distrubtion(episode, ref_episode)

    network_graph = make_network_agent_overview(episode)

    nb_actions = episode.action_data_table[[
        "action_line", "action_subs", "action_redisp"
    ]].sum()

    if nb_actions.sum() == 0:
        pie_figure = go.Figure(
            layout=layout_no_data("No Actions for this Agent"))
    else:
        pie_figure = go.Figure(
            layout=layout_def,
            data=[
                go.Pie(
                    labels=[
                        "Actions on Lines",
                        "Actions on Substations",
                        "Redispatching Actions",
                    ],
                    values=[
                        nb_actions["action_line"],
                        nb_actions["action_subs"],
                        nb_actions["action_redisp"],
                    ],
                )
            ],
        )

    return html.Div(
        className="lineBlock card",
        children=[
            html.H4("Indicators"),
            html.Div(
                className="card-body row",
                children=[
                    html.Div(
                        className="col-2",
                        children=[
                            html.H5("Study Agent Summary"),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(
                                        id="indicator_score_output",
                                        className=
                                        "border-bottom h3 mb-0 text-right",
                                        children=round(
                                            episode.meta["cumulative_reward"]),
                                    ),
                                    html.P(className="text-muted",
                                           children="Score"),
                                ],
                            ),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(
                                        id="indicator_survival_time",
                                        className=
                                        "border-bottom h3 mb-0 text-right",
                                        children="{}/{}".format(
                                            episode.meta["nb_timestep_played"],
                                            episode.
                                            meta["chronics_max_timestep"],
                                        ),
                                    ),
                                    html.P(
                                        className="text-muted",
                                        children="Agent's Survival",
                                    ),
                                ],
                            ),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(
                                        id="indicator_nb_overflow",
                                        className=
                                        "border-bottom h3 mb-0 text-right",
                                        children=episode.
                                        total_overflow_ts["value"].sum(),
                                    ),
                                    html.P(
                                        className="text-muted",
                                        children="Number of Overflow",
                                    ),
                                ],
                            ),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(
                                        id="indicator_nb_action",
                                        className=
                                        "border-bottom h3 mb-0 text-right",
                                        children=episode.action_data_table[[
                                            "action_line", "action_subs"
                                        ]].sum(axis=1).sum(),
                                    ),
                                    html.P(
                                        className="text-muted ",
                                        children="Number of Action",
                                    ),
                                ],
                            ),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(
                                        id="indicator_nb_maintenances",
                                        className=
                                        "border-bottom h3 mb-0 text-right",
                                        children=episode.nb_maintenances,
                                    ),
                                    html.P(
                                        className="text-muted",
                                        children="Number of Maintenances",
                                    ),
                                ],
                            ),
                        ],
                    ),
                    html.Div(
                        className="col-3",
                        children=[
                            html.H6(
                                className="text-center",
                                children="Type Action Repartition",
                            ),
                            dcc.Graph(id="agent_study_pie_chart",
                                      figure=pie_figure),
                        ],
                    ),
                    html.Div(
                        className="col-7",
                        children=[
                            html.H6(
                                className="text-center",
                                children=
                                "Impacted grid assets: attacks (dash orange) & overflow (red) and subs with action",
                            ),
                            dcc.Graph(id="network_actions",
                                      figure=network_graph),
                        ],
                    ),
                    html.Div(
                        className="col-12",
                        children=[
                            html.H2(
                                className="text-center",
                                children="Actions Distributions",
                            )
                        ],
                    ),
                    html.Div(
                        className="col-4",
                        children=[
                            html.H6(className="text-center",
                                    children="On Substations"),
                            dcc.Graph(
                                id="distribution_substation_action_chart",
                                figure=figures_distribution.on_subs,
                            ),
                        ],
                    ),
                    html.Div(
                        className="col-4",
                        children=[
                            html.H6(className="text-center",
                                    children="On Lines"),
                            dcc.Graph(
                                id="distribution_line_action_chart",
                                figure=figures_distribution.on_lines,
                            ),
                        ],
                    ),
                    html.Div(
                        className="col-4",
                        children=[
                            html.H6(className="text-center",
                                    children="Redispatching"),
                            dcc.Graph(
                                id="distribution_redisp_action_chart",
                                figure=figures_distribution.redisp,
                            ),
                        ],
                    ),
                ],
            ),
        ],
    )
Beispiel #7
0
def indicator_line(scenario, study_agent):
    episode = make_episode(study_agent, scenario)

    nb_actions = episode.action_data_table[['action_line',
                                            'action_subs']].sum()

    if nb_actions["action_line"] == 0 and nb_actions["action_subs"] == 0:
        pie_figure = go.Figure(
            layout=layout_no_data("No Actions for this Agent"))
    else:
        pie_figure = go.Figure(
            layout=layout_def,
            data=[
                go.Pie(labels=["Actions on Lines", "Actions on Substations"],
                       values=[
                           nb_actions["action_line"], nb_actions["action_subs"]
                       ])
            ])

    maintenances_data = hist_duration_maintenances(episode)
    if not maintenances_data:
        maintenance_figure = go.Figure(
            layout=layout_no_data("No Maintenances for this scenario"))
    else:
        maintenance_figure = go.Figure(
            layout=layout_def, data=[go.Histogram(x=maintenances_data)])
    maintenance_figure["layout"]["xaxis"]["rangemode"] = "tozero"

    return html.Div(
        className="lineBlock card",
        children=[
            html.H4("Indicators"),
            html.Div(
                className="card-body row",
                children=[
                    html.Div(
                        className="col-2",
                        children=[
                            dcc.Dropdown(id='agent_log_selector',
                                         options=[{
                                             'label': agent,
                                             'value': agent
                                         } for agent in agents],
                                         value=study_agent,
                                         placeholder="Agent log"),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.
                                    P(id="indicator_score_output",
                                      className=
                                      "border-bottom h3 mb-0 text-right",
                                      children=round(
                                          episode.meta["cumulative_reward"])),
                                    html.P(className="text-muted",
                                           children="Score")
                                ]),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(id="indicator_nb_overflow",
                                           className=
                                           "border-bottom h3 mb-0 text-right",
                                           children=episode.
                                           total_overflow_ts["value"].sum()),
                                    html.P(className="text-muted",
                                           children="Number of Overflow")
                                ]),
                            html.Div(
                                className="m-2",
                                children=[
                                    html.P(id="indicator_nb_action",
                                           className=
                                           "border-bottom h3 mb-0 text-right",
                                           children=episode.action_data_table[[
                                               'action_line', 'action_subs'
                                           ]].sum(axis=1).sum()),
                                    html.P(className="text-muted ",
                                           children="Number of Action")
                                ])
                        ]),
                    html.Div(className="col-3",
                             children=[
                                 html.H6(className="text-center",
                                         children="Type Action Repartition"),
                                 dcc.Graph(id="agent_study_pie_chart",
                                           figure=pie_figure)
                             ]),
                    html.Div(
                        className="col-7",
                        children=[
                            html.
                            H6(className="text-center",
                               children=
                               "Action Maintenance Duration (on the whole episode)"
                               ),
                            dcc.Graph(id="maintenance_duration",
                                      figure=maintenance_figure)
                        ])
                ]),
        ])