-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataparser.py
156 lines (127 loc) · 4.82 KB
/
dataparser.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
import json
import os
import numpy as np
class Skill:
def __init__(self, name, level):
super(Skill, self).__init__()
self.name = name
self.level = level
class Role:
def __init__(self, name, level):
super(Role, self).__init__()
self.name = name
self.skill_index = None
self.level = level
self.assigned = None
class Contributor:
def __init__(self, name, N):
super(Contributor, self).__init__()
self.name = name
self.N = N
self.temp_skill_increase = None
self.skills = []
self.work_time = 0
def get_score(self):
return np.sum(self.skills) + self.work_time
class Project:
def __init__(self, name, D, S, B, R):
super(Project, self).__init__()
self.name = name
self.D = D
self.S = S
self.B = B
self.R = R
self.roles = []
def get_score(self):
return self.S / (self.D * self.R)
def check_create_directory(path):
# Check whether the specified path exists or not
exists = os.path.exists(path)
if not exists:
# Create a new directory because it does not exist
os.makedirs(path)
def read_input_file(filename):
file1 = open(filename, 'r')
cp_line = file1.readline().split()
C = int(cp_line[0])
P = int(cp_line[1])
contributors = []
all_skills = []
for i in range(C):
contributor_line = file1.readline().split()
contributor_name = contributor_line[0]
N = int(contributor_line[1])
contributor = Contributor(contributor_name, N)
for j in range(N):
skill_line = file1.readline().split()
skill_name = skill_line[0]
all_skills.append(skill_name)
skill_level = int(skill_line[1])
contributor.skills.append(Skill(skill_name, skill_level))
contributors.append(contributor)
projects = []
for i in range(P):
project_line = file1.readline().split()
project_name = project_line[0]
D, S, B, R = int(project_line[1]), int(project_line[2]), int(project_line[3]), int(project_line[4])
project = Project(project_name, D, S, B, R)
for j in range(R):
skill_line = file1.readline().split()
skill_name = skill_line[0]
all_skills.append(skill_name)
skill_level = int(skill_line[1])
project.roles.append(Role(skill_name, skill_level))
projects.append(project)
all_skills = list(set(all_skills))
for contributor in contributors:
all_skill_levels = np.zeros(len(all_skills))
for skill in contributor.skills:
all_skill_levels[all_skills.index(skill.name)] = skill.level
contributor.skills = all_skill_levels
for project in projects:
for role in project.roles:
role.skill_index = all_skills.index(role.name)
return C, P, contributors, projects, all_skills
def read_output_file(filename):
# TODO: Implement Output File Parsing
file1 = open(filename, 'r')
return file1.readline().split()[1:]
class Data:
def __init__(self, task_name):
super(Data, self).__init__()
self.task_name = task_name
self.solution = None
self.C, self.P, self.contributors, self.projects, self.all_skills = read_input_file(f'input_data/{task_name}.in.txt')
def load_best_solution(self):
self.something = read_output_file(f'submission/{self.task_name}.out.txt')
def get_best_score(self):
with open("best_scores.json") as json_file:
json_object = json.load(json_file)
json_file.close()
if self.task_name in json_object:
return json_object[self.task_name]
return 0
def write_if_best(self, score):
current_best = self.get_best_score()
if score > current_best:
print(f'Improved Score on {self.task_name}: {score} - (Prev.: {current_best})')
self.write_solution()
with open("best_scores.json", "r+") as json_file:
data = json.load(json_file)
data[self.task_name] = score
json_file.seek(0) # rewind
json.dump(data, json_file)
json_file.truncate()
check_create_directory(f'runs/{self.task_name}')
self.write_solution(path=f'runs/{self.task_name}/{score}.out.txt')
def write_solution(self, path=None):
# TODO: Implement Output File Writing
if path is None:
path = f'submission/{self.task_name}.out.txt'
out_file = open(path, "w")
out_file.write(f'{len(self.solution)}\n')
for project in self.solution:
out_file.write(f'{project.name}\n')
names = [role.assigned.name for role in project.roles]
out_file.write(f'{" ".join(names)}\n')
out_file.close()