os.remove(schedule_file)
recruit_prefs = file_io.get_recruit_preferences()
professors = file_io.get_professor_availability()

# Merge data structures here for cell coloring
for recruit in recruits:
    for preference in recruit_prefs:
        if preference["name"] == recruit["name"]:
            recruit["preferences"] = preference["preferences"]

# Initialize necessary data stores to allow index access
for professor in professors:
    professor["slots"] = [""] * len(recruits[0]["slots"])
for recruit in recruits:
    recruit["clusters"] = [None] * len(recruit["slots"])

# Map recruit schedule to a professor schedule
for recruit in recruits:
    for i, slot in enumerate(recruit["slots"]):
        for professor in professors:
            if professor["name"] == slot:
                professor["slots"][i] += recruit["name"] + ", "
                recruit["clusters"][i] = professor["cluster"]
                break

# Clean up trailing commas
for professor in professors:
    professor["slots"] = [s[:-2] if s else "" for s in professor["slots"]]

file_io.write_schedule_xlsx(professors, recruits, schedule_file)
for recruit in recruits:
    recruit["preferences"] = set(recruit["preferences"]) - set(unavailable)

# Sort professors by flyness and print (a tribute to Peter's flyest_prof)
for professor in professors:
    professor["recruits"] = [recruit["name"] for recruit in recruits if
                             professor["name"] in recruit["preferences"]]
professors.sort(key=lambda p: len(p["recruits"]), reverse=True)

print "\nMatched professors (sorted by number of requests):"
for professor in professors:
    recruit_string = ", ".join(professor["recruits"])
    print "    " + professor["name"].ljust(15) + recruit_string

# Generate a schuedle. Change here to implement different algorithms.
algorithm = algorithms.RandomizedAlgorithm(professors, recruits,
                                           travel_weights, overrides)
professors, recruits = algorithm.run(iterations=iterations,
                                     free_recruit_slots=1)
print algorithm.generate_test_results()

# Sort by name for output
professors.sort(key=lambda p: p["name"])
recruits.sort(key=lambda r: r["name"].split(' ')[1])

# Write an excel doc containing the schedule
print("\nGenerating output spreadsheet...")
file_io.write_schedule_xlsx(professors, recruits, "../generated_schedule.xlsx")

print("\nDone!")