-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario_generator
executable file
·175 lines (135 loc) · 5.59 KB
/
scenario_generator
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
#!/usr/bin/env python
import argparse
import sys
import json
import yaml
from factory.ordering import Order, Customer
from factory.production import Bakery
from factory.delivery import StreetNetwork
def get_params(config_file):
file_stream = open(config_file, 'r')
params = yaml.load(file_stream)
return params
def export_json(dest, scenario=None, bakeries=None, customers=None,
street_network=None, orders=None):
meta = {}
meta['orders'] = len(orders)
meta['bakeries'] = len(bakeries)
meta['customers'] = scenario['meta']['customers']
meta['duration_days'] = scenario['meta']['days']
meta['products'] = scenario['meta']['products']
# meta['trucks'] = truck_id
if customers is not None:
customer_data = [customer.__dict__ for customer in customers]
else:
customer_data = None
if orders is not None:
order_data = [order.__dict__ for order in orders]
else:
order_data = None
if bakeries is not None:
bakery_data = [bakery.export() for bakery in bakeries]
else:
bakery_data = None
if street_network is not None:
street_network_data = street_network.export()
else:
street_network_data = None
data = {"bakeries": bakery_data,
"customers": customer_data,
"orders": order_data,
"street_network": street_network_data,
'meta': meta}
print("\nSummary\n----------------------------")
print "Orders: {}".format(meta['orders'])
print "Bakeries: {}".format(meta['bakeries'])
print "Customers: {}".format(meta['customers'])
print "Duration in days: {}".format(meta['duration_days'])
print "Number of proucts: {}".format(meta['products'])
# print "Total trucks: {}".format(meta['trucks'])
# Encode JSON data
with open(dest, 'w') as f:
json.dump(data, f, indent=2)
def main(filename="random", verbose=False):
dest = "scenarios/%s-scenario.json" % filename
scenario_params = get_params('config/scenarios.yaml')[filename]
products = get_params('config/products.yaml')
client_params = get_params('config/customers.yaml')
bakeries_qty = scenario_params['meta']['bakeries']
clients_qty = sum(scenario_params['meta']['customers'].values())
grid = scenario_params['street_network']['grid_size']
days = scenario_params['meta']['days']
truck_id = 1
oven_id = 1
machine_id = 1
table_id = 1
order_num = 1
bakery_list = []
customer_list = []
orders_list = []
street_network_list = []
# Create bakery names
bakeries_names = get_params('config/bakeries.yaml')
# Generate bakeries
print("\nCreating bakeries....")
bakery_params = scenario_params['bakeries']
product_params = scenario_params['products']
machine_params = bakery_params['kneading_machines']
table_params = bakery_params['dough_prep_tables']
streets = StreetNetwork(clients_qty, bakeries_qty, grid)
for n in range(bakeries_qty):
bakery = Bakery(n+1, bakeries_names[n],
products=products, product_params=product_params)
oven_id = bakery.add_ovens(start_id=oven_id,
params=bakery_params['ovens'])
machine_id = bakery.add_kneading_machines(machine_id, machine_params)
table_id = bakery.add_prep_tables(table_id, table_params)
truck_id = bakery.add_trucks(truck_id, scenario_params['trucks'])
if verbose:
print 'Name: %s' % bakery.name
print '- Ovens: %i' % len(bakery.ovens)
print '- Kneading Machines: %i' % len(bakery.kneading_machines)
print '- Dough Prep Tables: %i' % len(bakery.dough_prep_tables)
print '- Trucks: %i' % len(bakery.trucks)
bakery_list.append(bakery)
loc = streets.add_node(bakery, 'bakery')
bakery.update_location(loc)
print("\nCreating customers and orders...")
client_id = 1
# This is iterating through all the clients in the customer.yaml file
# The meta information in scenarios.yaml must match
# TODO Iterates only on what is specified in scenario.yaml
for category, customers in client_params.items():
if verbose:
print 'Category %i' % category
for client in customers:
customer = Customer(client_id, client, category=category)
customer_list.append(customer)
client_id = client_id + 1
client_orders, order_num = customer.order(days, order_num)
orders_list.extend(client_orders)
customer.location = streets.add_node(customer, 'customer')
if verbose:
print '- Name: %s' % customer.name
print ' * Orders: %i' % len(client_orders)
print("\nCreating street network...")
streets.add_streets()
streets.draw(filename, verbose)
export_json(dest, scenario=scenario_params, customers=customer_list,
orders=orders_list,
bakeries=bakery_list, street_network=streets)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Creates a random scenario')
parser.add_argument('--name',
nargs=1,
help='The scenario name.')
parser.add_argument('--verbose',
action='store_true',
default=False,
help='Show the details of the scenario while they are\
created')
args = parser.parse_args(sys.argv[1:])
try:
main(verbose=args.verbose)
except ValueError as vae:
parser.error(str(vae))