-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
138 lines (129 loc) · 6.22 KB
/
stats.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
import csv
import os
from sim import Sim
class Statistics:
_initialized = False
_files = {
'task' : [
'run', 'repetition', 'config',
'task_id', 'source_car_id', 'time_of_arrival', 'deadline', 'priority',
'complexity', 'status', 'processing_car', 'processing_start', 'processing_end', 'policy', 'lambda_exp'
],
'car' : [
'run', 'repetition', 'config',
'car_id', 'generated_tasks', 'processed_tasks', 'successful_tasks', 'queued_tasks', 'processing_power',
'total_processing_time', 'arrival', 'departure', 'lifetime', 'policy', 'lambda_exp'
],
'action' : [
'run', 'repetition', 'config',
'policy', 'lambda_exp', 'episode',
'time', 'action', 'reward', 'best_action', 'resource_count'
],
'episode' : [
'run', 'repetition', 'config',
'policy', 'lambda_exp', 'episode',
'total_reward', 'best_selection_ratio', 'num_actions'
]
}
@staticmethod
def _filename(stat_type):
filename = [
f"_r_{Sim.get_parameter('run')}",
f"_cf_{Sim.get_parameter('configfile')}" if Sim.get_parameter('configfile') else "",
f"_c_{Sim.get_parameter('sim_config')}" if Sim.get_parameter('sim_config') else "",
]
return f"{stat_type}" + "".join(name for name in filename if name) + ".csv"
@classmethod
def _initialize_files(cls):
# FIXME: The 'results/...' directory path is hardcoded here. We need a smarter handling of this -- Probably from the config file
directory = 'results' + '/' + Sim.get_parameter('sim_config')
os.makedirs(directory, exist_ok=True)
for stat_type, fieldnames in cls._files.items():
filepath = os.path.join(directory, Statistics._filename(stat_type))
with open(filepath, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter="\t")
writer.writeheader()
cls._initialized = True
@classmethod
def _check_initialization(cls):
if not cls._initialized:
cls._initialize_files()
@classmethod
def _save_stats(cls, stat_type, data):
cls._check_initialization()
# FIXME: The 'results/...' directory path is hardcoded here. We need a smarter handling of this -- Probably from the config file
filepath = os.path.join('results' + '/' + Sim.get_parameter('sim_config'), Statistics._filename(stat_type))
fieldnames = cls._files[stat_type]
with open(filepath, 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter="\t")
writer.writerow(data)
@classmethod
def save_task_stats(cls, task, processing_car_id):
data = {
'task_id': task.id,
'source_car_id': task.source_car.id,
'time_of_arrival': task.time_of_arrival,
'deadline': task.deadline,
'priority': task.priority,
'complexity': task.complexity,
'status': task.status,
'processing_car': processing_car_id,
'processing_start': task.processing_start,
'processing_end': task.processing_end,
'repetition': Sim.get_parameter('repetition'),
'policy': Sim.get_parameter('policy'),
'run': Sim.get_parameter('run'),
'config': Sim.get_parameter('sim_config'),
'lambda_exp': Sim.get_parameter('lambda_exp')() # FIXME: here I am executing the lambda to get the value that I need. This needs to be handled in a smarter way by Sim.get_parameter()
}
cls._save_stats('task', data)
@classmethod
def save_car_stats(cls, car, current_time):
data = {
'car_id': car.id,
'generated_tasks': car.generated_tasks_count,
'processed_tasks': car.processed_tasks_count,
'successful_tasks': car.successful_tasks,
'queued_tasks': len(car.assigned_tasks),
'processing_power': car.processing_power,
'total_processing_time': car.total_processing_time,
'arrival': car.time_of_arrival,
'departure': current_time,
'lifetime': current_time - car.time_of_arrival,
'repetition': Sim.get_parameter('repetition'),
'policy': Sim.get_parameter('policy'),
'run': Sim.get_parameter('run'),
'config': Sim.get_parameter('sim_config'),
'lambda_exp': Sim.get_parameter('lambda_exp')() # FIXME: here I am executing the lambda to get the value that I need. This needs to be handled in a smarter way by Sim.get_parameter()
}
cls._save_stats('car', data)
@classmethod
def save_action_stats(cls, current_time, episode, action, reward, is_best, resource_count):
data = {
'time': current_time,
'episode': episode,
'action': action,
'reward': reward,
'best_action': is_best,
'resource_count': resource_count,
'repetition': Sim.get_parameter('repetition'),
'policy': Sim.get_parameter('policy'),
'run': Sim.get_parameter('run'),
'config': Sim.get_parameter('sim_config'),
'lambda_exp': Sim.get_parameter('lambda_exp')() # FIXME: here I am executing the lambda to get the value that I need. This needs to be handled in a smarter way by Sim.get_parameter()
}
cls._save_stats('action', data)
@classmethod
def save_episode_stats(cls, episode, total_reward, best_selection_ratio, num_actions):
data = {
'episode': episode,
'total_reward': total_reward,
'best_selection_ratio': best_selection_ratio,
'repetition': Sim.get_parameter('repetition'),
'policy': Sim.get_parameter('policy'),
'run': Sim.get_parameter('run'),
'config': Sim.get_parameter('sim_config'),
'lambda_exp': Sim.get_parameter('lambda_exp')(), # FIXME: here I am executing the lambda to get the value that I need. This needs to be handled in a smarter way by Sim.get_parameter()
'num_actions': num_actions
}
cls._save_stats('episode', data)