-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_log.py
161 lines (127 loc) · 5.2 KB
/
time_log.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
# Copyright 2017 Integrity Software and Games, LLC
#
# ##### BEGIN GPL LICENSE BLOCK ######
# This file is part of UltiMaze.
#
# UltiMaze is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# UltiMaze is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with UltiMaze. If not, see <http://www.gnu.org/licenses/>.
# ##### END GPL LICENSE BLOCK #####
"""
Module for logging time to file for use by the time estimator.
Available Functions:
log_time - Logs time it took to create maze to file
"""
import os
import bpy
def log_time(elapsed_time):
"""Logs time it took to create maze to file.
Args:
elapsed_time - time it took to create maze
read_log_file - Reads log file and returns list of logs
for time estimation
"""
# get text from file
my_settings_dir = os.path.join(os.path.dirname(__file__), "settings")
time_log_file = os.path.join(my_settings_dir, "estimate_time_log.txt")
with open(time_log_file, "r") as t:
time_log = t.read()
log_list = time_log.split(";")
# delete the last one that gets created because of extra ";" at end
del log_list[len(log_list) - 1]
log_list += [str(bpy.context.scene.mg.mg_width * bpy.context.scene.mg.mg_height) + "," + str(elapsed_time)]
# convert into normal list
i = 0
for log in log_list:
split_log = log.split(",")
log_list[i] = (int(split_log[0]), float(split_log[1]))
i += 1
# sort
log_list.sort()
# average doubles
for i, log in enumerate(log_list):
if log_list[i][0] == log_list[i - 1][0]:
log_list[i] = (log_list[i][0], (log_list[i][1] + log_list[i - 1][1]) / 2)
del log_list[i - 1]
# convert to string
str_log_list = ""
i = 0
for log in log_list:
str_log_list = str_log_list + str(log[0]) + "," + str(log[1]) + ";"
i += 1
# write text to file
with open(time_log_file, "w") as t:
t.write(str_log_list)
def read_log_file():
"""Reads log file and returns list of logs for time estimation."""
# get text from file
my_settings_dir = os.path.join(os.path.dirname(__file__), "settings")
time_log_file = os.path.join(my_settings_dir, "estimate_time_log.txt")
with open(time_log_file, "r") as t:
time_log = t.read()
log_list = time_log.split(";")
# delete the last one that gets created because of extra ";" at end
del log_list[len(log_list) - 1]
# convert into normal list
i = 0
for log in log_list:
split_log = log.split(",")
log_list[i] = (int(split_log[0]), float(split_log[1]))
i += 1
return log_list
# Time estimate
class EstimateTimeMG(bpy.types.Operator):
bl_label = "Estimate Time"
bl_idname = "maze_gen.estimate_time_mg"
bl_description = ("Estimates the number of seconds the maze " +
"generator will take")
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
x_dim = context.scene.mg.mg_width
y_dim = context.scene.mg.mg_height
estimated_loops = round((x_dim * y_dim * 1.25))
self.report({'INFO'}, "Estimated Loops: " + str(estimated_loops) +
" loops")
# get log_list stored as [(size0, time0),(size1, time1),(size2, time2)]
log_list = read_log_file()
maze_size = x_dim * y_dim
# find logs that are closest and on either side of maze_size
small_size = 8
small_time = 0.01
large_size = 1000001
large_time = 300000
for log in log_list:
log_0 = log[0]
log_1 = log[1]
if small_size < log_0 < maze_size:
small_size = log_0
small_time = log_1
elif maze_size < log_0 < large_size:
large_size = log_0
large_time = log_1
elif log_0 == maze_size:
small_size = log_0
small_time = log_1
large_size = log_0
large_time = log_1
# make sure large_size is not small size (div by 0 error)
if large_size != small_size:
maze_time = (small_time + (((maze_size - small_size) /
(large_size - small_size)) * (large_time - small_time)))
else:
maze_time = large_time
time_est = (str(int(maze_time / 3600)) + " hours, " + str(int((maze_time
% 3600) / 60)) + " minutes, " + str(
int(maze_time % 60)) +
" seconds")
self.report({'INFO'}, "Estimated Time: " + time_est)
return {'FINISHED'}