From 1bf53b9c5a0ab04ab5c240e8f9bf0c91e611265b Mon Sep 17 00:00:00 2001 From: Nikhil Nair Date: Thu, 3 Feb 2022 17:01:14 +0530 Subject: [PATCH] Did Stuff --- __pycache__/RBF.cpython-39.pyc | Bin 0 -> 2050 bytes __pycache__/ac.cpython-39.pyc | Bin 0 -> 3207 bytes __pycache__/neuralnetwork.cpython-39.pyc | Bin 0 -> 1893 bytes __pycache__/singlearea.cpython-39.pyc | Bin 0 -> 1574 bytes __pycache__/two_area.cpython-39.pyc | Bin 0 -> 1993 bytes ac.py | 117 ++++++++++++++ actor.py | 92 +++++++++++ critic.py | 82 ++++++++++ neuralnetwork.py | 80 ++++++++++ singlearea_with_adaptive_pid_actor_critic.py | 145 ++++++++++++++++++ singlearea_with_adaptive_pid_nn.py | 131 ++++++++++++++++ two_area.py | 101 +++++++++++++ twoarea_with_adaptive_pid.py | 151 +++++++++++++++++++ 13 files changed, 899 insertions(+) create mode 100644 __pycache__/RBF.cpython-39.pyc create mode 100644 __pycache__/ac.cpython-39.pyc create mode 100644 __pycache__/neuralnetwork.cpython-39.pyc create mode 100644 __pycache__/singlearea.cpython-39.pyc create mode 100644 __pycache__/two_area.cpython-39.pyc create mode 100644 ac.py create mode 100644 actor.py create mode 100644 critic.py create mode 100644 neuralnetwork.py create mode 100644 singlearea_with_adaptive_pid_actor_critic.py create mode 100644 singlearea_with_adaptive_pid_nn.py create mode 100644 two_area.py create mode 100644 twoarea_with_adaptive_pid.py diff --git a/__pycache__/RBF.cpython-39.pyc b/__pycache__/RBF.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b3488846ca255ed0300ddbb04cd63897d8cb89e9 GIT binary patch literal 2050 zcmZuyOOG2x5bo}Ic>G-N1NMb@gbPO5EQAA>6_2puA&`k6B*+pPjoh=lHul)tGu~uZ z<5R+&AHZRA#`ZcPW6jsq)m=T+RbQ2(dc7j>{PxFR(Sx!O z|IlRlaWMG^Z@L4}Ld${Z$j^jU+Ib?h^H_D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1~`h;L>FG;@qp{f&>?9O%|P z%tDq-w)WX8%7H%S4B9@~UOk{u+GRri9qq08Beq7COaj7q*X4qL)~Zi;z5W+1l(!B; zUV}ni+aAO4>4SkB22u8XQ%?PBb2z>y%V~2=2_@dy_Cr`i(}i1H literal 0 HcmV?d00001 diff --git a/__pycache__/two_area.cpython-39.pyc b/__pycache__/two_area.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2e9fa18d5e99b781f19396b64f8f23abc7f56ca8 GIT binary patch literal 1993 zcma)7TW=dh6rP#AI<^zXb=&j;icmoq(L@P6A%vQy;v%F_H9;zVSuHzTcg=cjW;ZF0 z)=%vZv3cVcAo0KtnOC0j7m5UYXV$LMiieIh-<&z;?3tN!bJ%RIF^u2;_&a=5W9%>5 zTpkuS_b~I%0Fp_bvL44OQZcl87AKyS+C!)37@DVU&;5!?TRJb8be@ZzC;2h!x)=DK z9d)_3jzU`O_?Pi8BIe~Gu(^ks-vPwtsGg8qiWjVBNlV&TVOqLa9qGZauWKjIMh7Ym zA09{R_-Z+brrRH4<_b{OOBS%Q0pw*9C<<$4OFrkI9YYI4yCreFQULwZ0@g|!xK=vA zdg%fir3Y-5HQ;)=2HYrpV5?-a%{e1q;BwYcS6p~F`+@^N)BBW5bX(h#BG@yyZ}1Z> zvaxo~V>QaP^LVV{GtGa{;-rr`#w@h>cC4+Fy?t$k0RBijKgLD4S1n+i=+$#%i~CHVQ(7WoJCmeh?&CQUn2+%(nqd zxEw#x;Ejr+o#7DI73b|&QQcX79FY^3gDlZ&OnCoF2!8F?PW>r@7FivtPT%Qp0?G=?0hhs~cG6d~=Jw*Y`~7u9>$j&oFQ6 z+kGb8Upe0L>b^@}C0WSDQe){2XjNA0WEH+^n0r|U;iZ06I?ZAoGL*nzqr;w4>9nA~ zS#Ly8-b#5;nKkoMKcb46d3|@sBS4#ZVMuQz6RK%|GCE?TTweD{!FMz4kVF zU5BQ2LZ!lq7UM+o1I_Q7MA!UKQIjyKuf>7XmK+t@O_MB4``SvfGi}9_Sd06z>#Lij z`WA&mg<`5j-6rY=U>h_46u|67rZ4jSJ-)?n^E;f@?U%ma0e`)c@ecOyx^4UX=BgUr zEbxeW%5o4*epxj=RE6X>s5&SD!eRn68X(#qrd8N8stu|PZ!Q))zvOdqo%Cu84n-Go zaL5uhJ+G+G&`K-obFMNej!`dYdN!KgDqtsHcAJWN@f+<#QBc$e1n(0x2|fa7{#a4> z*Y+?ho@yt_voM22^1T+{7nNU)hsn9x{qD3FpB7((&Q1Rp+xWF@U$lfR&RZ+bRf=#r8IkiBa Jql0o^{0EJ$nTP-Y literal 0 HcmV?d00001 diff --git a/ac.py b/ac.py new file mode 100644 index 0000000..c4c4c6d --- /dev/null +++ b/ac.py @@ -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 + + + + diff --git a/actor.py b/actor.py new file mode 100644 index 0000000..acbf62b --- /dev/null +++ b/actor.py @@ -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] + + + + + diff --git a/critic.py b/critic.py new file mode 100644 index 0000000..a9c34fc --- /dev/null +++ b/critic.py @@ -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 + + + + diff --git a/neuralnetwork.py b/neuralnetwork.py new file mode 100644 index 0000000..d9e920d --- /dev/null +++ b/neuralnetwork.py @@ -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) + + + + diff --git a/singlearea_with_adaptive_pid_actor_critic.py b/singlearea_with_adaptive_pid_actor_critic.py new file mode 100644 index 0000000..5a9ff57 --- /dev/null +++ b/singlearea_with_adaptive_pid_actor_critic.py @@ -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() + diff --git a/singlearea_with_adaptive_pid_nn.py b/singlearea_with_adaptive_pid_nn.py new file mode 100644 index 0000000..00ecedb --- /dev/null +++ b/singlearea_with_adaptive_pid_nn.py @@ -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() + diff --git a/two_area.py b/two_area.py new file mode 100644 index 0000000..c1f58ab --- /dev/null +++ b/two_area.py @@ -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 + + + + + + + + + + + + + diff --git a/twoarea_with_adaptive_pid.py b/twoarea_with_adaptive_pid.py new file mode 100644 index 0000000..ee8e6e8 --- /dev/null +++ b/twoarea_with_adaptive_pid.py @@ -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() +