-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebalancer.py
76 lines (62 loc) · 2.42 KB
/
rebalancer.py
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
import constants
import csvhandler
distances = csvhandler.readDistances()
def balanceDepots(depots):
for depotCode in depots:
depot = depots[depotCode]
for SKU in depot.closingStock:
status = checkSKULevel(depot, SKU)
if status == "excess":
distribute(depot, SKU, depots)
elif status == "critical":
request(depot, SKU, depots)
else:
pass
def checkSKULevel(depot, SKU):
stockLevel = depot.getStockLevel(SKU)
if stockLevel > constants.IDEAL_STOCK_LEVEL:
result = "excess"
elif stockLevel < constants.CRITICAL_STOCK_LEVEL:
result = "critical"
else:
result = "ok"
return result
def distribute(depot, SKU, depots) :
depotsSortedByTransportCost = sortDepotsByTransportCost(depot, depots)
excedent = depot.calculeExcedent(SKU)
index = 0
while excedent > 0 and index < len(depotsSortedByTransportCost):
currentDepot = depotsSortedByTransportCost[index]
if SKU in currentDepot.closingStock:
need = currentDepot.calculeNeed(SKU)
if need < excedent:
toSend = need
else:
toSend = excedent
sendFromTo(depot, currentDepot, SKU, toSend)
excedent -= toSend
index += 1
def request(depot, SKU, depots):
depotsSortedByTransportCost = sortDepotsByTransportCost(depot, depots)
need = depot.calculeNeed(SKU)
index = 0
while need > 0 and index < len(depotsSortedByTransportCost):
currentDepot = depotsSortedByTransportCost[index]
if SKU in currentDepot.closingStock:
currDepotCanSend = currentDepot.saffetyRequest(SKU)
if currDepotCanSend > need:
currDepotCanSend = need
sendFromTo(currentDepot, depot, SKU, currDepotCanSend)
need -= currDepotCanSend
index += 1
def sortDepotsByTransportCost(originDepot, depots):
resultList = []
for key in depots:
resultList.append(depots[key])
resultList.sort(key=lambda destinyDepot: calculeTransportCost(originDepot, destinyDepot))
return resultList
def calculeTransportCost(originDepot, destinyDepot):
return distances[originDepot.code + ":" + destinyDepot.code]
def sendFromTo(originDepot, destinyDepot, SKU, toSend):
originDepot.removeSKUClosingStock(SKU, toSend)
destinyDepot.addSKUClosingStock(SKU, toSend)