def generate_root_polytope(self):
     gurobi_model = grb.Model()
     gurobi_model.setParam('OutputFlag', self.output_flag)
     input = generate_input_region(gurobi_model, self.input_template, self.input_boundaries, self.env_input_size)
     x_results = optimise(self.analysis_template, gurobi_model, input)
     if x_results is None:
         print("Model unsatisfiable")
         return None
     root = tuple(x_results)
     return root
Exemplo n.º 2
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
Exemplo n.º 3
0
 def check_intersection(self, poly1, poly2, eps=1e-5):
     '''checks if a polytope is inside another (even partially)'''
     gurobi_model = grb.Model()
     gurobi_model.setParam('OutputFlag', self.output_flag)
     input1 = generate_input_region(gurobi_model, self.analysis_template,
                                    poly1, self.env_input_size)
     generate_region_constraints(
         gurobi_model,
         self.analysis_template,
         input1,
         poly2,
         self.env_input_size,
         eps=eps)  # use epsilon to prevent single points
     x_results = optimise(self.analysis_template, gurobi_model, input1)
     if x_results is None:
         # not contained
         return False
     else:
         return True
Exemplo n.º 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)
         gurobi_model.setParam('Threads', 2)
         input = 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:
             x_prime_results = optimise(template, gurobi_model,
                                        input)  # h representation
             x_prime = 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_prime_results = self.h_repr_to_plot(
                 gurobi_model, template, x_second)
             if found_successor:
                 post.append(tuple(x_prime_results))
     return post
Exemplo n.º 5
0
 def additional_seen(self):
     # adds an element that captures all the states where battery is <=0
     gurobi_model = grb.Model()
     gurobi_model.setParam('OutputFlag', self.output_flag)
     additional = [
         self.safe_angle, self.safe_angle,
         float("inf"),
         float("inf"),
         float("inf"),
         float("inf"),
         float("inf"),
         float("inf")
     ]
     input = generate_input_region(gurobi_model,
                                   Experiment.octagon(self.env_input_size),
                                   additional, self.env_input_size)
     x_results = optimise(self.analysis_template, gurobi_model, input)
     if x_results is None:
         print("Model unsatisfiable")
         return None
     root = tuple(x_results)
     return [(root, 0)]
Exemplo n.º 6
0
    def show_sampleplot2d(self):
        '''Generates a plot from the probability of encountering an unsafe state from a sample of points'''
        root = self.generate_root_polytope()

        # convert to intervals
        gurobi_model = grb.Model()
        gurobi_model.setParam('OutputFlag', self.output_flag)
        input = generate_input_region(gurobi_model, self.input_template, self.input_boundaries, self.env_input_size)
        x_results = optimise(self.input_template, gurobi_model, input)
        if x_results is None:
            print("Model unsatisfiable")
            return None
        root2d = tuple(x_results)

        # samples = polytope.sample(1000, self.analysis_template, np.array(root))
        even_samples = self.generate_spaced_samples((-root2d[3], root2d[2]), (-root2d[1], root2d[0]))
        samples = list(even_samples)
        nn = self.get_nn_fn()
        t_max = 200
        n_trajectories = 500
        probabilities = []
        proc_ids = []
        with StandardProgressBar(prefix="Preparing AbstractStepWorkers ", max_value=len(samples)) as bar:
            while len(samples) != 0 or len(proc_ids) != 0:
                while len(proc_ids) < self.n_workers and len(samples) != 0:
                    sample = samples.pop(0) - np.array([28, 0])
                    proc_ids.append(sample_trajectory.remote(sample, nn, t_max, n_trajectories))
                    bar.update(bar.value + 1)
                ready_ids, proc_ids = ray.wait(proc_ids, num_returns=len(proc_ids), timeout=0.5)
                if len(ready_ids) != 0:
                    results_list = ray.get(ready_ids)
                    for result in results_list:
                        n_failures = result
                        probabilities.append(n_failures / n_trajectories)
                        # bar.update(len(probabilities))
        print(probabilities)
Exemplo n.º 7
0
    def post_milp(self, x, nn, output_flag, t, template):
        post = []
        observable_template_action1 = self.observable_templates[1]
        observable_result_action1 = self.observable_results[1]
        observable_template_action0 = self.observable_templates[0]
        observable_result_action0 = self.observable_results[0]

        def standard_op():
            gurobi_model = grb.Model()
            gurobi_model.setParam('OutputFlag', output_flag)
            input = generate_input_region(gurobi_model, template, x,
                                          self.env_input_size)
            z = self.apply_dynamic(input, gurobi_model, self.env_input_size)
            return gurobi_model, z, input

        # case 0
        gurobi_model, z, input = standard_op()
        feasible0 = self.generate_guard(gurobi_model, z, case=0)  # bounce
        if feasible0:  # action is irrelevant in this case
            # apply dynamic
            x_prime_results = optimise(template, gurobi_model, z)
            gurobi_model = grb.Model()
            gurobi_model.setParam('OutputFlag', output_flag)
            input2 = generate_input_region(gurobi_model, template,
                                           x_prime_results,
                                           self.env_input_size)
            x_second = self.apply_dynamic2(input2,
                                           gurobi_model,
                                           case=0,
                                           env_input_size=self.env_input_size)
            found_successor, x_second_results = self.h_repr_to_plot(
                gurobi_model, template, x_second)
            if found_successor:
                post.append(tuple(x_second_results))

        # case 1 : ball going down and hit
        gurobi_model, z, input = standard_op()
        feasible11 = self.generate_guard(gurobi_model, z, case=1)
        if feasible11:
            generate_region_constraints(gurobi_model,
                                        observable_template_action1, input,
                                        observable_result_action1, 2)
            gurobi_model.optimize()
            feasible12 = gurobi_model.status
            # feasible12 = self.generate_nn_guard(gurobi_model, input, nn, action_ego=1)  # check for action =1 over input (not z!)
            if feasible12:
                # apply dynamic
                x_prime_results = optimise(template, gurobi_model, z)
                gurobi_model = grb.Model()
                gurobi_model.setParam('OutputFlag', output_flag)
                input2 = generate_input_region(gurobi_model, template,
                                               x_prime_results,
                                               self.env_input_size)
                x_second = self.apply_dynamic2(
                    input2,
                    gurobi_model,
                    case=1,
                    env_input_size=self.env_input_size)
                found_successor, x_second_results = self.h_repr_to_plot(
                    gurobi_model, template, x_second)
                if found_successor:
                    post.append(tuple(x_second_results))
        # case 2 : ball going up and hit
        gurobi_model, z, input = standard_op()
        feasible21 = self.generate_guard(gurobi_model, z, case=2)
        if feasible21:
            generate_region_constraints(gurobi_model,
                                        observable_template_action1, input,
                                        observable_result_action1, 2)
            gurobi_model.optimize()
            feasible22 = gurobi_model.status
            # feasible22 = self.generate_nn_guard(gurobi_model, input, nn, action_ego=1)  # check for action =1 over input (not z!)
            if feasible22:
                # apply dynamic
                x_prime_results = optimise(template, gurobi_model, z)
                gurobi_model = grb.Model()
                gurobi_model.setParam('OutputFlag', output_flag)
                input2 = generate_input_region(gurobi_model, template,
                                               x_prime_results,
                                               self.env_input_size)
                x_second = self.apply_dynamic2(
                    input2,
                    gurobi_model,
                    case=2,
                    env_input_size=self.env_input_size)
                found_successor, x_second_results = self.h_repr_to_plot(
                    gurobi_model, template, x_second)
                if found_successor:
                    post.append(tuple(x_second_results))
        # case 1 alt : ball going down and NO hit
        gurobi_model, z, input = standard_op()
        feasible11_alt = self.generate_guard(gurobi_model, z, case=1)
        if feasible11_alt:
            generate_region_constraints(gurobi_model,
                                        observable_template_action0, input,
                                        observable_result_action0, 2)
            gurobi_model.optimize()
            feasible12_alt = gurobi_model.status
            # feasible12_alt = self.generate_nn_guard(gurobi_model, input, nn, action_ego=0)  # check for action = 0 over input (not z!)
            if feasible12_alt:
                # apply dynamic
                x_prime_results = optimise(template, gurobi_model, z)
                gurobi_model = grb.Model()
                gurobi_model.setParam('OutputFlag', output_flag)
                input2 = generate_input_region(gurobi_model, template,
                                               x_prime_results,
                                               self.env_input_size)
                x_second = self.apply_dynamic2(
                    input2,
                    gurobi_model,
                    case=3,
                    env_input_size=self.env_input_size)
                found_successor, x_second_results = self.h_repr_to_plot(
                    gurobi_model, template, x_second)

                if found_successor:
                    post.append(tuple(x_second_results))
        # case 2 alt : ball going up and NO hit
        gurobi_model, z, input = standard_op()
        feasible21_alt = self.generate_guard(gurobi_model, z, case=2)
        if feasible21_alt:
            generate_region_constraints(gurobi_model,
                                        observable_template_action0, input,
                                        observable_result_action0, 2)
            gurobi_model.optimize()
            feasible22_alt = gurobi_model.status
            # feasible22_alt = self.generate_nn_guard(gurobi_model, input, nn, action_ego=0)  # check for action = 0 over input (not z!)
            if feasible22_alt:
                # apply dynamic
                x_prime_results = optimise(template, gurobi_model, z)
                gurobi_model = grb.Model()
                gurobi_model.setParam('OutputFlag', output_flag)
                input2 = generate_input_region(gurobi_model, template,
                                               x_prime_results,
                                               self.env_input_size)
                x_second = self.apply_dynamic2(
                    input2,
                    gurobi_model,
                    case=3,
                    env_input_size=self.env_input_size)
                found_successor, x_second_results = self.h_repr_to_plot(
                    gurobi_model, template, x_second)
                if found_successor:
                    post.append(tuple(x_second_results))
        # case 3 : ball out of reach and not bounce
        gurobi_model, z, input = standard_op()
        feasible3 = self.generate_guard(gurobi_model, z,
                                        case=3)  # out of reach
        if feasible3:  # action is irrelevant in this case
            # apply dynamic
            x_prime_results = optimise(template, gurobi_model, z)
            gurobi_model = grb.Model()
            gurobi_model.setParam('OutputFlag', output_flag)
            input2 = generate_input_region(gurobi_model, template,
                                           x_prime_results,
                                           self.env_input_size)
            x_second = self.apply_dynamic2(input2,
                                           gurobi_model,
                                           case=3,
                                           env_input_size=self.env_input_size)
            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
Exemplo n.º 8
0
 def h_repr_to_plot(self, gurobi_model, template, x_prime):
     x_prime_results = optimise(template, gurobi_model,
                                x_prime)  # h representation
     return x_prime_results is not None, x_prime_results