示例#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]
示例#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
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
示例#4
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
示例#5
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 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, 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 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
示例#9
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 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
 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
示例#12
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
示例#13
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
示例#14
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 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
示例#16
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
示例#17
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
示例#18
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 color_change(p: Problem) -> Problem:
    assert is_same(p)
    # print(p)
    case_x: Case
    case_y: Case
    case_x_new: Case

    change_vec = -2 * np.ones(10, dtype=np.int64)

    for case_x, case_y in zip(p.train_x_list, p.train_y_list):

        # need to be one matter
        assert len(case_y.matter_list) == 1
        if len(case_x.matter_list) == 1:
            x_values = case_x.repr_values()
        else:
            x_values = trivial_reducer(case_x)
        y_values = case_y.repr_values()

        # same shape
        assert x_values.shape == y_values.shape

        # value range
        assert x_values.min() >= 0
        assert x_values.max() <= 9
        assert y_values.min() >= 0
        assert y_values.max() <= 9

        # one rule
        res_arr = find_color_change_rule_one(x_values, y_values)
        # print(res_arr)

        for c in range(10):
            if res_arr[c] == -2:
                pass
            elif change_vec[c] == -2:
                change_vec[c] = res_arr[c]
            else:
                assert change_vec[c] == res_arr[c]

    # print(change_vec)
    # fill
    for c in range(10):
        if (change_vec == c).sum() >= 3:
            change_vec[change_vec == -2] = c
    # fill
    if (change_vec == np.arange(10)).sum() >= 3:
        for c in range(10):
            if change_vec[c] == -2:
                change_vec[c] = c
    q: Problem
    q = p.copy()
    q.train_x_list = []
    q.test_x_list = []

    # transform
    # test_x first so that find exception faster
    for case_x in p.test_x_list:
        if len(case_x.matter_list) == 1:
            x_values = case_x.repr_values()
        else:
            x_values = trivial_reducer(case_x)
        # print(x_values)
        # value range
        assert x_values.min() >= 0
        assert x_values.max() <= 9

        # change_vec
        new_values = fit_color_change_rule_one(x_values, change_vec)
        assert new_values.min() >= 0  # no more-2
        # print(new_values)
        case_x_new = case_x.copy()
        case_x_new.matter_list = [
            Matter(new_values,
                   background_color=case_x_new.background_color,
                   new=True)
        ]
        q.test_x_list.append(case_x_new)
    # train_x
    for case_x in p.train_x_list:
        if len(case_x.matter_list) == 1:
            x_values = case_x.repr_values()
        else:
            x_values = trivial_reducer(case_x)
        # print(x_values)
        # value range
        assert x_values.min() >= 0
        assert x_values.max() <= 9

        # change_vec
        new_values = fit_color_change_rule_one(x_values, change_vec)
        # print(new_values)
        case_x_new = case_x.copy()
        case_x_new.matter_list = [
            Matter(new_values,
                   background_color=case_x_new.background_color,
                   new=True)
        ]
        q.train_x_list.append(case_x_new)

    return q
def divide(p: Problem) -> Problem:

    flag, d_row, d_col = is_division(p)
    flag_n = False

    if not flag:
        flag, nc_row, nc_col = is_constant(p)
        flag_n = True
        cnt_arr = np.zeros((2, 2), dtype=int)
    else:
        cnt_arr = np.zeros((d_row, d_col), dtype=int)
    assert flag

    q: Problem
    q = p.copy()
    q.train_x_list = []
    q.test_x_list = []
    c_x: Case
    c_x_new: Case

    # find best
    for c_x, c_y in zip(p.train_x_list, p.train_y_list):
        base_values = c_x.repr_values()
        res_values = c_y.repr_values()
        n_row, n_col = base_values.shape
        if flag_n:
            assert n_row % nc_row == 0
            assert n_col % nc_col == 0
            d_row = n_row // nc_row
            d_col = n_col // nc_col
        q_row, q_col = n_row // d_row, n_col // d_col
        # print(d_row, d_col)
        # is_division -> any
        if not flag_n:
            for i in range(d_row):
                for j in range(d_col):
                    if (base_values[i * q_row:(i + 1) * q_row,
                                    j * q_col:(j + 1) *
                                    q_col] == res_values).min():
                        cnt_arr[i, j] += 1
        # is_constant -> 4 corners
        else:
            if (base_values[:q_row, :q_col] == res_values).min():
                cnt_arr[0, 0] += 1
            if (base_values[:q_row, -q_col:] == res_values).min():
                cnt_arr[0, 1] += 1
            if (base_values[-q_row:, :q_col] == res_values).min():
                cnt_arr[1, 0] += 1
            if (base_values[-q_row:, -q_col:] == res_values).min():
                cnt_arr[1, 1] += 1
    # print(cnt_arr)
    # fit
    i0, j0 = -1, -1
    for i in range(cnt_arr.shape[0]):
        for j in range(cnt_arr.shape[1]):
            if cnt_arr[i, j] == len(p.train_x_list):
                i0, j0 = i, j
                break
        if i0 >= 0:
            break
    assert i0 >= 0 and j0 >= 0

    # fit
    for c_x in p.train_x_list:
        base_values = c_x.repr_values()
        n_row, n_col = base_values.shape
        if flag_n:
            assert n_row % nc_row == 0
            assert n_col % nc_col == 0
            d_row = n_row // nc_row
            d_col = n_col // nc_col
        q_row, q_col = n_row // d_row, n_col // d_col

        # is_division -> any
        if not flag_n:
            new_values = base_values[i0 * q_row:(i0 + 1) * q_row,
                                     j0 * q_col:(j0 + 1) * q_col].copy()
        # is_constant -> 4 corners
        else:
            if i0 == 0 and j0 == 0:
                new_values = base_values[:q_row, :q_col].copy()
            elif i0 == 0 and j0 == 1:
                new_values = base_values[:q_row, -q_col:].copy()
            elif i0 == 1 and j0 == 0:
                new_values = base_values[-q_row:, :q_col].copy()
            else:
                new_values = base_values[-q_row:, -q_col:].copy()

        c_x_new = c_x.copy()
        c_x_new.shape = q_row, q_col
        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:
        base_values = c_x.repr_values()
        n_row, n_col = base_values.shape
        if flag_n:
            assert n_row % nc_row == 0
            assert n_col % nc_col == 0
            d_row = n_row // nc_row
            d_col = n_col // nc_col
        q_row, q_col = n_row // d_row, n_col // d_col

        # is_division -> any
        if not flag_n:
            new_values = base_values[i0 * q_row:(i0 + 1) * q_row,
                                     j0 * q_col:(j0 + 1) * q_col].copy()
        # is_constant -> 4 corners
        else:
            if i0 == 0 and j0 == 0:
                new_values = base_values[:q_row, :q_col].copy()
            elif i0 == 0 and j0 == 1:
                new_values = base_values[:q_row, -q_col:].copy()
            elif i0 == 1 and j0 == 0:
                new_values = base_values[-q_row:, :q_col].copy()
            else:
                new_values = base_values[-q_row:, -q_col:].copy()

        c_x_new = c_x.copy()
        c_x_new.shape = q_row, q_col
        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
示例#21
0
def color_pile(p: Problem) -> Problem:

    case_x: Case
    case_y: Case
    case_x_new: Case

    rule_all = 10 * np.ones(10, dtype=np.int64)

    for case_x, case_y in zip(p.train_x_list, p.train_y_list):

        # same shape
        assert case_x.shape == case_y.shape

        # reduce count
        cnt_arr = np.zeros((case_x.shape[0], case_x.shape[1], 10), dtype=np.int64)
        # collect values
        for m in case_x.matter_list:
            if not m.bool_show:
                continue
            for i in range(m.shape[0]):
                for j in range(m.shape[1]):
                    if m.values[i, j] != m.background_color:
                        assert 0 <= m.values[i, j] <= 9
                        cnt_arr[m.x0 + i, m.y0 + j, m.values[i, j]] += 1

        y_values = case_y.repr_values()

        # one rule
        res_arr = color_pile_arr(cnt_arr, y_values, case_x.background_color)
        for color in range(10):
            if (y_values == color).sum():
                assert res_arr[color] < 10

        # reduce rule
        rule_all = reduce_rule(res_arr, rule_all)

    q: Problem
    q = p.copy()
    q.train_x_list = []
    q.test_x_list = []

    # transform
    # test_x first so that find exception faster
    for case_x in p.test_x_list:
        new_values = case_x.background_color * np.ones(case_x.shape, dtype=np.int64)
        cnt_arr = np.zeros((case_x.shape[0], case_x.shape[1], 10), dtype=np.int64)
        # collect values
        for m in case_x.matter_list:
            if not m.bool_show:
                continue
            for i in range(m.shape[0]):
                for j in range(m.shape[1]):
                    if m.values[i, j] != m.background_color:
                        assert 0 <= m.values[i, j] <= 9
                        cnt_arr[m.x0 + i, m.y0 + j, m.values[i, j]] += 1
        # reduce
        for color_order in range(9, -1, -1):
            for color in range(10):
                if rule_all[color] == color_order:
                    new_values[cnt_arr[:, :, color] > 0] = color
        case_x_new = case_x.copy()
        case_x_new.matter_list = [Matter(new_values, background_color=case_x_new.background_color, new=True)]
        q.test_x_list.append(case_x_new)
    # train_x
    for case_x in p.train_x_list:
        new_values = case_x.background_color * np.ones(case_x.shape, dtype=np.int64)
        cnt_arr = np.zeros((case_x.shape[0], case_x.shape[1], 10), dtype=np.int64)
        # collect values
        for m in case_x.matter_list:
            if not m.bool_show:
                continue
            for i in range(m.shape[0]):
                for j in range(m.shape[1]):
                    if m.values[i, j] != m.background_color:
                        assert 0 <= m.values[i, j] <= 9
                        cnt_arr[m.x0 + i, m.y0 + j, m.values[i, j]] += 1
        # reduce
        for color_order in range(9, -1, -1):
            for color in range(10):
                if rule_all[color] == color_order:
                    new_values[cnt_arr[:, :, color] > 0] = color
        case_x_new = case_x.copy()
        case_x_new.matter_list = [Matter(new_values, background_color=case_x_new.background_color, new=True)]
        q.train_x_list.append(case_x_new)

    return q
示例#22
0
 def matter(cls, m: Matter) -> Matter:
     new_values, xy0 = cls.array(m.values, m.background_color)
     return Matter(new_values, 0, 0, m.background_color, new=True)