def make_combined_lpi(self, automaton_transition=None, skip_inputs=False): 'create one lpi per guard, which will have both the star and input effects, as well as the guard condition' lpi = LpInstance(self.star.num_dims, self.star.num_dims) lpi.update_basis_matrix(self.star.basis_matrix) for lc in self.star.constraint_list: lpi.add_basis_constraint(lc.vector, lc.value) # add standard basis guard constraints if automaton_transition is not None: for lc in automaton_transition.condition_list: lpi.add_standard_constraint(lc.vector, lc.value) # add any input star constraints mode = self.star.mode if not skip_inputs and self.star.input_stars is not None: for input_star in self.star.input_stars: lpi.add_input_star(mode.u_constraints_a_t, mode.u_constraints_b, input_star.input_basis_matrix) return lpi
def compute_robust_ce_old(self): Timers.tic('Robust counter-example generation time') ce_object = self.compute_longest_ce() robust_points_in_initial_star = [] patch = ce_object.patch[1:len(ce_object.patch) - 1] for node in patch: usafe_lpi = LpInstance(self.num_dims, self.num_dims) usafe_lpi.update_basis_matrix(node.state.basis_matrix) error_std_preds = self.convert_star_pred_in_standard_pred( node.state) for pred in error_std_preds: usafe_lpi.add_standard_constraint(pred.vector, pred.value) for pred in self.usafe_set_constraint_list: usafe_lpi.add_standard_constraint(pred.vector, pred.value) directions = np.identity(self.num_dims, dtype=float) avg_points = [] for index in range(self.num_dims): direction = directions[index] result = np.ones(self.num_dims) is_feasible = usafe_lpi.minimize(direction, result, error_if_infeasible=False) if is_feasible: basis_matrix = np.identity(self.num_dims, dtype=float) point1 = np.dot(basis_matrix, result) result = np.ones(self.num_dims) is_feasible = usafe_lpi.minimize(-1 * direction, result, error_if_infeasible=False) if is_feasible: basis_matrix = np.identity(self.num_dims, dtype=float) point2 = np.dot(basis_matrix, result) avg_point = (point1 + point2) / 2 avg_points.append(avg_point) robust_point_in_star = np.zeros(self.num_dims, dtype=float) for point in avg_points: robust_point_in_star += point robust_point_in_star = robust_point_in_star / len(avg_points) # print "robust point in star {}".format(robust_point_in_star) basis_centers = [] prev_node_state = node.state while True: if isinstance( prev_node_state.parent, InitParent) or isinstance( prev_node_state.parent.star.parent, InitParent): break elif isinstance(prev_node_state.parent.star.parent, DiscretePostParent): basis_centers.append(prev_node_state.parent.star.parent. prestar_basis_center) prev_node_state = prev_node_state.parent.star.parent.prestar error_star_state = node.state.point_to_star_basis( robust_point_in_star) for basis_center in basis_centers[::-1]: error_star_state = error_star_state - basis_center robust_points_in_initial_star.append( np.dot(self.init_star.basis_matrix.T, error_star_state)) robust_point = 0.0 for point in robust_points_in_initial_star: robust_point += point robust_point = robust_point / len(robust_points_in_initial_star) print("Robust point: '{}'".format(robust_point)) Timers.toc('Robust counter-example generation time') return robust_point
def compute_deepest_ce(self, direction): start_node = self.reach_tree.nodes[0] node_queue = [start_node] depth = None deepest_node = None deepest_point = None Timers.tic("Deepest counter-example generation time") while len(node_queue) is not 0: node = node_queue[0] node_queue = node_queue[1:] if node.error is True: usafe_lpi = LpInstance(self.num_dims, self.num_dims) # usafe_lpi.update_basis_matrix(np.identity(self.num_dims)) usafe_lpi.update_basis_matrix(node.state.basis_matrix) error_std_preds = self.convert_star_pred_in_standard_pred( node.state) for pred in error_std_preds: usafe_lpi.add_standard_constraint(pred.vector, pred.value) for pred in self.usafe_set_constraint_list: usafe_lpi.add_standard_constraint(pred.vector, pred.value) # usafe_basis_preds = self.compute_usafe_set_pred_in_star_basis(node.state) # for pred in node.state.constraint_list: # usafe_lpi.add_basis_constraint(pred.vector, pred.value) # for pred in usafe_basis_preds: # usafe_lpi.add_basis_constraint(pred.vector, pred.value) result = np.ones(self.num_dims) is_feasible = usafe_lpi.minimize(-1 * direction, result, error_if_infeasible=False) if is_feasible: # basis_matrix = node.state.basis_matrix basis_matrix = np.identity(self.num_dims, dtype=float) point = np.dot(basis_matrix, result) current_depth = np.dot(direction, point) # print("Current depth {}".format(current_depth)) # print ("depth for the point {} at time {} in loc {} is : {}".format(point, node.state.total_steps, # node.state.mode.name, current_depth)) if depth is None or current_depth >= depth: depth = current_depth deepest_node = node deepest_point = point if node.cont_transition is not None: node_queue.append(node.cont_transition.succ_node) if len(node.disc_transitions) > 0 and node.disc_transitions[ 0].succ_node.cont_transition is not None: node_queue.append(node.disc_transitions[0].succ_node. cont_transition.succ_node) print("deepest point is '{}' in location '{}' with depth '{}'".format( deepest_point, deepest_node.state.mode.name, np.dot(abs(direction), deepest_point))) basis_centers = [] # basis_matrices = [] prev_node_state = deepest_node.state while True: if isinstance(prev_node_state.parent, InitParent) or isinstance( prev_node_state.parent.star.parent, InitParent): break elif isinstance(prev_node_state.parent.star.parent, DiscretePostParent): basis_centers.append( prev_node_state.parent.star.parent.prestar_basis_center) # basis_matrices.append(prev_node_state.parent.star.parent.prestar.basis_matrix) prev_node_state = prev_node_state.parent.star.parent.prestar print("Basis centers: {}".format(basis_centers)) # usafe_lpi = LpInstance(self.num_dims, self.num_dims) # usafe_lpi.update_basis_matrix(self.init_star.basis_matrix) # error_star_state = self.convert_std_state_in_star_state(deepest_point, deepest_node.state) error_star_state = deepest_node.state.point_to_star_basis( deepest_point) # for index in range(len(basis_centers)-1, -1, -1): # print "Index is {}".format(index) # basis_center = basis_centers[index] # basis_matrix = basis_matrices[index] # basis_center_coeffs = np.dot(inv(basis_matrix.T),basis_center) # error_star_state = error_star_state - basis_center_coeffs for basis_center in basis_centers[::-1]: error_star_state = error_star_state - basis_center deepest_ce = np.dot(self.init_star.basis_matrix.T, error_star_state) ce_depth = float(np.dot(abs(direction), deepest_point)) ce_object = CeObject(ce=deepest_ce, ce_depth=ce_depth) print("The deepest ce is '{}' with depth '{}'".format( deepest_ce, ce_depth)) Timers.toc("Deepest counter-example generation time") return ce_object
def compute_deepest_ce(self, depth_direction): compute_intersection = True Timers.tic('Deepest counter-example') points = [] for error_star in self.error_stars: usafe_lpi = LpInstance(self.num_dims, self.num_dims) usafe_lpi.update_basis_matrix(error_star.basis_matrix) for pred in self.usafe_set_constraint_list: usafe_lpi.add_standard_constraint(pred.vector, pred.value) for pred in error_star.constraint_list: usafe_lpi.add_basis_constraint(pred.vector, pred.value) result = np.zeros(self.num_dims) is_feasible = usafe_lpi.minimize(-1 * depth_direction, result, error_if_infeasible=False) if is_feasible: usafe_set_basis_matrix = np.identity(self.num_dims, dtype=float) points.append(np.dot(usafe_set_basis_matrix, result)) if len(points) is 0: print("Result (Deepest counter-example): No solution exists in this direction.") Timers.toc('Deepest counter-example') return # Find the index of the error_star corresponding to the max_depth max_depth = np.dot(depth_direction, points[0]) max_depth_error_star = self.error_stars[0].clone() max_point = points[0] for index in range(len(points)): point = points[index] depth = np.dot(depth_direction, point) if depth > max_depth: max_depth = depth max_depth_error_star = self.error_stars[index].clone() max_point = point print("The deepest point is: '{}' with max_depth '{}'".format(max_point, max_depth)) #print "Max error star: '{}'".format(max_depth_error_star) #max_depth_error_star = self.error_stars[max_depth_index].clone() feasible_point = None if max_depth_error_star.mode.name == self.init_star.mode.name: basis_matrix = max_depth_error_star.parent.star.basis_matrix usafe_lpi = LpInstance(self.num_dims, self.num_dims) usafe_lpi.update_basis_matrix(basis_matrix) usafe_basis_preds = self.compute_point_as_star_basis(max_point, max_depth_error_star) #usafe_basis_preds = self.compute_usafe_basis_pred_in_star_basis(max_depth_error_star, False) for pred in usafe_basis_preds: usafe_lpi.add_basis_constraint(pred.vector, pred.value) for pred in max_depth_error_star.parent.star.constraint_list: usafe_lpi.add_basis_constraint(pred.vector, pred.value) result = np.zeros(self.num_dims) is_feasible = usafe_lpi.minimize(-1 * depth_direction, result, error_if_infeasible=False) if is_feasible: feasible_point = np.dot(basis_matrix, result) print("CounterExample: '{}' with depth '{}' in the given direction".format(feasible_point, max_depth)) Timers.toc('Deepest counter-example') else: basis_center = max_depth_error_star.parent.star.parent.prestar_basis_center usafe_basis_preds = self.compute_usafe_basis_pred_in_star_basis(max_depth_error_star, compute_intersection) #print "Usafe Basis predicates: {}".format(usafe_basis_preds) #usafe_basis_preds = self.compute_point_as_star_basis(max_point, max_depth_error_star) basis_matrix = max_depth_error_star.parent.star.parent.prestar.parent.star.basis_matrix usafe_lpi = LpInstance(self.num_dims, self.num_dims) usafe_lpi.update_basis_matrix(basis_matrix) all_preds = [] for pred in usafe_basis_preds: all_preds.append(self.convert_usafe_basis_pred_in_basis_center(pred, basis_center)) #for pred in max_depth_error_star.parent.star.constraint_list: # all_preds.append(self.convert_usafe_basis_pred_in_basis_center(pred, basis_center)) ## adding constraints from the previous mode initial star (P) for pred in max_depth_error_star.parent.star.parent.prestar.parent.star.constraint_list: all_preds.append(pred.clone()) for pred in all_preds: usafe_lpi.add_basis_constraint(pred.vector, pred.value) result = np.zeros(self.num_dims) is_feasible = usafe_lpi.minimize(-1 * depth_direction, result, error_if_infeasible=False) if is_feasible: feasible_point = np.dot(basis_matrix, result) print("CounterExample: '{}' with depth '{}' in the given direction".format(feasible_point, max_depth)) Timers.toc('Deepest counter-example') return feasible_point