From 303bf558b5c995834643e5e89a773de37cbc0050 Mon Sep 17 00:00:00 2001 From: hppeng Date: Sun, 6 Feb 2022 00:04:01 -0800 Subject: [PATCH] Scripts for analyzing mana steal and mana regen using markov chains --- .gitignore | 1 + testing/ms_linalg.py | 48 +++++++++++++++++++++++++++++++++++ testing/ms_sslow.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 testing/ms_linalg.py create mode 100644 testing/ms_sslow.py diff --git a/.gitignore b/.gitignore index ce62501..91acd82 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ sets/ .idea/ *.iml +.editor_log.txt diff --git a/testing/ms_linalg.py b/testing/ms_linalg.py new file mode 100644 index 0000000..c3436f6 --- /dev/null +++ b/testing/ms_linalg.py @@ -0,0 +1,48 @@ +import numpy as np +import numpy.linalg as la +import matplotlib.pyplot as plt + +# Attack speed independent. Fast speed losses not accounted for +mana_consumption = 3 +mana_steal = 10 # /3s +mana_regen = 0 # /5s +#mana_steal = 5 # /3s +#mana_regen = 5 # /5s +natural_regen = 1 + +weight_natural = 8/15 # 4/5 * 2/3 +weight_mr = 2/15 # 1/5 * 2/3 +weight_ms = 4/15 # 4/5 * 1/3 +weight_mr_ms = 1/15 # 1/5 * 1/3 + +MAX_MANA = 20 +transition_matrix = np.zeros((MAX_MANA, MAX_MANA)) +for i in range(MAX_MANA): + natural_state = max(0, i - mana_consumption + natural_regen) + mr_state = min(19, natural_state + mana_regen) + ms_state = min(19, natural_state + mana_steal) + mr_ms_state = min(19, natural_state + mana_regen + mana_steal) + transition_matrix[natural_state, i] = weight_natural + transition_matrix[mr_state, i] += weight_mr + transition_matrix[ms_state, i] += weight_ms + transition_matrix[mr_ms_state, i] += weight_mr_ms + +eigval, eigvec = la.eig(transition_matrix) +print(eigval) +eps = 0.00001 +ind = np.argwhere(abs(eigval - 1) < eps) +steady_state = abs(eigvec[:, ind]) +steady_state /= np.sum(steady_state) +cumulative = np.cumsum(steady_state) +print("mana\tprob cumsum") +for i in range(MAX_MANA): + print(f"{i+1}\t{cumulative[i]}") + +plt.figure() +plt.scatter(range(len(steady_state)), steady_state) +plt.xlim(0, 19) +plt.ylim(0, 0.2) +plt.xlabel("Mana Value") +plt.ylabel("Probability at t=infty") +plt.title(f"Build={mana_regen}mr,{mana_steal}ms,{mana_consumption}mana/sec") +plt.show() diff --git a/testing/ms_sslow.py b/testing/ms_sslow.py new file mode 100644 index 0000000..3cd068b --- /dev/null +++ b/testing/ms_sslow.py @@ -0,0 +1,60 @@ +import numpy as np +import numpy.linalg as la +import matplotlib.pyplot as plt + +# Super slow attack speed. (Idealized to 1 hit/2s, 2/3 chance of proc +mana_consumption = 3 +mana_steal = 5 # /3s +mana_regen = 4 # /5s +#mana_steal = 5 # /3s +#mana_regen = 5 # /5s +natural_regen = 1 + +ms_period = 2 +ms_chance = ms_period / 3 +no_ms_chance = 1 - ms_chance + +MAX_MANA = 20 +TIME_CYCLE = 10 +transition_matrix = np.zeros((MAX_MANA * TIME_CYCLE, MAX_MANA * TIME_CYCLE)) +for j in range(TIME_CYCLE): + for i in range(MAX_MANA): + natural_state = max(0, i - mana_consumption + natural_regen) + if j % 5 == 0: # mr activation + natural_state = min(19, natural_state + mana_regen) + next_ind = ((j+1) % TIME_CYCLE) * MAX_MANA + if j % ms_period == 0: # ms activation + ms_state = min(19, natural_state + mana_steal) + transition_matrix[next_ind + natural_state, i+j*MAX_MANA] = no_ms_chance + transition_matrix[next_ind + ms_state, i+j*MAX_MANA] += ms_chance + else: + transition_matrix[next_ind + natural_state, i+j*MAX_MANA] = 1 + +eigval, eigvec = la.eig(transition_matrix) +print(eigval) +eps = 0.00001 +ind = np.argwhere(abs(eigval - 1) < eps) +steady_state = np.sum(abs(eigvec[:, ind]).reshape((TIME_CYCLE, MAX_MANA)), axis=0) +steady_state /= np.sum(steady_state) +cumulative = np.cumsum(steady_state) +print("mana\tcumulative probability") +for i in range(MAX_MANA): + print(f"{i+1}\t{cumulative[i]}") + +x_ticks = list(range(len(steady_state))) +plt.figure() +plt.scatter(x_ticks, steady_state, label="mana values") +plt.xlim(0, 19) +plt.ylim(0, 0.3) +plt.axvline(x=6+mana_consumption) +plt.xlabel("Mana Value") +plt.xticks(x_ticks) +plt.ylabel("Probability at t=infty") +plt.legend() +ax2 = plt.gca().twinx() +ax2.plot(x_ticks, cumulative, label="cumulative probability") +ax2.set_ylim(0, 1) +ax2.set_ylabel("Cumulative probability at t=infty") +plt.title(f"Build={mana_regen}mr,{mana_steal}ms,{mana_consumption}mana/sec") +plt.legend() +plt.show()