def get_example_1():
    # input
    g = DiGraph()
    g.add_edges_from([[1, 2], [1, 3], [2, 4], [3, 4]])

    g.node[1]['r'] = 1
    g.node[2]['r'] = 1
    g.node[3]['r'] = 1.5
    g.node[4]['r'] = 1

    g[1][2]['c'] = 2
    g[1][3]['c'] = 1
    g[2][4]['c'] = 1
    g[3][4]['c'] = 3

    U = range(6)  # 0...5

    expected_edge_list = [
        [],
        [(1, 3)],
        [(1, 3)],  # DiGraph([(1, 2)])
        [(1, 2), (1, 3)],
        [(1, 2), (1, 3), (2, 4)],
        [(1, 2), (1, 3), (2, 4)]
    ]
    return g, U, expected_edge_list
def get_example_3():
    """get a binarized example, whose original graph is
    more complicated than the above example
    """
    g = DiGraph()
    g.add_nodes_from(range(1, 10))
    g.add_edges_from([(1, 2), (1, 3), (1, 7), (2, 4), (2, 5), (2, 6), (2, 7),
                      (3, 8), (3, 9)])
    rewards = range(1, 10)
    for r, n in zip(rewards, g.nodes()):
        g.node[n]['r'] = r

    # all edges have cost 2 except 1 -> 2 and 1 -> 3(cost 1)
    for s, t in g.edges():
        g[s][t]['c'] = 2
    g[1][2]['c'] = 1
    g[1][3]['c'] = 1

    g = binarize_dag(g,
                     vertex_weight_key='r',
                     edge_weight_key='c',
                     dummy_node_name_prefix='d_')

    # parameters and expected output
    U = [0, 2, 3, 4, 100]
    expected_edges_set = [
        [],
        [(1, 7)],
        [(1, 'd_1'), ('d_1', 3), (3, 9)],
        [(1, 'd_1'), ('d_1', 3), (3, 9), ('d_1', 2)],
        # (1, 7) removed to make it a tree
        list(set(g.edges()) - set([(1, 7)]))
    ]

    return (g, U, expected_edges_set)
Exemple #3
0
def get_example_1():
    # input
    g = DiGraph()
    g.add_edges_from([[1, 2], [1, 3], [2, 4], [3, 4]])

    g.node[1]['r'] = 1
    g.node[2]['r'] = 1
    g.node[3]['r'] = 1.5
    g.node[4]['r'] = 1

    g[1][2]['c'] = 2
    g[1][3]['c'] = 1
    g[2][4]['c'] = 1
    g[3][4]['c'] = 3

    U = range(6)  # 0...5
    
    expected_edge_list = [
        [],
        [(1, 3)],
        [(1, 3)],  # DiGraph([(1, 2)])
        [(1, 2), (1, 3)],
        [(1, 2), (1, 3), (2, 4)],
        [(1, 2), (1, 3), (2, 4)]
    ]
    return g, U, expected_edge_list
Exemple #4
0
def get_example_2():
    # input
    g = DiGraph()
    g.add_edges_from([[1, 2], [1, 3], [2, 4], [3, 4]])
    
    g.node[1]['r'] = 1
    g.node[2]['r'] = 1
    g.node[3]['r'] = 1.5
    g.node[4]['r'] = 1
    
    g[1][2]['c'] = 0.021
    g[1][3]['c'] = 0.011
    g[2][4]['c'] = 0.009
    g[3][4]['c'] = 0.03

    U = [float(i) / 100 for i in xrange(6)]  # 0...5

    # expected value
    expected_edge_list = [
        [],
        [(1, 3)],
        [(1, 3)],
        [(1, 2), (1, 3)],
        [(1, 2), (1, 3), (2, 4)],
        [(1, 2), (1, 3), (2, 4)]
    ]

    return (g, U, expected_edge_list)
def test_variance_based_cost():
    D = {'u': {}, 'v': {10: {'x', 'dum'}}, 'w': {12: {'y'}}}
    G = DiGraph()
    G.add_edges_from([('u', 'v'), ('u', 'w'), ('v', 'dum'), ('dum', 'x'),
                      ('w', 'y')])
    G.node['dum']['dummy'] = True
    reprs = np.array([[0, 1], [1, 0], [0, 1], [1, 1], [0, 0]])
    real_nodes = ['u', 'v', 'w', 'x', 'y']
    for r, n in zip(reprs, real_nodes):
        G.node[n]['r'] = r
    n = 'u'
    children = [(10, 'v'), (12, 'w')]

    actual = get_all_nodes(G, n, D, children, ignore_dummy=True)
    expected = real_nodes
    assert_equal(sorted(expected), sorted(actual))

    cost_func = make_variance_cost_func(euclidean, 'r')

    actual = cost_func(n, D, G, children)
    mean_vec = np.mean(reprs, axis=0)
    expected = np.sum([euclidean(mean_vec, v) for v in reprs])
    np.testing.assert_almost_equal(expected, actual)

    # with fixed_point
    cost_func_fp = make_variance_cost_func(euclidean, 'r', fixed_point=2)
    actual = cost_func_fp(n, D, G, children)
    assert_equal(int(expected * 100), actual)
Exemple #6
0
    def map_path_to_element_sequence(self, edge2element, element2edge,
                                     pruned_subgraph, paths):
        element_sequences = []
        # put all path that share the same set of elements into one group
        for path in paths:
            element_sequence = []
            for i in range(len(path) - 1):
                element_sequence.append(edge2element[(path[i], path[i + 1])])
            is_added = False
            for i in range(len(element_sequences)):
                if set(element_sequences[i][0]) == set(element_sequence):
                    element_sequences[i].append(element_sequence)
                    is_added = True
                    break
            if not is_added:
                element_sequences.append([element_sequence])

        graph_with_maximum_width = DiGraph()
        width = 0
        height = 0
        # for each group, find one poset
        for ele_seq in element_sequences:
            # all pairs of elements from the first element
            linear_order = []
            for i in range(len(ele_seq[0])):
                for j in range(i + 1, len(ele_seq[0])):
                    linear_order.append((ele_seq[0][i], ele_seq[0][j]))
            # remove contradictive pairs
            for i in range(1, len(ele_seq)):
                for j in range(len(ele_seq[1]) - 1):
                    if (ele_seq[i][j + 1], ele_seq[i][j]) in linear_order:
                        linear_order.remove((ele_seq[i][j + 1], ele_seq[i][j]))
            # hasse diagram
            hasse = DiGraph()
            hasse.add_nodes_from(ele_seq[0])
            hasse.add_edges_from(linear_order)
            self.prune_subgraph(hasse)
            try:
                w = max([len(o) for o in nx.antichains(hasse)])
            except nx.exception.NetworkXUnfeasible:
                print(hasse.edges)
            # h = nx.dag_longest_path_length(hasse)
            h = len([
                e for e in hasse.nodes
                if pruned_subgraph.nodes[element2edge[e][0]]['label'] != '1'
            ])
            if w > width or (w == width and h > height):
                graph_with_maximum_width = hasse
                width = w
                height = h
        # print(height)
        return {edge for edge in graph_with_maximum_width.edges}, list(graph_with_maximum_width.nodes), \
                            graph_with_maximum_width
Exemple #7
0
 def build_static(self):
     for agent_index in range(len(
             self.graph['agent_prod_array'])):  #对每一个机器人的product自动机
         for other_agent in range(len(self.graph['agent_prod_array'])):
             if agent_index == other_agent:
                 continue
             else:
                 #no collative task
                 if len(self.graph['colla_ap'][other_agent]) == 0:
                     continue
                 else:
                     for ap_index in range(
                             len(self.graph['colla_ap'][other_agent])):
                         #zai region zhong zhao dao suo you yu ap dui ying de qu yu dian
                         cor_pos_list = self.find_region(
                             self.graph['colla_ap'][other_agent][ap_index],
                             self.graph['region_array'][other_agent])
                         #zai fts zhao dao mei ge qu yu dian de qian ji yi ji cost, qu zui xiao de cun xia lai
                         cost_static = float('inf')
                         fts_digraph = DiGraph()
                         fts_digraph.add_edges_from(
                             self.graph['agent_fts_array']
                             [other_agent].edges())
                         for pos in cor_pos_list:
                             if len(pos) != 3:
                                 continue
                             for pre_pos in fts_digraph.predecessors(pos):
                                 if pre_pos == pos:  #pai chu yuan di bu dong de qing kuang
                                     continue
                                 cost_buf = distance(pos, pre_pos)
                                 if cost_buf < cost_static:
                                     cost_static = cost_buf
                         #zai ben ji qi ren de product zhong jia bian ,ju ti jia fa an guomeng zhi qian gou jian product zi dong ji de fang fa
                         for static_edge in self.graph['static_list'][
                                 agent_index]:  #shu ju jie guo de tong yi
                             #yao qiu colla_ap gei chu dui ying de dong zuo,bi ru door dui ying open,zhe ge ke yi tong guo ren wu de sheng ming fang shi jin xing zi dong shi bie?
                             if self.graph['colla_ap'][agent_index][
                                     ap_index] in static_edge[2]:
                                 cost_local = distance(
                                     static_edge[0][0], static_edge[1][0])
                                 #print(static_edge[0])
                                 self.graph['agent_prod_array'][
                                     agent_index].add_edge(
                                         static_edge[0],
                                         static_edge[1],
                                         weight=cost_static + cost_local)
                                 self.req_edges[(static_edge[0][0],
                                                 static_edge[1][0])] = [
                                                     agent_index,
                                                     other_agent,
                                                     static_edge[2]
                                                 ]  #chuan ap?
Exemple #8
0
	def create_fair_graph(self,system):
		G = DiGraph()
		G.add_edges_from(self.edges(data=True))
		controls = nx.get_edge_attributes(self,'control')
		unfair_cycles = []
		for cycle in nx.simple_cycles(G):
			edges = [(cycle[i],cycle[(i+1)%len(cycle)]) for i in range(len(cycle))]
			trace = [(c[0],controls[c].values()) for c in edges]
			nbre_controls = [range(len(t[1])) for t in trace]
			control_configuration = itertools.product(*nbre_controls)
			for conf in control_configuration:
				current_trace = [(t[0],t[1][conf[i]]) for i,t in enumerate(trace)]
				if not self.is_cycle_fair(system,current_trace):
					unfair_cycles.append(current_trace)
		print "Unfair cycles ",unfair_cycles
def make_test_graph():
    '''     (4)  6
             ^   ^
             | X |
            (3)  5
             ^   ^
              \ /
              [2]
               ^
               |
              (1)
    '''
    web = DiGraph()
    web.add_edges_from([(1,2), (2,3), (3,4), (2,5), (3,6), (5,6), (5,4)])
    return web
def make_test_graph():
    '''     (4)  6
             ^   ^
             | X |
            (3)  5
             ^   ^
              \ /
              [2]
               ^
               |
              (1)
    '''
    web = DiGraph()
    web.add_edges_from([(1,2), (2,3), (3,4), (2,5), (3,6), (5,6), (5,4)])
    return web
def get_variance_example_1():
    g = DiGraph()
    g.add_edges_from([(0, 1), (0, 2), (2, 3), (3, 4), (2, 'dummy'),
                      ('dummy', 5)])
    g.node['dummy']['dummy'] = True

    for n in (0, 1, 2, 5):  # topic 1
        g.node[n]['repr'] = np.array([0, 0])
    for n in (3, 4):  # topic 2
        g.node[n]['repr'] = np.array([1, 1])
    for n in g.nodes_iter():
        g.node[n]['r'] = 1

    # correct is (0, 1, 2, 5) for cost 0
    U = [0, 42]
    expected_edge_set = [set(g.edges()) - {(2, 3), (3, 4)}, set(g.edges())]
    return (g, U, expected_edge_set)
def get_example_5():
    g = DiGraph()
    g.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 4), (2, 5), (2, 6)])
    for s, t in g.edges():
        g[s][t]['c'] = 1
    g[1][4]['c'] = 0
    g[2][4]['c'] = 0
    g[2][6]['c'] = 3
    for n in g.nodes():
        g.node[n]['r'] = 1
    g.node[3]['r'] = 10
    g.node[4]['r'] = 100
    g.node[5]['r'] = 11

    U = [10]
    # sub-optimal answer actually
    expected_edge_set = [[(0, 2), (2, 4), (2, 5), (2, 6)]]
    return (g, U, expected_edge_set)
Exemple #13
0
def get_example_5():
    g = DiGraph()
    g.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4),
                      (2, 4), (2, 5), (2, 6)])
    for s, t in g.edges():
        g[s][t]['c'] = 1
    g[1][4]['c'] = 0
    g[2][4]['c'] = 0
    g[2][6]['c'] = 3
    for n in g.nodes():
        g.node[n]['r'] = 1
    g.node[3]['r'] = 10
    g.node[4]['r'] = 100
    g.node[5]['r'] = 11

    U = [10]
    # sub-optimal answer actually
    expected_edge_set = [[(0, 2), (2, 4), (2, 5), (2, 6)]]
    return (g, U, expected_edge_set)
def get_example_4():
    g = DiGraph()
    g.add_edges_from([
        (0, 1),
        (1, 2),
        (2, 3),
        (2, 14),  # tree 1
        (2, 15),
        (3, 16),
        (3, 17),
        (0, 4),
        (4, 5),
        (4, 6),  # tree 2
        (5, 11),
        (6, 11),
        (6, 12),
        (6, 13),
        (0, 7),
        (7, 8),
        (7, 9),  # tree 3
        (8, 10),
        (8, 11),
        (9, 12),
        (9, 13)
    ])
    for s, t in g.edges():
        g[s][t]['c'] = 1
    for n in g.nodes():
        g.node[n]['r'] = 1
    g.node[10]['r'] = 2

    U = [7]
    expected_edge_set = [[
        (0, 7),
        (7, 8),
        (7, 9),  # tree 3
        (8, 10),
        (8, 11),
        (9, 12),
        (9, 13)
    ]]
    return (g, U, expected_edge_set)
Exemple #15
0
def get_example_4():
    g = DiGraph()
    g.add_edges_from([(0, 1), (1, 2), (2, 3), (2, 14),  # tree 1
                      (2, 15), (3, 16), (3, 17),
                      (0, 4), (4, 5), (4, 6),  # tree 2
                      (5, 11), (6, 11), (6, 12), (6, 13),
                      (0, 7), (7, 8), (7, 9),  # tree 3
                      (8, 10), (8, 11), (9, 12), (9, 13)])
    for s, t in g.edges():
        g[s][t]['c'] = 1
    for n in g.nodes():
        g.node[n]['r'] = 1
    g.node[10]['r'] = 2

    U = [7]
    expected_edge_set = [
        [(0, 7), (7, 8), (7, 9),  # tree 3
         (8, 10), (8, 11), (9, 12), (9, 13)]
    ]
    return (g, U, expected_edge_set)
def get_example_6():
    # IN-OPTIMAL CASE
    g = DiGraph()
    g.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 4), (2, 5)])
    for s, t in g.edges():
        g[s][t]['c'] = 0
    g[1][3]['c'] = 4
    g[1][4]['c'] = 4
    g[2][4]['c'] = 2
    g[2][5]['c'] = 1
    for n in g.nodes():
        g.node[n]['r'] = 0
    g.node[3]['r'] = 1
    g.node[4]['r'] = 100
    g.node[5]['r'] = 1

    U = [7]
    # sub-optimal answer actually
    expected_edge_set = [[(0, 2), (2, 4), (2, 5)]]
    return (g, U, expected_edge_set)
Exemple #17
0
def get_example_6():
    # IN-OPTIMAL CASE
    g = DiGraph()
    g.add_edges_from([(0, 1), (0, 2), (1, 3),
                      (1, 4), (2, 4), (2, 5)])
    for s, t in g.edges():
        g[s][t]['c'] = 0
    g[1][3]['c'] = 4
    g[1][4]['c'] = 4
    g[2][4]['c'] = 2
    g[2][5]['c'] = 1
    for n in g.nodes():
        g.node[n]['r'] = 0
    g.node[3]['r'] = 1
    g.node[4]['r'] = 100
    g.node[5]['r'] = 1

    U = [7]
    # sub-optimal answer actually
    expected_edge_set = [[(0, 2), (2, 4), (2, 5)]]
    return (g, U, expected_edge_set)
def get_example_2():
    # input
    g = DiGraph()
    g.add_edges_from([[1, 2], [1, 3], [2, 4], [3, 4]])

    g.node[1]['r'] = 1
    g.node[2]['r'] = 1
    g.node[3]['r'] = 1.5
    g.node[4]['r'] = 1

    g[1][2]['c'] = 0.021
    g[1][3]['c'] = 0.011
    g[2][4]['c'] = 0.009
    g[3][4]['c'] = 0.03

    U = [float(i) / 100 for i in xrange(6)]  # 0...5

    # expected value
    expected_edge_list = [[], [(1, 3)], [(1, 3)], [(1, 2), (1, 3)],
                          [(1, 2), (1, 3), (2, 4)], [(1, 2), (1, 3), (2, 4)]]

    return (g, U, expected_edge_list)
    def test_find_connecting_nodes_good_sourceDep(self):
        '''     (4)  6
                 ^   ^
                 | X |
                (3)  5
                 ^   ^
                  \ /
                  [2]
                   ^
                   |
                  (1)
        '''
        web = DiGraph()
        web.add_edges_from([(1,2), (2,3), (3,4), (2,5), (3,6), (5,6), (5,4)])

        inp = (1, 3, 4)
        out = (2,)
        (_, _, cn_nodes, _) = _research_calculation_routes(web, inp, out)
        all_out = {1,4,3}
        all_in = {2,5,6}
        self.assertTrue(cn_nodes - all_out == cn_nodes, cn_nodes)
        self.assertTrue(all_in & cn_nodes == all_in, cn_nodes)
Exemple #20
0
def test_variance_based_cost():
    D = {'u': {}, 'v': {10: {'x', 'dum'}}, 'w': {12: {'y'}}}
    G = DiGraph()
    G.add_edges_from([('u', 'v'),
                      ('u', 'w'),
                      ('v', 'dum'),
                      ('dum', 'x'),
                      ('w', 'y')])
    G.node['dum']['dummy'] = True
    reprs = np.array([[0, 1],
                      [1, 0],
                      [0, 1],
                      [1, 1],
                      [0, 0]])
    real_nodes = ['u', 'v', 'w', 'x', 'y']
    for r, n in zip(reprs, real_nodes):
        G.node[n]['r'] = r
    n = 'u'
    children = [(10, 'v'),
                (12, 'w')]

    actual = get_all_nodes(G, n, D, children, ignore_dummy=True)
    expected = real_nodes
    assert_equal(sorted(expected), sorted(actual))

    cost_func = make_variance_cost_func(euclidean, 'r')
    
    actual = cost_func(n, D, G,
                       children)
    mean_vec = np.mean(reprs, axis=0)
    expected = np.sum([euclidean(mean_vec, v)
                       for v in reprs])
    np.testing.assert_almost_equal(expected, actual)

    # with fixed_point
    cost_func_fp = make_variance_cost_func(euclidean, 'r', fixed_point=2)
    actual = cost_func_fp(n, D, G,
                          children)
    assert_equal(int(expected*100), actual)
Exemple #21
0
    def test_find_connecting_nodes_good_sourceDep(self):
        '''     (4)  6
                 ^   ^
                 | X |
                (3)  5
                 ^   ^
                  \ /
                  [2]
                   ^
                   |
                  (1)
        '''
        web = DiGraph()
        web.add_edges_from([(1,2), (2,3), (3,4), (2,5), (3,6), (5,6), (5,4)])

        inp = (1, 3, 4)
        out = (2,)
        (_, _, cn_nodes, _) = _research_calculation_routes(web, inp, out)
        all_out = {1,4,3}
        all_in = {2,5,6}
        self.assertTrue(cn_nodes - all_out == cn_nodes, cn_nodes)
        self.assertTrue(all_in & cn_nodes == all_in, cn_nodes)
Exemple #22
0
def get_example_3():
    """get a binarized example, whose original graph is
    more complicated than the above example
    """
    g = DiGraph()
    g.add_nodes_from(range(1, 10))
    g.add_edges_from([(1, 2), (1, 3), (1, 7),
                      (2, 4), (2, 5), (2, 6),
                      (2, 7), (3, 8), (3, 9)])
    rewards = range(1, 10)
    for r, n in zip(rewards, g.nodes()):
        g.node[n]['r'] = r
        
    # all edges have cost 2 except 1 -> 2 and 1 -> 3(cost 1)
    for s, t in g.edges():
        g[s][t]['c'] = 2
    g[1][2]['c'] = 1
    g[1][3]['c'] = 1
    
    g = binarize_dag(g,
                     vertex_weight_key='r',
                     edge_weight_key='c',
                     dummy_node_name_prefix='d_')
    
    # parameters and expected output
    U = [0, 2, 3, 4, 100]
    expected_edges_set = [
        [],
        [(1, 7)],
        [(1, 'd_1'), ('d_1', 3), (3, 9)],
        [(1, 'd_1'), ('d_1', 3), (3, 9), ('d_1', 2)],
        # (1, 7) removed to make it a tree
        list(set(g.edges()) - set([(1, 7)]))
    ]
    
    return (g, U, expected_edges_set)
Exemple #23
0
def get_variance_example_1():
    g = DiGraph()
    g.add_edges_from([
        (0, 1), (0, 2),
        (2, 3), (3, 4),
        (2, 'dummy'),
        ('dummy', 5)
    ])
    g.node['dummy']['dummy'] = True

    for n in (0, 1, 2, 5):  # topic 1
        g.node[n]['repr'] = np.array([0, 0])
    for n in (3, 4):  # topic 2
        g.node[n]['repr'] = np.array([1, 1])
    for n in g.nodes_iter():
        g.node[n]['r'] = 1

    # correct is (0, 1, 2, 5) for cost 0
    U = [0, 42]
    expected_edge_set = [
        set(g.edges()) - {(2, 3), (3, 4)},
        set(g.edges())
    ]
    return (g, U, expected_edge_set)
Exemple #24
0
def subgraph(graph:DiGraph, nodes, include_outgoing=True):
  sg = DiGraph()
  for src, dst_seq in graph.adjacency():
    sg.add_edges_from((src, dst) for dst in dst_seq if src in nodes and include_outgoing or dst in nodes)
  return sg
Exemple #25
0
def syn_plan_comb(prod_mdp, S_fi, gamma, alpha):
    #----Synthesize combined optimal plan prefix and suffix
    #----with bounded risk and minimal mean total cost----
    print "===========[plan prefix synthesis starts]==========="
    Sf = set()
    for mec in S_fi:
        Sf.update(mec[0])  # mec = (sf, ip, act)
    delta = 1.0
    for init_node in prod_mdp.graph['initial']:
        path_init = single_source_shortest_path(prod_mdp, init_node)
        print 'Reachable from init size: %s' % len(path_init.keys())
        if not set(path_init.keys()).intersection(Sf):
            print "Initial node can not reach Sf"
            return None, None, None, None, None, None, None, None
        Sn = set(path_init.keys()).difference(Sf)
        #----find bad states that can not reach MEC
        simple_digraph = DiGraph()
        simple_digraph.add_edges_from(((v, u) for u, v in prod_mdp.edges()))
        reachable_set = set()
        ip_set = set()
        for mec in S_fi:
            ip = mec[1]
            path = single_source_shortest_path(simple_digraph,
                                               random.sample(ip, 1)[0])
            reachable_set.update(set(path.keys()))
            ip_set.update(ip)
        print 'States that can reach Sf, size: %s' % str(len(reachable_set))
        Sd = Sn.difference(reachable_set)
        Sr = Sn.intersection(reachable_set)
        # #--------------
        print 'Sn size: %s; Sd inside size: %s; Sr inside size: %s' % (
            len(Sn), len(Sd), len(Sr))
        # ---------solve lp------------
        print '-----'
        print 'Gurobi starts now'
        print '-----'
        try:
            #if True:
            model = Model('plan_comb')
            #--------------------
            #prefix variable
            Y = defaultdict(float)
            # create variables
            for s in Sr:
                for u in prod_mdp.node[s]['act'].copy():
                    Y[(s, u)] = model.addVar(vtype=GRB.CONTINUOUS,
                                             lb=0,
                                             name='y[(%s, %s)]' % (s, u))
            print 'Prefix Y variables added'
            #--------------------
            #suffix variables
            Z = defaultdict(float)
            for mec in S_fi:
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    for u in act[s]:
                        Z[(s, u)] = model.addVar(vtype=GRB.CONTINUOUS,
                                                 lb=0,
                                                 name='z[(%s, %s)]' % (s, u))
            print 'Suffix Z variables added'
            model.update()
            # set objective
            obj = 0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    prop = prod_mdp.edge[s][t]['prop'].copy()
                    for u in prop.iterkeys():
                        pe = prop[u][0]
                        ce = prop[u][1]
                        obj += alpha * Y[(s, u)] * pe * ce
            for mec in S_fi:
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    for u in act[s]:
                        for t in prod_mdp.successors_iter(s):
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            if u in prop.keys():
                                pe = prop[u][0]
                                ce = prop[u][1]
                                obj += (1.0 - alpha) * Z[(s, u)] * pe * ce
            model.setObjective(obj, GRB.MINIMIZE)
            print 'alpha*Prefix + (1.0-alpha)*Suffix cost Objective function set'
            # add constraints
            #------------------------------
            y_to_sd = 0.0
            y_to_sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    if t in Sd:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sd += Y[(s, u)] * pe
                    elif t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sf += Y[(s, u)] * pe
            model.addConstr(y_to_sf + y_to_sd >= delta, 'sum_out')
            model.addConstr(y_to_sf >= (1.0 - gamma) * (y_to_sf + y_to_sd),
                            'risk')
            print 'Prefix risk constraint added'
            #--------------------
            for t in Sr:
                node_y_in = 0.0
                node_y_out = 0.0
                for u in prod_mdp.node[t]['act']:
                    node_y_out += Y[(t, u)]
                for f in prod_mdp.predecessors_iter(t):
                    if f in Sr:
                        prop = prod_mdp.edge[f][t]['prop'].copy()
                        for uf in prop.iterkeys():
                            node_y_in += Y[(f, uf)] * prop[uf][0]
                if t == init_node:
                    model.addConstr(node_y_out == 1.0 + node_y_in,
                                    'init_node_flow_balance')
                else:
                    model.addConstr(node_y_out == node_y_in,
                                    'middle_node_flow_balance')
            print 'Prefix initial node flow balanced'
            print 'Prefix middle node flow balanced'
            #----------------------
            for mec in S_fi:
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    constr3 = 0
                    constr4 = 0
                    y_in_sf = 0
                    for u in act[s]:
                        constr3 += Z[(s, u)]
                    for f in prod_mdp.predecessors_iter(s):
                        if (f in sf) and (s not in ip):
                            prop = prod_mdp.edge[f][s]['prop'].copy()
                            for uf in act[f]:
                                if uf in prop.keys():
                                    constr4 += Z[(f, uf)] * prop[uf][0]
                                else:
                                    constr4 += Z[(f, uf)] * 0.00
                        if (f in sf) and (s in ip) and (f != s):
                            prop = prod_mdp.edge[f][s]['prop'].copy()
                            for uf in act[f]:
                                if uf in prop.keys():
                                    constr4 += Z[(f, uf)] * prop[uf][0]
                                else:
                                    constr4 += Z[(f, uf)] * 0.00
                        elif (f not in sf):
                            prop = prod_mdp.edge[f][s]['prop'].copy()
                            for uf in prop.iterkeys():
                                pe = prop[uf][0]
                                y_in_sf += Y[(f, uf)] * pe
                    if (s not in ip):
                        model.addConstr(constr3 == constr4 + y_in_sf,
                                        'balance_with_y_in')
                    elif (s in ip):
                        model.addConstr(constr3 == y_in_sf,
                                        'balance_with_y_in')
                print 'Suffix balance condition added'
                print 'Suffix initial y_in_sf condition added'
                #--------------------
                y_to_ip = 0.0
                y_out = 0.0
                for s in sf:
                    for t in prod_mdp.successors_iter(s):
                        if t not in sf:
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            for u in prop.iterkeys():
                                if u in act[s]:
                                    pe = prop[u][0]
                                    y_out += Z[(s, u)] * pe
                        elif t in ip:
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            for u in prop.iterkeys():
                                if u in act[s]:
                                    pe = prop[u][0]
                                    y_to_ip += Z[(s, u)] * pe
                model.addConstr(y_to_ip + y_out >= delta, 'sum_out')
                model.addConstr(y_to_ip >= (1.0 - gamma) * (y_to_ip + y_out),
                                'risk')
                print 'Suffix risk constraint added'
            #----------------------
            # solve
            print '--optimization starts--'
            model.optimize()
            # print '--variables value--'
            # for v in model.getVars():
            #     print v.varName, v.x
            # print 'obj:', model.objVal
            #------------------------------
            # compute plan prefix given the LP solution
            plan_prefix = dict()
            for s in Sr:
                norm = 0
                U = []
                P = []
                U_total = prod_mdp.node[s]['act'].copy()
                for u in U_total:
                    norm += Y[(s, u)].X
                for u in U_total:
                    U.append(u)
                    if norm > 0.01:
                        P.append(Y[(s, u)].X / norm)
                    else:
                        P.append(1.0 / len(U_total))
                plan_prefix[s] = [U, P]
            print "----Prefix plan generated"
            #--------------------
            #--------------------
            # compute the risk given the plan prefix
            cost_pre = 0.0
            risk_pre = 0.0
            y_to_sd = 0.0
            y_to_sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    prop = prod_mdp.edge[s][t]['prop'].copy()
                    for u in prop.iterkeys():
                        pe = prop[u][0]
                        ce = prop[u][1]
                        cost_pre += Y[(s, u)].X * pe * ce
                    if t in Sd:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sd += Y[(s, u)].X * pe
                    elif t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sf += Y[(s, u)].X * pe
            if (y_to_sd + y_to_sf) > 0:
                risk_pre = y_to_sd / (y_to_sd + y_to_sf)
            print 'y_to_sd: %s; y_to_sd+y_to_sf: %s' % (y_to_sd,
                                                        y_to_sd + y_to_sf)
            print "----Prefix risk computed: %s" % str(risk_pre)
            print "----Prefix cost computed: %s" % str(cost_pre)
            #--------------------
            # compute optimal plan suffix given the LP solution
            plan_suffix = dict()
            for mec in S_fi:
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    norm = 0
                    U = []
                    P = []
                    for u in act[s]:
                        norm += Z[(s, u)].X
                    for u in act[s]:
                        U.append(u)
                        if norm > 0.01:
                            P.append(Z[(s, u)].X / norm)
                        else:
                            P.append(1.0 / len(act[s]))
                    plan_suffix[s] = [U, P]
            print "----Suffix plan added"
            # compute risk given the plan suffix
            Risk_suf = []
            Cost_suf = []
            for mec in S_fi:
                risk_suf = 0.0
                cost_suf = 0.0
                y_to_ip = 0.0
                y_out = 0.0
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    for t in prod_mdp.successors_iter(s):
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            if u in act[s]:
                                pe = prop[u][0]
                                ce = prop[u][1]
                                cost_suf += Z[(s, u)].X * pe * ce
                        if (t not in sf):
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            for u in prop.iterkeys():
                                if u in act[s]:
                                    pe = prop[u][0]
                                    y_out += Z[(s, u)].X * pe
                        elif (t in ip):
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            for u in prop.iterkeys():
                                if u in act[s]:
                                    pe = prop[u][0]
                                    y_to_ip += Z[(s, u)].X * pe
                if (y_to_ip + y_out) > 0:
                    risk_suf = y_out / (y_to_ip + y_out)
                Risk_suf.append(risk_suf)
                Cost_suf.append(cost_suf)
                print 'one AMEC: y_out: %s; y_to_ip+y_out: %s' % (
                    y_out, y_to_ip + y_out)
            print "----Suffix risk computed: %s" % str(Risk_suf)
            print "----Suffix cost computed: %s" % str(Cost_suf)
            total_cost = model.objval
            print "----alpha*Prefix + (1-alpha)*Suffix cost computed"
            return total_cost, plan_prefix, cost_pre, risk_pre, plan_suffix, Cost_suf, Risk_suf, [
                Sf, ip_set, Sr, Sd
            ]
        except GurobiError:
            print "Gurobi Error reported"
            return None, None, None, None, None, None, None, None
Exemple #26
0
def syn_plan_prefix(prod_mdp, MEC, gamma):
    #----Synthesize optimal plan prefix to reach accepting MEC or SCC----
    #----with bounded risk and minimal expected total cost----
    print "===========[plan prefix synthesis starts]==========="
    sf = MEC[0]
    ip = MEC[1]  #force convergence to ip
    delta = 1.0
    for init_node in prod_mdp.graph['initial']:
        path_init = single_source_shortest_path(prod_mdp, init_node)
        print 'Reachable from init size: %s' % len(path_init.keys())
        if not set(path_init.keys()).intersection(sf):
            print "Initial node can not reach sf"
            return None, None, None, None
        Sn = set(path_init.keys()).difference(sf)
        #----find bad states that can not reach MEC
        simple_digraph = DiGraph()
        simple_digraph.add_edges_from(((v, u) for u, v in prod_mdp.edges()))
        path = single_source_shortest_path(simple_digraph,
                                           random.sample(ip, 1)[0])
        reachable_set = set(path.keys())
        print 'States that can reach sf, size: %s' % str(len(reachable_set))
        Sd = Sn.difference(reachable_set)
        Sr = Sn.intersection(reachable_set)
        # #--------------
        print 'Sn size: %s; Sd inside size: %s; Sr inside size: %s' % (
            len(Sn), len(Sd), len(Sr))
        # ---------solve lp------------
        print '-----'
        print 'Gurobi starts now'
        print '-----'
        try:
            #if True:
            Y = defaultdict(float)
            model = Model('plan_prefix')
            # create variables
            for s in Sr:
                for u in prod_mdp.node[s]['act'].copy():
                    Y[(s, u)] = model.addVar(vtype=GRB.CONTINUOUS,
                                             lb=0,
                                             name='y[(%s, %s)]' % (s, u))
            model.update()
            print 'Variables added'
            # set objective
            obj = 0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    prop = prod_mdp.edge[s][t]['prop'].copy()
                    for u in prop.iterkeys():
                        pe = prop[u][0]
                        ce = prop[u][1]
                        obj += Y[(s, u)] * pe * ce
            model.setObjective(obj, GRB.MINIMIZE)
            print 'Objective function set'
            # add constraints
            #------------------------------
            y_to_sd = 0.0
            y_to_sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    if t in Sd:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sd += Y[(s, u)] * pe
                    elif t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sf += Y[(s, u)] * pe
            model.addConstr(y_to_sf + y_to_sd >= delta, 'sum_out')
            model.addConstr(y_to_sf >= (1.0 - gamma) * (y_to_sf + y_to_sd),
                            'risk')
            print 'Risk constraint added'
            #--------------------
            for t in Sr:
                node_y_in = 0.0
                node_y_out = 0.0
                for u in prod_mdp.node[t]['act']:
                    node_y_out += Y[(t, u)]
                for f in prod_mdp.predecessors_iter(t):
                    if f in Sr:
                        prop = prod_mdp.edge[f][t]['prop'].copy()
                        for uf in prop.iterkeys():
                            node_y_in += Y[(f, uf)] * prop[uf][0]
                if t == init_node:
                    model.addConstr(node_y_out == 1.0 + node_y_in,
                                    'init_node_flow_balance')

                else:
                    model.addConstr(node_y_out == node_y_in,
                                    'middle_node_flow_balance')
            print 'Initial node flow balanced'
            print 'Middle node flow balanced'
            #----------------------
            # solve
            print '--optimization starts--'
            model.optimize()
            # print '--variables value--'
            # for v in model.getVars():
            #     print v.varName, v.x
            # print 'obj:', model.objVal
            #------------------------------
            # compute plan prefix given the LP solution
            plan_prefix = dict()
            for s in Sr:
                norm = 0
                U = []
                P = []
                U_total = prod_mdp.node[s]['act'].copy()
                for u in U_total:
                    norm += Y[(s, u)].X
                for u in U_total:
                    U.append(u)
                    if norm > 0.01:
                        P.append(Y[(s, u)].X / norm)
                    else:
                        P.append(1.0 / len(U_total))
                plan_prefix[s] = [U, P]
            print "----Prefix plan generated"
            cost = model.objval
            print "----Prefix cost computed"
            # compute the risk given the plan prefix
            risk = 0.0
            y_to_sd = 0.0
            y_to_sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    if t in Sd:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sd += Y[(s, u)].X * pe
                    elif t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sf += Y[(s, u)].X * pe
            if (y_to_sd + y_to_sf) > 0:
                risk = y_to_sd / (y_to_sd + y_to_sf)
            print 'y_to_sd: %s; y_to_sd+y_to_sf: %s' % (y_to_sd,
                                                        y_to_sd + y_to_sf)
            print "----Prefix risk computed: %s" % str(risk)
            # compute the input flow to the suffix
            y_in_sf = dict()
            for s in Sn:
                for t in prod_mdp.successors_iter(s):
                    if t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            if t not in y_in_sf:
                                y_in_sf[t] = Y[(s, u)].X * pe
                            else:
                                y_in_sf[t] += Y[(s, u)].X * pe
            # normalize the input flow
            y_total = 0.0
            for s, y in y_in_sf.iteritems():
                y_total += y
            print 'actual y_total: %s' % str(y_total)
            if y_total > 0:
                for s in y_in_sf.iterkeys():
                    y_in_sf[s] = y_in_sf[s] / y_total
                print "----Y in Sf computed and normalized"
            #print y_in_sf
            return plan_prefix, cost, risk, y_in_sf, Sr, Sd
        except GurobiError:
            print "Gurobi Error reported"
            return None, None, None, None
Exemple #27
0
     continue
 else:
     #no collative task
     if len(colla_ap[other_agent]) == 0:
         continue
     else:
         for ap_item in colla_ap[other_agent]:
             print(ap_item)
             #zai region zhong zhao dao suo you yu ap dui ying de qu yu dian
             cor_pos_list = find_region(ap_item,
                                        region_array[other_agent])
             print(cor_pos_list)
             #zai fts zhao dao mei ge qu yu dian de qian ji yi ji cost, qu zui xiao de cun xia lai
             cost_static = float('inf')
             fts_digraph = DiGraph()
             fts_digraph.add_edges_from(fts_array[other_agent].edges())
             print(fts_digraph.edges())
             for pos in cor_pos_list:
                 if len(pos) != 3:
                     continue
                 for pre_pos in fts_digraph.predecessors(pos):
                     if pre_pos == pos:  #pai chu yuan di bu dong de qing kuang
                         continue
                     cost_buf = distance(pos, pre_pos)
                     if cost_buf < cost_static:
                         cost_static = cost_buf
             #zai ben ji qi ren de product zhong jia bian ,ju ti jia fa an guomeng zhi qian gou jian product zi dong ji de fang fa
             print(cost_static)
             for static_edge in static_list[
                     agent_index]:  #shu ju jie guo de tong yi
                 if ap_item in static_edge[2]:
Exemple #28
0
def syn_plan_prefix(prod_mdp, MEC, gamma, init_node):
    #----Synthesize optimal plan prefix to reach accepting MEC or SCC----
    #----with bounded risk and minimal expected total cost----
    print "===========[plan prefix synthesis starts]==========="
    gamma_o = gamma[0]
    gamma_r = gamma[1]
    Sf = MEC[0]
    ip = MEC[1]
    delta = 1.0
    print 'init_node', init_node
    print 'init node not in Sf', (init_node not in Sf)
    if init_node not in Sf:
        print '----- init node in PREFIX ------'
        path_init = single_source_shortest_path(prod_mdp, init_node)
        print 'Reachable from init size: %s' % len(path_init.keys())
        if not set(path_init.keys()).intersection(Sf):
            print "Initial node can not reach Sf"
            return None, None, None, None, None, None
        Sn = set(path_init.keys()).difference(Sf)
        print 'Reachable nodes from init and not in Sf, size: %s' % str(
            len(Sn))
        #----find bad states that can not reach MEC
        simple_digraph = DiGraph()
        simple_digraph.add_edges_from(((v, u) for u, v in prod_mdp.edges()))
        path = single_source_shortest_path(simple_digraph,
                                           random.sample(ip, 1)[0])
        reachable_set = set(path.keys())
        print 'States that can reach Sf, size: %s' % str(len(reachable_set))
        Sd = Sn.difference(reachable_set)
        Sr = Sn.intersection(reachable_set)
        # #--------------
        print 'Sn size: %s; Sd inside size: %s; Sr inside size: %s' % (
            len(Sn), len(Sd), len(Sr))
        # ---------solve lp------------
        print '-----'
        print 'Gurobi starts now'
        print '-----'
        try:
            Y = defaultdict(float)
            model = Model('plan_prefix')
            # create variables
            for s in Sr:
                for u in prod_mdp.node[s]['act'].copy():
                    Y[(s, u)] = model.addVar(vtype=GRB.CONTINUOUS,
                                             lb=0,
                                             name='y[(%s,%s)]' % (s, u))
            model.update()
            print 'Variables added'
            # set objective
            obj = 0
            for s in Sr:
                for u in prod_mdp.node[s]['act']:
                    for t in prod_mdp.successors(s):
                        prop = prod_mdp[s][t]['prop'].copy()
                        if u in prop.keys():
                            xi = prop[u][3]
                            pe = prop[u][0]
                            ce = prop[u][2]
                            obj += Y[(s, u)] * pe * (ce - xi)
            model.setObjective(obj, GRB.MINIMIZE)
            print 'Objective function set'
            # add constraints
            #------------------------------
            y_to_sd = 0.0
            y_to_Sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors(s):
                    prop = prod_mdp[s][t]['prop'].copy()
                    for u in prop.iterkeys():
                        sigma = prop[u][1]
                        pe = prop[u][0]
                        if t in Sd:
                            y_to_sd += Y[(s, u)] * pe * (1 + sigma)
                        elif t in Sf:
                            y_to_Sf += Y[(s, u)] * pe * (1 + sigma)
            # model.addConstr(y_to_Sf+y_to_sd >= delta, 'sum_out')
            model.addConstr(y_to_Sf >= gamma_o, 'risk')
            print 'Risk constraint added'
            #------------------------------
            y_to_return = 0.0
            p_to_return = 0.0
            for t in prod_mdp.successors(init_node):
                prop = prod_mdp[init_node][t]['prop'].copy()
                for u in prop.iterkeys():
                    sigma = prop[u][1]
                    v = prod_mdp.v_return[t[0]]
                    pe = prop[u][0]
                    y_to_return += Y[(init_node, u)] * pe * (v + sigma)
                    p_to_return += Y[(init_node, u)]
            model.addConstr(y_to_return >= gamma_r * p_to_return, 'safety')
            #model.addConstr(y_to_return >= gamma_r, 'safety')
            print 'Safety constraint added'
            #--------------------
            for t in Sr:
                node_y_in = 0.0
                node_y_out = 0.0
                for u in prod_mdp.node[t]['act']:
                    node_y_out += Y[(t, u)]
                for f in prod_mdp.predecessors(t):
                    if f in Sr:
                        prop = prod_mdp[f][t]['prop'].copy()
                        for uf in prop.iterkeys():
                            #print 'f, uf, t, prop[uf]', [f, uf, t, prop[uf]]
                            node_y_in += Y[(f, uf)] * prop[uf][0]
                if t == init_node:
                    model.addConstr(node_y_out == 1.0 + node_y_in,
                                    'init_node_flow_balance for %s' % str(t))
                    # model.addConstr(node_y_out == 1.0, 'init_node_out_flow for %s' %str(t))
                    # node_y_in = 0.0
                    # for f in prod_mdp.predecessors(t):
                    #     if (f in Sr) and (f != t):
                    #         prop = prod_mdp[f][t]['prop'].copy()
                    #         for uf in prop.iterkeys():
                    #             #print 'f, uf, t, prop[uf]', [f, uf, t, prop[uf]]
                    #             node_y_in += Y[(f,uf)]*prop[uf][0]
                    # model.addConstr(node_y_in == 0.0, 'init_node_in_flow for %s' %str(t))
                else:
                    model.addConstr(node_y_out == node_y_in,
                                    'middle_node_flow_balance_for_%s' % str(t))
            print 'Initial node flow balanced'
            print 'Middle node flow balanced'
            model.update()
            #model.write("debug.lp")
            #----------------------
            # solve
            print '--optimization starts--'
            model.optimize()
            # print '--variables value--'
            # for v in model.getVars():
            #     print v.varName, v.x
            # print 'obj:', model.objVal
            #------------------------------
            # model.computeIIS()
            # print "The following constraint(s) cannot be satisfied:"
            # for c in model.getConstrs():
            #     if c.IISConstr:
            #         print c.constrName
            # compute plan prefix given the LP solution
            plan_prefix = dict()
            for s in Sr:
                norm = 0
                U = []
                P = []
                U_total = prod_mdp.node[s]['act'].copy()
                for u in U_total:
                    norm += Y[(s, u)].X
                for u in U_total:
                    U.append(u)
                    if norm > 0.01:
                        P.append(Y[(s, u)].X / norm)
                    else:
                        P.append(1.0 / len(U_total))
                plan_prefix[s] = [U, P]
            print "----Prefix plan generated----"
            print "----solution for current_node %s----" % str(init_node)
            Y_init_node = dict()
            for u in prod_mdp.node[init_node]['act'].copy():
                Y_init_node[u] = Y[(init_node, u)].X
            print 'Y_init_node', Y_init_node
            print "----Plan for current_node %s----" % str(init_node)
            print plan_prefix[init_node]
            print "----Plan for current_node----"
            print plan_prefix[init_node]
            cost = model.objval
            print "----Prefix cost computed"
            # compute the risk given the plan prefix
            risk = 0.0
            y_to_sd = 0.0
            y_to_Sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors(s):
                    if t in Sd:
                        prop = prod_mdp[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_sd += Y[(s, u)].X * pe
                    elif t in Sf:
                        prop = prod_mdp[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            y_to_Sf += Y[(s, u)].X * pe
                            if Y[(s, u)].X > 0.0:
                                print '(s,u), Y[(s,u)].X, pe', [(s, u),
                                                                Y[(s, u)].X,
                                                                pe]
            if (y_to_sd + y_to_Sf) > 0:
                risk = y_to_sd / (y_to_sd + y_to_Sf)
            print 'y_to_sd: %s; y_to_Sf: %s' % (y_to_sd, y_to_Sf)
            print "----Prefix risk computed: %s" % str(risk)
            # compute the input flow to the suffix
            y_in_Sf = dict()
            for s in Sn:
                for t in prod_mdp.successors(s):
                    if t in Sf:
                        prop = prod_mdp[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            if t not in y_in_Sf:
                                y_in_Sf[t] = Y[(s, u)].X * pe
                            else:
                                y_in_Sf[t] += Y[(s, u)].X * pe
            # normalize the input flow
            y_total = 0.0
            for s, y in y_in_Sf.iteritems():
                y_total += y
            print 'actual y_total: %s' % str(y_total)
            if y_total > 0:
                for s in y_in_Sf.iterkeys():
                    y_in_Sf[s] = y_in_Sf[s] / y_total
                print "----Y in Sf computed and normalized"
            #print y_in_Sf
            # compute safety
            y_to_return = []
            p_to_return = []
            for t in prod_mdp.successors(init_node):
                prop = prod_mdp[init_node][t]['prop'].copy()
                for u in prop.iterkeys():
                    sigma = prop[u][1]
                    v = prod_mdp.v_return[t[0]]
                    pe = prop[u][0]
                    y_to_return.append(Y[(init_node, u)].X * pe * (v + sigma))
                    p_to_return.append(Y[(init_node, u)].X)
            print "----Prefix safety computed: *%s* (%s) of *%s* (%s) = %.2f for given gamma_r %s" % (
                sum(y_to_return), str(y_to_return), sum(p_to_return),
                str(p_to_return),
                (sum(y_to_return) / sum(p_to_return)), gamma_r)
            return plan_prefix, cost, risk, y_in_Sf, Sr, Sd
        except GurobiError:
            print "Gurobi Error reported"
            return None, None, None, None, None, None
    else:
        print '----- init node not in PREFIX ------'
        return None, None, None, None, None, None
Exemple #29
0
def syn_plan_prefix(prod_mdp, MEC, gamma):
    #----Synthesize optimal plan prefix to reach accepting MEC or SCC----
    #----with bounded risk and minimal expected total cost----
    print "===========[plan prefix synthesis starts]==========="    
    sf = MEC[0]
    ip = MEC[1] #force convergence to ip
    delta = 1.0
    for init_node in prod_mdp.graph['initial']:
        path_init = single_source_shortest_path(prod_mdp, init_node)
        print 'Reachable from init size: %s' %len(path_init.keys())
        if not set(path_init.keys()).intersection(sf):
            print "Initial node can not reach sf"
            return None, None, None, None, None, None
        Sn = set(path_init.keys()).difference(sf)
        #----find bad states that can not reach MEC
        simple_digraph = DiGraph()
        simple_digraph.add_edges_from(((v,u) for u,v in prod_mdp.edges()))
        path = single_source_shortest_path(simple_digraph, random.sample(ip,1)[0])
        reachable_set = set(path.keys())
        print 'States that can reach sf, size: %s' %str(len(reachable_set))
        Sd = Sn.difference(reachable_set)
        Sr = Sn.intersection(reachable_set)
        # #--------------
        print 'Sn size: %s; Sd inside size: %s; Sr inside size: %s' %(len(Sn),len(Sd), len(Sr))
        # ---------solve lp------------
        print '-----'
        print 'Gurobi starts now'
        print '-----'
        try:
        #if True:
            Y = defaultdict(float)
            model = Model('plan_prefix')
            # create variables
            for s in Sr:
                for u in prod_mdp.node[s]['act'].copy():
                    Y[(s,u)] = model.addVar(vtype=GRB.CONTINUOUS,lb=0, name='y[(%s, %s)]' %(s, u))
            model.update()
            print 'Variables added'
            # set objective
            obj = 0
            for s in Sr:                
                for t in prod_mdp.successors_iter(s):
                    prop = prod_mdp.edge[s][t]['prop'].copy()
                    for u in prop.iterkeys():
                        pe = prop[u][0]
                        ce = prop[u][1]
                        obj += Y[(s,u)]*pe*ce
            model.setObjective(obj, GRB.MINIMIZE)
            print 'Objective function set'
            # add constraints
            #------------------------------
            y_to_sd = 0.0
            y_to_sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    if t in Sd:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys(): 
                            pe = prop[u][0]
                            y_to_sd += Y[(s,u)]*pe
                    elif t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys(): 
                            pe = prop[u][0]
                            y_to_sf += Y[(s,u)]*pe
            model.addConstr(y_to_sf+y_to_sd >= delta, 'sum_out')
            model.addConstr(y_to_sf >= (1.0-gamma)*(y_to_sf+y_to_sd), 'risk')            
            print 'Risk constraint added'
            #--------------------
            for t in Sr:
                node_y_in = 0.0
                node_y_out = 0.0
                for u in prod_mdp.node[t]['act']:   
                    node_y_out += Y[(t,u)]
                for f in prod_mdp.predecessors_iter(t):
                    if f in Sr:
                        prop = prod_mdp.edge[f][t]['prop'].copy()
                        for uf in prop.iterkeys():
                            node_y_in += Y[(f,uf)]*prop[uf][0]
                if t == init_node:
                    model.addConstr(node_y_out == 1.0 + node_y_in, 'init_node_flow_balance')

                else:
                    model.addConstr(node_y_out == node_y_in, 'middle_node_flow_balance')
            print 'Initial node flow balanced'
            print 'Middle node flow balanced'
            #----------------------
            # solve
            print '--optimization starts--'
            model.optimize()
            # print '--variables value--'
            # for v in model.getVars():
            #     print v.varName, v.x
            # print 'obj:', model.objVal
            #------------------------------
            # compute plan prefix given the LP solution
            plan_prefix = dict()
            for s in Sr:
                norm = 0
                U = []
                P = []
                U_total = prod_mdp.node[s]['act'].copy()          
                for u in U_total:
                    norm += Y[(s,u)].X
                for u in U_total:
                    U.append(u)
                    if norm > 0.01:
                        P.append(Y[(s,u)].X/norm)
                    else:
                        P.append(1.0/len(U_total))
                plan_prefix[s] = [U, P]
            print "----Prefix plan generated"
            cost = model.objval
            print "----Prefix cost computed"
            # compute the risk given the plan prefix
            risk = 0.0
            y_to_sd = 0.0
            y_to_sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    if t in Sd:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys(): 
                            pe = prop[u][0]
                            y_to_sd += Y[(s,u)].X*pe
                    elif t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys(): 
                            pe = prop[u][0]
                            y_to_sf += Y[(s,u)].X*pe
            if (y_to_sd+y_to_sf) >0:
                risk = y_to_sd/(y_to_sd+y_to_sf)
            print 'y_to_sd: %s; y_to_sd+y_to_sf: %s' %(y_to_sd, y_to_sd+y_to_sf)
            print "----Prefix risk computed: %s" %str(risk)
            # compute the input flow to the suffix
            y_in_sf = dict()
            for s in Sn:
                for t in prod_mdp.successors_iter(s):
                    if t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            pe = prop[u][0]
                            if t not in y_in_sf:
                                y_in_sf[t] = Y[(s,u)].X*pe
                            else:
                                y_in_sf[t] += Y[(s,u)].X*pe
            # normalize the input flow
            y_total = 0.0
            for s, y in y_in_sf.iteritems():
                y_total += y
            print 'actual y_total: %s' %str(y_total)
            if y_total >0:
                for s in y_in_sf.iterkeys():
                    y_in_sf[s] = y_in_sf[s]/y_total
                print "----Y in Sf computed and normalized"
            #print y_in_sf
            return plan_prefix, cost, risk, y_in_sf, Sr, Sd
        except GurobiError:
            print "Gurobi Error reported"
            return None, None, None, None, None, None
Exemple #30
0
def syn_plan_comb(prod_mdp, S_fi, gamma, alpha):
    #----Synthesize combined optimal plan prefix and suffix
    #----with bounded risk and minimal mean total cost----
    print "===========[plan prefix synthesis starts]==========="
    Sf = set()
    for mec in S_fi:
        Sf.update(mec[0]) # mec = (sf, ip, act)
    delta = 1.0
    for init_node in prod_mdp.graph['initial']:
        path_init = single_source_shortest_path(prod_mdp, init_node)
        print 'Reachable from init size: %s' %len(path_init.keys())
        if not set(path_init.keys()).intersection(Sf):
            print "Initial node can not reach Sf"
            return None, None, None, None, None, None, None, None
        Sn = set(path_init.keys()).difference(Sf)
        #----find bad states that can not reach MEC
        simple_digraph = DiGraph()
        simple_digraph.add_edges_from(((v,u) for u,v in prod_mdp.edges()))
        reachable_set = set()
        ip_set = set()
        for mec in S_fi:
            ip = mec[1]
            path = single_source_shortest_path(simple_digraph, random.sample(ip,1)[0])
            reachable_set.update(set(path.keys()))
            ip_set.update(ip)
        print 'States that can reach Sf, size: %s' %str(len(reachable_set))
        Sd = Sn.difference(reachable_set)
        Sr = Sn.intersection(reachable_set)
        # #--------------
        print 'Sn size: %s; Sd inside size: %s; Sr inside size: %s' %(len(Sn),len(Sd), len(Sr))
        # ---------solve lp------------
        print '-----'
        print 'Gurobi starts now'
        print '-----'
        try:
        #if True:
            model = Model('plan_comb')
            #--------------------
            #prefix variable
            Y = defaultdict(float)
            # create variables
            for s in Sr:
                for u in prod_mdp.node[s]['act'].copy():
                    Y[(s,u)] = model.addVar(vtype=GRB.CONTINUOUS,lb=0, name='y[(%s, %s)]' %(s, u))
            print 'Prefix Y variables added'
            #--------------------
            #suffix variables
            Z = defaultdict(float)
            for mec in S_fi:
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    for u in act[s]:
                        Z[(s,u)] = model.addVar(vtype=GRB.CONTINUOUS,lb=0, name='z[(%s, %s)]' %(s, u))
            print 'Suffix Z variables added'
            model.update()
            # set objective
            obj = 0
            for s in Sr:                
                for t in prod_mdp.successors_iter(s):
                    prop = prod_mdp.edge[s][t]['prop'].copy()
                    for u in prop.iterkeys():
                        pe = prop[u][0]
                        ce = prop[u][1]
                        obj += alpha*Y[(s,u)]*pe*ce
            for mec in S_fi:
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    for u in act[s]:
                        for t in prod_mdp.successors_iter(s):
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            if u in prop.keys():
                                pe = prop[u][0]
                                ce = prop[u][1]
                                obj += (1.0-alpha)*Z[(s,u)]*pe*ce
            model.setObjective(obj, GRB.MINIMIZE)
            print 'alpha*Prefix + (1.0-alpha)*Suffix cost Objective function set'
            # add constraints
            #------------------------------
            y_to_sd = 0.0
            y_to_sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    if t in Sd:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys(): 
                            pe = prop[u][0]
                            y_to_sd += Y[(s,u)]*pe
                    elif t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys(): 
                            pe = prop[u][0]
                            y_to_sf += Y[(s,u)]*pe
            model.addConstr(y_to_sf+y_to_sd >= delta, 'sum_out')
            model.addConstr(y_to_sf >= (1.0-gamma)*(y_to_sf+y_to_sd), 'risk')            
            print 'Prefix risk constraint added'
            #--------------------
            for t in Sr:
                node_y_in = 0.0
                node_y_out = 0.0
                for u in prod_mdp.node[t]['act']:   
                    node_y_out += Y[(t,u)]
                for f in prod_mdp.predecessors_iter(t):
                    if f in Sr:
                        prop = prod_mdp.edge[f][t]['prop'].copy()
                        for uf in prop.iterkeys():
                            node_y_in += Y[(f,uf)]*prop[uf][0]
                if t == init_node:
                    model.addConstr(node_y_out == 1.0 + node_y_in, 'init_node_flow_balance')
                else:
                    model.addConstr(node_y_out == node_y_in, 'middle_node_flow_balance')
            print 'Prefix initial node flow balanced'
            print 'Prefix middle node flow balanced'
            #----------------------
            for mec in S_fi:
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    constr3 = 0
                    constr4 = 0
                    y_in_sf = 0
                    for u in act[s]:
                        constr3 += Z[(s,u)]
                    for f in prod_mdp.predecessors_iter(s):
                        if (f in sf) and (s not in ip):
                            prop = prod_mdp.edge[f][s]['prop'].copy()
                            for uf in act[f]:
                                if uf in prop.keys():  
                                    constr4 += Z[(f,uf)]*prop[uf][0]
                                else:
                                    constr4 += Z[(f,uf)]*0.00
                        if (f in sf) and (s in ip) and (f != s):
                            prop = prod_mdp.edge[f][s]['prop'].copy()
                            for uf in act[f]:
                                if uf in prop.keys():  
                                    constr4 += Z[(f,uf)]*prop[uf][0]
                                else:
                                    constr4 += Z[(f,uf)]*0.00
                        elif (f not in sf):
                            prop = prod_mdp.edge[f][s]['prop'].copy()
                            for uf in prop.iterkeys(): 
                                pe = prop[uf][0]
                                y_in_sf += Y[(f,uf)]*pe
                    if (s not in ip):
                        model.addConstr(constr3 == constr4 + y_in_sf, 'balance_with_y_in')
                    elif (s in ip):
                        model.addConstr(constr3 == y_in_sf, 'balance_with_y_in')
                print 'Suffix balance condition added'
                print 'Suffix initial y_in_sf condition added'            
                #--------------------
                y_to_ip = 0.0
                y_out = 0.0
                for s in sf:
                    for t in prod_mdp.successors_iter(s):
                        if t not in sf:
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            for u in prop.iterkeys():
                                if u in act[s]:
                                    pe = prop[u][0]
                                    y_out += Z[(s,u)]*pe
                        elif t in ip:
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            for u in prop.iterkeys():
                                if u in act[s]:
                                    pe = prop[u][0]
                                    y_to_ip += Z[(s,u)]*pe
                model.addConstr(y_to_ip+y_out >= delta, 'sum_out')
                model.addConstr(y_to_ip >= (1.0-gamma)*(y_to_ip+y_out), 'risk')
                print 'Suffix risk constraint added'
            #----------------------
            # solve
            print '--optimization starts--'
            model.optimize()
            # print '--variables value--'
            # for v in model.getVars():
            #     print v.varName, v.x
            # print 'obj:', model.objVal
            #------------------------------
            # compute plan prefix given the LP solution
            plan_prefix = dict()
            for s in Sr:
                norm = 0
                U = []
                P = []
                U_total = prod_mdp.node[s]['act'].copy()          
                for u in U_total:
                    norm += Y[(s,u)].X
                for u in U_total:
                    U.append(u)
                    if norm > 0.01:
                        P.append(Y[(s,u)].X/norm)
                    else:
                        P.append(1.0/len(U_total))
                plan_prefix[s] = [U, P]
            print "----Prefix plan generated"
            #--------------------
            #--------------------
            # compute the risk given the plan prefix
            cost_pre = 0.0
            risk_pre = 0.0
            y_to_sd = 0.0
            y_to_sf = 0.0
            for s in Sr:
                for t in prod_mdp.successors_iter(s):
                    prop = prod_mdp.edge[s][t]['prop'].copy()
                    for u in prop.iterkeys():
                        pe = prop[u][0]
                        ce = prop[u][1]
                        cost_pre += Y[(s,u)].X*pe*ce
                    if t in Sd:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys(): 
                            pe = prop[u][0]
                            y_to_sd += Y[(s,u)].X*pe
                    elif t in sf:
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys(): 
                            pe = prop[u][0]
                            y_to_sf += Y[(s,u)].X*pe
            if (y_to_sd+y_to_sf) >0:
                risk_pre = y_to_sd/(y_to_sd+y_to_sf)
            print 'y_to_sd: %s; y_to_sd+y_to_sf: %s' %(y_to_sd, y_to_sd+y_to_sf)
            print "----Prefix risk computed: %s" %str(risk_pre)
            print "----Prefix cost computed: %s" %str(cost_pre)                        
            #--------------------
            # compute optimal plan suffix given the LP solution
            plan_suffix = dict()
            for mec in S_fi:
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    norm = 0
                    U = []
                    P = []
                    for u in act[s]:
                        norm += Z[(s,u)].X
                    for u in act[s]:
                        U.append(u)
                        if norm > 0.01:
                            P.append(Z[(s,u)].X/norm)
                        else:
                            P.append(1.0/len(act[s])) 
                    plan_suffix[s] = [U, P]
            print "----Suffix plan added"
            # compute risk given the plan suffix
            Risk_suf = []
            Cost_suf = []
            for mec in S_fi:
                risk_suf = 0.0
                cost_suf = 0.0
                y_to_ip = 0.0
                y_out = 0.0
                sf = mec[0]
                ip = mec[1]
                act = mec[2].copy()
                for s in sf:
                    for t in prod_mdp.successors_iter(s):
                        prop = prod_mdp.edge[s][t]['prop'].copy()
                        for u in prop.iterkeys():
                            if u in act[s]:
                                pe = prop[u][0]
                                ce = prop[u][1]
                                cost_suf += Z[(s,u)].X*pe*ce
                        if (t not in sf):
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            for u in prop.iterkeys():
                                if u in act[s]:
                                    pe = prop[u][0]
                                    y_out += Z[(s,u)].X*pe
                        elif (t in ip):
                            prop = prod_mdp.edge[s][t]['prop'].copy()
                            for u in prop.iterkeys():
                                if u in act[s]:
                                    pe = prop[u][0]
                                    y_to_ip += Z[(s,u)].X*pe
                if (y_to_ip+y_out)>0:
                    risk_suf = y_out/(y_to_ip+y_out)
                Risk_suf.append(risk_suf)
                Cost_suf.append(cost_suf)
                print 'one AMEC: y_out: %s; y_to_ip+y_out: %s' %(y_out, y_to_ip+y_out)                
            print "----Suffix risk computed: %s" %str(Risk_suf)
            print "----Suffix cost computed: %s" %str(Cost_suf)
            total_cost = model.objval
            print "----alpha*Prefix + (1-alpha)*Suffix cost computed"
            return total_cost, plan_prefix, cost_pre, risk_pre, plan_suffix, Cost_suf, Risk_suf, [Sf,ip_set,Sr,Sd]
        except GurobiError:
            print "Gurobi Error reported"
            return None, None, None, None, None, None, None, None
Exemple #31
0
def load(fname):
    def clean_bool(string):
        if string == "0":
            return None
        else:
            return string

    def to_bool(string):
        if string == "1" or string == "True":
            return True
        elif string == "0" or string == "False":
            return False
        else:
            return string

    def to_float(string):
        if string == "None":
            return None
        try:
            return float(string)
        except:
            return string

    mode = "node0"
    nodes = []
    edges = []
    pointers = set()
    outputs = None
    inputs = None
    named_ranges = {}
    infile = gzip.GzipFile(fname, 'rb')

    for line in infile.read().splitlines():

        line = line.decode("utf-8")

        if line == "====":
            mode = "node0"
            continue
        if line == "-----":
            cellmap_temp = {n.address(): n for n in nodes}
            Range = RangeFactory(cellmap_temp)
            mode = "node0"
            continue
        elif line == "edges":
            cellmap = {n.address(): n for n in nodes}
            mode = "edges"
            continue
        elif line == "outputs":
            mode = "outputs"
            continue
        elif line == "inputs":
            mode = "inputs"
            continue
        elif line == "named_ranges":
            mode = "named_ranges"
            continue

        if mode == "node0":
            [
                address, formula, python_expression, is_range, is_named_range,
                is_pointer, should_eval
            ] = line.split(SEP)
            formula = clean_bool(formula)
            python_expression = clean_bool(python_expression)
            is_range = to_bool(is_range)
            is_named_range = to_bool(is_named_range)
            is_pointer = to_bool(is_pointer)
            should_eval = should_eval
            mode = "node1"
        elif mode == "node1":
            if is_range:
                reference = json.loads(
                    line
                ) if is_pointer else line  # in order to be able to parse dicts
                vv = Range(reference)
                if is_pointer:
                    if not is_named_range:
                        address = vv.name
                    pointers.add(address)
                cell = Cell(address, None, vv, formula, is_range,
                            is_named_range, should_eval)
                cell.python_expression = python_expression
                nodes.append(cell)
            else:
                value = to_bool(to_float(line))
                cell = Cell(address, None, value, formula, is_range,
                            is_named_range, should_eval)
                cell.python_expression = python_expression
                if formula:
                    if 'OFFSET' in formula or 'INDEX' in formula:
                        pointers.add(address)
                    cell.compile()
                nodes.append(cell)
        elif mode == "edges":
            source, target = line.split(SEP)
            edges.append((cellmap[source], cellmap[target]))
        elif mode == "outputs":
            outputs = line.split(SEP)
        elif mode == "inputs":
            inputs = line.split(SEP)
        elif mode == "named_ranges":
            k, v = line.split(SEP)
            named_ranges[k] = v

    G = DiGraph()
    G.add_nodes_from(nodes)
    G.add_edges_from(edges)

    print("Graph loading done, %s nodes, %s edges, %s cellmap entries" %
          (len(G.nodes()), len(G.edges()), len(cellmap)))

    return (G, cellmap, named_ranges, pointers, outputs, inputs)