This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
solve.py
64 lines (46 loc) · 1.69 KB
/
solve.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
"""Solves an instance.
Modify this file to implement your own solvers.
For usage, run `python3 solve.py --help`.
"""
import argparse
from pathlib import Path
from typing import Callable, Dict
from instance import Instance
from solution import Solution
from file_wrappers import StdinFileWrapper, StdoutFileWrapper
def solve_naive(instance: Instance) -> Solution:
return Solution(
instance=instance,
towers=instance.cities,
)
SOLVERS: Dict[str, Callable[[Instance], Solution]] = {
"naive": solve_naive
}
# You shouldn't need to modify anything below this line.
def infile(args):
if args.input == "-":
return StdinFileWrapper()
return Path(args.input).open("r")
def outfile(args):
if args.output == "-":
return StdoutFileWrapper()
return Path(args.output).open("w")
def main(args):
with infile(args) as f:
instance = Instance.parse(f.readlines())
solver = SOLVERS[args.solver]
solution = solver(instance)
assert solution.valid()
with outfile(args) as g:
print("# Penalty: ", solution.penalty(), file=g)
solution.serialize(g)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Solve a problem instance.")
parser.add_argument("input", type=str, help="The input instance file to "
"read an instance from. Use - for stdin.")
parser.add_argument("--solver", required=True, type=str,
help="The solver type.", choices=SOLVERS.keys())
parser.add_argument("output", type=str,
help="The output file. Use - for stdout.",
default="-")
main(parser.parse_args())