Example #1
0
def case1_synthesis(formula, ts_file):
    _, dfa_0, dfa_inf, bdd = twtl.translate(formula, kind='both', norm=True)

    logging.debug('alphabet: {}'.format(dfa_inf.props))

    for u, v, d in dfa_inf.g.edges_iter(data=True):
        logging.debug('({}, {}): {}'.format(u, v, d))

    dfa_inf.visualize(draw='matplotlib')
    plt.show()

    logging.debug('\nEnd of translate\n\n')

    logging.info('The bound of formula "%s" is (%d, %d)!', formula, *bdd)
    logging.info('Translated formula "%s" to normal DFA of size (%d, %d)!',
                 formula, *dfa_0.size())
    logging.info('Translated formula "%s" to infinity DFA of size (%d, %d)!',
                 formula, *dfa_inf.size())

    logging.debug('\n\nStart policy computation\n')

    ts = Ts(directed=True, multi=False)
    ts.read_from_file(ts_file)
    ets = expand_duration_ts(ts)

    for name, dfa in [('normal', dfa_0), ('infinity', dfa_inf)]:
        logging.info('Constructing product automaton with %s DFA!', name)
        pa = ts_times_fsa(ets, dfa)
        logging.info('Product automaton size is: (%d, %d)', *pa.size())

        if name == 'infinity':
            for u in pa.g.nodes_iter():
                logging.debug('{} -> {}'.format(u, pa.g.neighbors(u)))

            pa.visualize(draw='matplotlib')
            plt.show()

        # compute optimal path in PA and then project onto the TS
        policy, output, tau = compute_control_policy(pa, dfa, dfa.kind)
        logging.info('Max deadline: %s', tau)
        if policy is not None:
            logging.info('Generated output word is: %s',
                         [tuple(o) for o in output])

            policy = [x for x in policy if x not in ets.state_map]
            out = StringIO.StringIO()
            for u, v in zip(policy[:-1], policy[1:]):
                print >> out, u, '->', ts.g[u][v][0]['duration'], '->',
            print >> out, policy[-1],
            logging.info('Generated control policy is: %s', out.getvalue())
            out.close()

            logging.info('Relaxation is: %s',
                         twtl.temporal_relaxation(output, formula=formula))
        else:
            logging.info('No control policy found!')
Example #2
0
def case2_verification(formula, ts_file):
    _, dfa_inf, bdd = twtl.translate(formula, kind=DFAType.Infinity, norm=True)

    logging.info('The bound of formula "%s" is (%d, %d)!', formula, *bdd)
    logging.info('Translated formula "%s" to infinity DFA of size (%d, %d)!',
                 formula, *dfa_inf.size())

    ts = Ts(directed=True, multi=False)
    ts.read_from_file(ts_file)
    ts.g = nx.DiGraph(ts.g)
    ts.g.add_edges_from(ts.g.edges(), weight=1)

    for u, v in ts.g.edges_iter():
        print u, '->', v

    result = verify(ts, dfa_inf)
    logging.info('The result of the verification procedure is %s!', result)
def prep_for_learning(ep_len, m, n, h, init_states, obstacles, pick_up_state,
                      delivery_state, rewards, rew_val, custom_flag,
                      custom_task):
    # Create the environment and get the TS #
    ts_start_time = timeit.default_timer()
    disc = 1
    TS, obs_mat, state_mat = create_ts(m, n, h)
    path = '../data/ts_' + str(m) + 'x' + str(n) + 'x' + str(h) + '_1Ag_1.txt'
    paths = [path]
    bases = {init_states[0]: 'Base1'}
    obs_mat = update_obs_mat(obs_mat, state_mat, m, obstacles, init_states[0])
    TS = update_adj_mat_3D(m, n, h, TS, obs_mat)
    create_input_file(TS, state_mat, obs_mat, paths[0], bases, disc, m, n, h,
                      0)
    ts_file = paths
    ts_dict = Ts(directed=True, multi=False)
    ts_dict.read_from_file(ts_file[0])
    ts = expand_duration_ts(ts_dict)
    ts_timecost = timeit.default_timer() - ts_start_time

    # Get the DFA #
    dfa_start_time = timeit.default_timer()
    pick_ups = pick_up_state[0][0] * n + pick_up_state[0][1]
    deliveries = delivery_state[0][0] * n + delivery_state[0][1]
    pick_up = str(pick_ups)  # Check later
    delivery = str(deliveries)
    tf = str((ep_len - 1) / 2)  # time bound
    if custom_flag == 1:
        phi = custom_task
    else:
        phi = '[H^1 r' + pick_up + ']^[0, ' + tf + '] * [H^1 r' + delivery + ']^[0,' + tf + ']'  # Construc the task according to pickup/delivery )^[0, ' + tf + ']'
    _, dfa_inf, bdd = twtl.translate(
        phi, kind=DFAType.Infinity, norm=True
    )  # states and sim. time ex. phi = '([H^1 r47]^[0, 30] * [H^1 r31]^[0, 30])^[0, 30]'
    dfa_timecost = timeit.default_timer(
    ) - dfa_start_time  # DFAType.Normal for normal, DFAType.Infinity for relaxed

    # Get the PA #
    pa_start_time = timeit.default_timer()
    alpha = 1
    nom_weight_dict = {}
    weight_dict = {}
    pa_or = ts_times_fsa(ts, dfa_inf)  # Original pa
    edges_all = nx.get_edge_attributes(ts_dict.g, 'edge_weight')
    max_edge = max(edges_all, key=edges_all.get)
    norm_factor = edges_all[max_edge]
    for pa_edge in pa_or.g.edges():
        edge = (pa_edge[0][0], pa_edge[1][0], 0)
        nom_weight_dict[pa_edge] = edges_all[edge] / norm_factor
    nx.set_edge_attributes(pa_or.g, 'edge_weight', nom_weight_dict)
    nx.set_edge_attributes(pa_or.g, 'weight', 1)
    pa = copy.deepcopy(pa_or)  # copy the pa
    time_weight = nx.get_edge_attributes(pa.g, 'weight')
    edge_weight = nx.get_edge_attributes(pa.g, 'edge_weight')
    for pa_edge in pa.g.edges():
        weight_dict[pa_edge] = alpha * time_weight[pa_edge] + (
            1 - alpha) * edge_weight[pa_edge]
    nx.set_edge_attributes(pa.g, 'new_weight', weight_dict)
    pa_timecost = timeit.default_timer() - pa_start_time

    # Compute the energy of the states #
    energy_time = timeit.default_timer()
    compute_energy(pa)
    energy_dict = nx.get_node_attributes(pa.g, 'energy')
    energy_pa = []
    for ind in range(len(pa.g.nodes())):
        energy_pa.append(pa.g.nodes([0])[ind][1].values()[0])

    # projection of pa on ts #
    init_state = [init_states[0][0] * n + init_states[0][1]]
    pa2ts = []
    for i in range(len(pa.g.nodes())):
        if pa.g.nodes()[i][0] != 'Base1':
            pa2ts.append(int(pa.g.nodes()[i][0].replace("r", "")))
        else:
            pa2ts.append(init_state[0])
            i_s = i  # Agent's initial location in pa
    energy_timecost = timeit.default_timer() - pa_start_time

    # TS adjacency matrix and source-target
    TS_adj = TS
    TS_s = []
    TS_t = []
    for i in range(len(TS_adj)):
        for j in range(len(TS_adj)):
            if TS_adj[i, j] != 0:
                TS_s.append(i)
                TS_t.append(j)

    # pa adjacency matrix and source-target
    pa_adj_st = nx.adjacency_matrix(pa.g)
    pa_adj = pa_adj_st.todense()
    pa_s = []  # source node
    pa_t = []  # target node
    for i in range(len(pa_adj)):
        for j in range(len(pa_adj)):
            if pa_adj[i, j] == 1:
                pa_s.append(i)
                pa_t.append(j)

# PA rewards matrix
    rewards_ts = np.zeros(m * n)  #-0.25#
    rewards_pa = np.zeros(len(pa2ts))
    rewards_ts_indexes = []
    for i in range(len(rewards)):
        rewards_ts_indexes.append(
            rewards[i][0] * n + rewards[i][1]
        )  # rewards_ts_indexes[i] = rewards[i][0] * n + rewards[i][1]
        rewards_ts[rewards_ts_indexes[i]] = rew_val

    for i in range(len(rewards_pa)):
        rewards_pa[i] = rewards_ts[pa2ts[i]]

    # # Display some important info
    print('##### PICK-UP and DELIVERY MISSION #####' + "\n")
    print('Initial Location  : ' + str(init_states[0]) + ' <---> Region ' +
          str(init_state[0]))
    print('Pick-up Location  : ' + str(pick_up_state[0]) + ' <---> Region ' +
          pick_up)
    print('Delivery Location : ' + str(delivery_state[0]) + ' <---> Regions ' +
          delivery)
    print('Reward Locations  : ' + str(rewards) + ' <---> Regions ' +
          str(rewards_ts_indexes) + "\n")
    print('State Matrix : ')
    print(state_mat)
    print("\n")
    print('Mission Duration  : ' + str(ep_len) + ' time steps')
    print('TWTL Task : ' + phi + "\n")
    print('Computational Costst : TS created in ' + str(ts_timecost) +
          ' seconds')
    # print('			TS created in ' + str(ts_timecost) + ' seconds')
    print('		       DFA created in ' + str(dfa_timecost) + ' seconds')
    print('		       PA created in ' + str(pa_timecost) + ' seconds')
    print('		       Energy of PA states calculated in ' +
          str(energy_timecost) + ' seconds')

    return i_s, pa, pa_s, pa_t, pa2ts, energy_pa, rewards_pa, pick_up, delivery, pick_ups, deliveries, pa.g.nodes(
    )