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
#!/usr/bin/env python3
 
# SI 335 Algorithms, Spring 2016
# Project 3 Helper code
#
# This program can be used to generate a random input map
# with obstacles, assets, and targets.
 
import argparse
import sys
import random
 
def main():
    # parse command-line arguments
    parser = argparse.ArgumentParser(description="Generate a random map")
    parser.add_argument("rows", metavar="R", type=int, 
            help="number of rows in the map")
    parser.add_argument("cols", metavar="C", type=int, 
            help="number of columns in the map")
    parser.add_argument("nobst", metavar="B", type=int, 
            help="how many obstacles to place in the map")
    parser.add_argument("nasset", metavar="A", type=int, 
            help="how many assets to place in the map")
    parser.add_argument("ntarg", metavar="N", type=int, 
            help="how many targets to place in the map")
    parser.add_argument("max_targ", type=int, default=1000, nargs='?',
            help="the maximum target value (default 1000)")
    parser.add_argument("-o", "--output-file", nargs='?', dest='out',
            type=argparse.FileType('w'), default=sys.stdout,
            help="output file destination (defaults to standard out)")
    args = parser.parse_args()
 
    # make sure everything is nonzero
    assert all(x>0 for x in (args.rows, args.cols, args.nobst, args.nasset, args.ntarg, args.max_targ))
    # make sure everything fits
    assert args.nobst + args.nasset + args.ntarg <= args.rows * args.cols
 
    # get a list of (row,column) pairs in random order
    spaces = [(r,c) for r in range(args.rows) for c in range(args.cols)]
    random.shuffle(spaces)
    space_iter = iter(spaces)
 
    # choose obstacle and asset locations
    obstacles = sorted(next(space_iter) for _ in range(args.nobst))
    assets = sorted(next(space_iter) for _ in range(args.nasset))
 
    # choose target locations and values
    targets = []
    for _ in range(args.ntarg):
        r, c = next(space_iter)
        val = random.randrange(1, args.max_targ + 1)
        targets.append((r,c,val))
    targets.sort()
 
    # print everything out
    print(args.rows, args.cols, file=args.out)
    print(file=args.out)
    
    print(args.nobst, file=args.out)
    for r, c in obstacles:
        print(r, c, file=args.out)
    print(file=args.out)
 
    print(args.nasset, file=args.out)
    for r, c in assets:
        print(r, c, file=args.out)
    print(file=args.out)
 
    print(args.ntarg, file=args.out)
    for r, c, v in targets:
        print(r, c, v, file=args.out)
 
if __name__ == '__main__':
    main()