def solve_image(path, dist = False): pathparts = splitext(path) pathin = '%s.sat_in.txt' % pathparts[0] pathout = '%s.sat_out.txt' % pathparts[0] pathsol = '%s.sol.png' % pathparts[0] pathcrop = '%s.crop.png' % pathparts[0] pathlvl = '%s.lvl.txt' % pathparts[0] print('Parsing image ...') level, color_map, xmarks, ymarks = parse_image(path, pathcrop) level.write_to_file(pathlvl) print('Generating clauses ...') clauses = sat_get_clauses(level, dist) print('Got %d clauses' % len(clauses)) print('Writing to file ...') map = sat_write_clauses(clauses, pathin) # Release some memory clauses = [] collect() print('Running SAT solver ...') call([SAT_PATH, pathin, pathout]) print('Reading valuation ...') val = sat_read_valuation(level, map, pathout) if not val: print('Unsatisfiable ...') return #pprint(val) print('Displaying solution ...') img = get_solution(level, val, dist, color_map) img.show() img.save(pathsol) return val, xmarks, ymarks
def solve_file(path, dist=False): print("Reading level from", path) level = Level() level.load_from_file(path) print("Generating clauses ...") clauses = sat_get_clauses(level, dist) print("Got %d clauses" % len(clauses)) pathparts = splitext(path) pathin = "%s.sat_in%s" % pathparts pathout = "%s.sat_out%s" % pathparts pathsol = "%s.sol.png" % pathparts[0] print("Writing to file ...") map = sat_write_clauses(clauses, pathin) # Release some memory clauses = [] collect() print("Running SAT solver ...") call([SAT_PATH, pathin, pathout]) print("Reading valuation ...") val = sat_read_valuation(level, map, pathout) if not val: print("Unsatisfiable ...") return # pprint(val) print("Displaying solution ...") img = get_solution(level, val, dist) img.show() img.save(pathsol)