This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
175 lines (147 loc) · 4.75 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import multiprocessing
import os
import sys
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib.animation import FuncAnimation
import simulation
import utils
def start(i: int | None = None, j: int | None = None) -> simulation.Simulation:
l = os.listdir("extracted")
if len(l) == 0:
print("There is no data extracted. Please use extract.py")
exit(1)
if i is None:
i = utils.select(l)
path_other = os.path.join("extracted", l[i])
is_season = os.path.isdir(path_other) and (l[i][:6].lower() == "season")
is_race = os.path.isdir(path_other) and not is_season
if is_season:
l = sorted(os.listdir(path_other))
if len(l) == 0:
print("There is no race data here")
exit(1)
if j is None:
print("Season detected. Which race do you want to simulate ?")
j = utils.select(l)
path = os.path.join(path_other, l[j])
elif is_race:
l = sorted(os.listdir(path_other))
if len(l) == 0:
print("There is no race data here")
exit(1)
if j is None:
print("Race detected. For which season do you want to simulate ?")
j = utils.select(l)
path = os.path.join(path_other, l[j])
else:
path = path_other
sim = simulation.SlipstreamSim(0.05, name=os.path.basename(path))
# sim = simulation.SimpleSim(0.05)
sim.load_csv(path)
if is_season:
num = int(os.path.basename(path).split(" ")[0])
l = os.listdir(path_other)
for race in l:
if int(race.split(" ")[0]) < num:
sim.prepare_race(os.path.join(path_other, race))
elif is_race:
int_year, int_month, int_day = utils.extract_date(os.path.basename(path))
l = os.listdir(path_other)
for race in l:
year, month, day = utils.extract_date(race)
yes = False
yes = year < int_year
if not yes:
yes = month < int_month
if not yes:
yes = day < int_day
if yes:
sim.prepare_race(os.path.join(path_other, race))
return sim
def run(
values: tuple[int | None, int | None, bool, int]
) -> tuple[tuple[float, float, float], tuple[float, float, float]]:
i, j, render, s = values
if s is not None:
import time
time.sleep(s)
sim = start(i, j)
sim.render = render
sim.start()
while not sim.ended:
sim.update()
# sim.show_energy_evol(-1)
# sim.correctness()
# sim.compare_positions()
sim.write()
sim.give_points()
sim.render_write()
return (sim.excat_rate(), sim.adapt_rate())
i = None
j = None
MULTI_DEFAULT = 10
use_multi = -1
use_render = False
k = 0
while k < len(sys.argv):
arg = sys.argv[k]
if arg == "-i":
k += 1
i = int(sys.argv[k]) - 1
elif arg == "-j":
k += 1
j = int(sys.argv[k]) - 1
elif (arg == "-m") or (arg == "--multi"):
try:
use_multi = int(sys.argv[k + 1])
k += 1
except ValueError:
use_multi = MULTI_DEFAULT
except IndexError:
use_multi = MULTI_DEFAULT
elif (arg == "-r") or (arg == "--render"):
use_render = True
elif arg[-7:] == "main.py":
pass
elif (arg == "-h") or (arg == "--help"):
print("Help for main.py")
print("Launch the simulation of a nordic combined race\n")
print(
" -i [int] Select with file or folder (if race/season) to read in."
)
print(" Use the same number as shown when not using -i")
print(" -j [int] Select the race in season or year in race")
print(" Use the same number as shown when not using -j")
print(" -m/--multi [int] Select the number same run to do")
print(" -r/--render If set, write all images of the simulation")
else:
print(f"Unknow argument {arg}")
k += 1
if use_multi == -1:
for _ in range(50):
run((i, j, use_render, None))
else:
correct_a = 0.0
correct_e = 0.0
total_a = 0.0
total_e = 0.0
points = {}
pool = multiprocessing.Pool(12)
out = pool.map(run, [(i, j, use_render, k % 12) for k in range(use_multi)])
for race in out:
e, a = race
correct_a += a[0]
total_a += a[1]
correct_e += e[0]
total_e += e[1]
print(
f"\nExact position: {correct_e} / {total_e} ({(correct_e / total_e * 100):6.5}%)"
)
print(
f"Adapted metric: {correct_a} / {total_a} = ({(correct_a / total_a * 100):6.5}%)"
)
# ffmpeg command:
# ffmpeg -i imgs/%5d.png video.mp4