Skip to content

Commit

Permalink
Fixing integral error, controller tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
Ickaser committed Dec 12, 2020
1 parent 5d8948f commit 1f86c09
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
5 changes: 4 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
SAE = np.sum(np.abs(err[:etod]))
# print(err[:etod])
print("SAE", SAE)
print("etod", etod)

# Plot results up until time of death

Expand All @@ -77,7 +78,7 @@
plt.text(etod*.7, 60, f"style={style}")
plt.legend(fontsize=14)
plt.ylim(0, 100)
plt.xlim(0, etod)
plt.xlim(0, np.max([etod, 150]))
plt.savefig(proj_dir + f"health_skill{skill:d}_style{style[0]}.png");
plt.show()

Expand All @@ -90,6 +91,7 @@
plt.xlabel('Time (sec)',fontsize=18)
plt.ylabel('Damage per second',fontsize=18)
plt.ylim(0, 1.5)
plt.xlim(0, np.max([etod, 150]))
plt.axvline(150, color="gray", ls="--", label="")
plt.savefig(proj_dir + f"dps_skill{skill:d}_style{style[0]}.png");
plt.show()
Expand All @@ -98,6 +100,7 @@
plt.title('Enemy Attack',fontsize=24)
plt.xlabel('Time (sec)',fontsize=18)
plt.ylabel('Attack',fontsize=18)
plt.xlim(0, np.max([etod, 150]))
plt.savefig(proj_dir + f"eatk_skill{skill:d}_style{style[0]}.png");
plt.show();

Expand Down
60 changes: 30 additions & 30 deletions params.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
6.185000000000000142e+01 9.110496310522270269e-02
6.135000000000000142e+01 1.147940699589847235e-01
5.800000000000000000e+01 1.198197093070172414e-01
6.820000000000000284e+01 1.526013081822418516e-01
6.665000000000000568e+01 1.582350139619470297e-01
7.190000000000000568e+01 1.700341781992531498e-01
7.979999999999999716e+01 1.788746883719655456e-01
7.879999999999999716e+01 1.855329733205805520e-01
1.056500000000000057e+02 1.902041469940962126e-01
1.228499999999999943e+02 1.682673875464476554e-01
3.400000000000000000e+01 5.366252242736072042e-02
3.454999999999999716e+01 8.786304769087074917e-02
4.395000000000000284e+01 7.382434740133875239e-02
4.589999999999999858e+01 8.687011060051549882e-02
4.304999999999999716e+01 1.898921342488902875e-01
4.400000000000000000e+01 2.042359441643654405e-01
4.900000000000000000e+01 2.136238917251379599e-01
5.135000000000000142e+01 2.888511147237640686e-01
5.454999999999999716e+01 3.117799609850196996e-01
7.390000000000000568e+01 2.596296621685815764e-01
2.269999999999999929e+01 5.027830353079761061e-02
2.794999999999999929e+01 -3.151681375235237774e-03
2.789999999999999858e+01 7.877458372718487700e-02
3.454999999999999716e+01 6.615319001556019263e-02
3.764999999999999858e+01 1.092985999992179269e-01
4.275000000000000000e+01 1.403562012299598427e-01
4.495000000000000284e+01 1.919179507721606448e-01
4.525000000000000000e+01 2.400006823941317202e-01
5.160000000000000142e+01 3.100291448405274042e-01
4.460000000000000142e+01 3.326581837297094557e-01
3.574000000000000199e+01 -6.242717980375666470e-02
3.446000000000000085e+01 1.304435965918380180e-01
4.153000000000000114e+01 -5.879696023767413959e-02
5.321999999999999886e+01 3.879314157956487152e-02
4.935999999999999943e+01 2.357783438531881048e-02
5.868999999999999773e+01 -6.021071420278685526e-02
6.448999999999999488e+01 7.782304327822712857e-03
6.278000000000000114e+01 -1.276809944581068848e-02
7.542000000000000171e+01 -6.536743029162225826e-03
8.393999999999999773e+01 -3.441385887854338157e-03
1.716000000000000014e+01 2.847442466421459817e-02
1.363000000000000078e+01 4.047846685856690968e-02
1.055000000000000071e+01 1.614982683306922795e-01
1.785000000000000142e+01 6.437181009130500675e-02
2.217000000000000171e+01 -1.758899726078352044e-01
2.687999999999999901e+01 -2.241218869792709345e-01
3.318999999999999773e+01 5.739180369944014981e-02
3.985000000000000142e+01 7.779416867895011523e-02
5.246000000000000085e+01 1.510374578315355938e-02
5.777000000000000313e+01 -3.228649657438432269e-02
1.942999999999999972e+01 -1.192044277121115103e-01
1.623999999999999844e+01 5.062283552325429897e-02
1.791000000000000014e+01 2.820243413758025813e-02
2.328000000000000114e+01 -1.275619677958871601e-01
2.264000000000000057e+01 7.001564563467839453e-02
2.605999999999999872e+01 2.003345165521419613e-01
3.543999999999999773e+01 6.725649503273839480e-02
4.088000000000000256e+01 -8.475918500560533642e-02
4.014999999999999858e+01 -4.075058819520085800e-03
3.815999999999999659e+01 8.247852360611236363e-04
12 changes: 7 additions & 5 deletions sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def calc_params(kind):
gain = gain_arr[ind]
tau = tau_arr[ind]

tauI = tau / 5
Kc = 1/gain * 3
tauI = tau *1.5
Kc = 1/gain * 2
return Kc, tauI

#kc = np.array([Kc1,Kc2,Kc3])
Expand All @@ -130,12 +130,12 @@ def calc_params(kind):
dt = 1


def PID(i,SPH, H, H_last, Kc,tau_I, tau_D, INTerr_prev): #where i is the current time
def PID(i,SPH, H, H_last, Kc,tau_I, tau_D, INTerr_prev, i_last): #where i is the current time
# This function is written in terms of health, but works equivalently for a different PV.
Abias = 1
#P
error = SPH-H
sumierr = error*dt
sumierr = error*dt*(i-i_last)


#ID
Expand Down Expand Up @@ -174,6 +174,7 @@ def sim_gameplay(style, skill, adj=True, control=True):
count_enem = 1
# initialize DPS value
DPS_last = 1
i_last = 0
interr = 0
continue

Expand All @@ -197,8 +198,9 @@ def sim_gameplay(style, skill, adj=True, control=True):
Kc, tauI = calc_params(kind)
# PID control with parameters
DPS_now = (HP0 - HP_char[i-1])/i
new_At, interr = PID(i, SP_DPS, DPS_now, DPS_last, Kc, tauI, tauD, interr)
new_At, interr = PID(i, SP_DPS, DPS_now, DPS_last, Kc, tauI, tauD, interr, i_last)
DPS_last = DPS_now
i_last = i
# print("cntrl", end="")
Enemy[1] += new_At
elif adj and HP_char[i-1] <= 50:
Expand Down

0 comments on commit 1f86c09

Please sign in to comment.