Statistics
| Branch: | Revision:

mobicen / simulator.py @ e1cf8bea

History | View | Annotate | Download (1.61 KB)

1
import util.EventScheduler as ES
2
from tqdm import tqdm  # https://pypi.org/project/tqdm/#usage
3
from util.UnitDiskGraph import UnitDiskGraph
4
from util.ConfPars import ConfSettings
5

    
6
import random
7
import code  # code.interact(local=dict(globals(), **locals()))
8

    
9
import networkx as nx
10
import util.MyUtil as myu
11
from time import sleep
12

    
13

    
14
class Simulator(object):
15

    
16
    def __init__(self, settingsFile, sectionExp, gui):
17
        self.gui = gui
18
        self.sched = ES.EventScheduler()  # scheduler
19
        config = ConfSettings(settingsFile, sectionExp)
20
        self.params = config.getSimulationParameters()
21
        self.expName = sectionExp
22
        self.mob = config.configMobility(self.params)
23

    
24
    def runSimulation(self):
25
        self.sched.schedule_event(0, "move!")
26

    
27
        print "\nRunning " + self.expName + "..."
28
        with tqdm(total=self.params['duration']) as pbar:
29
            while(self.sched.elapsed_time() < self.params['duration']):
30
                pbar.update(self.sched.step())
31
                event = self.sched.pop_event()
32

    
33
                # Get new coordinates
34
                positions = next(self.mob)
35

    
36
                # Build new UnitDiskGraph
37
                G = UnitDiskGraph(positions, self.params['radius']).getGraph()
38

    
39
                if self.gui:
40
                    self.draw(G, positions, self.params)
41
                sleep(0.01)
42
                # schedule next DV
43
                jitter = [-1, 1][random.randrange(2)]*random.uniform(0, 0.866)
44
                self.sched.schedule_event(1.0 + jitter, "move!")
45

    
46
        return
47

    
48
    def draw(self, G, pos, params):
49
        myu.draw(G, pos, params['max_x'], params['max_y'])