def add_node(self, node):
        """Add newcomers one by one, using linear preferential attachment."""
        nodes = self.nodes()

        # Start with a core of m0 fully-connected agents...
        if len(nodes) <= self.m0:
            other_nodes = [n for n in nodes if n.id != node.id]
            for n in other_nodes:
                node.connect(direction="both", whom=n)

        # ...then add newcomers one by one with preferential attachment.
        else:
            for idx_newvector in xrange(self.m):

                these_nodes = [
                    n for n in nodes if (
                        n.id != node.id and
                        not n.is_connected(direction="either", whom=node))]

                outdegrees = [
                    len(n.vectors(direction="outgoing")) for n in these_nodes]

                # Select a member using preferential attachment
                ps = [(d / (1.0 * sum(outdegrees))) for d in outdegrees]
                rnd = random.random() * sum(ps)
                cur = 0.0
                for i, p in enumerate(ps):
                    cur += p
                    if rnd < cur:
                        vector_to = these_nodes[i]

                # Create vector from newcomer to selected member and back
                node.connect(direction="both", whom=vector_to)
def add_node ( self , node ) : nodes = self . nodes ( ) if len ( nodes ) <= self . m0 : other_nodes = [ n for n in nodes if n . id != node . id ] for n in other_nodes : node . connect ( direction = "both" , whom = n ) else : for idx_newvector in xrange ( self . m ) : these_nodes = [ n for n in nodes if ( n . id != node . id and not n . is_connected ( direction = "either" , whom = node ) ) ] outdegrees = [ len ( n . vectors ( direction = "outgoing" ) ) for n in these_nodes ] ps = [ ( d / ( 1.0 * sum ( outdegrees ) ) ) for d in outdegrees ] rnd = random . random ( ) * sum ( ps ) cur = 0.0 for i , p in enumerate ( ps ) : cur += p if rnd < cur : vector_to = these_nodes [ i ] node . connect ( direction = "both" , whom = vector_to )
Add newcomers one by one, using linear preferential attachment.