-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (152 loc) · 4.57 KB
/
main.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
package main
import (
"log"
"os"
"sort"
"strconv"
"gopkg.in/yaml.v3"
)
// Config is the config-struct
type Config struct {
Input string
Generations int
HillclimbGenerations int
Initiate string
Mutate string
Dot bool
PopulationSize int
MutationDruck float64
Crossover string
Algorithmus string
TurnierGegner int
}
var cfg Config
func main() {
readConfig(&cfg)
// Extract the date out of the File and check if there could be errors
verticesCount, customerDemand, Aij, Bij, Cij := parseFile(cfg.Input)
costsA := inputToGraph(verticesCount, Aij)
costsB := inputToGraph(verticesCount, Bij)
costsC := inputToGraph(verticesCount, Cij)
network, err := createNetwork(costsA, costsB, costsC)
errFunc(err)
// Generate a Graph based on the costs of A
if cfg.Dot {
makeGraph(costsA)
}
// Calculate the demand which is also the capacity of the source
// The demand could be seen as negative storage capacity
// This fact will be used later on
// Also the last node is the source
var sourceCapacity int64
var demand []int64
for i := range customerDemand {
sourceCapacity = sourceCapacity + customerDemand[i]
demand = append(demand, -1*customerDemand[i])
}
demand = append(demand, sourceCapacity)
var csvList [][]int64
if cfg.Algorithmus == "hillclimber" {
csvList = hillclimb(verticesCount, demand, network, costsA, costsB, costsC)
} else if cfg.Algorithmus == "evolutionär" {
csvList = geneticAlgorithm(verticesCount, demand, network, costsA, costsB, costsC)
}
var csvString string
for _, row := range csvList {
csvString = csvString + strconv.FormatInt(row[0], 10) + "," + strconv.FormatInt(row[1], 10) + ",\n"
}
fileName := "helpers/" + strconv.Itoa(cfg.PopulationSize) + "pop_" + strconv.Itoa(cfg.Generations) + "gen_" + strconv.Itoa(cfg.TurnierGegner) + "geg_" + strconv.FormatFloat(cfg.MutationDruck, 'f', -1, 32) + "druck_opc.csv"
f, err := os.Create(fileName)
errFunc(err)
defer f.Close()
_, err = f.WriteString(csvString)
errFunc(err)
}
func printChild(x Child, n int) {
print(n, ",")
for i := range x.storage {
print(x.storage[i], ",")
}
println(x.fitness, ",")
}
func geneticAlgorithm(verticesCount int, demand []int64, network [][]bool, costsA, costsB, costsC [][]int64) (csvList [][]int64) {
var population []Child
population = populate(network, verticesCount, demand, costsA, costsB, costsC)
population = selectionTurnier(population)
for i := 0; i < cfg.Generations; i++ {
population = selectionTurnier(population)
population = evolution(population, costsA, costsB, costsC, network)
// Output best child of this generation
var pool []Child
copy(pool, population)
sort.Sort(ByFitness(pool))
csvList = append(csvList, []int64{int64(i + 1), population[0].fitness})
printChild(population[0], i+1)
}
return
}
func hillclimb(verticesCount int, demand []int64, network [][]bool, costsA, costsB, costsC [][]int64) (csvList [][]int64) {
c := new(Child) // Always Child
x := new(Child) // Always parent
x.demand = make([]int64, verticesCount)
copy(x.demand, demand)
// Initiate the flow but make it dependend from the config
if cfg.Initiate == "zero" {
x.initiateFlowZero(verticesCount)
} else if cfg.Initiate == "one" {
x.initiateFlowOne(verticesCount, network)
} else if cfg.Initiate == "two" {
x.initiateFlowTwo(verticesCount, network)
}
for _, storage := range x.storage {
print(storage, ",")
}
x.costCalculator(costsA, costsB, costsC)
println(x.fitness, ",")
for i := 0; i < cfg.HillclimbGenerations; i++ {
// Find Neighbour of x
x.findNeighbourTwo(c, network)
c.costCalculator(costsA, costsB, costsC)
for k := range c.storage {
print(x.storage[k], ", ")
}
println(x.fitness, ",")
if c.fitness < x.fitness {
c.toParent(x)
}
csvList = append(csvList, []int64{int64(i + 1), x.fitness})
}
return csvList
}
func readConfig(cfg *Config) {
f, err := os.Open("cfg.yml")
errFunc(err)
defer f.Close()
decoder := yaml.NewDecoder(f)
err = decoder.Decode(cfg)
errFunc(err)
}
// produce a dotfile which displays the graph
func makeGraph(network [][]int64) {
graph := `digraph
{
`
for i, row := range network {
for j := range row {
if network[i][j] != 0 {
graph = graph + " " + strconv.Itoa(i) + " -> " + strconv.Itoa(j) + "[ label=" + strconv.FormatInt(network[i][j], 10) + "];\n"
}
}
}
graph = graph + "}"
// Dump dot-graph to file
f, err := os.Create("input.dot")
errFunc(err)
_, err = f.WriteString(graph)
errFunc(err)
}
func errFunc(err error) {
if err != nil {
log.Fatal(err)
}
}