Exemplo n.º 1
0
    def get_error_paths(self, initial_state_set,
                        final_state_set, pi_ref,
                        ci_ref, pi_cons, ci_cons,
                        max_paths):
        '''
        @type pi_cons: constraints.IntervalCons
        @type ci_cons: constraints.IntervalCons
        '''

        MAX_ERROR_PATHS = max_paths
        ci_dim = self.num_dims.ci
        pi_dim = self.num_dims.pi
#         init_set = set()
        path_list = []
        ci_seq_list = []
        pi_seq_list = []

        error_paths = self.compute_error_paths(initial_state_set, final_state_set, MAX_ERROR_PATHS)
        #bounded_error_paths = U.bounded_iter(error_paths, MAX_ERROR_PATHS)
        bounded_error_paths = error_paths

        def get_ci_seq(path):
            return self.get_seq_of_ci_pi(path)[0]

        def get_pi_seq(path):
            return self.get_seq_of_ci_pi(path)[1]

        def get_empty(_):
            return []

        get_ci = get_ci_seq if ci_dim != 0 else get_empty
        get_pi = get_pi_seq if pi_dim != 0 else get_empty

#         if ci_dim == 0:
#             for path in bounded_error_paths:
#                 ci_seq = []
#                 ci_seq_list.append(ci_seq)
#                 init_set.add(path[0])

#             return (list(init_set), ci_seq_list)
#         else:
        max_len = -np.inf
        min_len = np.inf
        unique_paths = set()
        for path in bounded_error_paths:
#             ci_seq = self.get_seq_of_ci(path)
            pi_seq_cells = get_pi(path)
            pi_ref.update_from_path(path, pi_seq_cells)
            # convert pi_cells to ival constraints
            #pi_seq = map(self.plant_abs.get_ival_cons_pi_cell, get_pi(path))
            pi_seq = [CM.get_ival_cons_cell(pi_cell, pi_ref.eps) for pi_cell in pi_seq_cells]
            if ci_ref is not None:
                ci_seq_cells = get_ci(path)
                ci_ref.update_from_path(path, ci_seq_cells)
                ci_seq = [CM.get_ival_cons_cell(ci_cell, ci_ref.eps) for ci_cell in ci_seq_cells]
            else:
                ci_seq = get_ci(path)

            #print(pi_seq)

            #FIXME: Why are uniqe paths found only for the case when dim(ci) != 0?
            plant_states_along_path = tuple(state.plant_state for state in path)
            if plant_states_along_path not in unique_paths:
                unique_paths.add(plant_states_along_path)

                if ci_dim != 0:
                    assert(len(ci_seq) == len(path) - 1)
                else:
                    assert(len(ci_seq) == 0)
                if pi_dim != 0:
                    assert(len(pi_seq) == len(path) - 1)
                else:
                    assert(len(pi_seq) == 0)

#                 max_len = max(len(ci_seq), max_len)
#                 min_len = min(len(ci_seq), min_len)

                max_len = max(len(path), max_len)
                min_len = min(len(path), min_len)

                ci_seq_list.append(ci_seq)
                pi_seq_list.append(pi_seq)
                path_list.append(path)

        assert(max_len <= self.N + 1)

        # normalize list lens by appending 0

#         if max_len != min_len or max_len < self.N:

#             # TODO: many a times we find paths, s.t. len(path) > self.N
#             # How should those paths be handled?
#             #   - Should they be ignored, shortened, or what?
#             #   - or should nothing be done about them?

#             for (idx, ci_seq) in enumerate(ci_seq_list):
#                 # instead of zeros, use random!
#                 num_of_missing_ci_tail = max(max_len, self.N) - len(ci_seq)
#                 ci_seq_list[idx] = ci_seq \
#                     + list(np.random.random((num_of_missing_ci_tail, ci_dim)))

#             for (idx, pi_seq) in enumerate(pi_seq_list):
#                 num_of_missing_pi_tail = max(max_len, self.N) - len(pi_seq)
#                 pi_seq_list[idx] = pi_seq \
#                     + list(np.random.random((num_of_missing_pi_tail, pi_dim)))

        for (idx, (ci_seq, pi_seq)) in enumerate(zip(ci_seq_list, pi_seq_list)):
            missing_ci_len = self.N - len(ci_seq)
            missing_pi_len = self.N - len(pi_seq)
            # row, column
            r, c_ci = missing_ci_len, ci_dim
            #FIXME: default random values
            if ci_ref is not None:
                ci_seq_list[idx] = ci_seq + [ci_cons] * missing_ci_len
            else:
                ci_seq_list[idx] = ci_seq + list(np.random.uniform(ci_cons.l, ci_cons.h, (r, c_ci)))
            #pi_seq_list[idx] = pi_seq + list(np.random.uniform(pi_cons.l, pi_cons.h, (r, c_pi)))
            pi_seq_list[idx] = pi_seq + [pi_cons] * missing_pi_len


        if __debug__:
            print('path states, min_len:{}, max_len:{}'.format(min_len, max_len))

        # ##!!##logger.debug('init_list:{}\n'.format(init_list))
        # ##!!##logger.debug('ci_seq_list:{}\n'.format(ci_seq_list))
        # print('len(init_list)', len(init_list))
        # print('len(ci_seq_list)', len(ci_seq_list))

#         for ci_seq in ci_seq_list:
#             print(ci_seq)
#         for pi_seq in pi_seq_list:
#             print(pi_seq)

        return (path_list, ci_seq_list, pi_seq_list)