Пример #1
0
 def generate_nn_polyhedral_guard(self, nn, chosen_action, output_flag):
     gurobi_model = grb.Model()
     gurobi_model.setParam('OutputFlag', output_flag)
     gurobi_model.setParam('Threads', 2)
     observation = gurobi_model.addMVar(shape=(2, ),
                                        lb=float("-inf"),
                                        ub=float("inf"),
                                        name="observation")
     Experiment.generate_nn_guard(gurobi_model,
                                  observation,
                                  nn,
                                  action_ego=chosen_action)
     observable_template = Experiment.octagon(2)
     # self.env_input_size = 2
     observable_result = optimise(observable_template, gurobi_model,
                                  observation)
     # self.env_input_size = 6
     return observable_template, observable_result
Пример #2
0
 def post_milp(self, x, x_label, nn, output_flag, t, template):
     """milp method"""
     post = []
     for chosen_action in range(2):
         gurobi_model = grb.Model()
         gurobi_model.setParam('OutputFlag', output_flag)
         input = generate_input_region(gurobi_model, template, x,
                                       self.env_input_size)
         # gurobi_model.addConstr(input[0] >= 0, name=f"input_base_constr1")
         # gurobi_model.addConstr(input[1] >= 0, name=f"input_base_constr2")
         # gurobi_model.addConstr(input[2] >= 20, name=f"input_base_constr3")
         observation = gurobi_model.addMVar(shape=(2, ),
                                            lb=float("-inf"),
                                            ub=float("inf"),
                                            name="observation")
         gurobi_model.addConstr(
             observation[1] <= input[0] - input[1] + self.input_epsilon / 2,
             name=f"obs_constr21")
         gurobi_model.addConstr(
             observation[1] >= input[0] - input[1] - self.input_epsilon / 2,
             name=f"obs_constr22")
         gurobi_model.addConstr(observation[0] <= self.v_lead - input[2] +
                                self.input_epsilon / 2,
                                name=f"obs_constr11")
         gurobi_model.addConstr(observation[0] >= self.v_lead - input[2] -
                                self.input_epsilon / 2,
                                name=f"obs_constr12")
         # gurobi_model.addConstr(input[3] <= self.max_speed, name=f"v_constr_input")
         # gurobi_model.addConstr(input[3] >= -self.max_speed, name=f"v_constr_input")
         feasible_action = Experiment.generate_nn_guard(
             gurobi_model, observation, nn, action_ego=chosen_action)
         # feasible_action = Experiment.generate_nn_guard(gurobi_model, input, nn, action_ego=chosen_action)
         if feasible_action:
             # apply dynamic
             x_prime = StoppingCarExperiment.apply_dynamic(
                 input,
                 gurobi_model,
                 action=chosen_action,
                 env_input_size=self.env_input_size)
             gurobi_model.update()
             gurobi_model.optimize()
             found_successor, x_prime_results = self.h_repr_to_plot(
                 gurobi_model, template, x_prime)
             if found_successor:
                 # post.append((tuple(x_prime_results),(x, x_label)))
                 successor_info = Experiment.SuccessorInfo()
                 successor_info.successor = tuple(x_prime_results)
                 successor_info.parent = x
                 successor_info.parent_lbl = x_label
                 successor_info.t = t + 1
                 successor_info.action = "policy"  # chosen_action
                 # successor_info.lb = ranges_probs[chosen_action][0]
                 # successor_info.ub = ranges_probs[chosen_action][1]
                 post.append(successor_info)
     return post
Пример #3
0
 def post_milp(self, x, x_label, nn, output_flag, t,
               template) -> List[Experiment.SuccessorInfo]:
     """milp method"""
     ranges_probs = self.create_range_bounds_model(template, x,
                                                   self.env_input_size, nn)
     post = []
     for chosen_action in range(2):
         gurobi_model = grb.Model()
         gurobi_model.setParam('OutputFlag', output_flag)
         gurobi_model.setParam('DualReductions', 0)
         input = generate_input_region(gurobi_model, template, x,
                                       self.env_input_size)
         observation = gurobi_model.addMVar(shape=(2, ),
                                            lb=float("-inf"),
                                            ub=float("inf"),
                                            name="input")
         gurobi_model.addConstr(
             observation[1] <= input[0] - input[1] + self.input_epsilon / 2,
             name=f"obs_constr21")
         gurobi_model.addConstr(
             observation[1] >= input[0] - input[1] - self.input_epsilon / 2,
             name=f"obs_constr22")
         gurobi_model.addConstr(observation[0] <= self.v_lead - input[2] +
                                self.input_epsilon / 2,
                                name=f"obs_constr11")
         gurobi_model.addConstr(observation[0] >= self.v_lead - input[2] -
                                self.input_epsilon / 2,
                                name=f"obs_constr12")
         feasible_action = Experiment.generate_nn_guard(
             gurobi_model, observation, nn, action_ego=chosen_action)
         if feasible_action:
             x_prime = StoppingCarExperiment.apply_dynamic(
                 input,
                 gurobi_model,
                 action=chosen_action,
                 env_input_size=self.env_input_size)
             gurobi_model.update()
             gurobi_model.optimize()
             found_successor, x_prime_results = self.h_repr_to_plot(
                 gurobi_model, template, x_prime)
             if found_successor:
                 successor_info = Experiment.SuccessorInfo()
                 successor_info.successor = tuple(x_prime_results)
                 successor_info.parent = x
                 successor_info.parent_lbl = x_label
                 successor_info.t = t + 1
                 successor_info.action = "policy"  # chosen_action
                 successor_info.lb = ranges_probs[chosen_action][0]
                 successor_info.ub = ranges_probs[chosen_action][1]
                 post.append(successor_info)
     return post
Пример #4
0
 def post_milp(self, x, nn, output_flag, t, template):
     """milp method"""
     post = []
     for chosen_action in range(2):
         gurobi_model = grb.Model()
         gurobi_model.setParam('OutputFlag', output_flag)
         input = generate_input_region(gurobi_model, template, x,
                                       self.env_input_size)
         observation = gurobi_model.addMVar(shape=(2, ),
                                            lb=float("-inf"),
                                            ub=float("inf"),
                                            name="input")
         gurobi_model.addConstr(
             observation[1] <= input[0] - input[1] + self.input_epsilon / 2,
             name=f"obs_constr21")
         gurobi_model.addConstr(
             observation[1] >= input[0] - input[1] - self.input_epsilon / 2,
             name=f"obs_constr22")
         gurobi_model.addConstr(observation[0] <= self.v_lead - input[2] +
                                self.input_epsilon / 2,
                                name=f"obs_constr11")
         gurobi_model.addConstr(observation[0] >= self.v_lead - input[2] -
                                self.input_epsilon / 2,
                                name=f"obs_constr12")
         feasible_action = Experiment.generate_nn_guard(
             gurobi_model, observation, nn, action_ego=chosen_action)
         # feasible_action = Experiment.generate_nn_guard(gurobi_model, input, nn, action_ego=chosen_action)
         if feasible_action:
             # apply dynamic
             x_prime = StoppingCarExperiment2.apply_dynamic(
                 input,
                 gurobi_model,
                 action=chosen_action,
                 env_input_size=self.env_input_size)
             gurobi_model.update()
             gurobi_model.optimize()
             found_successor, x_prime_results = self.h_repr_to_plot(
                 gurobi_model, template, x_prime)
             if found_successor:
                 post.append(tuple(x_prime_results))
     return post