-
Notifications
You must be signed in to change notification settings - Fork 0
/
FactoryGenerator.py
79 lines (69 loc) · 3.18 KB
/
FactoryGenerator.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
77
78
79
from Product import Product
from Workstation import Workstation
from Factory import Factory
import random
import numpy
class FactoryGenerator:
def __init__(self, workstationsJson):
self.workstationJson = workstationsJson
def generateRandomWorkstations(self, maxPosition):
result = []
for item in self.workstationJson['workStations']:
type = item['type']
for i in range(0, item['count']):
result.append((type, random.randint(0, maxPosition), random.randint(0, maxPosition)))
return result
def generateRandomProducts(self, productsCount, pathLenght):
productList = []
for i in range(productsCount):
path = ""
for j in range(pathLenght):
workstationTypeIndex = numpy.random.randint(len(self.workstationJson['workStations']))
path += self.workstationJson['workStations'][workstationTypeIndex]['type']
productList.append((0, 0, path))
return productList
def generateFactory(self, workstationPositions, visibilityType, products):
ws = self.set_position_for_workstations(workstationPositions)
p = self.generateProducts(ws, products)
cfs = self.initFieldStatus()
factory = Factory(ws, p, cfs, visibilityType)
return factory
def generateProducts(self, workStations, productList):
""" Generates products from the specified JSON file """
products = []
for product in productList:
products.append(Product(product[0], product[1], product[2], workStations))
return products
def initFieldStatus(self):
return set()
def count_workstations(self):
""" Counts the workstations saved in FactorySimulator """
count = 0
for item in self.workStations.values():
count += len(item)
return count
def set_position_for_workstations(self, workstation_positions):
""" Update workstation positions with Tupel (Type, x, y) - typically from evolutionary algorithm """
workStations = {}
# Iterate over Workstation positions given
workStationsWaitTimes = dict()
for ws in self.workstationJson['workStations']:
workStationsWaitTimes[ws['type']] = ws['time_at_ws']
for item in workstation_positions:
if not item[0] in workStations:
workStations.update({item[0]: []})
# Create new Worstation object
ws = Workstation(item[0], item[1], item[2])
# Add time at ws constraint to ws
ws.setTimeAtWs(workStationsWaitTimes[item[0]])
workStations[item[0]].append(ws)
self.checkWorkstationConstrait(workStations)
return workStations
def checkWorkstationConstrait(self, workStations):
# Check number of ws types
if not len(self.workstationJson['workStations']) == len(workStations):
raise Exception("Workstation constrait violated")
for item in self.workstationJson['workStations']:
# Check count of ws for every type
if not len(workStations[item['type']]) == item['count']:
raise Exception("Workstation constrait violated")