Commit df36001b authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Clean up random node example.

parent 76feb6fc
"""Example simulation with custom random nodes."""
__author__ = "Jarrod Pas <j.pas@usask.ca>"
from random import Random
from pydtn import Network, Node, random_trace, random_traffic
class RandomNode(Node):
"""Node that sends packets to a random neightbour."""
def __init__(self, seed=None, **options):
"""
Create a random node.
Keyword Arguments:
seed -- seed for random number generator
"""
super().__init__(**options)
self.random = Random(seed)
def forward(self, packet):
"""Pick a random neighbour to send packet to."""
neighbours = list(self.neighbours)
if neighbours:
target = self.random.choice(neighbours)
......@@ -17,26 +30,33 @@ class RandomNode(Node):
def main():
"""Run a simulation with random nodes."""
duration = 500
nodes = 50
seed = 42
traffic_speed = 1
node_options = {
'seed': seed,
'tick_rate': 1,
}
nodes = {
node_id: RandomNode(**node_options)
for node_id in range(nodes)
}
trace = random_trace(nodes, seed=seed)
traffic = random_traffic(nodes, speed=traffic_speed, seed=seed)
network = Network(nodes, traffic=traffic, trace=trace)
traffic_options = {
'seed': seed,
'speed': 1,
}
traffic = random_traffic(nodes, **traffic_options)
trace_options = {
'seed': seed,
}
trace = random_trace(nodes, **trace_options)
# run simulation for 500 ticks
network.run(until=500)
network = Network(nodes, traffic=traffic, trace=trace)
network.run(until=duration)
print(network.stats_summary)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment