def visualize(encoding_name, width, height, answer): """Verify and print the coloring.""" # decode the certificate grid = gridc.Grid(width, height) encoder = gridc.encoding(encoding_name)(grid) coloring = encoder.decode(answer) if not numpy.all(coloring < 4) or not numpy.all(coloring >= 0): raise ValueError("invalid color in coloring") if not grid.is_coloring(coloring): raise ValueError("constraint violation in coloring") print coloring
def main(root_path, encoding_name, min_width, max_width, min_height, max_height): """Encode a grid-coloring problem in CNF.""" cargo.enable_default_logging() for width in xrange(min_width, max_width + 1): for height in xrange(min_height, max_height + 1): # encode the instance grid = gridc.Grid(width, height) encoding = gridc.encoding(encoding_name) instance = encoding(grid).encode() # write it to disk out_name = "{0}x{1}.{2}.cnf".format(width, height, encoding_name) out_path = os.path.join(root_path, out_name) with open(out_path, "w") as out_file: instance.write(out_file)