Exemplo n.º 1
0
def test_consensus():
    """
    Test the consensus method (and that it is properly called in the __init__)
    for extremely simple cases.
    """

    A = np.zeros((3, 3), dtype=int)  # Dummy empty adjaceny matrix
    ones = np.ones(3)  # Dummy variable for stock, groth_rates etc.

    # Two unconnected network components with different strategies
    A[0, 1] = A[1, 0] = 1
    S = np.array([1, 1, 0])
    m = ExploitCore(A, S, ones, ones, ones, ones, 0.5, 2.0)
    assert m.get_consensus() == True

    # Both strategies in the same component
    S = np.array([1, 0, 1])
    m = ExploitCore(A, S, ones, ones, ones, ones, 0.5, 2.0)
    assert m.get_consensus() == False

    # One component with one strategy
    A[0, 2] = A[2, 0] = 1
    S = np.array([1, 1, 1])
    m = ExploitCore(A, S, ones, ones, ones, ones, 0.5, 2.0)
    assert m.get_consensus() == True
Exemplo n.º 2
0
def test_update():
    """
    Test if one update process changes either the adjacency matrix or the
    strategies.
    """
    A = nx.adj_matrix(nx.erdos_renyi_graph(10, 0.3)).toarray()
    S = np.random.randint(2, size=10)
    stocks = np.ones(10)
    ones = np.ones(10)

    # If rewire (phi=1) A has to change
    m = ExploitCore(A.copy(), S, stocks.copy(), ones, ones, ones, 1.0, 2.0)
    m.run(steps=1)
    assert np.sum(np.abs(m.get_adjacency() - A)) == 4
    # Stocks decreased
    assert (m.get_stocks() < stocks).all()

    # If no rewirte (phi=0) S has to change
    m = ExploitCore(A, S.copy(), stocks.copy(), ones, ones, ones, 0.0, 2.0)
    m.run(steps=1)
    assert np.sum(np.abs(m.get_strategies() - S)) == 1
    # Stocks decreased
    assert (m.get_stocks() < stocks).all()
Exemplo n.º 3
0
def test_getters():
    """
    Test if the assignement and return of the adjacency matrix works
    """

    A = np.random.randint(2, size=(10, 10))
    S = np.random.randint(2, size=10)
    stocks = np.random.rand(10)
    ones = np.ones(10)
    # Dummy values
    m = ExploitCore(A, S, stocks, ones, ones, ones, 0.5, 2.0)
    assert (m.get_adjacency() == A).all()
    assert (m.get_strategies() == S).all()
    assert (m.get_stocks() == stocks).all()
    assert m.get_time() == 0