-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathsolve.py
More file actions
57 lines (53 loc) · 2.07 KB
/
solve.py
File metadata and controls
57 lines (53 loc) · 2.07 KB
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
import argparse
from _data_generation import DataGenerator
from milp import ConstructModel
import matplotlib.pyplot as plt
from _utils import read_parameters_from_json
import sys
def parse_arguments(args_dict, arguments, message):
parser = argparse.ArgumentParser(
description="Solve the capacitated vehicle routing problem with"
"time windows (CVRVTW) by specifying arguments or"
"using defaults from the JSON file."
)
for key, description in message.items():
default_value = args_dict[key]
param_type = type(default_value)
if isinstance(description, dict) and "choices" in description:
parser.add_argument(
f"--{key}",
type=param_type,
default=default_value,
choices=description["choices"],
help=f"{description['description']} (default: {default_value})"
)
else:
parser.add_argument(
f"--{key}",
type=param_type,
default=default_value,
help=f"{description if isinstance(description, str) else description.get('description', '')} (default: {default_value})"
)
parsed_args = vars(parser.parse_args(arguments))
for key, value in parsed_args.items():
if value is not None:
args_dict[key] = value
return args_dict
if __name__ == '__main__':
# Read parameters and descriptions from the JSON file
args, descriptions = read_parameters_from_json('arguments.json')
# Dynamically parse command-line arguments
args = parse_arguments(args, sys.argv[1:], descriptions)
# Initialize the data generator with the updated args
data = DataGenerator(args)
print(
'Constructing model for the network with,'
'\nnum of vehicles = {} \nnum of customers = {}'.format(
data.args['V'], data.args['I']
)
)
# Solve the optimization problem
model = ConstructModel(data)
model.solve()
# Plot the solution routes
data._plot_routes(model._routes)