def test_add_node_with_initial_source_false(self, net):
        source = nodes.RandomBinaryStringSource(network=net)
        first_generation = self._fill(net)[0]

        # First generation is NOT conncted to source
        for agent in first_generation:
            assert source not in agent.neighbors(direction='from')
    def test_random_walk_from_source(self):

        net = models.Network()
        self.db.add(net)
        self.db.commit()

        agent1 = nodes.ReplicatorAgent(network=net)
        agent2 = nodes.ReplicatorAgent(network=net)
        agent3 = nodes.ReplicatorAgent(network=net)

        agent1.connect(whom=agent2)
        agent2.connect(whom=agent3)

        source = nodes.RandomBinaryStringSource(network=net)

        from operator import attrgetter
        source.connect(
            whom=min(net.nodes(type=Agent), key=attrgetter('creation_time')))
        source.create_information()

        processes.random_walk(net)

        agent1.receive()
        msg = agent1.infos()[0].contents

        processes.random_walk(net)
        agent2.receive()
        agent2.infos()[0].contents

        processes.random_walk(net)
        agent3.receive()
        agent3.infos()[0].contents

        assert msg == agent3.infos()[0].contents
Exemple #3
0
    def test_create_random_binary_string_source(self, db_session):
        net = models.Network()
        self.add(db_session, net)
        source = nodes.RandomBinaryStringSource(network=net)
        self.add(db_session, source)

        assert source
    def test_assigns_generation_correctly_when_addition_non_agent_included(self, net):
        nodes.RandomBinaryStringSource(network=net)
        net.max_size += 1  # Necessary hack if you want to add another Node.
        nodes.Environment(network=net)

        by_gen = self._fill(net)

        for generation in by_gen.values():
            assert len(generation) == net.generation_size
    def test_network_add_source_local(self, db_session):
        net = networks.Network()
        db_session.add(net)

        nodes.Agent(network=net)
        nodes.Agent(network=net)

        source = nodes.RandomBinaryStringSource(network=net)
        source.connect(whom=net.nodes(type=nodes.Agent)[0])

        assert len(net.vectors()) == 1
        assert [len(n.vectors(direction="outgoing")) for n in net.nodes(type=nodes.Agent)] == [0, 0]
        assert len(net.nodes(type=nodes.Source)[0].vectors(direction="outgoing")) == 1
Exemple #6
0
    def test_network_repr(self, db_session):
        net = networks.Network()
        db_session.add(net)

        nodes.Agent(network=net)
        nodes.Agent(network=net)

        source = nodes.RandomBinaryStringSource(network=net)
        source.connect(whom=net.nodes(type=nodes.Agent))

        assert repr(net) == ("<Network-" + str(net.id) +
                             "-network with 3 nodes, 2 vectors, "
                             "0 infos, 0 transmissions and 0 transformations>")
Exemple #7
0
    def test_chain_repr(self):
        net = networks.Chain()
        self.db.add(net)
        self.db.commit()

        source = nodes.RandomBinaryStringSource(network=net)
        net.add_node(source)

        for i in range(4):
            agent = nodes.ReplicatorAgent(network=net)
            net.add_node(agent)
        self.db.commit()

        assert repr(net) == ("<Network-" + str(net.id) +
                             "-chain with 5 nodes, 4 vectors, "
                             "0 infos, 0 transmissions and 0 transformations>")
    def test_moran_process_cultural(self, db_session):

        # Create a fully-connected network.
        net = models.Network()
        db_session.add(net)
        db_session.commit()

        agent1 = nodes.ReplicatorAgent(network=net)
        agent2 = nodes.ReplicatorAgent(network=net)
        agent3 = nodes.ReplicatorAgent(network=net)
        db_session.commit()

        agent1.connect(whom=agent2)
        agent1.connect(whom=agent3)
        agent2.connect(whom=agent1)
        agent2.connect(whom=agent3)
        agent3.connect(whom=agent1)
        agent3.connect(whom=agent2)

        # Add a global source and broadcast to all the nodes.
        source = nodes.RandomBinaryStringSource(network=net)
        for agent in net.nodes(type=Agent):
            source.connect(whom=agent)
            source.transmit(to_whom=agent)
            agent.receive()

        # Run a Moran process for 100 steps.
        for i in range(100):
            processes.moran_cultural(net)
            for agent in net.nodes(type=Agent):
                agent.receive()

        # Ensure that the process had reached fixation.
        from operator import attrgetter

        assert (max(agent1.infos(),
                    key=attrgetter("creation_time")).contents == max(
                        agent2.infos(),
                        key=attrgetter("creation_time")).contents)
        assert (max(agent2.infos(),
                    key=attrgetter("creation_time")).contents == max(
                        agent3.infos(),
                        key=attrgetter("creation_time")).contents)
        assert (max(agent3.infos(),
                    key=attrgetter("creation_time")).contents == max(
                        agent1.infos(),
                        key=attrgetter("creation_time")).contents)
Exemple #9
0
    def test_create_chain(self):
        net = networks.Chain()
        self.db.add(net)
        self.db.commit()

        source = nodes.RandomBinaryStringSource(network=net)
        net.add_node(source)

        for i in range(4):
            agent = nodes.Agent(network=net)
            net.add_node(agent)

        assert len(net.nodes(type=nodes.Agent)) == 4
        assert len(net.nodes(type=nodes.Source)) == 1
        assert len(net.vectors()) == 4
        assert net.nodes(type=nodes.Agent)[0].network == net
        assert net.nodes(type=nodes.Source)[0].network == net
    def test_add_node_with_initial_source_true(self, net):
        source = nodes.RandomBinaryStringSource(network=net)
        by_gen = self._fill(net)

        first_generation = by_gen[0]
        subsequent_generations = {gen: by_gen[gen] for gen in by_gen.keys() if gen > 0}

        # First generation is conncted to source
        for agent in first_generation:
            assert agent.neighbors(direction='from') == [source]

        # Subsequent generations get assigned a single random parent
        # from the previous generation
        for generation, agents in subsequent_generations.items():
            for agent in agents:
                parents = agent.neighbors(direction='from')
                assert len(parents) == 1
                assert parents[0] in by_gen[agent.generation - 1]
Exemple #11
0
    def test_transmit_random_binary_string_source(self, db_session):
        net = models.Network()
        self.add(db_session, net)
        source = nodes.RandomBinaryStringSource(network=net)
        agent = nodes.ReplicatorAgent(network=net)
        db_session.add(source)
        db_session.add(agent)
        db_session.commit()

        source.connect(whom=agent)
        self.add(db_session, source, agent)

        source.transmit(to_whom=agent)
        db_session.commit()

        agent.receive()
        db_session.commit()

        assert agent.infos()[0].contents in ["00", "01", "10", "11"]
Exemple #12
0
    def test_network_add_source_global(self):
        net = networks.Network()
        self.db.add(net)
        self.db.commit()

        agent1 = nodes.Agent(network=net)
        nodes.Agent(network=net)
        # self.db.commit()

        source = nodes.RandomBinaryStringSource(network=net)
        source.connect(whom=net.nodes(type=nodes.Agent))

        assert len(net.vectors()) == 2
        assert source.network == net
        assert agent1.network == net
        assert [
            len(n.vectors(direction="outgoing"))
            for n in net.nodes(type=nodes.Agent)
        ] == [0, 0]
        assert len(
            net.nodes(type=nodes.Source)[0].vectors(direction="outgoing")) == 2
Exemple #13
0
    def test_moran_process_sexual(self):

        # Create a fully-connected network.
        net = networks.Network()
        self.db.add(net)
        self.db.commit()

        agent1 = nodes.ReplicatorAgent(network=net)
        agent2 = nodes.ReplicatorAgent(network=net)
        agent3 = nodes.ReplicatorAgent(network=net)

        agent1.connect(direction="both", whom=[agent2, agent3])
        agent2.connect(direction="both", whom=agent3)

        # Add a global source and broadcast to all the nodes.
        source = nodes.RandomBinaryStringSource(network=net)
        source.connect(direction="to", whom=net.nodes(type=Agent))

        source.create_information()

        for agent in net.nodes(type=Agent):
            source.transmit(to_whom=agent)
            agent.receive()

        # Run a Moran process for 100 steps.
        for i in range(100):
            nodes.ReplicatorAgent(network=net)
            processes.moran_sexual(net)
            for agent in net.nodes(type=Agent):
                agent.receive()

        # Ensure that the process had reached fixation.
        assert agent1.failed is True
        assert agent2.failed is True
        assert agent3.failed is True

        for a in net.nodes(type=Agent):
            for a2 in net.nodes(type=Agent):
                assert a.infos()[0].contents == a2.infos()[0].contents
Exemple #14
0
    def test_broadcast_random_binary_string_source(self, db_session):
        net = models.Network()
        self.add(db_session, net)
        source = nodes.RandomBinaryStringSource(network=net)
        agent1 = nodes.ReplicatorAgent(network=net)
        agent2 = nodes.ReplicatorAgent(network=net)
        db_session.add(agent1)
        db_session.add(agent2)
        db_session.commit()
        source.connect(whom=agent1)
        source.connect(whom=agent2)
        self.add(db_session, source, agent1, agent2)

        source.transmit(what=source.create_information())
        db_session.commit()

        agent1.receive()
        agent2.receive()
        db_session.commit()

        assert agent1.infos()[0].contents in ["00", "01", "10", "11"]
        assert agent2.infos()[0].contents in ["00", "01", "10", "11"]
Exemple #15
0
    def test_create_sequential_microsociety(self):
        """Create a sequential microsociety."""
        net = networks.SequentialMicrosociety(n=3)
        self.db.add(net)
        self.db.commit()

        net.add_node(nodes.RandomBinaryStringSource(network=net))

        agent1 = nodes.Agent(network=net)
        net.add_node(agent1)

        agent2 = nodes.Agent(network=net)
        net.add_node(agent2)

        agent3 = nodes.Agent(network=net)
        net.add_node(agent3)

        agent4 = nodes.Agent(network=net)
        net.add_node(agent4)

        agent5 = nodes.Agent(network=net)
        net.add_node(agent5)

        agent6 = nodes.Agent(network=net)
        net.add_node(agent6)

        assert len(agent1.vectors(direction="outgoing")) == 2
        assert len(agent2.vectors(direction="outgoing")) == 2
        assert len(agent3.vectors(direction="outgoing")) == 2

        assert agent2.is_connected(direction="to", whom=agent3)
        assert agent2.is_connected(direction="to", whom=agent4)
        assert not agent2.is_connected(direction="to", whom=agent5)

        assert agent3.is_connected(direction="to", whom=agent4)
        assert agent3.is_connected(direction="to", whom=agent5)
        assert not agent3.is_connected(direction="to", whom=agent6)
    def test_add_node_fills_network_dimensions(self, net):
        nodes.RandomBinaryStringSource(network=net)
        self._fill(net)

        assert len(net.nodes(type=nodes.Source)) == 1
        assert len(net.nodes(type=nodes.Agent)) == self.n_gens * self.gen_size