Skip to content
Snippets Groups Projects
randomtrace.py 931 B
Newer Older
  • Learn to ignore specific revisions
  • Jarrod Pas's avatar
    Jarrod Pas committed
    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()