-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_scenario.py
171 lines (121 loc) · 5.45 KB
/
prepare_scenario.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
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
import sys, os, json
### Parse arguments
arguments = sys.argv[1:]
scenario_path = None
output_path = None
operator_paths = []
shipment_types = {}
consolidation_types = {}
salaries = {}
vehicle_type_properties = []
k = 0
while k < len(arguments):
if arguments[k] == "--scenario-path":
k += 1
if k == len(arguments):
raise RuntimeError("Need to provide path for scenario")
scenario_path = arguments[k]
print("Scenario:", scenario_path)
elif arguments[k] == "--output-path":
k += 1
if k == len(arguments):
raise RuntimeError("Need to provide output path")
output_path = arguments[k]
print("Output:", output_path)
elif arguments[k] == "--operator-path":
k += 1
if k == len(arguments):
raise RuntimeError("Need to provide operator path")
operator_paths.append(arguments[k])
print("Operator:", operator_paths[-1])
elif arguments[k].startswith("--shipment-type:"):
operator = arguments[k].split(":")[1]
k += 1
if k == len(arguments):
raise RuntimeError("Need to provide shipment type for {}".format(operator))
shipment_type = arguments[k]
if not shipment_type in ("delivery", "pickup", "none"):
raise RuntimeError("Invalid shipment type for {}".format(operator))
shipment_types[operator] = shipment_type
print("Shipment type", shipment_type, "for", operator)
elif arguments[k].startswith("--consolidation-type:"):
operator = arguments[k].split(":")[1]
k += 1
if k == len(arguments):
raise RuntimeError("Need to provide consolidation type for {}".format(operator))
consolidation_type = arguments[k]
if not consolidation_type in ("delivery", "pickup", "none"):
raise RuntimeError("Invalid consolidation type for {}".format(operator))
consolidation_types[operator] = consolidation_type
print("Consolidation type", consolidation_type, "for", operator)
elif arguments[k].startswith("--driver-salary:"):
operator = arguments[k].split(":")[1]
k += 1
if k == len(arguments):
raise RuntimeError("Need to provide driver salary for {}".format(operator))
salary = float(arguments[k])
salaries[operator] = salary
print("Salary ", salary, "EUR for", operator)
elif arguments[k].startswith("--vehicle-type:"):
vehicle_type, property = arguments[k].split(":")[1:]
k += 1
if k == len(arguments):
raise RuntimeError("Need to provide value for property {} of vehicle type{}".format(property, vehicle_type))
if property in ("cost_per_day_EUR", "cost_per_km_EUR", "co2_per_km_g", "energy_per_km_Wh", "speed_km_h"):
vehicle_type_properties.append((vehicle_type, property, float(arguments[k])))
elif property in ("capacity"):
vehicle_type_properties.append((vehicle_type, property, int(arguments[k])))
else:
raise RuntimeError("Unknown vehicle type property:", property)
else:
raise RuntimeError("Don't understand argument: {}".format(arguments[k]))
k += 1
if scenario_path is None:
raise RuntimeError("No scenario path (--scenario-path) specified")
if output_path is None:
raise RuntimeError("No output path (--output-path) specified")
### Load scenario$
with open(scenario_path) as f:
scenario = json.load(f)
### Integrate operators
if not "operators" in scenario: scenario["operators"] = []
operator_ids = set([operator["id"] for operator in scenario["operators"]])
for operator_path in operator_paths:
with open(operator_path) as f:
operator = json.load(f)
if operator["id"] in operator_ids:
raise RuntimeError("Operator {} from {} is already defined".format(
operator["id"], operator_path
))
scenario["operators"].append(operator)
operator_ids.add(operator["id"])
### Update shipment and consolidation types
for operator_id, shipment_type in shipment_types.items():
if not operator_id in operator_ids:
raise RuntimeError("Shipment type given for non-existing operator", operator)
for operator in scenario["operators"]:
if operator["id"] == operator_id:
operator["shipment_type"] = shipment_type
for operator_id, consolidation_type in consolidation_types.items():
if not operator_id in operator_ids:
raise RuntimeError("Consolidation type given for non-existing operator", operator)
for operator in scenario["operators"]:
if operator["id"] == operator_id:
operator["consolidation_type"] = consolidation_type
for operator_id, salary in salaries.items():
if not operator_id in operator_ids:
raise RuntimeError("Salary given for non-existing operator", operator)
for operator in scenario["operators"]:
if operator["id"] == operator_id:
operator["daily_driver_salary_EUR"] = salary
### Update vehicle type properties
vehicle_type_ids = set([vt["id"] for vt in scenario["vehicle_types"]])
for vehicle_type_id, property, value in vehicle_type_properties:
if not vehicle_type_id in vehicle_type_ids:
raise RuntimeError("Vehicle type does not exist:", vehicle_type_id)
for vehicle_type in scenario["vehicle_types"]:
if vehicle_type["id"] == vehicle_type_id:
vehicle_type[property] = value
### Output
with open(output_path, "w+") as f:
json.dump(scenario, f)