class TestParser(unittest.TestCase): def setUp(self): self.max_n = 50 self.max_m = 50 self.gen = Generator(self.max_n, self.max_m) self.n_cases = 10 self.gen.generate_test_cases(self.n_cases) self.gen.print_test_cases_to_file(self.n_cases, 'testfile') self.parser = Parser('testfile') def test_read_next_case(self): for i in range(self.n_cases): c = self.parser.read_next_case() self.assertTrue(len(c) < (self.max_m + 1)) no_case = self.parser.read_next_case() self.assertIsNone(no_case) no_case = self.parser.read_next_case() self.assertIsNone(no_case) def tearDown(self): os.remove('testfile')
def optimize_solution(input, solution, method): parser = Parser(input) sols = open(solution, 'r') tester = Tester() opt = Optimizer([], []) for i in range(parser.c): case = parser.read_next_case() s = sols.readline().split(':')[1] s = s[:-1] if "IMPOSSIBLE" in s: print "Skipping case #" + str(i + 1) + " " + s continue parsed_s = s.split() opt.solution = parsed_s opt.case = case opt.current_mattes = sum([int(x) for x in opt.solution]) opt.optimize(method) print "For case:" print case print "Optimizing solution: " + s + " ---> " + opt.get_solution() sols.close() parser.finish()
def read_and_solve_case(input, out=None, best=False, first=False): parser = Parser(input) solver = Solver(0, [], first) if out: output_file = open(out, 'w') for i in range(parser.c): solver.customers = parser.read_next_case() solver.length = parser.current_n solver.compute_solutions() if best: sol = solver.get_optimal_solution() else: sol = solver.get_random_solution() if sol != "IMPOSSIBLE": solution_string = "Case #" + str(i + 1) + ": " + " ".join(sol) else: solution_string = "Case #" + str(i + 1) + ": " + sol print solution_string if out: output_file.write(solution_string + '\n') if out: output_file.close()