Commit 5156cf90 authored by Gonzalo Belcredi's avatar Gonzalo Belcredi

storage update

parent 54b254ce
......@@ -4,14 +4,14 @@ import sys
# SAMPLING
t_start = 0.
t_end = 80. # units of time
t_end = 150. # units of time
step = .01
SimulationTimeslots = int(t_end / step)
time = np.arange(t_start,t_end,step)
# NUMBER NODES AND GATEWAYS
NumberNodes = 500
NumberNodes = 200
NumberGateways = NumberNodes / 20
# Problem data.
......@@ -19,7 +19,7 @@ mu = .003 # Processing Rate (tasks per time step)
lda = .004 # Incoming Rate (tasks per time step)
RxRate = lda
v = 1. # Tasks per Bit
alfa = .5 # Processing Factor
alfa = .7 # Processing Factor
#Channel
Cg = .1 # Gateway Maximum Throughput
......@@ -33,15 +33,17 @@ k1 = .1 * k # Saturated Slope
epsilon = .1
epsilon_energy = .5
beta = .1
delta = 1.
delta = k*v/10
eta = .1
#Energy
Grid = False
energy_model = 1
energy_price = 1.8
MinLevel = 0
energy_max_per_node = .8
energy_max = NumberNodes * energy_max_per_node
gamma_per_node = .15
gamma_per_node = .50
gamma = NumberNodes * gamma_per_node
energy_harvesting = 1
energy_tx = 1
......
......@@ -33,7 +33,6 @@ npl = np.zeros(SimulationTimeslots)
nidle = np.zeros(SimulationTimeslots)
battery_level = np.ones(SimulationTimeslots)
consumption = np.zeros(SimulationTimeslots)
with tqdm(total=SimulationTimeslots) as pbar:
for timeslot in range(SimulationTimeslots):
......@@ -44,7 +43,7 @@ with tqdm(total=SimulationTimeslots) as pbar:
ChannelObj.update_state(timeslot)
nodo.run()
battery_level[timeslot] = StorageObj.get_level()
consumption[timeslot] = battery_level[timeslot] - EnergyStart
#consumption[timeslot] = battery_level[timeslot] - EnergyStart
pbar.update(1)
ntx = StatsObj.Ntx / NumberNodes
......@@ -62,7 +61,7 @@ utils.phase_diagram(npl,ntx,nidle,params_s=['NumberNodes','gamma_per_node','alfa
#np.save('N='+str(config.NumberNodes)+'.npy',matrix)
############## EQUILIBRIUM POINTS
nt_lin_eq = 1/(1+config.k*config.v/(1-config.alfa)*(1/config.lda+config.alfa/config.mu))
np_lin_eq = 1/(1+config.mu/config.alfa*(1/config.lda+(1-config.alfa)/(config.k*config.v)))
......@@ -77,17 +76,101 @@ else:
nt_eq = nt_lin_eq
np_eq = np_lin_eq
N = 100
############## ENERGY
#N = 100
EnergyGridAvg = np.zeros(config.SimulationTimeslots)
for i in range(N,config.SimulationTimeslots):
EnergyGridAvg[i] = np.mean(StorageObj.EnergyGrid[i-N:i])
for i in range(1,config.SimulationTimeslots):
EnergyGridAvg[i] = EnergyGridAvg[i-1] * (i-1)/(i) + StorageObj.EnergyGrid[i]/i
consumption = -objects.StorageObj.Consumption
depletion = -StorageObj.Depletion
from matplotlib import pyplot as plt
plt.figure()
plt.plot(StorageObj.EnergyGrid)
plt.axhline(y = (nt_eq + np_eq)*config.NumberNodes*(1-math.sqrt(config.gamma_per_node/(nt_eq + np_eq))), color= 'red', label = 'Model (equilibrium)')
plt.plot(EnergyGridAvg, label = 'Moving Average (N = '+str(N)+')')
plt.plot(StorageObj.EnergyGrid, label = 'Grid Power')
plt.axhline(y = (nt_eq + np_eq)*config.NumberNodes*(1-math.sqrt(config.gamma_per_node/(nt_eq + np_eq))), color= 'green', label = 'Model (equilibrium)')
#plt.plot(EnergyGridAvg, label = 'Grid Power')
plt.plot(consumption, label='Total consumption')
plt.plot(depletion,label='Harvesting Power')
plt.ylabel('Energy purchased Power Grid')
plt.xlabel('Time (s)')
plt.legend()
plt.show()
############# BATTERY LEVEL
plt.figure(figsize=(14,5))
plt.plot(config.time,battery_level)
plt.grid('on')
plt.title('Battery Level')
plt.xlabel('Time (s)')
plt.show()
t_ini = 40
t_start = 130
crop = range(int(1/config.step)*t_ini,int(1/config.step)*t_start)
t_ini = 40
t_start = 50
consumption_crop = consumption[crop]
battery_level_crop = battery_level[crop]
time_crop = config.time[crop]
plt.figure(figsize=(14,5))
plt.plot(time_crop,battery_level_crop)
plt.grid('on')
plt.title('Battery Level')
plt.xlabel('Time (s)')
plt.show()
Ncount = 0
for i in range(len(battery_level_crop)):
if battery_level_crop[i] < 1:
Ncount +=1
P0 = Ncount / len(battery_level_crop)
rho1 = config.gamma / np.mean(consumption_crop)
rho2 = 1-math.sqrt(config.gamma_per_node / (np_eq + nt_eq))
print('Probabilidad de buffer de energía vacío (simulación): ', P0)
print('Probabilidad de buffer de energía vacío (diseño): ', 1-rho1)
############## COST FUNCTION
alfa_vector = np.linspace(0,1,1000)
cost_function = np.zeros(1000)
cost_server = np.zeros(1000)
cost_energy = np.zeros(1000)
beta = 1
def get_cost_server(alfa_i):
alfa_fog_sat = (config.lda*config.mu*(config.delta/(config.k*config.v) - 1) + config.mu * config.delta)/(config.lda*config.mu*(config.delta/(config.k*config.v) - 1) - config.mu * config.delta)
if alfa_i<alfa_fog_sat:
cost = beta * ((1-alfa_i)/(alfa_i*(config.k*config.v/config.mu -1) + config.k*config.v*config.lda+1)-config.delta/(config.k*config.v))
else:
cost = 0
return cost
def get_cost_energy(alfa_i):
ntx_eq = (1-alfa_i)/(alfa_i*(config.k*config.v/config.mu -1) + config.k*config.v*config.lda+1)
npl_eq = (alfa_i)/((1-alfa_i)*(config.mu /config.k*config.v -1) + config.mu*config.lda+1)
cost = max(0,config.energy_price*(npl_eq+ntx_eq - math.sqrt(config.gamma)*math.sqrt(npl_eq+ntx_eq)))
return cost
for i in range(0,len(cost_function)):
cost_server[i] = get_cost_server(alfa_vector[i])
cost_energy[i] = get_cost_energy(alfa_vector[i])
cost_function[i] = cost_server[i]+ cost_energy[i]
plt.figure()
plt.plot(alfa_vector,cost_server,label = 'Costo servidor')
plt.plot(alfa_vector,cost_energy,label = 'Costo energia')
plt.plot(alfa_vector,cost_function,label = 'Costo total')
plt.title('Función de costo según alfa')
plt.legend()
plt.show()
\ No newline at end of file
......@@ -10,6 +10,6 @@ import numpy as np
ChannelObj = channel.Channel(ChannelId=1, NumberNodes=config.NumberNodes,
Capacity=config.C, SlopeLin=config.k, SlopeSat=config.k1)
StorageObj = storage.Storage(100, config.gamma, config.MinLevel, Grid = True)
StorageObj = storage.Storage(100, config.gamma, config.MinLevel, config.Grid)
StatsObj = stats.Stats()
HarvestingVector = np.random.poisson(config.gamma, config.SimulationTimeslots)
\ No newline at end of file
......@@ -13,6 +13,8 @@ class Storage:
self.HarvestingRate = HarvestingRate
self.HarvestingAmount = 1
self.Level = 10
self.Consumption = np.zeros(config.SimulationTimeslots)
self.Depletion = np.zeros(config.SimulationTimeslots)
self.Status = 'Charged'
self.MinLevel = MinLevel
self.Leakage = 0
......@@ -24,11 +26,13 @@ class Storage:
def storage_access(self,Timestamp,NetFlow):
if (Timestamp < config.SimulationTimeslots):
if self.Level + NetFlow >= self.MinLevel:
if self.Level > self.MinLevel:
self.Level += NetFlow
self.Depletion[Timestamp] += NetFlow
else:
if self.Grid:
self.EnergyGrid[Timestamp] = self.EnergyGrid[Timestamp] - NetFlow
self.Consumption[Timestamp] += NetFlow
self.update_state()
def get_level(self):
......@@ -38,7 +42,10 @@ class Storage:
return self.Status
def update_state(self):
if self.Level >= self.MinLevel:
self.Status = 'Charged'
else:
self.Status = 'Discharged'
\ No newline at end of file
if self.Grid:
self.Status = 'Charged'
else:
if self.Level > self.MinLevel:
self.Status = 'Charged'
else:
self.Status = 'Discharged'
\ No newline at end of file
......@@ -83,6 +83,15 @@ def solve_ode(t_start,t_end,y0,alfa,lda,mu,c,k,k1,v,restrictions = False):
return n_p,n_t,t
def cost_function_1(npl,ntx,delta,beta):
cost = np.zeros(len(ntx))
for i in range(len(ntx)):
if ntx[i] < delta / (config.k*config.v):
cost[i] = 0
else:
cost[i] = beta * (ntx[i] - delta/(config.k*config.v) )
return cost
def phase_diagram(npl,ntx,nidle,params_s):
eta = (1/config.alfa)*(1-config.alfa)*config.mu/(config.k*config.v)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment