Skip to main content

nxsim

Nxsim is a Python package for simulating agents connected by any type of network using SimPy and Networkx in Python 3.4.

Install

pip3 install nxsim  # from PyPI
pip3 install git+git://github.com/kentwait/nxsim.git  # from GitHub

Quickstart

Nxsim provides a framework for doing forward-time simulations of events occurring in a network. It uses Networkx to create a network and SimPy 3 to create agents over each node in the network.

To create a simulation, nxsim requires a graph generated by Networkx and an “agent” class to populate each node of the network.

First, create a graph using Networkx.

import networkx as nx

number_of_nodes = 10
G = nx.complete_graph(number_of_nodes)

Then, subclass BaseNetworkAgent to create your own agent based on your needs.

from nxsim import BaseNetworkAgent

# Just like subclassing a process in SimPy
class MyAgent(BaseNetworkAgent):
    def __init__(self, environment=None, agent_id=0, state=()):  # Make sure to have these three keyword arguments
        super().__init__(environment=environment, agent_id=agent_id, state=state)
        # Add your own attributes here

    def run(self):
        # Add your behaviors here

Notice that “agents” in nxsim use the same concepts as “processes” in SimPy 3 except that their interactions can be limited by the graph in the simulation environment. For more information about SimPy, they have a great introduction posted on their website.

Here is a graph-based example:

import random
from nxsim import BaseNetworkAgent

class ZombieOutbreak(BaseNetworkAgent):
    def __init__(self, environment=None, agent_id=0, state=()):
        super().__init__(environment=environment, agent_id=agent_id, state=state)
        self.bite_prob = 0.05

    def run(self):
        while True:
            if self.state['id'] == 1:
                self.zombify()
                yield self.env.timeout(1)
            else:
                yield self.env.event()

    def zombify(self):
        normal_neighbors = self.get_neighboring_agents(state_id=0)
        for neighbor in normal_neighbors:
            if random.random() < self.bite_prob:
                neighbor.state['id'] = 1  # zombie
                print(self.env.now, self.id, neighbor.id, sep='\t')
                break

You can now set-up your simulation by creating a NetworkSimulation instance.

from nxsim import NetworkSimulation

# Initialize agent states. Let's assume everyone is normal.
# Add keys as as necessary, but "id" must always refer to that state category
init_states = [{'id': 0, } for _ in range(number_of_nodes)]

# Seed a zombie
init_states[5] = {'id': 1}
sim = NetworkSimulation(topology=G, states=init_states, agent_type=ZombieOutbreak,
                        max_time=30, dir_path='sim_01', num_trials=1, logging_interval=1.0)

And finally, start it up.

sim.run_simulation()

Running the simulation saves pickled dictionaries into the dir_path folder, in this case to “sim_01”. Now, let’s retrieve the history and states of the trial

trial = BaseLoggingAgent.open_trial_state_history(dir_path='sim_01', trial_id=0)

And plot the number of zombies per time interval using matplotlib:

from matplotlib import pyplot as plt
zombie_census = [sum([1 for node_id, state in g.items() if state['id'] == 1]) for t,g in trial.items()]
plt.plot(zombie_census)

And that’s it!

Note

This package is still under development. If you encounter a bug, please file an issue at https://github.com/kentwait/nxsim/issues to get it resolved.

Acknowledgment

Thanks to Joé Schaul for bringing ComplexNetworkSim to the world. This project is a SimPy 3- and Python 3.4-compatible fork of ComplexNetworkSim.

Metadata

Release files for nxsim 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nxsim 0.1.2
File Size Uploaded Provenance Actions
nxsim-0.1.2.tar.gz 5.2 kB Without provenance Details for nxsim-0.1.2.tar.gz

Release files / nxsim-0.1.2.tar.gz

Download URL nxsim-0.1.2.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b8bb8a03b21bac69b7de51435b37835629b3affa7994357346a8ca6f9819616f
BLAKE2b-256 checksum
How to use checksums
1a9b54dc90f75a1a1bb0b6ca1e7de668859925b99fa15dd34bb44d6cda415085
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for nxsim 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page