/
run.py
executable file
·77 lines (62 loc) · 2.45 KB
/
run.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
#!/usr/bin/env python
import argparse
import logging
import sys
import time
import traceback
from datetime import datetime
import numpy as np
import pandas as pd
import methods
import utils
from method import Method
from neighbor import Neighbor
def main(method, solution_file, args):
try:
log.info("Optimizing trips...")
method.run(args)
if method.verify_trips():
method.evaluate_trips()
method.write_trips(solution_file)
else:
method.write_trips("last-failure.csv")
except Exception as ex:
method.write_trips("last-failure.csv")
log.critical("{} during evaluation: {}".format(type(ex).__name__, str(ex)))
log.critical(traceback.format_exc())
sys.exit(1)
finally:
mins, secs = divmod(time.time() - start_time, 60)
hours, mins = divmod(mins, 60)
time_string = \
"{}h{}m{:.1f}s".format(int(hours), int(mins), secs) if hours > 0 else \
"{}m{:.1f}s".format(int(mins), secs) if mins > 0 else \
"{:.1f}s".format(secs)
log.warning("Finished execution after {}".format(time_string))
logging.shutdown()
def get_all_methods(gifts, log):
return [sub(gifts, log) for sub in Method.__subclasses__()]
if __name__ == "__main__":
start_time = time.time()
log = utils.get_logger("santa-sleigh")
Neighbor.log = log
gifts = pd.read_csv("data/gifts.csv")
methods = {method.name: method for method in get_all_methods(gifts, log)}
parser = argparse.ArgumentParser()
# general arguments
parser.add_argument("method", choices=methods.keys(), help="method to try")
parser.add_argument("--random-seed", required=False, type=int, nargs="?", const=42, help="Random seed")
# method-specific arguments
parser.add_argument("--from-file", required=False, help=
"Pattern to match files under 'data/' against which contains a solution to load as basis for the new evaluation")
parser.add_argument("--alpha", required=False, type=int,
help="Factor for temperature decrease in SA")
parser.add_argument("--temperature", required=False, type=int,
help="Initial temperature to use for SA")
args = parser.parse_args()
evaluation_id = "{}-{}".format(args.method, datetime.utcnow().strftime("%Y-%m-%d-%H:%M:%S"))
args.evaluation_id = evaluation_id
solution_file = "solutions/{}_{}.csv".format(evaluation_id, args.random_seed)
log.debug("method: '{}', args: '{}'".format(args.method, args))
np.random.seed(args.random_seed)
main(methods[args.method], solution_file, args)