Did Stuff

This commit is contained in:
Nikhil Nair
2022-02-03 17:01:14 +05:30
parent e59096aed9
commit 1bf53b9c5a
13 changed files with 899 additions and 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+117
View File
@@ -0,0 +1,117 @@
import numpy as np
class Actor:
def __init__(self , aw , av , au , gamma ,h = 3 ):
# Initialize all parameters
self.X = np.zeros((3,1))
self.h = h
self.wh = np.zeros( (h,3) )
self.K =np.zeros( (3,1) )
self.w = np.zeros( (3, h) )
self.output = np.zeros( (h,1) )
# Learning Rates
self.aw = aw
self.au = au
self.gamma = gamma
def HiddenLayer(self):
# Description : Takes in the state vector at a given time step and computes the output vector for the next layer
output = 1/(1 + np.exp(self.wh.dot(self.X)) )
self.output = output
def OutputLayer(self):
# Description : Takes in output from Hiddenlayer and computes Ki,Kp and Kd values
self.K = self.w.dot(self.output)
# print(self.K)
def Update1(self,y_ref,yt_0,yt_1,yt_2,yt_3,V,Vprev):
# Update Params for next episode
del_TD = 0.5 * ( y_ref - yt_0 )**2 + self.gamma*V - Vprev
# Update w matrix
self.w[0] = self.w[0] - self.aw * del_TD*(yt_1 - yt_2)*self.output.T
self.w[1] = self.w[1] + self.aw * del_TD*self.X[0,0]*self.output.T
self.w[2] = self.w[2] + self.aw * del_TD*(yt_1 - 2*yt_2 + yt_3)*self.output.T
def Update2(self,y_ref,yt_0,yt_1,yt_2,yt_3,V,Vprev,v_prev):
# Update Params for next episode
del_TD = 0.5 * ( y_ref - yt_0 )**2 + self.gamma*V - Vprev
for i in range(self.h):
self.wh[i,0] = self.wh[i,0] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[0]
self.wh[i,1] = self.wh[i,1] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[1]
self.wh[i,2] = self.wh[i,2] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[2]
class Critic:
def __init__(self , aw , av , au , gamma ,h = 3 ):
# Initialize all parameters
self.X = np.zeros((3,1))
self.h = h
self.wh = np.zeros( (h,3) )
self.Vprev = 0
self.V = 0
self.v = np.zeros( (1, h) )
self.output = np.zeros( (h,1) )
# Learning Rates
self.av = av
self.au = au
self.gamma = gamma
def HiddenLayer(self):
# Description : Takes in the state vector at a given time step and computes the output vector for the next layer
output = 1/(1 + np.exp(self.wh.dot(self.X)) )
self.output = output
def OutputLayer(self):
# Description : Takes in output from Hiddenlayer and computes Ki,Kp and Kd values
self.Vprev = self.V
self.V = self.v.dot(self.output)
def Update(self,y_ref,yt_0,yt_1,yt_2,yt_3):
# Update Params for next episode
del_TD = 0.5 * ( y_ref - yt_0 )**2 + self.gamma*self.V - self.Vprev
# Updating the v value
v_prev = self.v
self.v = self.v + self.av * del_TD * self.output.T
for i in range(self.h):
self.wh[i,0] = self.wh[i,0] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[0]
self.wh[i,1] = self.wh[i,1] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[1]
self.wh[i,2] = self.wh[i,2] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[2]
return v_prev
+92
View File
@@ -0,0 +1,92 @@
import numpy as np
class Actor:
def __init__(self , aw , av , au , gamma ,h = 3 ):
# Initialize all parameters
self.X = np.zeros((3,1))
self.h = h
self.wh = np.zeros( (h,3) )
# actor
self.K =np.zeros( (3,1) )
# actor
self.w = np.zeros( (3, h) )
self.output = np.zeros( (h,1) )
# Learning Rates
# actor
self.aw = aw
# both
self.au = au
def HiddenLayer(self):
# Description : Takes in the state vector at a given time step and computes the output vector for the next layer
output = 1/(1 + np.exp(self.wh.dot(self.X)) )
self.output = output
def OutputLayer(self):
# Description : Takes in output from Hiddenlayer and computes Ki,Kp and Kd values
# actor
self.K = self.w.dot(self.output)
# print(self.K)
def Update1(self,y_ref,yt_0,yt_1,yt_2,yt_3,V,Vprev):
# Update Params for next episode
# both
del_TD = 0.5 * ( y_ref - yt_0 )**2 + self.gamma*V - Vprev
# actor
# Update w matrix
self.w[0] = self.w[0] - self.aw * del_TD*(yt_1 - yt_2)*self.output.T
self.w[1] = self.w[1] + self.aw * del_TD*self.X[0,0]*self.output.T
self.w[2] = self.w[2] + self.aw * del_TD*(yt_1 - 2*yt_2 + yt_3)*self.output.T
def Update2(self,y_ref,yt_0,yt_1,yt_2,yt_3,V,Vprev,v_prev):
# Update Params for next episode
del_TD = 0.5 * ( y_ref - yt_0 )**2 + self.gamma*V - Vprev
for i in range(self.h):
self.wh[i,0] = self.wh[i,0] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[0]
self.wh[i,1] = self.wh[i,1] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[1]
self.wh[i,2] = self.wh[i,2] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[2]
+82
View File
@@ -0,0 +1,82 @@
import numpy as np
class Critic:
def __init__(self , aw , av , au , gamma ,h = 3 ):
# Initialize all parameters
self.X = np.zeros((3,1))
self.h = h
self.wh = np.zeros( (h,3) )
# critic
self.Vprev = 0
self.V = 0
# critic
self.v = np.zeros( (1, h) )
self.output = np.zeros( (h,1) )
# Learning Rates
# critic
self.av = av
# both
self.au = au
# critic
self.gamma = gamma
def HiddenLayer(self):
# Description : Takes in the state vector at a given time step and computes the output vector for the next layer
output = 1/(1 + np.exp(self.wh.dot(self.X)) )
self.output = output
def OutputLayer(self):
# Description : Takes in output from Hiddenlayer and computes Ki,Kp and Kd values
# critic
self.Vprev = self.V
self.V = self.v.dot(self.output)
def Update(self,y_ref,yt_0,yt_1,yt_2,yt_3):
# Update Params for next episode
del_TD = 0.5 * ( y_ref - yt_0 )**2 + self.gamma*self.V - self.Vprev
# critic
# Updating the v value
v_prev = self.v
self.v = self.v + self.av * del_TD * self.output.T
for i in range(self.h):
self.wh[i,0] = self.wh[i,0] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[0]
self.wh[i,1] = self.wh[i,1] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[1]
self.wh[i,2] = self.wh[i,2] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[2]
return v_prev
+80
View File
@@ -0,0 +1,80 @@
import numpy as np
class NeuralNetwork:
def __init__(self , aw , av , au , gamma ,h = 3 ):
# Initialize all parameters
self.X = np.zeros((3,1))
self.h = h
self.wh = np.zeros( (h,3) )
self.K =np.zeros( (3,1) )
self.Vprev = 0
self.V = 0
self.w = np.zeros( (3, h) )
self.v = np.zeros( (1, h) )
self.output = np.zeros( (h,1) )
# Learning Rates
self.aw = aw
self.av = av
self.au = au
self.gamma = gamma
def HiddenLayer(self):
# Description : Takes in the state vector at a given time step and computes the output vector for the next layer
output = 1/(1 + np.exp(self.wh.dot(self.X)) )
self.output = output
def OutputLayer(self):
# Description : Takes in output from Hiddenlayer and computes Ki,Kp and Kd values
self.K = self.w.dot(self.output)
# print(self.K)
self.Vprev = self.V
self.V = self.v.dot(self.output)
def Update(self,y_ref,yt_0,yt_1,yt_2,yt_3):
# Update Params for next episode
del_TD = 0.5 * ( y_ref - yt_0 )**2 + self.gamma*self.V - self.Vprev
# Update w matrix
self.w[0] = self.w[0] - self.aw * del_TD*(yt_1 - yt_2)*self.output.T
self.w[1] = self.w[1] + self.aw * del_TD*self.X[0,0]*self.output.T
self.w[2] = self.w[2] + self.aw * del_TD*(yt_1 - 2*yt_2 + yt_3)*self.output.T
# Updating the v value
v_prev = self.v
self.v = self.v + self.av * del_TD * self.output.T
# Updating the centers and widths of hidden layers
# print("Printing Shapes of Stuff")
# print("Shape of self.au :", v_prev)
for i in range(self.h):
self.wh[i,0] = self.wh[i,0] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[0]
self.wh[i,1] = self.wh[i,1] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[1]
self.wh[i,2] = self.wh[i,2] + self.au*del_TD*v_prev[0][i]*self.output[i]*( 1 - self.output[i] )*self.X[2]
# print(self.K)
@@ -0,0 +1,145 @@
from audioop import cross
import matplotlib.pyplot as plt
import numpy as np
from numpy.lib.function_base import append
from singlearea import *
import ac
def y(yd):
actor = ac.Actor( aw = 0.0003, av = 0.1, au = 0.0025 , gamma = 0.9)
critic = ac.Critic( aw = 0.0003, av = 0.1, au = 0.0025 , gamma = 0.9)
Tg = 0.08
Tt = 0.3
M = 0.2
D = 0.01
R = 2
T = dt = 1/400
yt_1 = 0
yt_2 = 0
yt_3 = 0
System = SingleArea( Tg , Tt , M , D , R , T , yt_1 , yt_2 , yt_3 )
initial_states = [ yt_1, yt_2 , yt_3]
plot_data = {"ut":[] , "pl" : [] , "delF":[] , 'KI' : [], 'KP' : [] , 'KD' : [] , "time" : []}
Ki = 0
Kd = 0
Kp = 0
ut_1 = 0
t = 10
y=[]
x=[]
for i in range(0, int(t/dt) ):
# print(System.yt_1)
e_t = 0 - System.yt_1
del_y = System.yt_1 - System.yt_2
del2_y = System.yt_1 - 2*System.yt_2 + System.yt_3
actor.X[:,0]= critic.X[:,0] = [ e_t , -del_y , -del2_y]
actor.HiddenLayer()
critic.HiddenLayer()
actor.OutputLayer()
critic.OutputLayer()
# ut_1 = ut_1 + 0.00043*e_t - 0.01*del_y - 0*del2_y
ut_1 = ut_1 + actor.K[1]*e_t - actor.K[0]*del_y - actor.K[2]*del2_y
plot_data["ut"].append(ut_1)
PL = 0.2 if( i*dt >= 0.2 ) else 0
plot_data["pl"].append(PL)
Ut = [ [ut_1] , [ PL] ]
System.Output(Ut)
print(actor.K)
V,Vprev = critic.V, critic.Vprev
actor.Update1(0 ,System.Y[0,0] ,System.yt_1 , System.yt_2, System.yt_3 , V,Vprev)
vprev = critic.Update(0 ,System.Y[0,0] ,System.yt_1 , System.yt_2, System.yt_3)
actor.Update2(0 ,System.Y[0,0] ,System.yt_1 , System.yt_2, System.yt_3 , V,Vprev , vprev)
plot_data["delF"].append(System.Y[0,0])
plot_data["time"].append(i*dt)
plot_data["KI"].append(actor.K[1])
plot_data["KP"].append(actor.K[0])
plot_data["KD"].append(actor.K[2])
return plot_data,initial_states
if __name__=="__main__":
yd = [0 for i in range(10*400) ]
## Generate Reference array here
plot_data,i = y(yd)
plt.subplot(2,2,1)
plt.plot(plot_data["time"],plot_data["pl"], label="Reference Signal")
plt.title( "Load vs Time")
plt.ylabel(" Output from System ")
plt.xlabel("Time (s)")
plt.subplot(2,2,2)
plt.plot(plot_data["time"],plot_data["KI"], label="KI")
plt.plot(plot_data["time"],plot_data["KP"], label="KP")
plt.plot(plot_data["time"],plot_data["KD"], label="KD")
plt.title( "KI, KP, KD vs Time")
plt.ylabel("KI, KP, KD")
plt.xlabel("Time (s)")
plt.legend()
plt.subplot(2,2,3)
plt.plot(plot_data["time"],plot_data["ut"], label="Reference Signal")
plt.title( "Control Signal vs Time")
plt.ylabel("Control Signal")
plt.xlabel("Time (s)")
plt.subplot(2,2,4)
plt.plot(plot_data["time"],yd, label="Reference Signal")
plt.plot(plot_data["time"],plot_data["delF"],label ="Output")
plt.title( " Initial States y(t-1) , y(t-2) and y(t-3) are " + str(i[0]) + ", " + str(i[1]) +" and "+ str(i[2]) )
plt.legend()
plt.ylabel(" Output from System ")
plt.xlabel("Time (s)")
plt.show()
+131
View File
@@ -0,0 +1,131 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy.lib.function_base import append
from singlearea import *
import neuralnetwork
def y(yd):
nn = neuralnetwork.NeuralNetwork( aw = 0.0003, av = 0.1, au = 0.0025 , gamma = 0.9)
Tg = 0.08
Tt = 0.3
M = 0.2
D = 0.01
R = 2
T = dt = 1/400
yt_1 = 0
yt_2 = 0
yt_3 = 0
System = SingleArea( Tg , Tt , M , D , R , T , yt_1 , yt_2 , yt_3 )
initial_states = [ yt_1, yt_2 , yt_3]
plot_data = {"ut":[] , "pl" : [] , "delF":[] , 'KI' : [], 'KP' : [] , 'KD' : [] , "time" : []}
Ki = 0
Kd = 0
Kp = 0
ut_1 = 0
t = 10
y=[]
x=[]
for i in range(0, int(t/dt) ):
# print(System.yt_1)
e_t = 0 - System.yt_1
del_y = System.yt_1 - System.yt_2
del2_y = System.yt_1 - 2*System.yt_2 + System.yt_3
nn.X[:,0] = [ e_t , -del_y , -del2_y]
nn.HiddenLayer()
nn.OutputLayer()
# ut_1 = ut_1 + 0.00043*e_t - 0.01*del_y - 0*del2_y
ut_1 = ut_1 + nn.K[1]*e_t - nn.K[0]*del_y - nn.K[2]*del2_y
plot_data["ut"].append(ut_1)
PL = 0.2 if( i*dt >= 0.2 ) else 0
plot_data["pl"].append(PL)
Ut = [ [ut_1] , [ PL] ]
System.Output(Ut)
print(nn.K)
nn.Update(0 ,System.Y[0,0] ,System.yt_1 , System.yt_2, System.yt_3 )
plot_data["delF"].append(System.Y[0,0])
plot_data["time"].append(i*dt)
plot_data["KI"].append(nn.K[1])
plot_data["KP"].append(nn.K[0])
plot_data["KD"].append(nn.K[2])
return plot_data,initial_states
if __name__=="__main__":
yd = [0 for i in range(10*400) ]
## Generate Reference array here
plot_data,i = y(yd)
plt.subplot(2,2,1)
plt.plot(plot_data["time"],plot_data["pl"], label="Reference Signal")
plt.title( "Load vs Time")
plt.ylabel(" Output from System ")
plt.xlabel("Time (s)")
plt.subplot(2,2,2)
plt.plot(plot_data["time"],plot_data["KI"], label="KI")
plt.plot(plot_data["time"],plot_data["KP"], label="KP")
plt.plot(plot_data["time"],plot_data["KD"], label="KD")
plt.title( "KI, KP, KD vs Time")
plt.ylabel("KI, KP, KD")
plt.xlabel("Time (s)")
plt.legend()
plt.subplot(2,2,3)
plt.plot(plot_data["time"],plot_data["ut"], label="Reference Signal")
plt.title( "Control Signal vs Time")
plt.ylabel("Control Signal")
plt.xlabel("Time (s)")
plt.subplot(2,2,4)
plt.plot(plot_data["time"],yd, label="Reference Signal")
plt.plot(plot_data["time"],plot_data["delF"],label ="Output")
plt.title( " Initial States y(t-1) , y(t-2) and y(t-3) are " + str(i[0]) + ", " + str(i[1]) +" and "+ str(i[2]) )
plt.legend()
plt.ylabel(" Output from System ")
plt.xlabel("Time (s)")
plt.show()
+101
View File
@@ -0,0 +1,101 @@
import numpy as np
from numpy.core.numeric import NaN
from scipy.linalg import expm
import math
class TwoAreaPS:
def __init__(self, Tg, Tp, Tt, Kp, T12, a12, R, T, beta1, beta2, yt_1,yt_2,yt_3):
self.yt_1 = yt_1
self.yt_2 = yt_2
self.yt_3 = yt_3
self.Xprev = np.zeros( (7,1) )
self.Y = np.zeros( (2,1) )
self.Tg = Tg
self.Tp = Tp
self.Tt = Tt
self.Kp = Kp
self.T12 = T12
self.a12 = a12
self.R = R
self.beta1 = beta1
self.beta2 = beta2
self.T = T
self.CalcDiscreteCoef()
def CalcDiscreteCoef(self):
# Calculating Continous coef
Tg = self.Tg
Tp = self.Tp
Tt = self.Tt
Kp = self.Kp
T12 = self.T12
a12 = self.a12
R = self.R
self.A = np.array( [ [-1/Tp , Kp/Tp , 0 , -Kp/Tp , 0 , 0 , 0 ] ,
[0 , -1/Tt , 1/Tt , 0 , 0 ,0 , 0 ] ,
[-1/(R*Tg) , 0 , -1/Tg , 0 , 0, 0, 0 ] ,
[ 2*np.pi*T12 , 0 , 0 , 0 , -2*np.pi*T12 , 0 , 0 ] ,
[0 ,0 ,0 , -Kp*a12/Tp , -1/Tp , Kp/Tp , 0 ] ,
[ 0,0,0,0,0, -1/Tt, 1/Tt] ,
[0,0,0,0,-1/(R*Tg) , 1/Tg , -1/Tg ] ] )
self.B = np.array( [ [0 ,0, -Kp/Tp , 0 ],
[0 , 0, 0 ,0 ],
[1/Tg, 0 , 0, 0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,1/Tg,0,-Kp/Tp] ])
self.C = np.array( [ [ self.beta1 , 0 , 0 ,1 , 0, 0 , 0 ] ])
# [ 0 , 0, 0, 1, self.beta2, 0, 0 ] ] )
# Calculating Discrete Coefs
self.Ad = expm(self.A*self.T)
# Add check later
self.Bd = np.dot( np.dot(np.linalg.inv(self.A),(self.Ad - np.eye(7) )), self.B )
def Output(self,Ut):
self.yt_1 , self.yt_2 , self.yt_3 = self.Y[0,0], self.yt_1, self.yt_2
self.X = np.dot( self.Ad, self.Xprev ) + np.dot( self.Bd, Ut )
self.Y = np.dot( self.C, self.Xprev)
# print("Chooth :" , self.Y)
self.Xprev = self.X
if (math.isnan(self.Y[0,0])):
return True
return False
+151
View File
@@ -0,0 +1,151 @@
import matplotlib.pyplot as plt
import numpy as np
from numpy.lib.function_base import append
from two_area import *
import RBF
def y(yd):
rbf = RBF.RBF( aw = 0.0003, av = 0.021, au = 0.025 , asig = 0.01, gamma = 0.9)
Tg = 0.08
Tp = 20
Tt = 0.3
Kp = 120
T12 = 0.545/(2*np.pi)
a12 = -1
R = 2.4
T = dt = 1/400
beta1 = 0.425
beta2 = 0.425
yt_1 = 0
yt_2 = 0
yt_3 = 0
System = TwoAreaPS( Tg, Tp, Tt, Kp, T12, a12, R, T, beta1, beta2, yt_1,yt_2,yt_3 )
initial_states = [ yt_1, yt_2 , yt_3]
plot_data = {"ut":[] , "pl" : [] , "delF":[] , 'KI' : [], 'KP' : [] , 'KD' : [] , "time" : []}
Ki = 0
Kd = 0
Kp = 0
ut_1 = 0
t = 100
y=[]
x=[]
for i in range(0, int(t/dt) ):
# print(System.yt_1)
e_t = 0 - System.yt_1
del_y = System.yt_1 - System.yt_2
del2_y = System.yt_1 - 2*System.yt_2 + System.yt_3
rbf.X[:,0] = [ e_t , -del_y , -del2_y]
rbf.HiddenLayer()
rbf.OutputLayer()
# ut_1 = ut_1 + 0.00043*e_t - 0.01*del_y - 0*del2_y
ut_1 = ut_1 + rbf.K[1]*e_t - rbf.K[0]*del_y - rbf.K[2]*del2_y
plot_data["ut"].append(ut_1)
PL = 0.2 if( i*dt >= 0.2 ) else 0
plot_data["pl"].append(PL)
Ut = [ [ut_1] , [0] , [ PL] , [0] ]
System.Output(Ut)
print(rbf.K)
rbf.Update(0 ,System.Y[0,0] ,System.yt_1 , System.yt_2, System.yt_3 )
plot_data["delF"].append(System.Y[0,0])
plot_data["time"].append(i*dt)
plot_data["KI"].append(rbf.K[1])
plot_data["KP"].append(rbf.K[0])
plot_data["KD"].append(rbf.K[2])
return plot_data,initial_states
if __name__=="__main__":
yd = [0 for i in range(100*400) ]
## Generate Reference array here
plot_data,i = y(yd)
plt.subplot(2,3,1)
plt.plot(plot_data["time"],plot_data["pl"], label="Reference Signal")
plt.title( "Load vs Time")
plt.ylabel(" Output from System ")
plt.xlabel("Time (s)")
plt.subplot(2,3,2)
plt.plot(plot_data["time"],plot_data["ut"], label="Reference Signal")
plt.title( "Control Signal vs Time")
plt.ylabel("Control Signal")
plt.xlabel("Time (s)")
plt.subplot(2,3,3)
plt.plot(plot_data["time"],yd, label="Reference Signal")
plt.plot(plot_data["time"],plot_data["delF"],label ="Output")
plt.title( " Initial States y(t-1) , y(t-2) and y(t-3) are " + str(i[0]) + ", " + str(i[1]) +" and "+ str(i[2]) )
plt.legend()
plt.subplot(2,3,4)
plt.plot(plot_data["time"],plot_data["KI"], label="KI")
plt.title( "KI vs Time")
plt.ylabel("KI")
plt.xlabel("Time (s)")
plt.subplot(2,3,5)
plt.plot(plot_data["time"],plot_data["KP"], label="KP")
plt.title( "KP vs Time")
plt.ylabel("KP ")
plt.xlabel("Time (s)")
plt.subplot(2,3,6)
plt.plot(plot_data["time"],plot_data["KD"], label="KD")
plt.title( "KD vs Time")
plt.ylabel("KD")
plt.xlabel("Time (s)")
plt.ylabel(" Output from System ")
plt.xlabel("Time (s)")
plt.show()