import warnings
warnings.filterwarnings('ignore')
import numpy as np
import matplotlib.pylab as plt
import random
Wup = 150
Wdown = 10
class generator():
def __init__(self, load):
self.load = load
self.on = True
self.broken = False
self.history = []
self.onoffhistory = []
self.hasChanged = 10
def switch(self, gridLoad):
if(self.hasChanged > 0):
return
if ((gridLoad + Wup > -self.load and random.random() < 0.1) or self.broken):
self.on = False
elif(not self.broken and random.random() < 0.1):
self.on = True
if(self.broken and random.random() <= 0.10):
self.broken = False
elif(not self.broken and random.random() <= 0.02):
self.broken = True
def upkeep(self):
self.hasChanged -= 1
if(self.on and not self.broken):
self.history.append(self.load)
self.onoffhistory.append(1)
return self.load
else:
self.onoffhistory.append(0)
self.history.append(0)
return 0
class door():
def __init__(self, load):
self.on = False
self.load = load
self.offLoad = load/10
self.cycle = 0
self.history = []
self.broken = False
self.onoffhistory = []
def switch(self, gridLoad):
if((self.cycle <= 0 or gridLoad <= Wup) and random.random() < 0.3 and self.broken):
self.on = False
elif(self.cycle > 0 and random.random() < 0.15 and not self.broken):
self.on = True
if(self.broken and random.random() <= 0.05):
self.broken = False
elif(not self.broken and random.random() <= 0.01):
self.broken = True
def upkeep(self):
self.cycle -= 1
if(self.cycle < -5):
self.cycle = 5
if(self.on and not self.broken):
self.onoffhistory.append(1)
self.history.append(self.load)
return self.load
else:
self.onoffhistory.append(0)
self.history.append(self.offLoad)
return self.offLoad
class sensor():
def __init__(self, load):
self.on = False
self.load = load
self.offload = load/50
self.history = []
self.broken = False
self.onoffhistory = []
def switch(self, gridLoad):
if (gridLoad >= Wup and random.random() < 0.25 and not self.broken):
self.on = True
elif(gridLoad < Wup and random.random() < 0.15 and self.broken):
self.on = False
if(self.broken and random.random() <= 0.05):
self.broken = False
elif(not self.broken and random.random() <= 0.01):
self.broken = True
def upkeep(self):
if(self.on and not self.broken):
self.onoffhistory.append(1)
self.history.append(self.load)
return self.load
else:
self.onoffhistory.append(0)
self.history.append(self.offload)
return self.offload
class appliences():
def __init__(self, load):
self.on = False
self.load = load
self.offload = load/10
self.history = []
self.broken = False
self.onoffhistory = []
def switch(self, voltage):
if (voltage >= Wdown and random.random() < 0.3):
self.on = True
elif(voltage < Wdown and random.random() < 0.15):
self.on = False
if(self.broken and random.random() <= 0.05):
self.broken = False
elif(not self.broken and random.random() <= 0.01):
self.broken = True
def upkeep(self):
if(self.on and not self.broken):
self.onoffhistory.append(1)
self.history.append(self.load)
return self.load
else:
self.onoffhistory.append(0)
self.history.append(self.offload)
return self.offload
class turret():
def __init__(self, load):
self.timer = 10
self.load = load
self.offload = load/100
self.history = []
self.broken = False
self.onoffhistory = []
def switch(self, gridload):
if(self.timer == 0 and gridload > Wup and random.random() < 0.025):
self.timer = 10
if(self.broken and random.random() <= 0.05):
self.broken = False
elif(not self.broken and random.random() <= 0.01):
self.broken = True
def upkeep(self):
if(self.timer > 0 and not self.broken):
self.timer -= 1
self.onoffhistory.append(1)
self.history.append(self.load)
return self.load
else:
self.onoffhistory.append(0)
self.history.append(self.offload)
return self.offload
def switch(electronics, gridLoad):
for e in electronics:
e.switch(gridLoad)
def upkeep(electronics):
sum = []
for e in electronics:
sum.append(e.upkeep())
return sum
electronics = []
doors = []
sensors = []
appliances = []
turrets = []
generators = []
for i in range(6):
generators.append(generator(-400))
for i in range(20):
doors.append(door(20))
for i in range(4):
sensors.append(sensor(50))
for i in range(10):
appliances.append(appliences(10))
for i in range(5):
turrets.append(turret(250))
electronics.extend(doors)
electronics.extend(sensors)
electronics.extend(appliances)
electronics.extend(turrets)
electronics.extend(generators)
gridloadOverTime = [sum([x.upkeep() for x in generators]) * (-1)]
faultPoints = []
activeGenerators = []
equipmentBroken = []
brokenAlarmPoints = []
for time in range(500):
switch(electronics, gridloadOverTime[time])
if (gridloadOverTime[time] < Wdown):
faultPoints.append((time, gridloadOverTime[time]))
currentLoad = sum(upkeep(electronics)) * (-1)
if(currentLoad < Wdown):
for x in generators:
if(not x.broken and not x.on and x.hasChanged <= 0):
x.on = True
currentLoad -= x.load
if( currentLoad > Wdown ):
break
activeGenerators.append(0)
for x in generators:
if(x.on):
activeGenerators[time] += 1
equipmentBroken.append(0)
for x in electronics:
if(x.broken):
equipmentBroken[time] += 1
if(equipmentBroken[time] > 12):
brokenAlarmPoints.append((time, equipmentBroken[time]))
gridloadOverTime.append(currentLoad)
downLine = [10] * 500
upLine = [500] * 500
plt.plot(downLine,'--',label='lower threshold value')
plt.plot(upLine,'--',label='upper threshold value')
plt.plot(gridloadOverTime, label="grid load")
for x, fault in enumerate(faultPoints):
if(x== 0):
plt.plot(fault[0], fault[1], 'ro',markersize=10, label="fault detection points")
else:
plt.plot(fault[0], fault[1], 'ro',markersize=10)
plt.ylabel('free gridLoad [w]')
plt.xlabel('Time [s]')
plt.grid()
plt.legend(loc='lower right')
plt.show()
gridLoad1 = [0] * 500
for x in doors:
for number, y in enumerate(x.history):
gridLoad1[number] += y
plt.plot(gridLoad1, label="doors")
gridLoad2 = [0] * 500
for x in sensors:
for number, y in enumerate(x.history):
gridLoad2[number] += y
plt.plot(gridLoad2, label="sensors")
gridLoad3 = [0] * 500
for x in appliances:
for number, y in enumerate(x.history):
gridLoad3[number] += y
plt.plot(gridLoad3, label="switches, communication, etc.")
gridLoad4 = [0] * 500
for x in turrets:
for number, y in enumerate(x.history):
gridLoad4[number] += y
plt.plot(gridLoad4, label="turrets")
plt.ylim((0 , 800))
plt.ylabel('grid load by different appliences')
plt.xlabel('Time [s]')
plt.grid()
plt.legend(loc='lower right')
plt.show()
plt.plot(activeGenerators, label="generators")
plt.ylim((-1 , 7))
plt.ylabel('Active generators')
plt.xlabel('Time [s]')
plt.grid()
plt.legend(loc='lower right')
plt.show()
for x, fault in enumerate(brokenAlarmPoints):
if(x== 0):
plt.plot(fault[0], fault[1], 'ro',markersize=10, label="fault detection points")
else:
plt.plot(fault[0], fault[1], 'ro',markersize=10)
plt.plot(equipmentBroken, label="equipment broken")
downLine = [12] * 500
plt.plot(downLine,'--',label='lower threshold value')
plt.legend(loc='lower right')
plt.ylim((-1 , 20))
plt.ylabel('Equipment broken')
plt.xlabel('Time [s]')
plt.grid()
plt.show()
Run to view results