Skip to content
Snippets Groups Projects
Commit b5256c4b authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Modularize traces

parent 2e0d3c70
No related branches found
No related tags found
No related merge requests found
from .csvtrace import CSVTrace
from .randomtrace import RandomTrace
types = {
'csv': CSVTrace,
'random': RandomTrace
}
import csv
import random
from core import Process, TickProcess
class Trace(Process):
def __init__(self, duration, nodes):
super().__init__()
if duration < 1:
raise ValueError('duration must be greater than or equal to 1')
if nodes < 2:
raise ValueError('nodes must be greater than or equal to 2')
self.duration = duration
self.nodes = nodes
def process(self, network):
raise NotImplementedError
yield None
class RandomTrace(Trace, TickProcess):
def __init__(self, duration=100, nodes=10,
tick=1, min_toggles=1, max_toggles=2):
Trace.__init__(self, duration, nodes)
TickProcess.__init__(self, tick)
if min_toggles < 0:
raise ValueError('min_toggles < 0')
if min_toggles > max_toggles:
raise ValueError('min_toggles > max_toggles')
if max_toggles > nodes:
raise ValueError('max_toggles > nodes')
self.min_toggles = min_toggles
self.max_toggles = max_toggles
def process(self, network):
''''''
env = self.env
links = network.links
while env.now < self.duration:
to_toggle = random.randint(self.min_toggles, self.max_toggles)
for a, b in random.sample(links, to_toggle):
network.toggle_link(a, b)
yield env.timeout(self.tick_time)
from .trace import Trace
class CSVTrace(Trace):
def __init__(self, path=None):
......@@ -72,8 +28,3 @@ class CSVTrace(Trace):
self.file.close()
types = {
'random': RandomTrace,
'csv': CSVTrace,
}
from random import (
randint,
sample,
)
from .trace import TickTrace
class RandomTrace(TickTrace):
def __init__(self, duration=100, nodes=100, tick=1,
min_toggles=1, max_toggles=10):
super().__init__(duration, nodes, tick)
if min_toggles < 0:
raise ValueError('min_toggles < 0')
if min_toggles > max_toggles:
raise ValueError('min_toggles > max_toggles')
if max_toggles > nodes:
raise ValueError('max_toggles > nodes')
self.min_toggles = min_toggles
self.max_toggles = max_toggles
def process(self, network):
''''''
env = self.env
links = network.links
while env.now < self.duration:
to_toggle = randint(self.min_toggles, self.max_toggles)
for a, b in sample(links, to_toggle):
network.toggle_link(a, b)
yield self.tick()
from pydyton.core import Process, TickProcess
class Trace(Process):
def __init__(self, duration, nodes):
Process.__init__(self)
if duration < 1:
raise ValueError('duration must be greater than or equal to 1')
if nodes < 2:
raise ValueError('nodes must be greater than or equal to 2')
self.duration = duration
self.nodes = nodes
class TickTrace(Trace, TickProcess):
def __init__(self, duration, nodes, tick):
Trace.__init__(self, duration, nodes)
TickProcess.__init__(self, tick)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment