-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
executable file
·90 lines (79 loc) · 3.16 KB
/
main.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
#/usr/bin/env python
"""
Produces an optimal seating chart using the simulated annealing algorithm.
Run by cd'ing to outer directory, then running:
python -m seating.main seating/people.csv seating/tables.csv
"""
from copy import deepcopy
import argparse
from seating import config
from seating.seating_io import InputData, write_tables_to_csv, write_people_to_csv
from seating.build import build_guess
from seating.anneal import anneal
from seating.display_messages import print_init_cost, \
print_progress, print_final_metrics
def create_parser():
parser = argparse.ArgumentParser(description="Run the Seating Chart Creator command-line app")
parser.add_argument("people_file", action="store")
parser.add_argument("tables_file", action="store")
parser.add_argument("-p", "--output_people_filename", default='output-people.csv', action="store")
parser.add_argument("-t", "--output_tables_filename", default='output-tables.csv', action="store")
return parser
def cost_of_random_solutions(people_in, tables_in, days):
"""
Computes the average cost of 100 random solutions
"""
config.build_smart = False
costs = 0
for _ in range(100):
people = deepcopy(people_in)
tables = deepcopy(tables_in)
solution = build_guess(people, tables, days)
costs += solution.cost
avg_cost = costs/100.0
config.build_smart = True
return avg_cost
class DummySolution(object):
"""
For delivering baseline cost information to Tk GUI.
Needs to be structured this way to match 2nd 'yield'
statement in main() - Tk expects the information in this
format.
"""
def __init__(self, baseline_cost):
self.cost = "Baseline cost"
self.baseline_cost = baseline_cost
self.dummy_T = 1.0
def main(input_data):
people = deepcopy(input_data.people)
tables = deepcopy(input_data.tables)
# for initializing axes of graph
baseline_cost = cost_of_random_solutions(people, tables, input_data.days)
dummy_solution = DummySolution(baseline_cost)
yield (dummy_solution, dummy_solution.dummy_T)
init_solution = build_guess(people, tables, input_data.days)
best_solution = deepcopy(init_solution)
print_init_cost(init_solution.cost)
if config.anneal:
for (solution, T) in anneal(init_solution):
print_progress(solution, T)
yield solution, T
else:
yield best_solution, None
if __name__ == '__main__':
parser = create_parser()
args = parser.parse_args()
input_data = InputData(args.people_file,
args.tables_file)
if not config.test_cost:
for (best_solution, T) in main(input_data):
if best_solution.cost == 'Baseline cost':
print 'Average cost of random solutions: ' + str(best_solution.baseline_cost)
else:
print_progress(best_solution, T)
print_final_metrics(best_solution)
write_people_to_csv(best_solution, args.output_people_filename)
write_tables_to_csv(best_solution, args.output_tables_filename)
if config.test_cost:
for i in range(0,500):
print main(input_data).next()[0].cost