-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_verification.py
106 lines (80 loc) · 2.97 KB
/
run_verification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import math
from copy import deepcopy
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from miyanmaayeh.runner import Runner
from miyanmaayeh.utils import get_steady_state
PLOT_DIR = "verification/"
Path(PLOT_DIR).mkdir(parents=True, exist_ok=True)
NUM_RUNS = 5
ITERS = 500
def generate_plots(runners):
sns.set_theme()
plt.figure("Verification")
width = NUM_RUNS
height = int(math.floor(float(len(runners)) / float(width)))
fig, ax = plt.subplots(height, width, figsize=(45, 25))
fig.suptitle("Market Prices", fontsize=54)
for i, item in enumerate(runners):
config, runner = item[0], item[1]
plot_x = int(i // width)
plot_y = int(i % width)
prices = [item.price for item in runner.history]
ticks = np.arange(0, len(prices))
steady_ticks, steady_prices = get_steady_state(prices)
z = np.polyfit(steady_ticks, steady_prices, 1)
trendline_func = np.poly1d(z)
trendline = [trendline_func(tick) for tick in steady_ticks]
ax[plot_x, plot_y].plot(ticks, prices, label="Market Price")
ax[plot_x, plot_y].plot(
steady_ticks,
trendline,
label=f"Trendline - y = {trendline_func.coefficients[0]:0.4f}x + {trendline_func.coefficients[1]:0.4f}",
color="green",
)
ax[plot_x, plot_y].legend(loc="upper right", fontsize=14)
ax[plot_x, plot_y].set_title(
f"Cash={config['agents-config']['initial-cash']}, INV={config['agents-config']['initial-inventory']} - {i % NUM_RUNS + 1}",
fontsize=24,
)
ax[plot_x, plot_y].set_xlabel("Time", fontsize=16)
ax[plot_x, plot_y].set_ylabel("Price", fontsize=16)
plt.savefig(PLOT_DIR + "prices.png")
plt.close("Verification")
def main():
steps = math.ceil(ITERS / 1)
config = {
"snapshots_in": [i for i in range(0, ITERS, steps)] + [ITERS - 1],
"initial_agents": 300,
"verifier_count": 100 / 100,
"agents-config": {
"production-average": 3000,
"production-std": 200,
"producers-percentage": 0,
"income-alpha": 4,
"income-beta": 0,
"initial-inventory": 1000,
"initial-cash": 20000,
},
}
runners = []
for i in range(NUM_RUNS):
runner = Runner(config=config)
runner.run(ITERS)
runners.append((deepcopy(config), runner))
config["agents-config"]["initial-cash"] *= 2
for i in range(NUM_RUNS):
runner = Runner(config=config.copy())
runner.run(ITERS)
runners.append((deepcopy(config), runner))
config["agents-config"]["initial-cash"] /= 2
config["agents-config"]["initial-inventory"] *= 2
for i in range(NUM_RUNS):
runner = Runner(config=config.copy())
runner.run(ITERS)
runners.append((deepcopy(config), runner))
generate_plots(runners)
if __name__ == "__main__":
main()