-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolver.go
More file actions
103 lines (87 loc) · 2.1 KB
/
solver.go
File metadata and controls
103 lines (87 loc) · 2.1 KB
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
package main
import (
"fmt"
"sort"
"strconv"
)
type Ants struct {
Name string
Path *[]Vertex
Index int
}
func QueueThem(NumAnts int, MaxFlow [][]Vertex) [][]string {
//Sort them from shortest to longest
sort.Slice(MaxFlow, func(i, j int) bool { return len(MaxFlow[j]) > len(MaxFlow[i]) })
//start queuing them using edmonds-karp
QueuedAnts := make([][]string, len(MaxFlow))
//here, we are adding all ants to the only path we have
//hence why len(MaxFlow) would be 1
if len(MaxFlow) == 1 {
for i := 1; i <= NumAnts; i++ {
AntName := "L"
QueuedAnts[0] = append(QueuedAnts[0], AntName)
}
} else {
for i := 1; i <= NumAnts; i++ {
AntName := "L"
//after adding an ant to the queue
//we need to decide which path does it
//correspond to
for j := 0; j < len(MaxFlow); j++ {
if j < len(MaxFlow)-1 {
PathSize1 := len(MaxFlow[j]) + len(QueuedAnts[j])
PathSize2 := len(MaxFlow[j+1]) + len(QueuedAnts[j+1])
if PathSize1 <= PathSize2 {
QueuedAnts[j] = append(QueuedAnts[j], AntName)
break
}
} else if j == len(MaxFlow)-1 {
QueuedAnts[j] = append(QueuedAnts[j], AntName)
}
}
}
}
//Name the ants properly
counter := 1
PathLengthCount := 0
for counter <= NumAnts {
for _, v := range QueuedAnts {
if len(v)-1 < PathLengthCount {
continue
}
v[PathLengthCount] += strconv.Itoa(counter)
counter++
}
PathLengthCount++
}
return QueuedAnts
}
func PrintResult(QueuedAnts [][]string, MaxFlow [][]Vertex, NumAnts int) {
var ants []Ants
var queueCount = len(QueuedAnts)
var completedQueueCount int = 0
for i := 0; NumAnts > 0; i++ {
for j, v := range QueuedAnts {
if i > len(v)-1 {
completedQueueCount++
if completedQueueCount >= queueCount {
break
}
} else {
ants = append(ants, Ants{Name: v[i], Path: &MaxFlow[j], Index: 1})
}
}
for i, ant := range ants {
if ant.Index < len(*ant.Path) {
vertex := *ant.Path
fmt.Printf("%s-%s ", ant.Name, vertex[ant.Index].Name)
ant.Index++
if ant.Index >= len(vertex) {
NumAnts--
}
ants[i] = ant
}
}
fmt.Println()
}
}