-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
85 lines (69 loc) · 2.18 KB
/
main.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
import copy
import json
import nextmv
from nextpipe import FlowSpec, app, log, needs, repeat, step
# >>> Workflow definition
class Flow(FlowSpec):
@step
def prepare(input: dict):
"""Prepares the data."""
return input
@needs(predecessors=[prepare])
@step
def convert(input: dict):
"""Converts the data."""
clone = copy.deepcopy(input)
if "defaults" in clone and "stops" in clone["defaults"] and "quantity" in clone["defaults"]["stops"]:
clone["defaults"]["stops"]["quantity"] *= -1
for stop in clone["stops"]:
if "quantity" in stop:
stop["quantity"] *= -1
return clone
@repeat(repetitions=2)
@app(app_id="routing-nextroute", instance_id="latest")
@needs(predecessors=[prepare])
@step
def run_nextroute():
"""Runs the model."""
pass
@app(app_id="routing-ortools", instance_id="latest")
@needs(predecessors=[convert])
@step
def run_ortools():
"""Runs the model."""
pass
@app(app_id="routing-pyvroom", instance_id="latest")
@needs(predecessors=[convert])
@step
def run_pyvroom():
"""Runs the model."""
pass
@needs(predecessors=[run_nextroute, run_ortools, run_pyvroom])
@step
def pick_best(
results_nextroute: list[dict],
result_ortools: dict,
result_pyvroom: dict,
):
"""Aggregates the results."""
results = results_nextroute + [result_ortools, result_pyvroom]
best_solution_idx = min(
range(len(results)),
key=lambda i: results[i]["statistics"]["result"]["value"],
)
values = [result["statistics"]["result"]["value"] for result in results]
values.sort()
log(f"Values: {values}")
# For test stability reasons, we always return the or-tools result
_ = results.pop(best_solution_idx)
return result_ortools
def main():
# Load input data
input = nextmv.load_local()
# Run workflow
flow = Flow("DecisionFlow", input.data)
flow.run()
result = flow.get_result(flow.pick_best)
print(json.dumps(result))
if __name__ == "__main__":
main()