def plot(environment_name, storage: prism.state_storage.StateStorage, *, plot_type="lb", folder_path, file_name_save: str = None): state_size = len(storage.root[0]) # states = unroll_methods.get_n_states(storage,horizon) # shortest_path_abstract = nx.shortest_path(storage.graph, source=storage.root) # layers = unroll_methods.get_layers(storage.graph,storage.root) save_path = f"{folder_path}/{file_name_save}_{plot_type}.svg" if file_name_save is not None else None if plot_type == "lb": results = [(x, prob) for (x, action), prob in unroll_methods.get_property_at_timestep(storage, 1, ["lb"])] # if state_size > 2: # results = [(x, prob) for x, prob in results if x[3][0] <= 0 <= x[3][1] and x[2][0] <= 0 <= x[2][1]] utils.show_heatmap(results, save_to=save_path, rounding=2) # , title="Heatmap of the lower bound measured at the initial state" elif plot_type == "ub": results = [(x, prob) for (x, action), prob in unroll_methods.get_property_at_timestep(storage, 1, ["ub"])] if state_size > 2: results = [(x, prob) for x, prob in results if x[3][0] <= 0 <= x[3][1] and x[2][0] <= 0 <= x[2][1]] utils.show_heatmap(results, save_to=save_path, rounding=2) # , title="Heatmap of the upper bound measured at the initial state" elif plot_type == "error": results = [(x, ub - lb) for (x, action), lb, ub in unroll_methods.get_property_at_timestep(storage, 1, ["lb", "ub"])] # difference between boundaries # if state_size > 2: # results = [(x, prob) for x, prob in results if x[3][0] <= 0 <= x[3][1] and x[2][0] <= 0 <= x[2][1]] utils.show_heatmap(results, save_to=save_path, rounding=2) # title="Heatmap of the probability error measured at the initial state" elif plot_type == "safe_unsafe": results = [(x, lb, ub) for (x, action), lb, ub in unroll_methods.get_property_at_timestep(storage, 1, ["lb", "ub"])] safe = [] unsafe = [] undecidable = [] for x, lb, ub in results: if lb >= 0.8: unsafe.append(x) elif ub <= 0.2: safe.append(x) else: undecidable.append(x) utils.show_plot(safe, unsafe, undecidable, legend=["Safe", "Unsafe", "Undecidable"]) elif plot_type == "p_chart": results = [(x, prob) for (x, action), prob in unroll_methods.get_property_at_timestep(storage, 1, ["ub"])] utils.p_chart(results, save_to=save_path, rounding=2) # , title="Barchart of volumes in the initial state grouped by the upper bound probability " elif plot_type == "pca": results = [(x, utils.centre_tuple(x), utils.area_tuple(x), action, prob) for (x, action), prob in unroll_methods.get_property_at_timestep(storage, 1, ["ub"])] pca_map(results, save_path, state_size) else: raise Exception("Option for plot_type not recognised")
env_class, n_workers, explorer, verification_model, state_size, horizon=horizon, allow_assign_actions=True) # if time.time() - time_from_last_save >= 60 * 5: # storage.save_state(f"/home/edoardo/Development/SafeDRL/save/nx_graph_e{rounding}.p") # rtree.save_to_file(f"/home/edoardo/Development/SafeDRL/save/union_states_total_e{rounding}.p") # print("Graph Saved - Checkpoint") # time_from_last_save = time.time() if not split_performed or iterations == 1000: # utils.save_graph_as_dot(storage.graph) if not split_performed: print("No more split performed") break iterations += 1 # %% # storage.save_state(f"/home/edoardo/Development/SafeDRL/save/nx_graph_e{rounding}.p") # rtree.save_to_file(f"/home/edoardo/Development/SafeDRL/save/union_states_total_e{rounding}.p") # %% # unroll_methods.remove_spurious_nodes(storage.graph) # storage.remove_unreachable() # storage.recreate_prism() # utils.save_graph_as_dot(storage.graph) utils.show_heatmap([ (x, prob) for (x, action), prob in unroll_methods.get_property_at_timestep( storage, 1, ["lb"]) ])
state = np.array((params['param1'], params['param2'])) current_intervals.append(torch.tensor(state)) tensor_intervals = torch.stack(current_intervals) # ix = Symbolic_interval(lower=torch.tensor([[0, 0]], dtype=torch.float64, requires_grad=False), upper=torch.tensor([[0.01, 0.01]], dtype=torch.float64, requires_grad=False)) ix2 = Symbolic_interval(lower=tensor_intervals, upper=tensor_intervals + np.array([delta_x, delta_y])) # verif.get_boundaries(ix, 0) inet = Interval_network(sequential_nn.double(), None) result_interval = inet(ix2) upper_bound = result_interval.u lower_bound = result_interval.l upper_bound_rectangles = [] for x, y in zip(tensor_intervals, upper_bound[:, 0]): x_numpy = x.numpy() x_numpy = np.stack([x_numpy, x_numpy + np.array([delta_x, delta_y])]) from_numpy = HyperRectangle.from_numpy(x_numpy) upper_bound_rectangles.append((from_numpy, y.item())) lower_bound_rectangles = [] for x, y in zip(tensor_intervals, lower_bound[:, 0]): x_numpy = x.numpy() x_numpy = np.stack([x_numpy, x_numpy + np.array([delta_x, delta_y])]) from_numpy = HyperRectangle.from_numpy(x_numpy) lower_bound_rectangles.append((from_numpy, y.item())) utils.show_heatmap(upper_bound_rectangles, title="Upper bound probability of action=0", rounding=3) utils.show_heatmap(lower_bound_rectangles, title="Lower bound probability of action=0", rounding=3) print("Finished")
next_state2, reward2, done2, _ = env.step(action) next_state = next_state.round(rounding) next_state2 = next_state2.round(rounding) new_states.append(next_state) new_states.append(next_state2) storage.store_sticky_successors(tuple(next_state), tuple(next_state2), tuple(current_interval)) if done: storage.mark_as_fail([tuple(next_state)]) if done2: storage.mark_as_fail([tuple(next_state2)]) current_intervals = new_states print("Finished") storage.save_state( f"/home/edoardo/Development/SafeDRL/save/nx_graph_concrete_e{rounding}.p" ) storage.recreate_prism(horizon * 2) storage.save_state( f"/home/edoardo/Development/SafeDRL/save/nx_graph_concrete_e{rounding}.p" ) # %% # utils.show_heatmap(unroll_methods.get_property_at_timestep(storage, 1, ["lb"]), rounding=2, concrete=True) storage.recreate_prism(horizon * 2) utils.show_heatmap(unroll_methods.get_property_at_timestep(storage, 1, ["ub"]), rounding=3, concrete=True, title=f"MC concrete horizon:{horizon}") # utils.show_heatmap(unroll_methods.get_property_at_timestep(storage, 1, ["lb"]), rounding=3, concrete=True, title=f"LB concrete horizon:{horizon}")
rtree.save_to_file( f"/home/edoardo/Development/SafeDRL/save/union_states_total_e{rounding}.p" ) print("Graph Saved - Checkpoint") time_from_last_save = time.time() if not split_performed or iterations == 50: # utils.save_graph_as_dot(storage.graph) if not split_performed: print("No more split performed") break iterations += 1 # %% if allow_save: storage.save_state( f"/home/edoardo/Development/SafeDRL/save/nx_graph_e{rounding}.p") rtree.save_to_file( f"/home/edoardo/Development/SafeDRL/save/union_states_total_e{rounding}.p" ) # %% # unroll_methods.remove_spurious_nodes(storage.graph) # storage.remove_unreachable() # storage.recreate_prism() # utils.save_graph_as_dot(storage.graph) storage.recreate_prism(horizon * 2) utils.show_heatmap(unroll_methods.get_property_at_timestep(storage, 1, ["lb"]), rounding=2, title=f"LB abstract horizon:{horizon}") utils.show_heatmap(unroll_methods.get_property_at_timestep(storage, 1, ["ub"]), rounding=2, title=f"UB abstract horizon:{horizon}")