Esempio n. 1
0
    def test_QueueNetwork_transitions(self):

        degree = [len(self.qn.out_edges[k]) for k in range(self.qn.nV)]
        v, deg = np.argmax(degree), max(degree)

        trans = np.random.uniform(size=deg)
        trans = trans / sum(trans)
        probs = {v: {e[1]: p for e, p in zip(self.qn.g.out_edges(v), trans)}}

        self.qn.set_transitions(probs)
        mat = self.qn.transitions()
        tra = mat[v, [e[1] for e in self.qn.g.out_edges(v)]]

        self.assertTrue((tra == trans).all())

        tra = self.qn.transitions(return_matrix=False)
        tra = np.array([tra[v][e[1]] for e in self.qn.g.out_edges(v)])
        self.assertTrue((tra == trans).all())

        mat = qt.generate_transition_matrix(self.g)
        self.qn.set_transitions(mat)
        tra = self.qn.transitions()

        self.assertTrue(np.allclose(tra, mat))

        mat = qt.generate_transition_matrix(self.g)
        self.qn.set_transitions({v: {e[1]: mat[e] for e in self.qn.g.out_edges(v)}})
        tra = self.qn.transitions()

        self.assertTrue(np.allclose(tra[v], mat[v]))
Esempio n. 2
0
    def test_QueueNetwork_transitions(self):

        degree = [len(self.qn.out_edges[k]) for k in range(self.qn.nV)]
        v, deg = np.argmax(degree), max(degree)
        out_edges = sorted(self.qn.g.out_edges(v))

        trans = np.random.uniform(size=deg)
        trans = trans / sum(trans)
        probs = {v: {e[1]: p for e, p in zip(out_edges, trans)}}

        self.qn.set_transitions(probs)
        mat = self.qn.transitions()
        tra = mat[v, [e[1] for e in out_edges]]

        self.assertTrue((tra == trans).all())

        tra = self.qn.transitions(return_matrix=False)
        tra = np.array([tra[v][e[1]] for e in out_edges])
        self.assertTrue((tra == trans).all())

        mat = qt.generate_transition_matrix(self.g)
        self.qn.set_transitions(mat)
        tra = self.qn.transitions()

        self.assertTrue(np.allclose(tra, mat))

        mat = qt.generate_transition_matrix(self.g)
        self.qn.set_transitions({v: {e[1]: mat[e] for e in out_edges}})
        tra = self.qn.transitions()

        self.assertTrue(np.allclose(tra[v], mat[v]))
Esempio n. 3
0
    def test_generate_transition(self):
        g = qt.generate_random_graph(20)
        mat = qt.generate_transition_matrix(g)

        ans = np.sum(mat, axis=1)
        self.assertTrue(np.allclose(ans, 1))

        mat = qt.generate_transition_matrix(g, seed=10)
        ans = np.sum(mat, axis=1)
        self.assertTrue(np.allclose(ans, 1))
    def test_generate_transition(self):
        g = qt.generate_random_graph(20)
        mat = qt.generate_transition_matrix(g)

        ans = np.sum(mat, axis=1)
        self.assertTrue(np.allclose(ans, 1))

        mat = qt.generate_transition_matrix(g, seed=10)
        ans = np.sum(mat, axis=1)
        self.assertTrue(np.allclose(ans, 1))
Esempio n. 5
0
    def test_QueueNetwork_add_arrival(self):

        adj = {0: [1], 1: [2, 3]}
        g = qt.adjacency2graph(adj)
        qn = qt.QueueNetwork(g)
        mat = qt.generate_transition_matrix(g)
        qn.set_transitions(mat)

        qn.initialize(edges=(0, 1))
        qn.start_collecting_data(edge=[(1, 2), (1, 3)])

        qn.simulate(150000)

        data = qn.get_queue_data(edge=[(1, 2), (1, 3)])
        e0, e1 = qn.out_edges[1]

        p0 = np.sum(data[:, 5] == e0, dtype=float) / data.shape[0]
        p1 = np.sum(data[:, 5] == e1, dtype=float) / data.shape[0]

        trans = qn.transitions(False)

        self.assertAlmostEqual(trans[1][2], p0, 2)
        self.assertAlmostEqual(trans[1][3], p1, 2)
Esempio n. 6
0
    def test_QueueNetwork_add_arrival(self):

        adj = {0: [1], 1: [2, 3]}
        g = qt.adjacency2graph(adj)
        qn = qt.QueueNetwork(g)
        mat = qt.generate_transition_matrix(g)
        qn.set_transitions(mat)

        qn.initialize(edges=(0, 1))
        qn.start_collecting_data(edge=[(1, 2), (1, 3)])

        qn.simulate(150000)

        data = qn.get_queue_data(edge=[(1, 2), (1, 3)])
        e0, e1 = qn.out_edges[1]

        p0 = np.sum(data[:, 5] == e0, dtype=float) / data.shape[0]
        p1 = np.sum(data[:, 5] == e1, dtype=float) / data.shape[0]

        trans = qn.transitions(False)

        self.assertAlmostEqual(trans[1][2], p0, 2)
        self.assertAlmostEqual(trans[1][3], p1, 2)
Esempio n. 7
0
def create_mat(graph,seed):

    mat = qt.generate_transition_matrix(graph, seed=100)
    return mat
Esempio n. 8
0
 def test_test_graph_importerror(self):
     with self.assertRaises(TypeError):
         qt.generate_transition_matrix(1)
Esempio n. 9
0
"""
Returns the routing probabilities for each vertex in the graph.

Parameters:	
            return_matrix : bool (optional, the default is True)


Specifies whether an ndarray is returned. If False, a dict is returned instead.

Returns:	
out : a dict or ndarray

The transition probabilities for each vertex in the graph. 
If out is an ndarray, then out[v, u] returns the probability of a transition from vertex v to vertex u. 
If out is a dict then out_edge[v][u] is the probability of moving from vertex v to the vertex u.

"""

import queueing_tool as qt
import networkx as nx

g = nx.sedgewick_maze_graph()
net = qt.QueueNetwork(g)

mat = qt.generate_transition_matrix(g, seed=96)
net.set_transitions(mat)\

print(qt.graph2dict(g, False))  # adjacency
print(net.transitions(True))
 def test_test_graph_importerror(self):
     with self.assertRaises(TypeError):
         qt.generate_transition_matrix(1)