Revision fa4a0a42 simulator.py

View differences:

simulator.py
1
import warnings
1 2
import util.EventScheduler as ES
2 3
from tqdm import tqdm  # https://pypi.org/project/tqdm/#usage
3 4
from util.UnitDiskGraph import UnitDiskGraph
4 5
from util.ConfPars import ConfSettings
6
import pandas as pd
5 7

  
6 8
import random
7 9
import code  # code.interact(local=dict(globals(), **locals()))
......
9 11
import networkx as nx
10 12
import util.MyUtil as myu
11 13
from time import sleep
14
import matplotlib.pyplot as plt
15
plt.ion()
12 16

  
13 17

  
14 18
class Simulator(object):
15 19

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

  
24 29
    def runSimulation(self):
30
        warnings.filterwarnings("ignore", module="matplotlib")
31
        sched = self.sched
25 32
        self.sched.schedule_event(0, "move!")
26 33

  
27 34
        print "\nRunning " + self.expName + "..."
28 35
        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()
36
            while(sched.elapsed_time() < self.params['duration']):
37
                pbar.update(sched.step())
38
                event = sched.pop_event()
32 39

  
33 40
                # Get new coordinates
34 41
                positions = next(self.mob)
......
37 44
                G = UnitDiskGraph(positions, self.params['radius']).getGraph()
38 45

  
39 46
                if self.gui:
40
                    self.draw(G, positions, self.params)
41
                sleep(0.01)
47
                    self.draw(G, positions)
48
                stats = ['BC', 'LC', 'DEG']
49

  
50
                self.logStatistics(G, stats, sched.processed_events())
42 51
                # schedule next DV
43 52
                jitter = [-1, 1][random.randrange(2)]*random.uniform(0, 0.866)
44
                self.sched.schedule_event(1.0 + jitter, "move!")
53
                sched.schedule_event(1.0 + jitter, "move!")
45 54

  
46 55
        return
47 56

  
48
    def draw(self, G, pos, params):
49
        myu.draw(G, pos, params['max_x'], params['max_y'])
57
    def logStatistics(self, G, whatLog, tag):
58
        tag = '%08d' % tag
59
        for stat in whatLog:
60
            if stat == 'BC':
61
                bw_centrality = nx.betweenness_centrality(
62
                    G, normalized=False,  weight='weight', endpoints=False)
63
                df = pd.DataFrame(bw_centrality.items(),
64
                                  columns=['Node', 'BC'])
65
                df.to_csv(self.OP+"/BC"+tag+".csv", sep=',',
66
                          encoding='utf-8', index=False)
67
            if stat == 'LC':
68
                load_indexes = nx.load_centrality(
69
                    G, normalized=False, weight='weight')
70
                df = pd.DataFrame(load_indexes.items(), columns=['Node', 'LC'])
71
                df.to_csv(self.OP+"/LC"+tag+".csv", sep=',',
72
                          encoding='utf-8', index=False)
73
            if stat == 'DEG':
74
                degrees = dict(G.degree())
75
                df = pd.DataFrame(degrees.items(), columns=['Node', 'DEG'])
76
                df.to_csv(self.OP+"/DEG"+tag+".csv", sep=',',
77
                          encoding='utf-8', index=False)
78

  
79
    def draw(self, G, pos):
80
        xmax = self.params['max_x']
81
        ymax = self.params['max_y']
82
        myu.draw(G, pos, xmax, ymax)

Also available in: Unified diff