Beispiel #1
0
    def matter(cls, m: Matter) -> List[Matter]:
        """
        function to split a matter by mesh
        others in one and mesh
        :param m: matter to split
        :return: List[Matter]
        """
        c = find_mesh(m.values)
        assert c != -1

        arr_list, mesh_arr = split_by_mesh(m.values, m.background_color)

        # reduce
        main_values = m.background_color * np.ones(m.shape, dtype=np.int)
        for arr, xy0 in arr_list:
            x0, y0 = xy0
            main_values[x0:x0 + arr.shape[0], y0:y0 + arr.shape[1]] = arr

        # main
        matter_main = Matter(main_values,
                             background_color=m.background_color,
                             new=True)

        # mesh
        matter_mesh = Matter(mesh_arr,
                             background_color=m.background_color,
                             new=True)
        matter_mesh.is_mesh = True

        return [matter_main, matter_mesh]
Beispiel #2
0
    def matter(cls, m: Matter) -> List[Matter]:
        """
        function to split a matter by mesh
        split others by mesh, and mesh
        :param m: matter to split
        :return: List[Matter]
        """
        assert find_mesh(m.values) != -1
        arr_list, mesh_arr = split_by_mesh(m.values, m.background_color)
        res_list = []

        # split
        for arr, xy0 in arr_list:
            x0, y0 = xy0
            new_matter = Matter(arr, x0, y0, m.background_color, new=True)
            res_list.append(new_matter)

        # mesh
        matter_mesh = Matter(mesh_arr,
                             background_color=m.background_color,
                             new=True)
        matter_mesh.is_mesh = True
        res_list.append(matter_mesh)

        return res_list
Beispiel #3
0
 def matter(cls, m: Matter) -> Matter:
     new_matter: Matter = m.copy()
     new_values = m.background_color * np.ones(m.shape, dtype=np.int)
     keep_color = m.max_color()
     new_values[m.values == keep_color] = keep_color
     new_matter.set_values(new_values)
     return new_matter
Beispiel #4
0
    def matter(cls, m: Matter) -> List[Matter]:

        arr_list = cls.array(m.values, m.background_color)
        res_list = []
        for arr in arr_list:
            x_arr = arr[0]
            # trim
            x_sum = (x_arr != m.background_color).sum(axis=1)
            y_sum = (x_arr != m.background_color).sum(axis=0)

            min_x = min([i for i in range(x_arr.shape[0]) if x_sum[i]])
            max_x = max([i for i in range(x_arr.shape[0]) if x_sum[i]])
            min_y = min([i for i in range(x_arr.shape[1]) if y_sum[i]])
            max_y = max([i for i in range(x_arr.shape[1]) if y_sum[i]])

            new_values = x_arr[min_x:max_x + 1, min_y:max_y + 1].copy()
            m = Matter(new_values,
                       min_x,
                       min_y,
                       background_color=m.background_color,
                       new=True)
            m.color = arr[1]
            res_list.append(m)

        return res_list
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
Beispiel #6
0
def point_cross_cnt_mat(m: Matter, y_arr: np.array) -> Tuple[np.array, int]:
    assert m.n_color() == 1
    color_cnt = m.color_count()
    color_cnt[m.background_color] = 0
    m_color = [c for c in range(10) if color_cnt[c] > 0][0]
    x_arr = np.zeros(y_arr.shape, dtype=np.int)
    try:
        x_arr[m.x0:m.x0 + m.shape[0], m.y0:m.y0 + m.shape[1]] = m.values
    except:
        print(x_arr, y_arr, m.x0, m.x0 + m.shape[0], m.y0, m.y0 + m.shape[1])
        raise
    return point_cross_cnt_arr(x_arr, y_arr, m_color), m_color
Beispiel #7
0
    def matter(cls, m: Matter, allow_diagonal: bool, case_background,
               boundary_none) -> List[Matter]:
        arr_list = cls.array(m.values, allow_diagonal, m.background_color)
        # avoid meaningless
        assert len(arr_list) >= 2

        res_list = []

        for i, x_arr in enumerate(arr_list):
            if i > 0:
                # trim
                x_sum = (x_arr != 0).sum(axis=1)
                y_sum = (x_arr != 0).sum(axis=0)
                min_x = min([i for i in range(x_arr.shape[0]) if x_sum[i]])
                max_x = max([i for i in range(x_arr.shape[0]) if x_sum[i]])
                min_y = min([i for i in range(x_arr.shape[1]) if y_sum[i]])
                max_y = max([i for i in range(x_arr.shape[1]) if y_sum[i]])
                new_values = x_arr[min_x:max_x + 1, min_y:max_y + 1].copy()
                # print(new_values)
                if case_background == 0:
                    m_values = 1 - new_values
                    new_m: Matter = Matter(m_values,
                                           min_x,
                                           min_y,
                                           background_color=1,
                                           new=True)
                else:
                    new_m: Matter = Matter(new_values * case_background,
                                           min_x,
                                           min_y,
                                           background_color=0,
                                           new=True)
                if min_x > 0 and max_x < m.shape[
                        0] - 1 and min_y > 0 and max_y < m.shape[1] - 1:
                    new_m.a = 1
                else:
                    if not boundary_none:
                        new_m.a = 0
            else:
                new_values = x_arr.copy()
                new_m: Matter = Matter(new_values,
                                       0,
                                       0,
                                       background_color=m.background_color,
                                       new=True)
                # new_m.a = None
            res_list.append(new_m)

        return res_list
Beispiel #8
0
 def matter(cls, m: Matter, color_dict: dict) -> Matter:
     if m.a is not None:
         if m.a in color_dict.keys():
             new_matter = m.paste_color(color_dict[m.a])
         else:
             flag, bar = is_monotone(color_dict)
             if flag:
                 if m.a >= flag:
                     new_matter = m.paste_color(color_dict[max(color_dict.keys())])
                 else:
                     new_matter = m.paste_color(color_dict[min(color_dict.keys())])
             else:
                 raise AssertionError
         return new_matter
     else:
         return m
    def matter(cls, m: Matter, allow_diagonal: bool) -> List[Matter]:
        arr_list = cls.array(m.values, allow_diagonal, m.background_color)
        # avoid meaningless
        assert len(arr_list) >= 2

        res_list = []

        for x_arr in arr_list:
            # trim
            x_sum = (x_arr != m.background_color).sum(axis=1)
            y_sum = (x_arr != m.background_color).sum(axis=0)

            min_x = min([i for i in range(x_arr.shape[0]) if x_sum[i]])
            max_x = max([i for i in range(x_arr.shape[0]) if x_sum[i]])
            min_y = min([i for i in range(x_arr.shape[1]) if y_sum[i]])
            max_y = max([i for i in range(x_arr.shape[1]) if y_sum[i]])

            new_values = x_arr[min_x:max_x + 1, min_y:max_y + 1].copy()

            res_list.append(
                Matter(new_values,
                       min_x,
                       min_y,
                       background_color=m.background_color,
                       new=True))

        return res_list
 def case_row_col(cls, c: Case) -> Case:
     new_case = c.copy()
     new_values = cls.auto_fill_row_col(c.repr_values(), c.background_color)
     new_case.matter_list = [
         Matter(new_values, background_color=c.background_color, new=True)
     ]
     return new_case
 def matter(cls, m: Matter, hole_type: str) -> Matter:
     if m.is_filled_rectangle() and min(m.shape) >= 3:
         new_matter = m.copy()
         new_values = m.values.copy()
         if hole_type == "simple":
             new_values[1:-1, 1:-1] = m.background_color
         elif hole_type == "mesh":
             new_values[1:-1:2, 1:-1:2] = m.background_color
         elif hole_type == "mesh_x":
             new_values[1:-1:2, 1:-1:2] = m.background_color
             new_values[2:-1:2, 2:-1:2] = m.background_color
         else:
             raise NotImplementedError
         new_matter.set_values(new_values)
         return new_matter
     else:
         return m
Beispiel #12
0
    def case(cls, c: Case) -> Case:

        res_arr = c.repr_fractal_values()

        new_case: Case = c.copy()
        new_case.shape = res_arr.shape
        new_case.matter_list = [Matter(res_arr, background_color=c.background_color, new=True)]
        return new_case
    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 matter(cls, m: Matter) -> Matter:
     assert m.n_color() <= 2
     if m.is_mesh:
         return m
     elif m.n_color() == 0:
         return m
     elif m.n_color() == 1:
         color_count = m.color_count()
         for c in range(10):
             if color_count[c] > 0 and c != m.background_color:
                 base_color = c
         new_matter: Matter = m.copy()
         new_values = m.background_color * np.ones(m.shape, dtype=np.int)
         new_values[m.values == m.background_color] = base_color
         new_matter.set_values(new_values)
         return new_matter
     else:  # m.n_color() == 2
         color_count = m.color_count()
         switch = []
         for c in range(10):
             if color_count[c] > 0 and c != m.background_color:
                 switch.append(c)
         new_matter: Matter = m.copy()
         new_values = m.background_color * np.ones(m.shape, dtype=np.int)
         new_values[m.values == switch[0]] = switch[1]
         new_values[m.values == switch[1]] = switch[0]
         new_matter.set_values(new_values)
         return new_matter
 def matter(cls, m: Matter, n_row, n_col):
     assert m.a is not None
     assert m.a > 0
     if m.b is None:
         assert max(m.a * n_row, m.a * n_col) <= 30
         new_matter: Matter = m.copy()
         new_matter.set_values(duplicate_array(m.values, m.a, m.a))
         new_matter.x0 = 0
         new_matter.y0 = 0
         return new_matter, m.a, m.a
     else:
         assert m.b > 0
         assert max(m.a * n_row, m.b * n_col) <= 30
         new_matter: Matter = m.copy()
         new_matter.set_values(duplicate_array(m.values, m.a, m.b))
         new_matter.x0 = 0
         new_matter.y0 = 0
         return new_matter, m.a, m.b
 def case(cls, c: Case) -> Case:
     new_case = c.copy()
     x_arr = trivial_reducer(c)
     # print(x_arr)
     new_values = cls.array(x_arr)
     new_case.matter_list = [
         Matter(new_values, background_color=c.background_color, new=True)
     ]
     new_case.shape = new_values.shape
     return new_case
Beispiel #17
0
    def matter(cls, m: Matter, color_add: int) -> Matter:
        res_bool = cls.array(m.values)
        new_values = m.values.copy()

        new_values[res_bool] = color_add

        new_m: Matter = m.copy()
        new_m.set_values(new_values)

        return new_m
 def case(cls, c_x: Case, pattern_arr: np.array) -> Case:
     x_values = c_x.repr_values()
     if c_x.color_delete is None:
         new_x_values = cls.array(x_values, pattern_arr,
                                  c_x.background_color)
     else:
         new_x_values = cls.array(x_values, pattern_arr, c_x.color_delete)
     new_case: Case = c_x.copy()
     new_case.matter_list = [Matter(new_x_values, new=True)]
     return new_case
Beispiel #19
0
def point_cross_fit_mat(m: Matter, n_row, n_col, op_arr: np.array) -> Matter:
    assert m.n_color() == 1
    color_cnt = m.color_count()
    color_cnt[m.background_color] = 0
    m_color = [c for c in range(10) if color_cnt[c] > 0][0]

    x_arr = np.zeros((n_row, n_col), dtype=np.int)
    x_arr[m.x0:m.x0 + m.shape[0], m.y0:m.y0 + m.shape[1]] = m.values

    res_arr = point_cross_fit_arr(x_arr, m_color, op_arr[m_color])
    new_values = np.zeros(x_arr.shape, dtype=int)
    new_values[res_arr == 1] = m_color
    new_values[res_arr == 0] = m.background_color
    new_matter = m.copy()
    new_matter.set_values(new_values)
    new_matter.x0 = 0
    new_matter.y0 = 0

    return new_matter
 def matter(cls, m: Matter, op: str, const: int, color_add: int) -> Matter:
     assert m.a is not None
     if op == "<=":
         if m.a <= const:
             return m.paste_color(color_add)
         else:
             return m
     elif op == "==":
         if m.a == const:
             return m.paste_color(color_add)
         else:
             return m
     elif op == ">=":
         if m.a >= const:
             return m.paste_color(color_add)
         else:
             return m
     else:
         raise NotImplementedError
Beispiel #21
0
 def case(cls, c: Case, full) -> Case:
     assert c.shape[0] > 2 and c.shape[1] > 2
     new_case = c.copy()
     if c.color_delete is None:
         new_values = cls.array(c.repr_values(), c.background_color, full)
     else:
         new_values = cls.array(c.repr_values(), c.color_delete, full)
     new_case.matter_list = [
         Matter(new_values, background_color=c.background_color, new=True)
     ]
     return new_case
Beispiel #22
0
 def case_col(cls, c: Case) -> Case:
     new_case = c.copy()
     if c.color_delete is None:
         new_values = cls.auto_fill_symmetry_col_background(c.repr_values())
     else:
         new_values = cls.auto_fill_symmetry_col(c.repr_values(),
                                                 c.color_delete)
     new_case.matter_list = [
         Matter(new_values, background_color=c.background_color, new=True)
     ]
     return new_case
Beispiel #23
0
 def case(cls, c: Case) -> Case:
     if c.color_delete is not None:
         new_case = c.copy()
         new_values = fill_somewhere(c.repr_values(), c.color_delete)
         new_case.matter_list = [
             Matter(new_values,
                    background_color=c.background_color,
                    new=True)
         ]
         return new_case
     else:
         return c
 def matter(cls, m: Matter) -> Matter:
     new_matter = m.deepcopy()
     if new_matter.is_filled_rectangle():
         if new_matter.is_square():
             new_matter.a = 2
         else:
             new_matter.a = 1
     else:
         if m.a is not None:
             new_matter.a = 0
         else:
             new_matter.a = None
     return new_matter
    def case_21(cls, c: Case, compare_c: Case) -> Case:
        assert c.color_add is not None

        new_values = trivial_reducer(c)
        compare_values = trivial_reducer(compare_c)

        assert new_values.shape == compare_values.shape
        new_values[new_values != compare_values] = c.color_add

        new_case: Case = c.copy()
        new_case.matter_list = [Matter(new_values, background_color=c.background_color, new=True)]

        return new_case
Beispiel #26
0
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
Beispiel #27
0
    def case(cls, c: Case) -> Case:
        m: Matter
        x0_arr = np.array([m.x0 for m in c.matter_list if not m.is_mesh])
        y0_arr = np.array([m.y0 for m in c.matter_list if not m.is_mesh])
        x1_arr = np.array(
            [m.x0 + m.shape[0] for m in c.matter_list if not m.is_mesh])
        y1_arr = np.array(
            [m.y0 + m.shape[1] for m in c.matter_list if not m.is_mesh])
        c_arr = np.array(
            [m.max_color() for m in c.matter_list if not m.is_mesh])

        res_arr = align_xy(x0_arr, y0_arr, x1_arr, y1_arr, c_arr)

        new_case: Case = c.copy()
        new_case.shape = res_arr.shape
        new_case.matter_list = [
            Matter(res_arr, background_color=c.background_color, new=True)
        ]
        return new_case
Beispiel #28
0
    def matter(cls, m: Matter) -> List[Matter]:
        """
        function to split a matter by mesh
        others aligned and drop mesh
        :param m: matter to split
        :return: List[Matter]
        """
        assert find_mesh(m.values) != -1
        arr_list, mesh_arr = split_by_mesh(m.values, m.background_color)
        res_list = []

        assert len(arr_list) > 0

        # split and align
        arr_0 = arr_list[0][0]
        for arr, _ in arr_list:  # drop position
            assert arr.shape == arr_0.shape
            new_matter = Matter(arr, np.int(0), np.int(0), m.background_color, new=True)
            res_list.append(new_matter)

        return res_list
 def __init__(self, m: Matter, ind: int, hash_count: int):
     self.a_init = m.a
     self.n_color = m.n_color()
     self.n_cell = m.n_cell()
     self.size = m.shape[0] * m.shape[1]
     self.is_filled_rectangle = m.is_filled_rectangle()
     if m.is_filled_rectangle():
         if m.is_square():
             self.is_rectangle = 2
         else:
             self.is_rectangle = 1
     else:
         if m.a is not None:
             self.is_rectangle = 0
         else:
             self.is_rectangle = None
     # assume trimmed
     x_arr = m.values
     # row
     res_row = 0
     for i in range(x_arr.shape[0] // 2):
         res_row += (x_arr[i, :] != x_arr[x_arr.shape[0] - 1 - i, :]).sum()
     # col
     res_col = 0
     for j in range(x_arr.shape[1] // 2):
         res_col += (x_arr[:, j] != x_arr[:, x_arr.shape[1] - 1 - j]).sum()
     res = 0
     if res_row == 0:
         res += 1
     if res_col == 0:
         res += 2
     self.is_symmetry = res
     self.ind = ind
     self.hash = hash_count
     self.n_noise = (m.values != m.max_color()).sum()
     self.a = None
     self.y = None
 def matter(cls, m: Matter, color_add: int) -> Matter:
     new_matter: Matter = m.copy()
     new_values = m.values.copy()
     new_values[new_values == m.background_color] = color_add
     new_matter.set_values(new_values)
     return new_matter