コード例 #1
0
def test_setup():
    """ Test setup() for all object types """
    class MySetup:
        def setup(self, a):
            self.a = a + 1

    class MyAgentType(MySetup, ap.Agent):
        pass

    class MySpaceType(MySetup, ap.Space):
        pass

    class MyNwType(MySetup, ap.Network):
        pass

    class MyGridType(MySetup, ap.Grid):
        pass

    model = ap.Model()
    agents = ap.AgentList(model, 1, b=1)
    agents.extend(ap.AgentList(model, 1, MyAgentType, a=1))
    model.S1 = MySpaceType(model, shape=(1, 1), a=2)
    model.G1 = MyGridType(model, shape=(1, 1), a=3)
    model.N1 = MyNwType(model, a=4)

    # Standard setup implements keywords as attributes
    # Custom setup uses only keyword a and adds 1
    assert agents[0].b == 1
    assert agents[1].a == 2
    assert model.S1.a == 3
    assert model.G1.a == 4
    assert model.N1.a == 5
コード例 #2
0
def test_basics():
    model = ap.Model()
    l1 = ap.AgentList(model, 0)
    l2 = ap.AgentList(model, 1)
    l3 = ap.AgentList(model, 2)
    assert l1.__repr__() == "AgentList (0 objects)"
    assert l2.__repr__() == "AgentList (1 object)"
    assert l3.__repr__() == "AgentList (2 objects)"

    agentlist = ap.AgentList(model, 2)
    AgentDList = ap.AgentDList(model, 2)
    agentiter = ap.AgentIter(model, agentlist)
    AgentDListiter = ap.AgentDListIter(agentlist)
    attriter = agentiter.id

    assert np.array(agentlist).tolist() == list(agentlist)
    assert np.array(AgentDList).tolist() == list(AgentDList)
    assert np.array(agentiter).tolist() == list(agentiter)
    assert np.array(AgentDListiter).tolist() == list(AgentDListiter)
    assert np.array(attriter).tolist() == list(attriter)

    with pytest.raises(AgentpyError):
        _ = agentiter[2]  # No item lookup allowed

    # Seta and get attribute
    for agents in [agentlist, AgentDList, agentiter]:
        agents.x = 1
        agents.y = 1
        assert list(agents.x) == [1, 1]
        agents.x += agents.x
        assert list(agents.x) == [2, 2]
コード例 #3
0
ファイル: test_grid.py プロジェクト: JoelForamitti/agentpy
def test_move_torus():
    model = ap.Model()
    agents = ap.AgentList(model, 1)
    agent, = agents
    grid = ap.Grid(model, (4, 4), torus=True)
    grid.add_agents(agents, [[0, 0]])

    assert grid.positions[agent] == (0, 0)
    grid.move_by(agent, [-1, -1])
    assert grid.positions[agent] == (3, 3)
    grid.move_by(agent, [1, 0])
    assert grid.positions[agent] == (0, 3)
    grid.move_by(agent, [0, 1])
    assert grid.positions[agent] == (0, 0)

    model = ap.Model()
    agents = ap.AgentList(model, 1)
    agent, = agents
    grid = ap.Grid(model, (4, 4), torus=False)
    grid.add_agents(agents, [[0, 0]])

    assert grid.positions[agent] == (0, 0)
    grid.move_by(agent, [-1, -1])
    assert grid.positions[agent] == (0, 0)
    grid.move_by(agent, [6, 6])
    assert grid.positions[agent] == (3, 3)
コード例 #4
0
ファイル: test_grid.py プロジェクト: JoelForamitti/agentpy
def test_neighbors_with_torus():

    model = ap.Model()
    agents = ap.AgentList(model, 5)
    grid = ap.Grid(model, (4, 4), torus=True)
    grid.add_agents(agents, [[0, 0], [1, 3], [2, 0], [3, 2], [3, 3]])

    grid.apply(len).tolist()

    assert list(grid.neighbors(agents[0]).id) == [5, 2]

    model = ap.Model()
    agents = ap.AgentList(model, 5)
    grid = ap.Grid(model, (4, 4), torus=True)
    grid.add_agents(agents, [[0, 1], [1, 3], [2, 0], [3, 2], [3, 3]])

    grid.apply(len).tolist()

    assert list(grid.neighbors(agents[0]).id) == [4]
    assert list(grid.neighbors(agents[1]).id) == [3]

    for d in [2, 3, 4]:

        model = ap.Model()
        agents = ap.AgentList(model, 5)
        grid = ap.Grid(model, (4, 4), torus=True)
        grid.add_agents(agents, [[0, 1], [1, 3], [2, 0], [3, 2], [3, 3]])

        grid.apply(len).tolist()

        assert list(grid.neighbors(agents[0], distance=d).id) == [2, 3, 4, 5]
        assert list(grid.neighbors(agents[1], distance=d).id) == [1, 3, 4, 5]
コード例 #5
0
ファイル: test_network.py プロジェクト: JoelForamitti/agentpy
def test_add_agents():

    # Add agents to existing nodes
    graph = nx.Graph()
    graph.add_node(0)
    graph.add_node(1)
    model = ap.Model()

    env = ap.Network(model, graph=graph)
    agents = ap.AgentList(model, 2)
    env.add_agents(agents, positions=env.nodes)
    for agent in agents:
        agent.pos = env.positions[agent]
    agents.node = env.nodes
    env.graph.add_edge(*agents.pos)

    # Test structure
    assert list(agents.pos) == list(agents.node)
    assert env.nodes == env.graph.nodes()
    assert list(env.graph.edges) == [tuple(agents.pos)]
    assert list(env.neighbors(agents[0]).id) == [3]

    # Add agents as new nodes
    model2 = ap.Model()
    agents2 = ap.AgentList(model2, 2)
    env2 = ap.Network(model2)
    env2.add_agents(agents2)
    for agent in agents2:
        agent.pos = env2.positions[agent]
    env2.graph.add_edge(*agents2.pos)

    # Test if the two graphs are identical
    assert env.graph.nodes.__repr__() == env2.graph.nodes.__repr__()
    assert env.graph.edges.__repr__() == env2.graph.edges.__repr__()
コード例 #6
0
 def setup(self):
     self.E31 = EnvType3(self)
     self.E41 = EnvType4(self)
     self.E42 = EnvType4(self)
     self.agents1 = ap.AgentList(self, 2, AgentType1)
     self.agents2 = ap.AgentList(self, 2, AgentType2)
     self.agents = ap.AgentList(self, self.agents1 + self.agents2)
     self.envs = ap.AgentList(self, [self.E31, self.E41, self.E42])
コード例 #7
0
ファイル: test_grid.py プロジェクト: JoelForamitti/agentpy
def test_remove():
    model = ap.Model()
    agents = ap.AgentList(model, 2)
    grid = ap.Grid(model, (2, 2))
    grid.add_agents(agents)
    grid.remove_agents(agents[0])
    assert grid.apply(len).tolist() == [[0, 1], [0, 0]]

    # With track_empty
    model = ap.Model()
    agents = ap.AgentList(model, 2)
    grid = ap.Grid(model, (2, 2), track_empty=True)
    grid.add_agents(agents)
    assert list(grid.empty) == [(1, 1), (1, 0)]
    grid.remove_agents(agents[0])
    assert list(grid.empty) == [(1, 1), (1, 0), (0, 0)]
コード例 #8
0
ファイル: test_grid.py プロジェクト: JoelForamitti/agentpy
def test_grid_iter():
    model = ap.Model()
    agents = ap.AgentList(model, 4)
    grid = ap.Grid(model, (2, 2))
    grid.add_agents(agents)
    assert len(grid.agents) == 4
    assert len(grid.agents[0:1, 0:1]) == 1
コード例 #9
0
def make_space(s, n=0, torus=False):

    model = ap.Model()
    agents = ap.AgentList(model, n)
    space = ap.Space(model, (s, s), torus=torus)
    space.add_agents(agents)
    for agent in agents:
        agent.pos = space.positions[agent]
    return model, space, agents
コード例 #10
0
ファイル: examples.py プロジェクト: JoelForamitti/agentpy
    def setup(self):

        # Parameters
        s = self.p.size
        n = self.n = int(self.p.density * (s ** 2))

        # Create grid and agents
        self.grid = ap.Grid(self, (s, s), track_empty=True)
        self.agents = ap.AgentList(self, n, SegregationAgent)
        self.grid.add_agents(self.agents, random=True, empty=True)
コード例 #11
0
def test_combine_vars():

    model = ap.Model()
    model.record('test', 1)
    results = model.run(1, display=False)
    assert results._combine_vars().shape == (1, 1)

    model = ap.Model()
    agents = ap.AgentList(model, 1)
    agents.record('test', 1)
    results = model.run(1, display=False)
    assert results._combine_vars().shape == (1, 1)

    model = ap.Model()
    agents = ap.AgentList(model, 1)
    model.record('test', 1)
    agents.record('test', 2)
    results = model.run(1, display=False)
    assert results._combine_vars().shape == (2, 1)

    model = ap.Model()
    agents = ap.AgentList(model, 1)
    model.record('test', 1)
    agents.record('test', 2)
    results = model.run(1, display=False)
    assert results._combine_vars(obj_types="Model").shape == (1, 1)

    model = ap.Model()
    agents = ap.AgentList(model, 1)
    model.record('test', 1)
    agents.record('test', 2)
    results = model.run(1, display=False)
    assert results._combine_vars(obj_types="Doesn't exist") is None

    model = ap.Model()
    results = model.run(1, display=False)
    assert results._combine_vars() is None
    assert results._combine_pars() is None

    model = ap.Model({'test': 1})
    results = model.run(1, display=False)
    assert results._combine_pars(constants=False) is None
コード例 #12
0
def test_attr_list():
    model = ap.Model()
    model.agents = ap.AgentList(model, 2)
    model.agents.id[1] = 5
    assert model.agents.id[1] == 5
    model.agents.x = 1
    model.agents.f = lambda: 2
    assert list(model.agents.x) == [1, 1]
    assert list(model.agents.f()) == [2, 2]
    with pytest.raises(AttributeError):
        assert list(model.agents.y)  # Convert to list to call attribute
    with pytest.raises(TypeError):
        assert model.agents.x()  # noqa

    model = ap.Model()
    l3 = ap.AgentList(model, 2)
    assert l3.id == [1, 2]
    assert l3.id.__repr__() == "[1, 2]"
    assert l3.p.update({1: 1}) == [None, None]
    assert l3.p == [{1: 1}, {1: 1}]
コード例 #13
0
def test_add():
    model = ap.Model()
    agents1 = ap.AgentList(model, 2)
    agents2 = ap.AgentList(model, 2)
    agents3 = agents1 + agents2

    assert list(agents3.id) == [1, 2, 3, 4]

    agents4 = agents3 + [ap.Agent(model)]

    assert list(agents4.id) == [1, 2, 3, 4, 5]

    model = ap.Model()
    agents1 = ap.AgentDList(model, 2)
    agents2 = ap.AgentDList(model, 2)
    agents3 = agents1 + agents2

    assert list(agents3.id) == [1, 2, 3, 4]

    agents4 = agents3 + [ap.Agent(model)]

    assert list(agents4.id) == [1, 2, 3, 4, 5]
コード例 #14
0
def test_random():
    """ Test random shuffle and selection. """
    # Agent List
    model = ap.Model()
    model.run(steps=0, seed=1, display=False)
    model.agents = ap.AgentList(model, 10)
    assert list(model.agents.random())[0].id == 2
    assert model.agents.shuffle()[0].id == 9
    assert list(model.agents.random(11, replace=True).id)[0] == 6
    assert list(model.agents.random(2).id) == [9, 5]

    # Test with single agent
    model = ap.Model()
    agents = ap.AgentList(model, 1)
    assert agents.shuffle()[0] is agents[0]
    assert list(agents.random())[0] is agents[0]

    # Agent Group
    model = ap.Model()
    model.run(steps=0, seed=1, display=False)
    model.agents = ap.AgentDList(model, 10)
    assert list(model.agents.random())[0].id == 2
    assert list(model.agents.shuffle())[0].id == 9
コード例 #15
0
def test_sort():
    """ Test sorting method. """
    model = ap.Model()
    model.agents = ap.AgentList(model, 2)
    model.agents[0].x = 1
    model.agents[1].x = 0
    model.agents.sort('x')
    assert list(model.agents.x) == [0, 1]
    assert list(model.agents.id) == [2, 1]

    model = ap.Model()
    model.agents = ap.AgentDList(model, 2)
    model.agents[0].x = 1
    model.agents[1].x = 0
    model.agents = model.agents.sort('x')  # Not in-place
    assert list(model.agents.x) == [0, 1]
    assert list(model.agents.id) == [2, 1]
コード例 #16
0
ファイル: forest_fire.py プロジェクト: hayoc/peepo
    def setup(self):

        # Create agents (trees)
        n_trees = int(self.p['Tree density'] * (self.p.size**2))
        trees = self.agents = ap.AgentList(self, n_trees)

        # Create grid (forest)
        self.forest = ap.Grid(self, [self.p.size]*2, track_empty=True)
        self.forest.add_agents(trees, random=True, empty=True)

        # Initiate a dynamic variable for all trees
        # Condition 0: Alive, 1: Burning, 2: Burned
        self.agents.condition = 0

        # Start a fire from the left side of the grid
        unfortunate_trees = self.forest.agents[0:self.p.size, 0:2]
        unfortunate_trees.condition = 1
コード例 #17
0
ファイル: test_network.py プロジェクト: JoelForamitti/agentpy
def test_remove_agents():

    model = ap.Model()
    agents = ap.AgentList(model, 2)
    nw = ap.Network(model)
    nw.add_agents(agents)
    agent = agents[0]
    node = nw.positions[agent]
    nw.remove_agents(agent)
    assert len(nw.agents) == 1
    assert len(nw.nodes) == 2
    nw.remove_node(node)
    assert len(nw.agents) == 1
    assert len(nw.nodes) == 1
    agent2 = agents[1]
    nw.remove_node(nw.positions[agent2])
    assert len(nw.agents) == 0
    assert len(nw.nodes) == 0
コード例 #18
0
def test_agent_group():
    class MyAgent(ap.Agent):
        def method(self, x):
            if self.id == 2:
                self.model.agents.pop(x)
            self.model.called.append(self.id)

    # Delete later element in list
    model = ap.Model()
    model.called = []
    model.agents = ap.AgentDList(model, 4, MyAgent)
    model.agents.buffer().method(2)
    assert model.called == [1, 2, 4]

    # Delete earlier element in list
    model = ap.Model()
    model.called = []
    model.agents = ap.AgentDList(model, 4, MyAgent)
    model.agents.buffer().method(0)
    assert model.called == [1, 2, 3, 4]

    # Incorrect result without buffer
    model = ap.Model()
    model.called = []
    model.agents = ap.AgentList(model, 4, MyAgent)
    model.agents.method(0)
    assert model.called == [1, 2, 4]

    # Combine with buffer - still number 3 that gets deleted
    model = ap.Model()
    model.run(seed=2, steps=0, display=False)
    model.called = []
    model.agents = ap.AgentDList(model, 4, MyAgent)
    model.agents.shuffle().buffer().method(2)
    assert model.called == [2, 4, 1]

    # Combination order doesn't matter
    model = ap.Model()
    model.run(seed=2, steps=0, display=False)
    model.called = []
    model.agents = ap.AgentDList(model, 4, MyAgent)
    model.agents.buffer().shuffle().method(2)
    assert model.called == [2, 4, 1]
コード例 #19
0
def test_create_output():
    """ Should put variables directly into output if there are only model
    variables, or make a subdict if there are also other variables. """

    model = ap.Model()
    model.record('x', 0)
    model.run(1)
    assert list(model.output.variables.Model.keys()) == ['x']

    model = ap.Model(_run_id=(1, 2))
    model.agents = ap.AgentList(model, 1)
    model.agents.record('x', 0)
    model.record('x', 0)
    model.run(1)
    assert list(model.output.variables.keys()) == ['Agent', 'Model']

    # Run id and scenario should be added to output
    assert model.output.variables.Model.reset_index()['sample_id'][0] == 1
    assert model.output.variables.Model.reset_index()['iteration'][0] == 2
コード例 #20
0
def test_select():
    """ Select subsets with boolean operators. """
    model = ap.Model()
    model.agents = ap.AgentList(model, 3)
    selection1 = model.agents.id == 2
    selection2 = model.agents.id != 2
    selection3 = model.agents.id < 2
    selection4 = model.agents.id > 2
    selection5 = model.agents.id <= 2
    selection6 = model.agents.id >= 2
    assert selection1 == [False, True, False]
    assert selection2 == [True, False, True]
    assert selection3 == [True, False, False]
    assert selection4 == [False, False, True]
    assert selection5 == [True, True, False]
    assert selection6 == [False, True, True]
    assert list(model.agents.select(selection1).id) == [2]

    model = ap.Model()
    model.agents = ap.AgentDList(model, 3)
    selection1 = model.agents.id == 2
    assert selection1 == [False, True, False]
    assert list(model.agents.select(selection1).id) == [2]
コード例 #21
0
def test_arithmetics():
    """ Test arithmetic operators """

    model = ap.Model()
    model.agents = ap.AgentList(model, 3)
    agents = model.agents

    agents.x = 1
    assert agents.x.attr == "x"
    assert list(agents.x) == [1, 1, 1]

    agents.y = ap.AttrIter([1, 2, 3])
    assert list(agents.y) == [1, 2, 3]

    agents.x = agents.x + agents.y
    assert list(agents.x) == [2, 3, 4]

    agents.x = agents.x - ap.AttrIter([1, 1, 1])
    assert list(agents.x) == [1, 2, 3]

    agents.x += 1
    assert list(agents.x) == [2, 3, 4]

    agents.x -= 1
    assert list(agents.x) == [1, 2, 3]

    agents.x *= 2
    assert list(agents.x) == [2, 4, 6]

    agents.x = agents.x * agents.x
    assert list(agents.x) == [4, 16, 36]

    agents.x = agents.x / agents.x
    assert list(agents.x)[0] == pytest.approx(1.)

    agents.x /= 2
    assert list(agents.x)[0] == pytest.approx(0.5)
コード例 #22
0
def test_remove():
    model = ap.Model()
    agents = ap.AgentList(model, 3, ap.Agent)
    assert list(agents.id) == [1, 2, 3]
    agents.remove(agents[0])
    assert list(agents.id) == [2, 3]

    model = ap.Model()
    agents = ap.AgentDList(model, 3, ap.Agent)
    assert list(agents.id) == [1, 2, 3]
    agents.remove(agents[0])
    assert list(agents.id) == [3, 2]

    model = ap.Model()
    agents = ap.AgentDList(model, 3, ap.Agent)
    assert list(agents.id) == [1, 2, 3]
    agents.pop(0)
    assert list(agents.id) == [3, 2]

    model = ap.Model()
    agents = ap.AgentSet(model, 3, ap.Agent)
    assert set(agents.id) == set([1, 2, 3])
    agents.remove(next(iter(agents)))
    assert len(agents.id) == 2
コード例 #23
0
 def step(self):
     self.report('x', self.p.x)
     self.agents = ap.AgentList(self, 1)
     self.agents.record('id')
     self.record('id')
     self.stop()
コード例 #24
0
ファイル: test_grid.py プロジェクト: JoelForamitti/agentpy
def test_add_agents():
    model = ap.Model()
    grid = ap.Grid(model, (2, 2))
    agents = ap.AgentList(model, 5)
    grid.add_agents(agents)
    assert grid.apply(len).tolist() == [[2, 1], [1, 1]]

    # Passed positions
    model = ap.Model()
    grid = ap.Grid(model, (2, 2))
    agents = ap.AgentList(model, 2)
    grid.add_agents(agents, [[0, 0], [1, 1]])
    assert grid.apply(len).tolist() == [[1, 0], [0, 1]]

    model = ap.Model()
    model.sim_setup(seed=1)
    grid = ap.Grid(model, (2, 2))
    agents = ap.AgentList(model, 5)
    grid.add_agents(agents, random=True)
    assert grid.apply(len).tolist() == [[0, 3], [1, 1]]

    with pytest.raises(AgentpyError):
        # Can't add more agents than empty positions
        model = ap.Model()
        model.sim_setup(seed=1)
        grid = ap.Grid(model, (2, 2), track_empty=True)
        agents = ap.AgentList(model, 5)
        grid.add_agents(agents, empty=True)

    with pytest.raises(AgentpyError):
        # Can't use empty if track_empty is False
        model = ap.Model()
        model.sim_setup(seed=1)
        grid = ap.Grid(model, (2, 2))
        agents = ap.AgentList(model, 5)
        grid.add_agents(agents, empty=True)

    model = ap.Model()
    model.sim_setup(seed=1)
    grid = ap.Grid(model, (2, 2), track_empty=True)
    agents = ap.AgentList(model, 2)
    grid.add_agents(agents, empty=True)
    agents = ap.AgentList(model, 2)
    grid.add_agents(agents, empty=True)
    assert grid.apply(len).tolist() == [[1, 1], [1, 1]]

    model = ap.Model()
    model.sim_setup(seed=1)
    grid = ap.Grid(model, (2, 2), track_empty=True)
    agents = ap.AgentList(model, 2)
    grid.add_agents(agents)
    agents = ap.AgentList(model, 2)
    grid.add_agents(agents)
    assert grid.apply(len).tolist() == [[2, 2], [0, 0]]

    model = ap.Model()
    model.sim_setup(seed=2)
    grid = ap.Grid(model, (2, 2), track_empty=True)
    agents = ap.AgentList(model, 2)
    grid.add_agents(agents, empty=True)
    agents = ap.AgentList(model, 1)
    grid.add_agents(agents, random=True, empty=True)
    assert grid.apply(len).tolist() == [[1, 1], [0, 1]]

    model = ap.Model()
    model.sim_setup(seed=2)
    grid = ap.Grid(model, (2, 2), track_empty=True)
    agents = ap.AgentList(model, 2)
    grid.add_agents(agents, empty=True)
    agents = ap.AgentList(model, 1)
    grid.add_agents(agents, random=True)
    assert grid.apply(len).tolist() == [[2, 1], [0, 0]]
コード例 #25
0
ファイル: test_grid.py プロジェクト: JoelForamitti/agentpy
def make_grid(s, n=0, track_empty=False, agent_cls=ap.Agent):
    model = ap.Model()
    agents = ap.AgentList(model, n, agent_cls)
    grid = ap.Grid(model, (s, s), track_empty=track_empty)
    grid.add_agents(agents)
    return model, grid, agents
コード例 #26
0
 def setup(self):
     self.agents = ap.AgentList(
         self, self.p.n, MoneyAgent)
コード例 #27
0
ファイル: examples.py プロジェクト: JoelForamitti/agentpy
 def setup(self):
     self.agents = ap.AgentList(self, self.p.agents, WealthAgent)