Statistics
| Branch: | Revision:

mobicen / simulator.py @ fa4a0a42

History | View | Annotate | Download (2.98 KB)

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

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

    
11
import networkx as nx
12
import util.MyUtil as myu
13
from time import sleep
14
import matplotlib.pyplot as plt
15
plt.ion()
16

    
17

    
18
class Simulator(object):
19

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

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

    
34
        print "\nRunning " + self.expName + "..."
35
        with tqdm(total=self.params['duration']) as pbar:
36
            while(sched.elapsed_time() < self.params['duration']):
37
                pbar.update(sched.step())
38
                event = sched.pop_event()
39

    
40
                # Get new coordinates
41
                positions = next(self.mob)
42

    
43
                # Build new UnitDiskGraph
44
                G = UnitDiskGraph(positions, self.params['radius']).getGraph()
45

    
46
                if self.gui:
47
                    self.draw(G, positions)
48
                stats = ['BC', 'LC', 'DEG']
49

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

    
55
        return
56

    
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)