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) gurobi_model.setParam('Threads', 2) input = Experiment.generate_input_region(gurobi_model, template, x, self.env_input_size) max_theta, min_theta, max_theta_dot, min_theta_dot = self.get_theta_bounds( gurobi_model, input) sin_cos_table = self.get_sin_cos_table(max_theta, min_theta, max_theta_dot, min_theta_dot, action=chosen_action) feasible_action = CartpoleExperiment.generate_nn_guard( gurobi_model, input, nn, action_ego=chosen_action) if feasible_action: thetaacc, xacc = CartpoleExperiment.generate_angle_milp( gurobi_model, input, sin_cos_table) # apply dynamic x_prime = self.apply_dynamic( input, gurobi_model, thetaacc=thetaacc, xacc=xacc, 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
def post_milp(self, x, nn, output_flag, t, template): """milp method""" post = [] for chosen_action in range(2): observable_template = self.observable_templates[chosen_action] observable_result = self.observable_results[chosen_action] gurobi_model = grb.Model() gurobi_model.setParam('OutputFlag', output_flag) gurobi_model.setParam('Threads', 2) input = Experiment.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] <= input[2] - input[3] + self.input_epsilon / 2, name=f"obs_constr11") gurobi_model.addConstr( observation[0] >= input[2] - input[3] - 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) Experiment.generate_region_constraints(gurobi_model, observable_template, observation, observable_result, 2) gurobi_model.optimize() feasible_action = gurobi_model.status == 2 if feasible_action: # apply dynamic # x_prime_results = self.optimise(template, gurobi_model, input) # h representation # x_prime = Experiment.generate_input_region(gurobi_model, template, x_prime_results, self.env_input_size) x_second = 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_second_results = self.h_repr_to_plot( gurobi_model, template, x_second) if found_successor: post.append(tuple(x_second_results)) return post
def post_milp(self, x, nn, output_flag, t, template): """milp method""" post = [] for chosen_action in range(2): observable_template = self.observable_templates[chosen_action] observable_result = self.observable_results[chosen_action] if USE_GUROBI: gurobi_model = grb.Model() gurobi_model.setParam('OutputFlag', output_flag) gurobi_model.setParam('Threads', 2) input = Experiment.generate_input_region( gurobi_model, template, x, self.env_input_size) Experiment.generate_region_constraints( gurobi_model, observable_template, input, observable_result, env_input_size=self.env_input_size) gurobi_model.optimize() feasible_action = gurobi_model.status == 2 if feasible_action: max_theta, min_theta, max_theta_dot, min_theta_dot = self.get_theta_bounds( gurobi_model, input) sin_cos_table = self.get_sin_cos_table( max_theta, min_theta, max_theta_dot, min_theta_dot, action=chosen_action, step_thetaacc=100) x_prime_results = self.optimise(template, gurobi_model, input) # h representation x_prime = Experiment.generate_input_region( gurobi_model, template, x_prime_results, self.env_input_size) thetaacc, xacc = CartpoleExperiment.generate_angle_milp( gurobi_model, x_prime, sin_cos_table) # apply dynamic x_second = self.apply_dynamic( x_prime, gurobi_model, thetaacc=thetaacc, xacc=xacc, env_input_size=self.env_input_size) gurobi_model.update() gurobi_model.optimize() found_successor, x_second_results = self.h_repr_to_plot( gurobi_model, template, x_second) if found_successor: post.append(tuple(x_second_results)) else: model = pyo.ConcreteModel() input = Experiment.generate_input_region_pyo( model, template, x, self.env_input_size) feasible_action = ORACartpoleExperiment.generate_nn_guard_pyo( model, input, nn, action_ego=chosen_action, M=1e04) if feasible_action: # performs action 2 automatically when battery is dead max_theta, min_theta, max_theta_dot, min_theta_dot = self.get_theta_bounds_pyo( model, input) sin_cos_table = self.get_sin_cos_table( max_theta, min_theta, max_theta_dot, min_theta_dot, action=chosen_action, step_thetaacc=100) x_prime_results = self.optimise_pyo(template, model, input) x_prime = Experiment.generate_input_region_pyo( model, template, x_prime_results, self.env_input_size, name="x_prime_input") thetaacc, xacc = ORACartpoleExperiment.generate_angle_milp_pyo( model, x_prime, sin_cos_table) model.del_component(model.obj) model.obj = pyo.Objective(expr=thetaacc, sense=pyo.maximize) result = Experiment.solve(model, solver=Experiment.use_solver) assert (result.solver.status == SolverStatus.ok) and ( result.solver.termination_condition == TerminationCondition.optimal ), f"LP wasn't optimally solved {x}" # apply dynamic x_second = self.apply_dynamic_pyo( x_prime, model, thetaacc=thetaacc, xacc=xacc, env_input_size=self.env_input_size, action=chosen_action) x_second_results = self.optimise_pyo( template, model, x_second) found_successor = x_prime_results is not None if found_successor: post.append((tuple(x_second_results))) return post