Exemplo n.º 1
0
    def test_networkx(self):
        try:
            import networkx as nx
        except ImportError:
            return

        try:
            GraphSet.converters['to_graph'] = nx.Graph
            GraphSet.converters['to_edges'] = nx.Graph.edges

            g = nx.grid_2d_graph(3, 3)
            GraphSet.set_universe(g)
            g = GraphSet.universe()
            self.assertTrue(isinstance(g, nx.Graph))
            self.assertEqual(len(g.edges()), 12)

            v00, v01, v10 = (0, 0), (0, 1), (1, 0)
            e1, e2 = (v00, v01), (v00, v10)
            gs = GraphSet([nx.Graph([e1])])
            self.assertEqual(len(gs), 1)
            g = gs.pop()
            self.assertEqual(len(gs), 0)
            self.assertTrue(isinstance(g, nx.Graph))
            self.assertTrue(g.edges() == [(v00, v01)]
                            or g.edges() == [(v01, v00)])
            gs.add(nx.Graph([e2]))
            self.assertEqual(len(gs), 1)
        except:
            raise
        finally:
            GraphSet.converters['to_graph'] = lambda edges: edges
            GraphSet.converters['to_edges'] = lambda graph: graph
Exemplo n.º 2
0
    def test_networkx(self):
        try:
            import networkx as nx
        except ImportError:
            return

        try:
            if nx.__version__[0] == "1": # for NetworkX version 1.x
                GraphSet.converters['to_graph'] = nx.Graph
                GraphSet.converters['to_edges'] = nx.Graph.edges
            else: # for NetworkX version 2.x
                GraphSet.converters['to_graph'] = nx.from_edgelist
                GraphSet.converters['to_edges'] = nx.to_edgelist

            g = nx.grid_2d_graph(3, 3)
            GraphSet.set_universe(g)
            g = GraphSet.universe()
            self.assertTrue(isinstance(g, nx.Graph))
            self.assertEqual(len(g.edges()), 12)

            v00, v01, v10 = (0,0), (0,1), (1,0)
            e1, e2 = (v00, v01), (v00, v10)
            gs = GraphSet([nx.Graph([e1])])
            self.assertEqual(len(gs), 1)
            g = gs.pop()
            self.assertEqual(len(gs), 0)
            self.assertTrue(isinstance(g, nx.Graph))
            self.assertTrue(list(g.edges()) == [(v00, v01)] or list(g.edges()) == [(v01, v00)])
            gs.add(nx.Graph([e2]))
            self.assertEqual(len(gs), 1)
        except:
            raise
        finally:
            GraphSet.converters['to_graph'] = lambda edges: edges
            GraphSet.converters['to_edges'] = lambda graph: graph
Exemplo n.º 3
0
    def test_modifiers(self):
        v = [g0, g12, g13]
        gs = GraphSet(v)
        gs.add(g1)
        self.assertTrue(g1 in gs)

        gs.remove(g1)
        self.assertTrue(g1 not in gs)
        self.assertRaises(KeyError, gs.remove, g1)

        gs.add(g0)
        gs.discard(g0)
        self.assertTrue(g0 not in gs)
        gs.discard(g0)  # no exception raised

        gs = GraphSet(v)
        gs.add(e2)
        self.assertEqual(gs, GraphSet([g12, g123, g2]))

        gs = GraphSet(v)
        gs.remove(e2)
        self.assertEqual(gs, GraphSet([g0, g1, g13]))
        self.assertRaises(KeyError, gs.remove, e4)

        gs = GraphSet(v)
        gs.discard(e2)
        self.assertEqual(gs, GraphSet([g0, g1, g13]))
        gs.discard(e4)  # no exception raised

        v = [g1, g12, g13]
        gs = GraphSet(v)
        g = gs.pop()
        self.assertTrue(isinstance(g, list))
        self.assertTrue(g not in gs)
        self.assertEqual(gs | GraphSet([g]), GraphSet(v))

        self.assertTrue(gs)
        gs.clear()
        self.assertFalse(gs)

        self.assertRaises(KeyError, gs.pop)

        self.assertRaises(KeyError, gs.add, [(1,4)])
        self.assertRaises(KeyError, gs.remove, [(1,4)])
        self.assertRaises(KeyError, gs.discard, [(1,4)])

        self.assertRaises(KeyError, gs.add, (1,4))
        self.assertRaises(KeyError, gs.remove, (1,4))
        self.assertRaises(KeyError, gs.discard, (1,4))

        u = [g0, g1, g12, g123, g1234, g134, g14, g4]
        gs = GraphSet(u)
        gs.flip(e1)
        self.assertEqual(gs, GraphSet([g0, g1, g14, g2, g23, g234, g34, g4]))
Exemplo n.º 4
0
    def test_modifiers(self):
        v = [g0, g12, g13]
        gs = GraphSet(v)
        gs.add(g1)
        self.assertTrue(g1 in gs)

        gs.remove(g1)
        self.assertTrue(g1 not in gs)
        self.assertRaises(KeyError, gs.remove, g1)

        gs.add(g0)
        gs.discard(g0)
        self.assertTrue(g0 not in gs)
        gs.discard(g0)  # no exception raised

        gs = GraphSet(v)
        gs.add(e2)
        self.assertEqual(gs, GraphSet([g12, g123, g2]))

        gs = GraphSet(v)
        gs.remove(e2)
        self.assertEqual(gs, GraphSet([g0, g1, g13]))
        self.assertRaises(KeyError, gs.remove, e4)

        gs = GraphSet(v)
        gs.discard(e2)
        self.assertEqual(gs, GraphSet([g0, g1, g13]))
        gs.discard(e4)  # no exception raised

        v = [g1, g12, g13]
        gs = GraphSet(v)
        g = gs.pop()
        self.assertTrue(isinstance(g, list))
        self.assertTrue(g not in gs)
        self.assertEqual(gs | GraphSet([g]), GraphSet(v))

        self.assertTrue(gs)
        gs.clear()
        self.assertFalse(gs)

        self.assertRaises(KeyError, gs.pop)

        self.assertRaises(KeyError, gs.add, [(1, 4)])
        self.assertRaises(KeyError, gs.remove, [(1, 4)])
        self.assertRaises(KeyError, gs.discard, [(1, 4)])

        self.assertRaises(KeyError, gs.add, (1, 4))
        self.assertRaises(KeyError, gs.remove, (1, 4))
        self.assertRaises(KeyError, gs.discard, (1, 4))

        u = [g0, g1, g12, g123, g1234, g134, g14, g4]
        gs = GraphSet(u)
        gs.flip(e1)
        self.assertEqual(gs, GraphSet([g0, g1, g14, g2, g23, g234, g34, g4]))
Exemplo n.º 5
0
    print(weights_opt)

    # -----alpha----------
    f = calc_f(nk)
    m_np = calc_mnp(nk, f)
    failure_list = GraphSet()

    for i in range(0, f):
        failure_list.update(gc.len(E - i))
    print(failure_list.len())

    cand_select = gc.len(E - f)
    #print(cand_select.len())
    for cnt in range(0, m_np):
        rand_graph = next(cand_select.rand_iter())
        failure_list.add(rand_graph)
        cand_select.remove(rand_graph)

    print("total {0} patterns to consider".format(failure_list.len()))

    # weights_opt = {(0,1):1,(0,2):3,(0,3):2,(1,2):4,(1,3):2,(2,3):1}
    r = []
    # cng_list = []
    for i in failure_list:
        # print(i)
        G_fail = failed_G_cap(G_cap, i)
        # print(G_fail)
        cng_s, cng_d, r_val, congestion = calc_r(universe, i, tr, weights_opt,
                                                 G_fail)
        r.append((cng_s, cng_d,
                  round(r_val,
Exemplo n.º 6
0
tl_so = []
R_min_so = np.inf
w_opt_so = copy.deepcopy(weights)

#-----Set F_mf in PSO-M  and non-failure case in SO-----
cand_mf = GraphSet()
cand_so = GraphSet()

#Setting for F_mf
for i in range(0, f):
    cand_mf.update(gc.len(E - i))
cand_select = gc.len(E - f)
for cnt in range(0, m_np):
    rand_graph = next(cand_select.rand_iter())
    cand_mf.add(rand_graph)
    cand_select.remove(rand_graph)

#Setting non-failure set
cand_so = GraphSet()
cand_so.update(gc.len(E))
# for i in cand_so:
#     print(i)

# print("total {0} patterns to consider".format(cand_mf.len()))
# print("total {0} patterns to consider".format(cand_so.len()))
# for i in cand_mf:
#       print(i)

for loop in range(0, I_max):
Exemplo n.º 7
0
tl = []
R_min_global = np.inf
for itr_cand in range(1):
    cand_list = GraphSet()

    #fより小さい故障本数を持つものはcand_listへ
    for i in range(0, f):
        cand_list.update(gc.len(E - i))
    #print(cand_list.len())

    #f本故障はm_npの数だけ抽出(ひとまずランダムに)
    cand_select = gc.len(E - f)
    #print(cand_select.len())
    for cnt in range(0, m_np):
        rand_graph = next(cand_select.rand_iter())
        cand_list.add(rand_graph)
        cand_select.remove(rand_graph)

    print("total {0} patterns to consider".format(cand_list.len()))
    for i in cand_list:
        print(i)

    #-------------Optimization------------------
    I_max = 5
    tl = []
    R_min = np.inf
    MAX_loop = 100
    weights_opt = copy.deepcopy(weights)
    for i in range(0, I_max):
        C_cnt = 0
        C_max = 15