generated from Gophing-Around/hashcode-golang-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithm.go
277 lines (233 loc) · 7.47 KB
/
algorithm.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"sort"
)
type PlannedProject struct {
name string
startDay int
ended bool
contributors []*Contributor
project *Project
}
func algorithm(
maxDays int,
config *Config,
contributors []*Contributor,
projects []*Project,
rolesContributor map[string][]*Contributor,
) []*PlannedProject {
plannedProjects := make([]*PlannedProject, 0)
sort.Slice(projects, func(a, b int) bool {
projectA := projects[a]
projectB := projects[b]
scoreA := projectA.nRoles / projectA.nDays // / projectA.nDays
scoreB := projectB.nRoles / projectB.nDays // / projectB.nDays
return scoreA < scoreB
})
nextDay := 0
for day := 0; day < maxDays && len(plannedProjects) < len(projects); day++ {
if day < nextDay {
continue
}
for _, plannedProject := range plannedProjects {
if plannedProject.ended {
continue
}
if day >= plannedProject.startDay+plannedProject.project.nDays {
plannedProject.ended = true
for rolePosition, contrib := range plannedProject.contributors {
requiredRole := plannedProject.project.rolesList[rolePosition]
oldSkills := contrib.skills
if contrib.skills[requiredRole.name] <= requiredRole.level {
oldLevel := oldSkills[requiredRole.name]
oldSkills[requiredRole.name] = oldLevel + 1
}
contrib.skills = oldSkills
contrib.allocated = false
}
}
}
minNextDay := maxDays
for _, contributor := range contributors {
var bestSkillName string
bestSkillLevel := 0
for contribSkillName, contribSkillLevel := range contributor.skills {
if contribSkillLevel == bestSkillLevel {
bestSkillName = contribSkillName
bestSkillLevel = contribSkillLevel
break
}
// contributorLevelForRole := contributor.skills[projectRole.name]
}
for _, project := range projects {
plannedProject := &PlannedProject{
name: project.name,
contributors: make([]*Contributor, project.nRoles),
project: project,
}
for rolePosition, projectRole := range project.rolesList {
if projectRole.name == bestSkillName && projectRole.level == bestSkillLevel {
plannedProject.contributors[rolePosition] = contributor
}
}
}
}
for _, project := range projects {
if project.alreadyPlanned || project.bestBefore+project.score-project.nDays < day {
continue
}
plannedProject := &PlannedProject{
name: project.name,
contributors: make([]*Contributor, project.nRoles),
project: project,
}
for rolePosition, role := range project.rolesList {
availableContributors := rolesContributor[role.name]
requiredLevel := role.level
if role.mentored {
bestContribScore := 0
var bestContrib *Contributor
for _, contributor := range availableContributors {
if contributor.allocated {
continue
}
contribSkillLevel := contributor.skills[role.name]
currContribScore := contributor.nSkills
if contribSkillLevel == requiredLevel-1 && (bestContribScore == 0 || currContribScore < bestContribScore) {
bestContrib = contributor
bestContribScore = currContribScore
}
}
if bestContrib != nil {
plannedProject.contributors[rolePosition] = bestContrib
bestContrib.allocated = true
for _, projectRole := range project.rolesList {
if bestContrib.skills[projectRole.name] >= projectRole.level {
projectRole.mentored = true
}
}
continue
}
}
bestContribScore := 0
var bestContrib *Contributor
for _, contributor := range availableContributors {
if contributor.allocated {
continue
}
contribSkillLevel := contributor.skills[role.name]
if contribSkillLevel >= requiredLevel {
currContribScore := 0
for _, roles := range project.rolesList {
if contributor.skills[roles.name] >= project.rolesMap[roles.name].level {
currContribScore += 1
}
}
if currContribScore > bestContribScore || (bestContrib != nil && currContribScore == bestContribScore && bestContrib.nSkills > contributor.nSkills) {
bestContribScore = currContribScore
bestContrib = contributor
}
// // || (contribSkillLevel == requiredLevel-1 && findMenthor(plannedProject.contributors)
// // plannedProject.contributors = append(plannedProject.contributors, contributor)
// plannedProject.contributors[rolePosition] = contributor
// contributor.allocated = true
// break
}
}
if bestContrib != nil {
plannedProject.contributors[rolePosition] = bestContrib
bestContrib.allocated = true
for _, projectRole := range project.rolesList {
if bestContrib.skills[projectRole.name] >= projectRole.level {
projectRole.mentored = true
}
}
}
}
// // project roles not filled!!
if hasunfilledRoles(plannedProject.contributors) {
for rolePosition, filledRole := range plannedProject.contributors {
if filledRole != nil {
continue
}
unfilledRole := project.rolesList[rolePosition]
for _, contributor := range plannedProject.contributors {
if contributor == nil {
continue
}
contributorSkillLevel := contributor.skills[unfilledRole.name]
if contributorSkillLevel >= unfilledRole.level {
availableContributors := rolesContributor[unfilledRole.name]
for _, availableContributor := range availableContributors {
if contributor.allocated {
continue
}
if availableContributor.skills[unfilledRole.name] == unfilledRole.level-1 || (availableContributor.skills[unfilledRole.name] == 0 && unfilledRole.level == 1) {
plannedProject.contributors[rolePosition] = contributor
contributor.allocated = true
break
}
}
}
}
}
}
// can't use the project!
// if len(plannedProject.contributors) != project.nRoles {
if hasunfilledRoles(plannedProject.contributors) {
for _, contrib := range plannedProject.contributors {
if contrib == nil {
continue
}
contrib.allocated = false
}
for _, role := range plannedProject.project.rolesList {
role.mentored = false
}
continue
}
// append
plannedProject.startDay = day
plannedProject.project.alreadyPlanned = true
plannedProjects = append(plannedProjects, plannedProject)
}
for _, plannedProject := range plannedProjects {
if day+plannedProject.project.nDays < minNextDay {
minNextDay = day + plannedProject.project.nDays
}
}
nextDay = minNextDay
}
// for _, project := range projects {
// plannedProject := &PlannedProject{
// name: project.name,
// contributors: make([]string, 0),
// }
// for _, role := range project.rolesList {
// availableContributors := rolesContributor[role.name]
// requiredLevel := role.level
// for _, contributor := range availableContributors {
// contribSkillLevel := contributor.skills[role.name]
// if contribSkillLevel >= requiredLevel {
// plannedProject.contributors = append(plannedProject.contributors, contributor.name)
// break
// }
// }
// }
// // Non abbiamo riempito i ruoli!
// if len(plannedProject.contributors) != project.nRoles {
// break
// }
// // append
// plannedProjects = append(plannedProjects, plannedProject)
// }
return plannedProjects
}
func hasunfilledRoles(contributors []*Contributor) bool {
for _, contrib := range contributors {
if contrib == nil {
return true
}
}
return false
}