forked from Berkeley-CS170/project-sp22-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
110 lines (85 loc) · 3.41 KB
/
generate.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
"""Generates instance inputs of small, medium, and large sizes.
Modify this file to generate your own problem instances.
For usage, run `python3 generate.py --help`.
"""
import argparse
from pathlib import Path
from typing import Callable, Dict
from instance import Instance
from size import Size
from point import Point
from file_wrappers import StdoutFileWrapper
def make_small_instance() -> Instance:
"""Creates a small problem instance.
Size.SMALL.instance() handles setting instance constants. Your task is to
specify which cities are in the instance by constructing Point() objects,
and add them to the cities array. The skeleton will check that the instance
is valid.
"""
cities = []
# YOUR CODE HERE
return Size.SMALL.instance(cities)
def make_medium_instance() -> Instance:
"""Creates a medium problem instance.
Size.MEDIUM.instance() handles setting instance constants. Your task is to
specify which cities are in the instance by constructing Point() objects,
and add them to the cities array. The skeleton will check that the instance
is valid.
"""
cities = []
# YOUR CODE HERE
return Size.MEDIUM.instance(cities)
def make_large_instance() -> Instance:
"""Creates a large problem instance.
Size.LARGE.instance() handles setting instance constants. Your task is to
specify which cities are in the instance by constructing Point() objects,
and add them to the cities array. The skeleton will check that the instance
is valid.
"""
cities = []
# YOUR CODE HERE
return Size.LARGE.instance(cities)
# You shouldn't need to modify anything below this line.
SMALL = 'small'
MEDIUM = 'medium'
LARGE = 'large'
SIZE_STR_TO_GENERATE: Dict[str, Callable[[], Instance]] = {
SMALL: make_small_instance,
MEDIUM: make_medium_instance,
LARGE: make_large_instance,
}
SIZE_STR_TO_SIZE: Dict[str, Size] = {
SMALL: Size.SMALL,
MEDIUM: Size.MEDIUM,
LARGE: Size.LARGE,
}
def outfile(args, size: str):
if args.output_dir == "-":
return StdoutFileWrapper()
return (Path(args.output_dir) / f"{size}.in").open("w")
def main(args):
for size, generate in SIZE_STR_TO_GENERATE.items():
if size not in args.size:
continue
with outfile(args, size) as f:
instance = generate()
assert instance.valid(), f"{size.upper()} instance was not valid."
assert SIZE_STR_TO_SIZE[size].instance_has_size(instance), \
f"{size.upper()} instance did not meet size requirements."
print(f"# {size.upper()} instance.", file=f)
instance.serialize(f)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate problem instances.")
parser.add_argument("output_dir", type=str, help="The output directory to "
"write generated files to. Use - for stdout.")
parser.add_argument("--size", action='append', type=str,
help="The input sizes to generate. Defaults to "
"[small, medium, large].",
default=None,
choices=[SMALL, MEDIUM, LARGE])
# action='append' with a default value appends new flags to the default,
# instead of creating a new list. https://bugs.python.org/issue16399
args = parser.parse_args()
if args.size is None:
args.size = [SMALL, MEDIUM, LARGE]
main(args)