Switch to dictionary for the network matrices

This commit is contained in:
Loic Guegan 2022-06-10 19:49:19 +02:00
parent 94b47ceab5
commit 7540e8290d
26 changed files with 72 additions and 72 deletions

66
esds.py
View file

@ -190,13 +190,14 @@ class Simulator:
one may not gives accurate results because of missing entries in the nodes received queues. one may not gives accurate results because of missing entries in the nodes received queues.
""" """
def __init__(self,B_wlan0,L_wlan0,B_eth0,L_eth0): def __init__(self,netmat):
self.B_wlan0=B_wlan0 """
self.L_wlan0=L_wlan0 Format of netmat: { "wlan0": (BW,L), "eth0": (BW,L) }
self.B_eth0=B_eth0 Where BW are the bandwidth matrices and L the latency matrices
self.L_eth0=L_eth0 """
self.netmat=netmat
self.nodes=list() self.nodes=list()
self.sharing=np.zeros(len(B_eth0)) self.sharing=np.zeros(len(netmat["eth0"]["bandwidth"]))
self.events=np.empty((0,4),dtype=object) self.events=np.empty((0,4),dtype=object)
self.events_dirty=True # For optimization reasons self.events_dirty=True # For optimization reasons
self.startat=-1 self.startat=-1
@ -207,44 +208,29 @@ class Simulator:
self.wait_end_nodes=list() # Keep track of nodes that wait for the end of the simulation self.wait_end_nodes=list() # Keep track of nodes that wait for the end of the simulation
self.time_truncated=format(self.time,self.precision) # Truncated version is used in log print self.time_truncated=format(self.time,self.precision) # Truncated version is used in log print
def update_network(self,B_wlan0,L_wlan0,B_eth0,L_eth0): def update_network(self,netmat):
for event in self.events: for event in self.events:
if int(event[0]) == 0: if int(event[0]) == 0:
cur_event=event[2] cur_event=event[2]
ts=float(event[1]) ts=float(event[1])
src_id,dst_id,interface, data, datasize,duration, datasize_remaining,start_at=cur_event src_id,dst_id,interface, data, datasize,duration, datasize_remaining,start_at=cur_event
if interface == "wlan0": new_bw=netmat[interface]["bandwidth"][int(src_id),int(dst_id)]
new_bw=B_wlan0[int(src_id),int(dst_id)] old_bw=self.netmat[interface]["bandwidth"][int(src_id),int(dst_id)]
old_bw=self.B_wlan0[int(src_id),int(dst_id)] new_lat=netmat[interface]["latency"][int(src_id),int(dst_id)]
new_lat=L_wlan0[int(src_id),int(dst_id)] old_lat=self.netmat[interface]["latency"][int(src_id),int(dst_id)]
old_lat=self.L_wlan0[int(src_id),int(dst_id)] if new_bw != old_bw or new_lat != old_lat:
if new_bw != old_bw or new_lat != old_lat: new_datasize_remaining=float(datasize_remaining)*((ts-self.time)/float(duration))
new_datasize_remaining=float(datasize_remaining)*((ts-self.time)/float(duration)) if new_datasize_remaining > 0:
if new_datasize_remaining > 0: latency_factor=new_datasize_remaining/float(datasize)
latency_factor=new_datasize_remaining/float(datasize) if interface == "wlan0":
new_duration=new_datasize_remaining*8/new_bw+new_lat*latency_factor new_duration=new_datasize_remaining*8/new_bw+new_lat*latency_factor
event[1]=self.time+new_duration else:
event[2][6]=new_datasize_remaining
event[2][5]=new_duration
else:
new_bw=B_eth0[int(src_id),int(dst_id)]
old_bw=self.B_eth0[int(src_id),int(dst_id)]
new_lat=L_eth0[int(src_id),int(dst_id)]
old_lat=self.L_eth0[int(src_id),int(dst_id)]
if new_bw != old_bw or new_lat != old_lat:
new_datasize_remaining=float(datasize_remaining)*((ts-self.time)/float(duration))
if new_datasize_remaining > 0:
latency_factor=new_datasize_remaining/float(datasize)
new_duration=new_datasize_remaining*8/(new_bw/self.sharing[int(dst_id)])+new_lat*latency_factor new_duration=new_datasize_remaining*8/(new_bw/self.sharing[int(dst_id)])+new_lat*latency_factor
event[1]=self.time+new_duration event[1]=self.time+new_duration
event[2][6]=new_datasize_remaining event[2][6]=new_datasize_remaining
event[2][5]=new_duration event[2][5]=new_duration
self.netmat=netmat
self.B_wlan0=B_wlan0
self.L_wlan0=L_wlan0
self.B_eth0=B_eth0
self.L_eth0=L_eth0
def debug(self): def debug(self):
""" """
Log all the informations for debugging Log all the informations for debugging
@ -467,7 +453,7 @@ class Simulator:
self.log("Send "+str(datasize)+" bytes on "+interface,node=src) self.log("Send "+str(datasize)+" bytes on "+interface,node=src)
for dst in self.list_wireless_receivers(nsrc): for dst in self.list_wireless_receivers(nsrc):
if self.nodes[dst]["turned_on"]: if self.nodes[dst]["turned_on"]:
duration=datasize*8/self.B_wlan0[src,dst]+self.L_wlan0[src,dst] duration=datasize*8/self.netmat["wlan0"]["bandwidth"][src,dst]+self.netmat["wlan0"]["latency"][src,dst]
if src == dst: if src == dst:
self.add_event(0,duration+self.time,(src,dst,interface,data,datasize,duration,datasize,self.time)) self.add_event(0,duration+self.time,(src,dst,interface,data,datasize,duration,datasize,self.time))
elif not self.interferences: elif not self.interferences:
@ -479,7 +465,7 @@ class Simulator:
self.log("Send "+str(datasize)+" bytes to n"+str(dst)+" on "+interface,node=src) self.log("Send "+str(datasize)+" bytes to n"+str(dst)+" on "+interface,node=src)
self.update_sharing(dst,1) # Update sharing first self.update_sharing(dst,1) # Update sharing first
# Note that in the following we send more data than expected to handle bandwidth sharing (datasize*8*sharing): # Note that in the following we send more data than expected to handle bandwidth sharing (datasize*8*sharing):
duration=datasize*8/(self.B_eth0[src,dst]/self.sharing[dst])+self.L_eth0[src,dst] duration=datasize*8/(self.netmat["eth0"]["bandwidth"][src,dst]/self.sharing[dst])+self.netmat["eth0"]["latency"][src,dst]
self.add_event(0,duration+self.time,(src,dst,interface,data,datasize,duration,datasize,self.time)) self.add_event(0,duration+self.time,(src,dst,interface,data,datasize,duration,datasize,self.time))
else: else:
nsrc["state"]="request" # Try later when node is on nsrc["state"]="request" # Try later when node is on
@ -489,7 +475,7 @@ class Simulator:
""" """
Deduce reachable receivers from the bandwidth matrix Deduce reachable receivers from the bandwidth matrix
""" """
selector = self.B_wlan0[node.node_id,] > 0 selector = self.netmat["wlan0"]["bandwidth"][node.node_id,] > 0
return np.arange(0,selector.shape[0])[selector] return np.arange(0,selector.shape[0])[selector]

View file

@ -26,7 +26,7 @@ B=np.full((n,n),5) # 5Mbps
L=np.full((n,n),0) # 0s L=np.full((n,n),0) # 0s
##### Create the simulator ##### Create the simulator
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
##### Instantiate nodes ##### Instantiate nodes
uptime=180 # 180s uptime uptime=180 # 180s uptime

View file

@ -13,7 +13,7 @@ B[0,2]=0
B[2,0]=0 B[2,0]=0
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")

View file

@ -26,13 +26,15 @@ import numpy as np
B=np.full((2,2),8) B=np.full((2,2),8)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")
def callback(simulator): def callback(simulator):
simulator.log("Network update!") simulator.log("Network update!")
simulator.update_network(simulator.B_wlan0*2,simulator.L_wlan0,simulator.B_eth0*2,simulator.L_eth0) new_bw_wlan0=simulator.netmat["wlan0"]["bandwidth"]*2
new_bw_eth0=simulator.netmat["eth0"]["bandwidth"]*2
simulator.update_network({"wlan0":{"bandwidth":new_bw_wlan0, "latency":L},"eth0":{"bandwidth":new_bw_eth0, "latency":L}})
s.run(breakpoints_every=1/2,breakpoint_callback=callback) s.run(breakpoints_every=1/2,breakpoint_callback=callback)

View file

@ -28,7 +28,7 @@ import numpy as np
B=np.full((3,3),8) B=np.full((3,3),8)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("sender") s.create_node("sender")
@ -36,6 +36,8 @@ s.create_node("receiver")
def callback(simulator): def callback(simulator):
simulator.log("Network update!") simulator.log("Network update!")
simulator.update_network(simulator.B_wlan0*2,simulator.L_wlan0,simulator.B_eth0*2,simulator.L_eth0) new_bw_wlan0=simulator.netmat["wlan0"]["bandwidth"]*2
new_bw_eth0=simulator.netmat["eth0"]["bandwidth"]*2
simulator.update_network({"wlan0":{"bandwidth":new_bw_wlan0, "latency":L},"eth0":{"bandwidth":new_bw_eth0, "latency":L}})
s.run(breakpoints_every=1,breakpoint_callback=callback,debug=True) s.run(breakpoints_every=1,breakpoint_callback=callback,debug=True)

View file

@ -27,13 +27,15 @@ import numpy as np
B=np.full((2,2),8) B=np.full((2,2),8)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")
def callback(simulator): def callback(simulator):
simulator.log("Network update!") simulator.log("Network update!")
simulator.update_network(simulator.B_wlan0,simulator.L_wlan0+1/2,simulator.B_eth0,simulator.L_eth0+1/2) new_lat_wlan0=simulator.netmat["wlan0"]["latency"]+1/2
new_lat_eth0=simulator.netmat["eth0"]["latency"]+1/2
simulator.update_network({"wlan0":{"bandwidth":B, "latency":new_lat_wlan0},"eth0":{"bandwidth":B, "latency":new_lat_eth0}})
s.run(breakpoints_every=1/2,breakpoint_callback=callback) s.run(breakpoints_every=1/2,breakpoint_callback=callback)

View file

@ -29,7 +29,7 @@ import numpy as np
B=np.full((3,3),8) B=np.full((3,3),8)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("sender") s.create_node("sender")
@ -37,6 +37,8 @@ s.create_node("receiver")
def callback(simulator): def callback(simulator):
simulator.log("Network update!") simulator.log("Network update!")
simulator.update_network(simulator.B_wlan0,simulator.L_wlan0+0.5,simulator.B_eth0,simulator.L_eth0+0.5) new_lat_wlan0=simulator.netmat["wlan0"]["latency"]+1/2
new_lat_eth0=simulator.netmat["eth0"]["latency"]+1/2
simulator.update_network({"wlan0":{"bandwidth":B, "latency":new_lat_wlan0},"eth0":{"bandwidth":B, "latency":new_lat_eth0}})
s.run(breakpoints_every=1,breakpoint_callback=callback,debug=True) s.run(breakpoints_every=1,breakpoint_callback=callback,debug=True)

View file

@ -26,13 +26,16 @@ import numpy as np
B=np.full((2,2),8) B=np.full((2,2),8)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")
def callback(simulator): def callback(simulator):
simulator.log("Network update!") simulator.log("Network update!")
simulator.update_network(simulator.B_wlan0*2,simulator.L_wlan0,simulator.B_eth0*2,simulator.L_eth0) new_bw_wlan0=simulator.netmat["wlan0"]["bandwidth"]*2
new_bw_eth0=simulator.netmat["eth0"]["bandwidth"]*2
simulator.update_network({"wlan0":{"bandwidth":new_bw_wlan0, "latency":L},"eth0":{"bandwidth":new_bw_eth0, "latency":L}})
s.run(breakpoints_every=1/2,breakpoint_callback=callback) s.run(breakpoints_every=1/2,breakpoint_callback=callback)

View file

@ -27,13 +27,16 @@ import numpy as np
B=np.full((2,2),8) B=np.full((2,2),8)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")
def callback(simulator): def callback(simulator):
simulator.log("Network update!") simulator.log("Network update!")
simulator.update_network(simulator.B_wlan0,simulator.L_wlan0+1/2,simulator.B_eth0,simulator.L_eth0+1/2) new_lat_wlan0=simulator.netmat["wlan0"]["latency"]+1/2
new_lat_eth0=simulator.netmat["eth0"]["latency"]+1/2
simulator.update_network({"wlan0":{"bandwidth":B, "latency":new_lat_wlan0},"eth0":{"bandwidth":B, "latency":new_lat_eth0}})
s.run(breakpoints_every=1/2,breakpoint_callback=callback) s.run(breakpoints_every=1/2,breakpoint_callback=callback)

View file

@ -9,7 +9,7 @@ import numpy as np
n=2 n=2
B=np.full((2,2),n) B=np.full((2,2),n)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("node") s.create_node("node")
s.create_node("node") s.create_node("node")

View file

@ -9,7 +9,7 @@ import numpy as np
n=2 n=2
B=np.full((n,n),n) B=np.full((n,n),n)
L=np.full((n,n),0) L=np.full((n,n),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("node") s.create_node("node")
s.create_node("node") s.create_node("node")

View file

@ -9,7 +9,7 @@ import numpy as np
n=2 n=2
B=np.full((n,n),n) B=np.full((n,n),n)
L=np.full((n,n),0) L=np.full((n,n),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("node") s.create_node("node")
s.create_node("node") s.create_node("node")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((5,5),5) B=np.full((5,5),5)
L=np.full((5,5),0) L=np.full((5,5),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("node") s.create_node("node")
s.create_node("node") s.create_node("node")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((2,2),2) B=np.full((2,2),2)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("node") s.create_node("node")
s.create_node("node") s.create_node("node")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((3,3),3) B=np.full((3,3),3)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("sender") s.create_node("sender")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((3,3),3) B=np.full((3,3),3)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("sender") s.create_node("sender")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((2,2),8) B=np.full((2,2),8)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((2,2),8) B=np.full((2,2),8)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((3,3),8) B=np.full((3,3),8)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("sender") s.create_node("sender")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((4,4),8) B=np.full((4,4),8)
L=np.full((4,4),0) L=np.full((4,4),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("sender") s.create_node("sender")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((3,3),8) B=np.full((3,3),8)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((3,3),8) B=np.full((3,3),8)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("sender") s.create_node("sender")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((2,2),8) B=np.full((2,2),8)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((3,3),8) B=np.full((3,3),8)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("sender") s.create_node("sender")
s.create_node("receiver") s.create_node("receiver")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((2,2),2) B=np.full((2,2),2)
L=np.full((2,2),0) L=np.full((2,2),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("node") s.create_node("node")
s.create_node("node") s.create_node("node")

View file

@ -8,7 +8,7 @@ import numpy as np
B=np.full((3,3),2) B=np.full((3,3),2)
L=np.full((3,3),0) L=np.full((3,3),0)
s=esds.Simulator(B,L,B,L) s=esds.Simulator({"wlan0":{"bandwidth":B, "latency":L},"eth0":{"bandwidth":B, "latency":L}})
s.create_node("node") s.create_node("node")
s.create_node("node") s.create_node("node")