-
Notifications
You must be signed in to change notification settings - Fork 2
/
logic.go
266 lines (259 loc) Β· 7.25 KB
/
logic.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
package anthill
// SearchShortPath - search shortest path from start to end with Bellman-Ford algorithm (Suurballe`s algorithm).
// found path state will be saved on Parents (needed to check from End Room)
// returns true if new path found, otherwise false
func searchShortPath(terrain *anthill) bool {
usableRoomsQueue := &sortedQueue{}
startRoom := terrain.Rooms[terrain.Start]
endRoom := terrain.Rooms[terrain.End]
startRoom.VisitIn, startRoom.VisitOut = true, true
usableRoomsQueue.Enqueue(startRoom, 0, true)
for usableRoomsQueue.Front != nil && !(endRoom.VisitIn || endRoom.VisitOut) {
current := usableRoomsQueue.Dequeue()
currentRoom := current.Room
for next, value := range currentRoom.Paths {
if value == BLOCKED || (!current.Mark && value == STABLE) {
continue
}
addNext(currentRoom, next, current.Weight, value, usableRoomsQueue)
}
}
isFind := endRoom.VisitIn || endRoom.VisitOut
if isFind {
replaceEdges(startRoom, endRoom)
// clear flags
for _, value := range terrain.Rooms {
if value.VisitIn || value.VisitOut {
value.ParentIn, value.ParentOut = nil, nil
value.VisitIn, value.VisitOut = false, false
value.Weight[0], value.Weight[1] = 0, 0
}
}
startRoom.Separated = false
endRoom.Separated = false
usableRoomsQueue = nil
}
return isFind
}
// addNext - add into usableRoomsQueue next room with following rules
func addNext(cur, next *room, weight, state int, usableRoomsQueue *sortedQueue) {
// if next room isn't visited then add without checking weights
if !(next.VisitIn || next.VisitOut) {
// we'll check next room for using on previous paths (separated flag)
if next.Separated {
next.VisitIn = true
next.ParentIn = cur
next.Weight[1] = weight + state
// if it's usually path between nodes then add only in_node (check Surrballe's algo)
if state == STABLE {
usableRoomsQueue.Enqueue(next, next.Weight[1], false)
return
}
}
next.VisitOut = true
next.ParentOut = cur
next.Weight[0] = weight + state
usableRoomsQueue.Enqueue(next, next.Weight[0], true)
return
}
if !next.Separated {
if weight+state >= next.Weight[0] {
return
}
next.ParentOut = cur
next.Weight[0] = weight + state
usableRoomsQueue.Enqueue(next, next.Weight[0], true)
return
}
if state == STABLE {
if next.VisitIn && weight+state >= next.Weight[1] {
return
}
next.VisitIn = true
next.ParentIn = cur
next.Weight[1] = weight + state
usableRoomsQueue.Enqueue(next, next.Weight[1], false)
return
}
if (next.VisitIn && weight+state < next.Weight[1]) || !next.VisitIn {
next.VisitIn = true
next.ParentIn = cur
next.Weight[1] = weight + state
usableRoomsQueue.Enqueue(next, next.Weight[1], false)
}
if (next.VisitOut && weight+state < next.Weight[0]) || !next.VisitOut {
next.VisitOut = true
next.ParentOut = cur
next.Weight[0] = weight + state
usableRoomsQueue.Enqueue(next, next.Weight[0], true)
}
}
// replaceEdges - replace edges for finded paths. (Suurballe`s algorithm)
func replaceEdges(startRoom, endRoom *room) {
r := endRoom
for r != startRoom {
var parent *room
if r.ParentOut != nil && r.ParentIn != nil {
i := 0
for _, value := range r.Paths {
if value == BLOCKED {
i++
}
}
if i > 1 {
if r.Paths[r.ParentOut] == BLOCKED {
parent = r.ParentOut
} else {
parent = r.ParentIn
}
} else {
if r.Paths[r.ParentIn] == STABLE {
parent = r.ParentIn
} else {
parent = r.ParentOut
}
}
} else if r.ParentOut != nil {
parent = r.ParentOut
} else {
parent = r.ParentIn
}
// reversing
if r.Paths[parent] == STABLE {
parent.Separated = true
r.Separated = true
r.Paths[parent] = REVERSED
parent.Paths[r] = BLOCKED
} else {
parent.Separated = false
r.Paths[parent] = STABLE
parent.Paths[r] = STABLE
}
r = parent
}
}
// checking effective of new short path
// current steps count < previous steps count
// if effective then replace result to new (returns true)
// if not then return previous result (returns false)
func checkEffective(terrain *anthill) bool {
startRoom, endRoom := terrain.Rooms[terrain.Start], terrain.Rooms[terrain.End]
i, lenNewPaths := 0, 0
for _, value := range startRoom.Paths {
if value == BLOCKED {
lenNewPaths++
}
}
newPaths := make([]*list, lenNewPaths)
for key, value := range startRoom.Paths {
if value == BLOCKED {
newPaths[i] = &list{}
cur := key
for cur != endRoom {
newPaths[i].PushBack(cur)
for next, vNext := range cur.Paths {
if vNext == BLOCKED {
cur = next
break
}
}
}
newPaths[i].PushBack(endRoom)
i++
}
}
curStepsCount, used := fastCalcSteps(terrain.AntsCount, newPaths)
// For debug
// fmt.Printf("Steps: %d\n", curStepsCount)
// for i := range newPaths {
// start := newPaths[i].Front
// fmt.Printf("Len: %d | %s", newPaths[i].Len, start.Room.Name)
// for start.Room != endRoom {
// start = start.Next
// fmt.Printf(" --> %s", start.Room.Name)
// }
// fmt.Println()
// }
// fmt.Println()
if terrain.StepsCount == 0 || (terrain.StepsCount >= curStepsCount && used) {
terrain.StepsCount = curStepsCount
terrain.Result.Paths = newPaths
return curStepsCount != 1
}
return false
}
// fastCalcSteps - calculate steps for paths and ants count
func fastCalcSteps(ants int, paths []*list) (int, bool) {
steps, lossPerStep := 0, 0
max, maxUsed := 0, false
comingAnts := make(map[int]int)
for _, value := range paths {
comingAnts[value.Len]++
if max < value.Len {
max = value.Len
}
}
if comingAnts[1] > 0 {
return 1, true
}
for ants > 0 {
steps++
ants -= lossPerStep
if steps == max && ants >= comingAnts[max] {
maxUsed = true
}
lossPerStep += comingAnts[steps]
ants -= comingAnts[steps]
}
return steps, maxUsed
}
// calcSteps LOGIC
// Example: PathsLens: [2, 5, 5, 6] | ants: 12
// -=-=-= Start =-=-=-
// Steps = 6 = slice[slice.len-1] | (lastElem + 1) - eachElem -> [5, 2, 2, 1]
// ants = ants - sumElems
// DEL: [5, 2, 2, 1] | ants/slice.Len = 2/slice.len = 0.5 | ants = 2 | steps: 6+0 = 6
// MOD: [6, 3, 2, 1] | ants%slice.Len = 2%4 = 2, 2-2 = 0 | ants = 0 | steps: 6+1 = 7
// On Sending Ants
// [ 1, 1, 1, 1 ] len = 0 | 12 | on st 7 = 0 | will not
// [ x, 1, 1, 1 ] len = 1 | 11 | on st 6
// [ x, 1, 1, 1 ] len = 1 | 10 | on st 5
// [ x, 1, 1, 1 ] len = 1 | 9 | on st 4
// [ x, x, x, 1 ] len = 3 | 6 | on st 3
// [ x, x, x, x ] len = 4 | 2 | on st 2
// [ x, x, -, - ] len = 2 | 0 | on st 1
// Inputs Sorted Paths, and AntsCount should be > 0
// Function designed for the optimal number of paths for ants count
func calcSteps(antsCount int, sortedPaths []*list) (int, []int) {
if len(sortedPaths) < 1 {
return 0, []int{}
}
if sortedPaths[0].Len == 1 {
return 1, []int{antsCount}
}
// Create Result
lenPaths := len(sortedPaths)
result := make([]int, lenPaths)
steps, lastElem := sortedPaths[lenPaths-1].Len, sortedPaths[lenPaths-1].Len+1
for i := 0; i < lenPaths; i++ {
result[i] = lastElem - sortedPaths[i].Len
antsCount -= result[i]
}
if antsCount > 0 {
if antsCount >= lenPaths {
del := antsCount / lenPaths
antsCount %= lenPaths
steps += del
for i := 0; i < lenPaths; i++ {
result[i] += del
}
}
if antsCount > 0 {
steps++
for i := 0; i < antsCount; i++ {
result[i]++
}
}
}
return steps, result
}