-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
393 lines (292 loc) · 12.1 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
We prepare our map in `plant.map` file, then go to inputs folder to define our models such as workstation, train and materials.
Then program loads `plant.map` and the model files first.
Afterwards the simulation does is checking coordinations in given model files if they're in-line with the map.
If they are not in-line, simulation stops running.
After checking the models, simulation finds all possible ways from warehouse stops at through all workstations
and then back to warehouse again with calculating the cost of the route. Amongst the all routes the program picks the shortest route,
Then bring all the output into "output.txt" file.
*/
package main
import (
"fmt"
"io"
"os"
"strings"
"semesterproject/models"
"semesterproject/pather"
"semesterproject/solver"
"semesterproject/utils"
)
var harita solver.Map
var train *solver.Tile
var workstations solver.Tiles
var options solver.Options
var bestOption *solver.Option
var trainModel models.Train
var workstationsModels models.Workstations
var usedStations models.Workstations
var collectedWorkstations models.Workstations
var globalTime float64
var fileWriter io.Writer
type cheaper struct {
ws *models.Workstation
cost float64
}
var border = strings.Repeat("-", 45)
// Gets all the possible routes within the map and returns the options as `Options` type.
func buildRoute(workstations solver.Tiles, startPoint *solver.Tile) solver.Options {
var options solver.Options
routes := solver.GetPermutation(workstations)
for _, route := range routes {
var totalCost float64
var innerCosts []float64
paths := make([][]pather.Pather, 0)
route = append([]*solver.Tile{startPoint}, route...)
for i := 1; i < len(route); i++ {
path, cost, found := pather.Path(route[i], route[i-1])
if !found {
fmt.Println("Cant't find the route")
} else {
cost = cost * 1 / trainModel.Speed
totalCost += cost
paths = append(paths, path)
innerCosts = append(innerCosts, cost)
}
}
options = options.Append(&solver.Option{Route: route, Cost: totalCost, Path: paths, InnerCosts: innerCosts})
}
return options
}
func main() {
harita = solver.ParseMap(utils.GetMaze())
train = harita.GetKind(solver.Train)[0]
workstations = harita.GetKind(solver.Workstation)
models.LoadMaterials()
workstationsModels = models.LoadWorkstations()
filterUnusedStations()
trainModel = models.LoadTrain()
trainModel.LoadFromStorage(workstationsModels)
options = buildRoute(workstations, train)
getResult()
}
// Returns the results of the simulation and prints to the user
func getResult() {
if !harita.CrossCheck() {
fmt.Println("Please check all locations in models and the map. Locations are not in-line")
fmt.Println("Simulation stops.")
os.Exit(1)
}
bestOption = options.GetBestResult()
f, err := os.Create("output.txt")
if err != nil {
fmt.Errorf("couldn't create the file")
}
defer f.Close()
fileWriter = io.MultiWriter(os.Stdout, f)
printRoute(*bestOption)
}
func printRoute(o solver.Option) {
row, col := harita.GetSize()
fmt.Fprintf(fileWriter, "\n%s\nMAP SPECS:\n%s\n", border, border)
fmt.Fprintf(fileWriter, "Map Size: %dx%d\n", row, col)
fmt.Fprintf(fileWriter, "Number of Workstations: %d\n%s\n", len(workstationsModels), border)
fmt.Fprintf(fileWriter, "\n%s\nTRAIN SPECS:\n%s\nLocation: %d,%d\nCapacity: %d\n%s\n\n", border, border, trainModel.X, trainModel.Y, trainModel.MaxCapacity, border)
fmt.Fprintf(fileWriter, "%s\nWORKSTATIONS\n%s\n", border, border)
for i := 0; i < len(workstationsModels); i++ {
var matSep = workstationsModels[i].PrintRequirements()
fmt.Fprintf(fileWriter, "Name: %s\nLocation: %d,%d\nProcess Time: %.2f\nLoad Time: %.2f\nUnload Time: %.2f\nMaterials Demand:\n%s%s\n",
workstationsModels[i].Name, workstationsModels[i].X, workstationsModels[i].Y,
workstationsModels[i].Speed, workstationsModels[i].LoadTime, workstationsModels[i].UnloadTime, matSep, border)
}
deliverStuff(o)
}
func deliverStuff(o solver.Option) {
var (
totalPathCost float64
totalUnloadCost float64
totalLoadCost float64
totalIdleCost float64
)
fmt.Fprintf(fileWriter, "\n\n%s", border)
fmt.Fprintf(fileWriter, "\nSIMULATION REPORT\n%s\n", border)
for i := 0; i < len(o.Path); i++ {
var (
pathCost float64
loadCost float64
)
fmt.Fprintf(fileWriter, "\n%s\n", border)
if i == 0 {
to := o.Path[i][len(o.Path[i])-1].(*solver.Tile).Get().(*models.Workstation)
usedStations = append(usedStations, to)
pathCost = o.InnerCosts[i]
loadCost = to.LoadTime
totalCost := pathCost + loadCost
fmt.Fprintf(fileWriter, "Delivering from warehouse point to %s\n\n", to.Name)
fmt.Fprintf(fileWriter, "Available Stock: %s\n", trainModel.Stock.Details())
trainModel.Unload(to, globalTime+totalCost)
fmt.Fprintf(fileWriter, "\nWarehouse Demand:\n%s", to.PrintRequirements())
fmt.Fprintf(fileWriter, "\nTime to load the material: %.2f\n", loadCost)
fmt.Fprintf(fileWriter, "Time to reach to workstation: %.2f\n", pathCost)
fmt.Fprintf(fileWriter, "Total time to deliver the product: %.2f\n\n", totalCost)
fmt.Fprintf(fileWriter, "MAP PREVIEW\n")
harita.PrintMap(fileWriter, o.Path[i])
} else if i == len(o.Path)-1 {
from := o.Path[i][0].(*solver.Tile).Get().(*models.Workstation)
to := o.Path[i][len(o.Path[i])-1].(*solver.Tile).Get().(*models.Workstation)
usedStations = append(usedStations, to)
pathCost = o.InnerCosts[i]
loadCost = to.LoadTime
totalCost := pathCost + loadCost
fmt.Fprintf(fileWriter, "From %s to %s\n\n", from.Name, to.Name)
fmt.Fprintf(fileWriter, "Train Stock: %s\n", trainModel.Stock.Details())
trainModel.Unload(to, globalTime+totalCost)
fmt.Fprintf(fileWriter, "\nWarehouse Demand:\n%s", to.PrintRequirements())
fmt.Fprintf(fileWriter, "\nLoad Time: %.2f\n", loadCost)
fmt.Fprintf(fileWriter, "Time to reach: %.2f\n", pathCost)
fmt.Fprintf(fileWriter, "Total time to deliver the product: %.2f\n\n", totalCost)
fmt.Fprintf(fileWriter, "MAP PREVIEW\n")
harita.PrintMap(fileWriter, o.Path[i])
fmt.Fprintf(fileWriter, "\n\nALL MATERIALS ARE DELIVERED!\n")
fmt.Fprintf(fileWriter, "\nTotal delivery cost: %.2f\n", totalPathCost)
fmt.Fprintf(fileWriter, "Total loading cost: %.2f\n", totalLoadCost)
fmt.Fprintf(fileWriter, "TOTAL COST: %.2f\n", globalTime)
to, unloadPathCost, unloadCost, idleTime := collectAll(to)
path, cost, _ := pather.Path(harita.GetTile(to.X, to.Y), train)
pathCost += cost
fmt.Fprintf(fileWriter, "\nFrom %s back to storage\n\n", to.Name)
fmt.Fprintf(fileWriter, "Time to reach: %.2f\n", pathCost)
fmt.Fprintf(fileWriter, "MAP PREVIEW\n")
harita.PrintMap(fileWriter, path)
pathCost += unloadPathCost
totalUnloadCost += unloadCost
totalIdleCost = idleTime
fmt.Fprintf(fileWriter, "\n\n%s\n", border)
fmt.Fprintf(fileWriter, "\nALL MATERIALS ARE COLLECTED AND RETURNED TO WAREHOUSE!\n\n")
fmt.Fprintf(fileWriter, "Total return path cost: %v\n", unloadPathCost+cost)
fmt.Fprintf(fileWriter, "Total unload cost: %v\n", unloadCost)
fmt.Fprintf(fileWriter, "Total idle cost: %v\n", idleTime)
fmt.Fprintf(fileWriter, "TOTAL COLLECTION COST: %v\n", idleTime+unloadCost+unloadPathCost)
fmt.Fprintf(fileWriter, "\n%s\n", border)
} else {
from := o.Path[i][0].(*solver.Tile).Get().(*models.Workstation)
to := o.Path[i][len(o.Path[i])-1].(*solver.Tile).Get().(*models.Workstation)
usedStations = append(usedStations, to)
pathCost = o.InnerCosts[i]
loadCost = to.LoadTime
totalCost := pathCost + loadCost
fmt.Fprintf(fileWriter, "From %s to %s\n\n", from.Name, to.Name)
fmt.Fprintf(fileWriter, "Train Stock: %s\n", trainModel.Stock.Details())
trainModel.Unload(to, globalTime+totalCost)
fmt.Fprintf(fileWriter, "\nWarehouse Demand:\n%s", to.PrintRequirements())
fmt.Fprintf(fileWriter, "\nLoad Time: %.2f\n", loadCost)
fmt.Fprintf(fileWriter, "Time to reach: %.2f\n", pathCost)
fmt.Fprintf(fileWriter, "Total time to deliver the product: %.2f\n\n", totalCost)
fmt.Fprintf(fileWriter, "MAP PREVIEW\n")
harita.PrintMap(fileWriter, o.Path[i])
}
totalPathCost += pathCost
totalLoadCost += loadCost
globalTime += pathCost + loadCost + totalUnloadCost + totalIdleCost
}
fmt.Fprintf(fileWriter, "%s\n", border)
fmt.Fprintf(fileWriter, "Total simulation cost: %v\n", globalTime)
fmt.Fprintf(fileWriter, "Total path cost: %v\n", totalPathCost)
fmt.Fprintf(fileWriter, "Total loading cost: %v\n", totalLoadCost)
fmt.Fprintf(fileWriter, "Total unloading cost: %v\n", totalUnloadCost)
fmt.Fprintf(fileWriter, "Total idle time cost: %v\n", totalIdleCost)
fmt.Fprintf(fileWriter, "%s\n", border)
}
func filterUnusedStations() {
for i, station := range workstationsModels {
if harita.GetTile(station.X, station.Y).Kind != solver.Workstation {
workstationsModels = append(workstationsModels[:i], workstationsModels[i+1:]...)
}
}
}
func collectAll(startPoint *models.Workstation) (*models.Workstation, float64, float64, float64) {
var totalPathCost, totalUnLoadCost, totalIdleTime float64
var pathCost, unLoadCost, idleTime float64
fmt.Fprintf(fileWriter, "\n%s\n", border)
fmt.Fprintf(fileWriter, "COLLECTION FROM WORKSTATIONS\n")
fmt.Fprintf(fileWriter, "%s\n", border)
station := startPoint
for i := 0; i < len(usedStations); i++ {
nextStation := getWorkstationAvailable(station, usedStations)
pathCost, unLoadCost, idleTime = collectOne(station, nextStation)
// globalTime += pathCost + unLoadCost + idleTime
totalPathCost += pathCost
totalUnLoadCost += unLoadCost
totalIdleTime += idleTime
if !models.IsIn(nextStation, collectedWorkstations, false) {
collectedWorkstations = append(collectedWorkstations, nextStation)
}
station = nextStation
}
return station, totalPathCost, totalUnLoadCost, totalIdleTime
}
func collectOne(from, to *models.Workstation) (float64, float64, float64) {
var idleTime float64
unloadCost := to.UnloadTime
path, pathCost, _ := pather.Path(harita.GetTile(from.X, from.Y), harita.GetTile(to.X, to.Y))
if to.GetReadyTime() < globalTime+pathCost {
idleTime = 0.0
} else {
idleTime = to.GetReadyTime() - globalTime - pathCost
}
fmt.Fprintf(fileWriter, "\nCollecting from %s to %s\n", from.Name, to.Name)
fmt.Fprintf(fileWriter, "\nWorkstation %s will be ready at: %.2f\n", to.Name, to.GetReadyTime())
fmt.Fprintf(fileWriter, "\nTrain idle time %.2f\n", idleTime)
fmt.Fprintf(fileWriter, "Time to reach: %.2f\n", pathCost)
fmt.Fprintf(fileWriter, "Workstation Unload Time: %.2f\n", unloadCost)
fmt.Fprintf(fileWriter, "Total time spent: %.2f\n", unloadCost+pathCost+idleTime)
fmt.Fprintf(fileWriter, "\nMAP PREVIEW\n")
harita.PrintMap(fileWriter, path)
fmt.Fprintf(fileWriter, "%s\n", border)
return pathCost, unloadCost, idleTime
}
func getWorkstationAvailable(startPoint *models.Workstation, workstations models.Workstations) *models.Workstation {
var cheap cheaper
var topPriorityWS []cheaper
var lowPriorityWS []cheaper
for _, workstation := range workstations {
if !models.IsIn(workstation, collectedWorkstations, false) {
_, pathCost, _ := pather.Path(harita.GetTile(startPoint.X, startPoint.Y), harita.GetTile(workstation.X, workstation.Y))
cost := pathCost + workstation.GetReadyTime() + globalTime
topPriorityWS = append(topPriorityWS, cheaper{workstation, cost})
}
}
for _, workstation := range workstations {
if !models.IsIn(workstation, collectedWorkstations, false) {
_, pathCost, _ := pather.Path(harita.GetTile(startPoint.X, startPoint.Y), harita.GetTile(workstation.X, workstation.Y))
cost := pathCost + workstation.GetReadyTime() + globalTime
if workstation.GetReadyTime() < globalTime {
topPriorityWS = append(topPriorityWS, cheaper{workstation, cost})
} else {
lowPriorityWS = append(lowPriorityWS, cheaper{workstation, cost})
}
}
}
if len(topPriorityWS) != 0 {
for _, workstation := range topPriorityWS {
if cheap.ws == nil {
cheap = workstation
} else {
if workstation.cost < cheap.cost {
cheap = workstation
}
}
}
} else {
for _, workstation := range lowPriorityWS {
if cheap.ws == nil {
cheap = workstation
} else {
if workstation.cost < cheap.cost {
cheap = workstation
}
}
}
}
return cheap.ws
}