Revision 23c7ab1e simulator.py

View differences:

simulator.py
1
import graph_tool.centrality as gtc
1
import graph_tool.all as gt
2 2
import warnings
3 3
import util.EventScheduler as ES
4 4
from tqdm import tqdm  # https://pypi.org/project/tqdm/#usage
......
9 9

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

  
12
from pprint import pprint
13 13
import networkx as nx
14 14
import util.MyUtil as myu
15
import util.nx2gt as nx2gt
16 15
from time import sleep
17 16
import matplotlib.pyplot as plt
18 17
plt.ion()
......
22 21
    pos, radius = params['pos'], params['radius']
23 22
    # Build new UnitDiskGraph
24 23
    G, nxG = UnitDiskGraph(pos, radius).getGraph()
24
    params['gtG'] = G
25 25
    params['nxG'] = nxG
26 26
    ws = G.edge_properties["weight"]
27
    vp, ep = gtc.betweenness(G, norm=False, weight=ws)
27
    vp, ep = gt.betweenness(G, norm=False, weight=ws)
28 28
    btwd = {i: vp.a[i] for i in range(len(vp.a))}
29 29
    params['btw'] = btwd
30
    kcore = gt.kcore_decomposition(G)
31
    kcd = {i: kcore.a[i] for i in range(len(kcore.a))}
32
    params['kcore'] = kcd
33
    deg = G.degree_property_map("out")
34
    degd = {i: deg.a[i] for i in range(len(deg.a))}
35
    params['deg'] = degd
30 36
    return params
31 37

  
32 38

  
......
46 52
        for i in range(NN):
47 53
            f = open(outPath+"/stats"+'%08d' % i+".csv", 'a+')
48 54
            self.statsFiles[i] = f
49
            f.write("Time,BC\n")
55
            f.write("Time,BC,DEG,KCORE\n")
50 56

  
51 57
    def batch_tasks(self, sched, mobi, num_workers=5):
52 58
        # prepare next num_workers tasks
......
81 87
                current = sched.processed_events()
82 88
                pbar.update(current-prev)
83 89

  
84
                #code.interact(local=dict(globals(), **locals()))
85 90
                # Log results to disk
86 91
                for res in results:
87
                    self.logBTW(res['btw'], res['tag'])
92
                    #code.interact(local=dict(globals(), **locals()))
93
                    self.logRes(res, stats=['btw', 'deg', 'kcore'])
88 94

  
89
                #code.interact(local=dict(globals(), **locals()))
90 95
                if self.gui and self.num_workers == 1:
91 96
                    res = results[0]
92 97
                    self.draw(res['nxG'], res['pos'], res['btw'])
......
96 101
            self.statsFiles[f].close()
97 102
        return
98 103

  
99
    def logBTW(self, btw, tag):
100
        tag = '%08d' % tag
101
        for n, bc in btw.items():
102
            self.statsFiles[n].write(tag+","+str(bc)+"\n")
104
    def logRes(self, res, stats):
105
        tag = '%08d' % res['tag']
106
        for n in range(self.params['nodes_number']):
107
            btw = res['btw'][n]
108
            deg = res['deg'][n]
109
            kcore = res['kcore'][n]
110
            self.statsFiles[n].write(
111
                ",".join(map(str, [tag, btw, deg, kcore]))+"\n")
103 112

  
104 113
    def draw(self, G, pos, measures=None):
105 114
        xmax = self.params['max_x']

Also available in: Unified diff