def set_is_line_symmetry_row(p: Problem) -> None: c: Case for c in p.train_y_list: c_arr = c.repr_values() if c_arr.shape[0] <= IGNORE_N_LINES: p.is_line_symmetry_row = False return None if c.color_add is None: res_arr = is_line_symmetry_row(c_arr, -1) else: res_arr = is_line_symmetry_row(c_arr, c.color_add) # print(res_arr) if c_arr.shape[0] < 8: # must be all if c_arr.shape[0] % 2 == 0: if res_arr[c_arr.shape[0] // 2 - 1, 1] < 0: p.is_line_symmetry_row = False return None else: if res_arr[c_arr.shape[0] // 2, 0] < 0: p.is_line_symmetry_row = False return None elif res_arr[3:-3, :].max() < 0: p.is_line_symmetry_row = False return None p.is_line_symmetry_row = True return None
def set_is_rot_symmetry(p: Problem) -> None: c: Case for c in p.train_y_list: c_arr = c.repr_values() if c_arr.shape[0] <= IGNORE_N_LINES or c_arr.shape[1] <= IGNORE_N_LINES: p.is_rot_symmetry = False return None if c.color_add is None: res_arr_1 = is_rot_symmetry_point(c_arr, -1) res_arr_2 = is_rot_symmetry_valley(c_arr, -1) else: res_arr_1 = is_rot_symmetry_point(c_arr, c.color_add) res_arr_2 = is_rot_symmetry_valley(c_arr, c.color_add) # print(c.color_add) # must be in center # print(res_arr_1, res_arr_2) res_arr_1[:res_arr_1.shape[0] // 3, :] = -1 res_arr_1[-res_arr_1.shape[0] // 3:, :] = -1 res_arr_1[:, :res_arr_1.shape[1] // 3] = -1 res_arr_1[:, -res_arr_1.shape[1] // 3:] = -1 res_arr_2[:res_arr_2.shape[0] // 3, :] = -1 res_arr_2[-res_arr_2.shape[0] // 3:, :] = -1 res_arr_2[:, :res_arr_2.shape[1] // 3] = -1 res_arr_2[:, -res_arr_2.shape[1] // 3:] = -1 # print(res_arr_1, res_arr_2) if max(res_arr_1.max(), res_arr_2.max()) <= 20: p.is_rot_symmetry = False return None p.is_rot_symmetry = True return None
def problem(cls, p: Problem) -> Problem: if p.is_line_symmetry_row and p.is_line_symmetry_col: q: Problem = p.copy() q.train_x_list = [cls.case_row_col(c) for c in p.train_x_list] q.test_x_list = [cls.case_row_col(c) for c in p.test_x_list] elif p.is_line_symmetry_row: q: Problem = p.copy() q.train_x_list = [cls.case_row(c) for c in p.train_x_list] q.test_x_list = [cls.case_row(c) for c in p.test_x_list] elif p.is_line_symmetry_col: q: Problem = p.copy() q.train_x_list = [cls.case_col(c) for c in p.train_x_list] q.test_x_list = [cls.case_col(c) for c in p.test_x_list] else: raise AssertionError # check if some more to do res = 0 c: Case for c in q.train_x_list: if c.color_delete is not None: res += (c.repr_values() == c.color_delete).sum() for c in q.test_x_list: if c.color_delete is not None: res += (c.repr_values() == c.color_delete).sum() if res == 0: return q else: return FillSomewhere.problem(q)
def hand_pick(p: Problem) -> List[Problem]: q0: Problem = p.copy() q0.train_x_list = [hand_pick_case(c, 0) for c in p.train_x_list] q0.test_x_list = [hand_pick_case(c, 0) for c in p.test_x_list] q1: Problem = p.copy() q1.train_x_list = [hand_pick_case(c, 1) for c in p.train_x_list] q1.test_x_list = [hand_pick_case(c, 1) for c in p.test_x_list] q2: Problem = p.copy() q2.train_x_list = [hand_pick_case(c, 2) for c in p.train_x_list] q2.test_x_list = [hand_pick_case(c, 2) for c in p.test_x_list] return [q0, q1, q2]
def problem(cls, p: Problem, shadow_type: str) -> Problem: if shadow_type == "problem_max": max_color = (sum([c.color_count() for c in p.train_x_list])).argmax() # print(max_color) q: Problem = p.copy() q.train_x_list = [cls.case(c, shadow_type, max_color) for c in p.train_x_list] q.test_x_list = [cls.case(c, shadow_type, max_color) for c in p.test_x_list] else: q: Problem = p.copy() q.train_x_list = [cls.case(c, shadow_type) for c in p.train_x_list] q.test_x_list = [cls.case(c, shadow_type) for c in p.test_x_list] return q
def set_is_periodic_col(p: Problem) -> None: c: Case for c in p.train_y_list: c_arr = c.repr_values() if c_arr.shape[1] <= IGNORE_N_LINES: p.is_periodic_col = False return None if AutoFillRowColPeriodicity.find_periodicity_col( c_arr, -1) == c_arr.shape[1]: p.is_periodic_col = False return None p.is_periodic_col = True return None
def set_is_periodic_row(p: Problem) -> None: c: Case for c in p.train_y_list: c_arr = c.repr_values() if c_arr.shape[0] <= IGNORE_N_LINES: p.is_periodic_row = False return None if find_periodicity_row(c_arr, -1) == c_arr.shape[0]: p.is_periodic_row = False return None p.is_periodic_row = True return None
def get_problem_from_model(task_json, model, size): p = Problem() data = dict() data["train"] = [] data["test"] = [] for pair in task_json['train']: inp = pair["input"] oup = predict(inp, model, size) data["train"].append({"input": oup, "output": pair["output"]}) for pair in task_json['test']: inp = pair["input"] oup = predict(inp, model, size) data["test"].append({"input": oup}) p.initialize(data) return p
def problem(cls, p: Problem) -> Problem: if p.is_line_symmetry_row and p.is_line_symmetry_col: q: Problem = p.copy() q.train_x_list = [cls.case_row_col(c) for c in p.train_x_list] q.test_x_list = [cls.case_row_col(c) for c in p.test_x_list] elif p.is_line_symmetry_row: q: Problem = p.copy() q.train_x_list = [cls.case_row(c) for c in p.train_x_list] q.test_x_list = [cls.case_row(c) for c in p.test_x_list] elif p.is_line_symmetry_col: q: Problem = p.copy() q.train_x_list = [cls.case_col(c) for c in p.train_x_list] q.test_x_list = [cls.case_col(c) for c in p.test_x_list] else: raise AssertionError return q
def problem(cls, p: Problem) -> Problem: flag, m_row, m_col = is_multiple(p) assert flag q: Problem = p.copy() q.train_x_list = [cls.case(c, m_row, m_col) for c in p.train_x_list] q.test_x_list = [cls.case(c, m_row, m_col) for c in p.test_x_list] return q
def problem(cls, p: Problem) -> Problem: # fast check if p.history == ["color"]: color = True else: color = False for c_x, c_y in zip(p.train_x_list, p.train_y_list): flag_keep, flag_color = cls.case_assert(c_x, c_y, color) assert flag_keep # assign flag train_list = [] test_list = [] for c_x, c_y in zip(p.train_x_list, p.train_y_list): y_list = cls.case_create_flag(c_x, c_y, flag_color) c_ax: AbstractCase = AbstractCase(c_x) c_ax.y_list = y_list train_list.append(c_ax) for c_x in p.test_x_list: c_ax: AbstractCase = AbstractCase(c_x) test_list.append(c_ax) cls.auto_solve(train_list, test_list) q: Problem = p.copy() q.train_x_list = [ cls.case_fit(c, ca) for c, ca in zip(p.train_x_list, train_list) ] q.test_x_list = [ cls.case_fit(c, ca) for c, ca in zip(p.test_x_list, test_list) ] return q
def problem(cls, p: Problem) -> Problem: # fast check for c_x, c_y in zip(p.train_x_list, p.train_y_list): assert cls.case_assert(c_x, c_y) # assign flag train_list = [] test_list = [] for c_x, c_y in zip(p.train_x_list, p.train_y_list): y_list = cls.case_create_flag(c_x, c_y) c_ax: AbstractCase = AbstractCase(c_x) c_ax.y_list = y_list train_list.append(c_ax) for c_x in p.test_x_list: c_ax: AbstractCase = AbstractCase(c_x) test_list.append(c_ax) cls.auto_solve(train_list, test_list) q: Problem = p.copy() q.train_x_list = [ cls.case_fit(c, ca) for c, ca in zip(p.train_x_list, train_list) ] q.test_x_list = [ cls.case_fit(c, ca) for c, ca in zip(p.test_x_list, test_list) ] return q
def problem(cls, p: Problem) -> Problem: q = p.copy() q.train_x_list = [] q.train_y_list = [] q.test_x_list = [] # check if fusion-able c_x: Case c_y: Case c_list = [] for c_x, c_y in zip(p.train_x_list, p.train_y_list): x_arr = c_x.repr_values() y_arr = c_y.repr_values() new_y_arr, x0, x1, y0, y1, c = fusion_arr_train(x_arr, y_arr) # print(new_y_arr) # print(new_y_arr, x0, x1, y0, y1, c) q.train_x_list.append(cls.case_x(c_x, x0, x1, y0, y1)) q.train_y_list.append(cls.case_y(c_y, new_y_arr, x0, x1, y0, y1)) c_list.append(c) for c_x in p.test_x_list: x_arr = c_x.repr_values() c_suggest = c_list[0] x0, x1, y0, y1, c = fusion_arr_test(x_arr, c_suggest) q.test_x_list.append(cls.case_x(c_x, x0, x1, y0, y1)) # pre_solve again is_pattern.set_is_pattern(q) is_periodic.set_is_periodic_row(q) is_periodic.set_is_periodic_col(q) is_symmetry.set_is_line_symmetry_row(q) is_symmetry.set_is_line_symmetry_col(q) return q
def problem(cls, p: Problem) -> Problem: assert is_same(p) if p.is_periodic_row and p.is_periodic_col: q: Problem = p.copy() q.train_x_list = [cls.case_row_col(c) for c in p.train_x_list] q.test_x_list = [cls.case_row_col(c) for c in p.test_x_list] elif p.is_periodic_row: q: Problem = p.copy() q.train_x_list = [cls.case_row(c) for c in p.train_x_list] q.test_x_list = [cls.case_row(c) for c in p.test_x_list] elif p.is_periodic_col: q: Problem = p.copy() q.train_x_list = [cls.case_col(c) for c in p.train_x_list] q.test_x_list = [cls.case_col(c) for c in p.test_x_list] else: raise AssertionError return q
def set_shape(p: Problem, new_shape: Tuple[int, int]) -> Problem: q: Problem q = p.copy() q.train_x_list = [ attr_case.set_shape(x, new_shape) for x in p.train_x_list ] q.test_x_list = [attr_case.set_shape(x, new_shape) for x in p.test_x_list] q.train_y_list = [x for x in p.train_y_list] return q
def problem(cls, p: Problem) -> Problem: t = 1 q = cls.problem_one(p) while t < 100: q = cls.problem_one(p) if p.__repr__() == q.__repr__(): break t += 1 return q
def problem(cls, p: Problem) -> Problem: assert p.is_pattern pattern_arr = get_pattern_arr(p) q: Problem = p.copy() q.train_x_list = [] q.test_x_list = [] q.train_x_list = [cls.case(c_x, pattern_arr) for c_x in p.train_x_list] q.test_x_list = [cls.case(c_x, pattern_arr) for c_x in p.test_x_list] return q
def set_color_add(p: Problem, color_add: int) -> Problem: q: Problem q = p.copy() q.color_add = color_add q.train_x_list = [ attr_case.set_color_add(x, color_add) for x in p.train_x_list ] q.test_x_list = [ attr_case.set_color_add(x, color_add) for x in p.test_x_list ] q.train_y_list = [x for x in p.train_y_list] return q
def duplicate(p: Problem) -> Problem: flag, m_row, m_col = is_multiple(p) flag_n = False if not flag: flag, nc_row, nc_col = is_constant(p) flag_n = True assert flag q: Problem q = p.copy() q.train_x_list = [] q.test_x_list = [] c_x: Case c_x_new: Case for c_x in p.train_x_list: c_x_new = c_x.copy() base_values = c_x.repr_values() n_row, n_col = base_values.shape if flag_n: assert nc_row % n_row == 0 assert nc_col % n_col == 0 m_row = nc_row // n_row m_col = nc_col // n_col c_x_new.shape = m_row * n_row, m_col * n_col new_values = np.zeros((m_row * n_row, m_col * n_col), dtype=np.int) for i in range(m_row): for j in range(m_col): new_values[i * n_row:(i + 1) * n_row, j * n_col:(j + 1) * n_col] = base_values c_x_new.matter_list = [ Matter(new_values, background_color=c_x.background_color, new=True) ] q.train_x_list.append(c_x_new) for c_x in p.test_x_list: c_x_new = c_x.copy() base_values = c_x.repr_values() n_row, n_col = base_values.shape c_x_new.shape = m_row * n_row, m_col * n_col new_values = np.zeros((m_row * n_row, m_col * n_col), dtype=np.int) for i in range(m_row): for j in range(m_col): new_values[i * n_row:(i + 1) * n_row, j * n_col:(j + 1) * n_col] = base_values c_x_new.matter_list = [ Matter(new_values, background_color=c_x.background_color, new=True) ] q.test_x_list.append(c_x_new) return q
def problem(cls, p: Problem, allow_diagonal: bool = False, boundary_none: bool = False) -> Problem: q: Problem = p.copy() q.train_x_list = [ cls.case(c, allow_diagonal, boundary_none) for c in p.train_x_list ] q.test_x_list = [ cls.case(c, allow_diagonal, boundary_none) for c in p.test_x_list ] return q
def set_problem_color(p: Problem) -> None: color_add = define_color(p) # print(color_add) if color_add != -1: p.color_add = color_add c: Case m: Matter for c in p.train_x_list: c.color_add = color_add for c in p.train_y_list: c.color_add = color_add for c in p.test_x_list: c.color_add = color_add deleted_colors = deleted_color(p) if len(deleted_colors) == 1: color_delete = deleted_colors[0] elif len(deleted_colors) == 2 and p.background_color in deleted_colors: if deleted_colors[0] == p.background_color: color_delete = deleted_colors[1] else: color_delete = deleted_colors[0] else: color_delete = None if color_delete is not None: p.color_delete = color_delete c: Case m: Matter for c in p.train_x_list: c.color_delete = color_delete for c in p.train_y_list: c.color_delete = color_delete for c in p.test_x_list: c.color_delete = color_delete return None
def problem(cls, p: Problem) -> Problem: q: Problem = p.copy() q.train_x_list = [cls.case(c) for c in p.train_x_list] q.test_x_list = [cls.case(c) for c in p.test_x_list] background_colors = [] for c in q.train_x_list: background_colors.append(c.background_color) for c in q.test_x_list: background_colors.append(c.background_color) if len(set(background_colors)) == 1: assert background_colors[0] != 0 q.background_color = background_colors[0] return q
def problem(cls, p: Problem) -> Problem: assert only_color(p) # assert non_background coincide q: Problem = p.copy() for op in ["<=", "==", ">="]: for const in range(1, 10): q.train_x_list = [ cls.case(c, op, const) for c in p.train_x_list ] q.test_x_list = [cls.case(c, op, const) for c in p.test_x_list] ac, n_train = q.judge() if ac == n_train: return q raise AssertionError
def problem(cls, p: Problem) -> Problem: # color_dict color_dict = dict() for c_x, c_y in zip(p.train_x_list, p.train_y_list): new_dict = cls.case_xy(c_x, c_y) # print(new_dict) for k in new_dict.keys(): if k in color_dict.keys(): assert color_dict[k] == new_dict[k] else: color_dict[k] = new_dict[k] # fit q: Problem = p.copy() q.train_x_list = [cls.case(c, color_dict) for c in p.train_x_list] q.test_x_list = [cls.case(c, color_dict) for c in p.test_x_list] return q
def problem(cls, p: Problem) -> Problem: c_x: Case c_y: Case # find rule x_arr_list = [] y_arr_list = [] for c_x, c_y in zip(p.train_x_list, p.train_y_list): assert c_x.background_color == p.background_color x_arr_list.append(c_x.repr_values()) y_arr_list.append(c_y.repr_values()) rule_33 = find_replace_rule_33_all(x_arr_list, y_arr_list, p.background_color) # fit rule q: Problem = p.copy() q.train_x_list = [] q.test_x_list = [] c_x: Case c_x_new: Case for c_x in p.train_x_list: c_x_new = c_x.copy() new_values = fit_replace_rule_33_one_all(c_x.repr_values(), rule_33, p.background_color) c_x_new.matter_list = [ Matter(new_values, background_color=p.background_color, new=True) ] q.train_x_list.append(c_x_new) for c_x in p.test_x_list: c_x_new = c_x.copy() new_values = fit_replace_rule_33_one_all(c_x.repr_values(), rule_33, p.background_color) c_x_new.matter_list = [ Matter(new_values, background_color=p.background_color, new=True) ] q.test_x_list.append(c_x_new) return q
def problem(cls, p: Problem) -> List[Problem]: res_list = [] c: Case for c in p.train_y_list: q: Problem = p.copy() new_values = c.repr_values() q.train_x_list = [] for _ in range(p.len_train): new_case = Case(new=True) new_case.initialize(new_values) q.train_x_list.append(new_case) q.test_x_list = [] for _ in range(p.len_test): new_case = Case(new=True) new_case.initialize(new_values) q.test_x_list.append(new_case) res_list.append(q) return res_list
def run_map(p: Problem, command: str) -> Problem: q: Problem # run if command == "identity": q = p.copy() raise DeprecationWarning elif command == "color": q = MapColor.problem(p) elif command == "connect": q = MapConnect.problem(p, True) elif command == "connect4": q = MapConnect.problem(p, False) elif command == "color_connect": q = MapColorConnect.problem(p, True) elif command == "color_connect4": q = MapColorConnect.problem(p, False) elif command == "divide_row_col": q = Divide.problem(p) elif command == "multiple_row_col": q = Multiple.problem(p) elif command == "mesh_2": q = SplitMeshTwo.problem(p) elif command == "mesh_align": q = SplitMeshAlign.problem(p) elif command == "mesh_split": q = SplitMesh.problem(p) elif command == "fusion": q = Fusion.problem(p) elif command == "map_interior": q = MapInterior.problem(p, boundary_none=False) elif command == "map_interior_in": q = MapInterior.problem(p, boundary_none=True) elif command == "map_interior_pierce": q = MapInteriorPierce.problem(p, allow_diagonal=False, boundary_none=True) else: raise NotImplementedError return q
def problem(cls, p: Problem) -> Problem: q: Problem = p.copy() flag, m_row, m_col = is_multiple(p) if flag: q.train_x_list = [ cls.case_m(c, m_row, m_col) for c in p.train_x_list ] q.test_x_list = [ cls.case_m(c, m_row, m_col) for c in p.test_x_list ] return q flag, n_row, n_col = is_constant(p) if flag: q.train_x_list = [ cls.case_n(c, n_row, n_col) for c in p.train_x_list ] q.test_x_list = [ cls.case_n(c, n_row, n_col) for c in p.test_x_list ] return q raise AssertionError
def fit_replace_rule_33(p: Problem) -> Problem: # assert case_x: Case case_y: Case x_arr_list = [] y_arr_list = [] for case_x, case_y in zip(p.train_x_list, p.train_y_list): # same shape x_values = case_x.repr_values() y_values = case_y.repr_values() assert x_values.shape == y_values.shape x_arr_list.append(x_values) y_arr_list.append(y_values) rule_33 = find_replace_rule_33(x_arr_list, y_arr_list, p.background_color) q: Problem q = p.copy() q.train_x_list = [] q.test_x_list = [] c_x: Case c_x_new: Case for c_x in p.train_x_list: c_x_new = c_x.copy() new_values = fit_replace_rule_33_one(c_x.repr_values(), rule_33, p.background_color) c_x_new.matter_list = [Matter(new_values, background_color=p.background_color, new=True)] q.train_x_list.append(c_x_new) for c_x in p.test_x_list: c_x_new = c_x.copy() new_values = fit_replace_rule_33_one(c_x.repr_values(), rule_33, p.background_color) c_x_new.matter_list = [Matter(new_values, background_color=p.background_color, new=True)] q.test_x_list.append(c_x_new) return q
def point_cross(p: Problem) -> Problem: res_problem = point_cross_cnt(p) op_arr = np.zeros(10, dtype=int) for c in range(10): c_min = res_problem[c, :].min() # keep > delete > cross > row > col if res_problem[c, 1] == c_min: op_arr[c] = 1 elif res_problem[c, 0] == c_min: op_arr[c] = 0 elif res_problem[c, 4] == c_min: op_arr[c] = 4 elif res_problem[c, 2] == c_min: op_arr[c] = 2 else: # res_problem[c, 3] == c_min: op_arr[c] = 3 q: Problem = p.copy() q.train_x_list = [point_cross_fit_case(c, op_arr) for c in p.train_x_list] q.test_x_list = [point_cross_fit_case(c, op_arr) for c in p.test_x_list] return q