Barbell graph

Example I - Simple Barbell from scratch

[1]:
import pop2net as p2n

env = p2n.Environment()
creator = p2n.Creator(env)
inspector = p2n.NetworkInspector(env)


class Bell(p2n.LocationDesigner):
    n_locations = 2


class Bar(p2n.LocationDesigner):
    n_locations = 1

    def bridge(self, actor):
        return actor.Bell


env.add_actors([p2n.Actor() for _ in range(20)])

creator.create_locations(
    location_designers=[
        Bell,
        Bar,
    ]
)

inspector.plot_networks()

Example II - Connected opinion cluster

[2]:
class OpinionCluster(p2n.LocationDesigner):
    def split(self, actor):
        return actor.opinion


class Bridge(p2n.LocationDesigner):
    n_locations = 1

    def bridge(self, actor):
        return actor.opinion


env = p2n.Environment()
creator = p2n.Creator(env)
inspector = p2n.NetworkInspector(env)

for i in range(30):
    actor = p2n.Actor()
    actor.opinion = i % 3
    env.add_actor(actor)

creator.create_locations(location_designers=[OpinionCluster, Bridge])

inspector.plot_networks(actor_color="opinion")

Example III - Barbells with long bars

[3]:
import networkx as nx

env = p2n.Environment()
creator = p2n.Creator(env)
inspector = p2n.NetworkInspector(env)


class Bell(p2n.LocationDesigner):
    n_actors = 5
    n_locations = 4


class Bar(p2n.LocationDesigner):
    n_locations = 2
    nxgraph = nx.path_graph(5)

    def filter(self, actor):
        return not actor.Bell


class Bridge(p2n.LocationDesigner):
    def filter(self, actor):
        return actor.Bar_head or actor.Bar_tail or actor.Bell_head

    def bridge(self, actor):
        return actor.Bar_assigned


env.add_actors([p2n.Actor() for _ in range(30)])

creator.create_locations(
    location_designers=[
        Bell,
        Bar,
        Bridge,
    ]
)

inspector.plot_networks()