Created
May 3, 2026 23:36
-
-
Save faddah/ed4ed152da4d2b4ed6689584c8999866 to your computer and use it in GitHub Desktop.
Python Diminishing Retuns Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """ | |
| diminishing-returns.ipynb | |
| Automatically generated by Colab. | |
| Original file is located at | |
| https://colab.research.google.com/drive/1IIuPxP1aVJnPxhgx4sV12wig6N_yl_BL | |
| """ | |
| import micropip | |
| await micropip.install("matplotlib") | |
| base_atk = 1000 | |
| atk_bonus_step = .03 | |
| dmg_bonus_step = .03 | |
| total_atk = base_atk * (1 + atk_bonus_step) | |
| percent_buff = total_atk / base_atk | |
| print(total_atk, percent_buff) | |
| # pylint: disable=invalid-name | |
| old_atk = total_atk | |
| total_atk = base_atk * (1 + 2*atk_bonus_step) | |
| percent_buff = total_atk / old_atk | |
| # pylint: enable=invalid-name | |
| print(total_atk, percent_buff) | |
| # ---- Visualizing Saturation ---- | |
| import matplotlib.pyplot as plt | |
| base_atk = 1000 | |
| atk_bonus_step = .03 | |
| dmg_bonus_step = .03 | |
| increments = [x for x in range(10)] | |
| total_atk = [base_atk * (1 + 10*i*atk_bonus_step) for i in increments] | |
| percent_buff = [] | |
| for i in increments: | |
| if i == 0: | |
| percent_buff.append(0.0) | |
| else: | |
| percent_buff.append(total_atk[i] / total_atk[i-1]) | |
| fig, ax = plt.subplots(ncols=2, nrows=1, figsize=(10, 3)) | |
| ax[0].scatter(increments[1:], total_atk[1:]) | |
| ax[0].set_xlabel("atk subs (cnt)") | |
| ax[0].set_ylabel("total atk") | |
| for i, label in enumerate(total_atk): | |
| ax[0].annotate(label, (increments[i], total_atk[i])) | |
| ax[1].scatter(increments[1:], percent_buff[1:]) | |
| ax[1].set_xlabel("atk subs (cnt)") | |
| ax[1].set_ylabel("% dmg increase over prev step") | |
| total_dmg = [] | |
| dmg_or_atk = [] | |
| colors = [] | |
| dmg_percent = 1.25 | |
| atk_percent = 1.0 | |
| for i in increments: | |
| plus_dmg = base_atk * atk_percent * (dmg_percent + dmg_bonus_step) | |
| plus_atk = base_atk * (atk_percent + atk_bonus_step) * dmg_percent | |
| if plus_dmg > plus_atk: | |
| dmg_percent += dmg_bonus_step | |
| dmg_or_atk.append("dmg") | |
| colors.append('g') | |
| total_dmg.append(plus_dmg) | |
| elif plus_atk > plus_dmg: | |
| atk_percent += atk_bonus_step | |
| dmg_or_atk.append("atk") | |
| colors.append('r') | |
| total_dmg.append(plus_atk) | |
| else: | |
| atk_percent += atk_bonus_step | |
| dmg_or_atk.append("either") | |
| colors.append('b') | |
| total_dmg.append(plus_atk) | |
| plt.scatter(increments, total_dmg, c=colors) | |
| plt.xlabel("step") | |
| plt.ylabel("total dmg") | |
| print(dmg_or_atk) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment