Example #1
0
    def test_diagonal_rectangulation_15rect(self):
        w = 320
        h = 180
        k = 1.5
        # 0.1: proportion is predominant
        # 0.05: seems good ballanced
        c = 0.05

        B, dim = r.do_diagonal_rectangulation(
            [7, 12, 6, 4, 10, 1, 13, 5, 14, 8, 9, 2, 0, 3, 11])
        Bc = np.array([[0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 11, 11, 11, 11],
                       [1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 11, 11, 11, 11],
                       [1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 11, 11, 11, 11],
                       [1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 11, 11, 11, 11],
                       [4, 4, 4, 4, 4, 5, 5, 5, 8, 9, 9, 11, 11, 11, 11],
                       [4, 4, 4, 4, 4, 5, 5, 5, 8, 9, 9, 11, 11, 11, 11],
                       [6, 6, 6, 6, 6, 6, 6, 6, 8, 9, 9, 11, 11, 11, 11],
                       [7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 11, 11],
                       [7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 11, 11],
                       [7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 9, 11, 11, 11, 11],
                       [7, 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 11, 11, 11, 11],
                       [7, 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 11, 11, 11, 11],
                       [7, 7, 7, 7, 7, 7, 7, 7, 12, 12, 12, 12, 12, 13, 14],
                       [7, 7, 7, 7, 7, 7, 7, 7, 12, 12, 12, 12, 12, 13, 14],
                       [7, 7, 7, 7, 7, 7, 7, 7, 12, 12, 12, 12, 12, 13, 14]],
                      dtype=int)
        self.assertTrue((B == Bc).all())
        r.draw_resized_rectangles(B, dim, 1, 1)
        dim[0, :] *= w
        dim[1, :] *= h
        E = r.build_rectangulation_equations(B)
        mat_sol = r.minimize_rectangulation(E, dim, w, h, k, c)
        r.draw_resized_rectangles(B, mat_sol, w, h)
Example #2
0
    def test_scipy_minimize(self):
        w = 320
        h = 180
        k = 1.5
        c = 0.05

        Bc = [
            np.array([[0, 1, 2], [0, 1, 2], [0, 1, 2]], dtype=int),
            np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2]], dtype=int),
            np.array([[0, 0, 2], [1, 1, 2], [1, 1, 2]], dtype=int),
            np.array([[0, 1, 1, 3, 3], [0, 1, 1, 3, 3], [2, 2, 2, 3, 3],
                      [2, 2, 2, 3, 3], [2, 2, 2, 4, 4]],
                     dtype=int)
        ]
        Ec = [[[1., 1., 1., 0., 0., 0.], [0., 0., 0., 1., 0., 0.],
               [0., 0., 0., 1., -1., 0.], [0., 0., 0., 0., 1., -1.]],
              [[1., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 1., 1.],
               [1., -1., 0., 0., 0., 0.], [0., 1., -1., 0., 0., 0.]],
              [[1., 0., 1., 0., 0., 0.], [0., 0., 0., 1., 1., 0.],
               [1., -1., 0., 0., 0., 0.], [0., 0., 0., 1., 1., -1.]],
              [[1., 1., 0., 1., 0., 0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0., 1., 0., 1., 0., 0.],
               [1., 1., -1., 0., 0., 0., 0., 0., 0., 0.],
               [0., 0., 0., 1., -1., 0., 0., 0., 0., 0.],
               [0., 0., 0., 0., 0., 1., -1., 0., 0., 0.],
               [0., 0., 0., 0., 0., 0., 1., 1., -1., -1.]]]
        sol_c = [
            np.array([[106.66666667, 106.66666667, 106.66666667],
                      [180., 180., 180.]]),
            np.array([[320., 320., 320.], [60., 60., 60.]]),
            np.array([[203.63636358, 203.63636358, 116.36363642],
                      [90.00000004, 89.99999996, 180.]]),
            np.array([[
                94.76075857, 94.76075848, 189.52151705, 130.47848295,
                130.47848295
            ],
                      [
                          109.87696965, 109.87696965, 70.12303035, 89.99999997,
                          90.00000003
                      ]])
        ]
        diagonals = [[0, 1, 2], [2, 1, 0], [1, 0, 2], [2, 0, 4, 1, 3]]
        for i, diag in enumerate(diagonals):
            B, dim = r.do_diagonal_rectangulation(diag)
            self.assertTrue((B == Bc[i]).all())
            dim[0, :] *= w
            dim[1, :] *= h
            E = r.build_rectangulation_equations(B)
            self.assertTrue((E == Ec[i]).all())
            sol = r.minimize_rectangulation(E, dim, w, h, k, c)
            self.assertLess(np.sum(np.abs(sol_c[i] - sol)), 0.01)
            r.draw_resized_rectangles(B, dim, w, h)
Example #3
0
 def test_fit_sides(self):
     w = 320
     h = 180
     k = 1.5
     diagonals = [[0, 1, 2], [2, 1, 0], [1, 0, 2], [2, 0, 4, 1, 3]]
     for diag in diagonals:
         N = len(diag)
         B, dim = r.do_diagonal_rectangulation(diag)
         dim[0, :] *= w
         dim[1, :] *= h
         E = r.build_rectangulation_equations(B)
         sol = rfs.solve_fit_rectangles(E, B, w, h, k)
         mat_sol = self.get_matrix_from_finiteset(N, sol)
         r.draw_resized_rectangles(B, mat_sol, w, h)
Example #4
0
    def test_diagonal_rectangulation_random(self):
        w = 320
        h = 180
        k = 1.5
        c = 0.05

        rect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        shuffle(rect)
        print(rect)
        B, dim = r.do_diagonal_rectangulation(rect)
        r.draw_resized_rectangles(B, dim, 1, 1)
        dim[0, :] *= w
        dim[1, :] *= h
        E = r.build_rectangulation_equations(B)
        mat_sol = r.minimize_rectangulation(E, dim, w, h, k, c)
        r.draw_resized_rectangles(B, mat_sol, w, h)
Example #5
0
    def test_lagrange_method_analitycal(self):
        w = 320
        h = 180
        k = 1.5
        # [2, 0, 4, 1, 3] can be handled, but takes a little bit
        diagonals = [[0, 1, 2], [2, 1, 0], [1, 0, 2]]
        for diag in diagonals:
            N = len(diag)
            B, dim = r.do_diagonal_rectangulation(diag)
            dim[0, :] *= w
            dim[1, :] *= h
            E = r.build_rectangulation_equations(B)

            sol = rl.solve_rectangle_eqs(E, w, h, k)
            print(sol)
            mat_sol = self.get_matrix_from_finiteset(N, sol)
            r.draw_resized_rectangles(B, mat_sol, w, h)
Example #6
0
    def test_lagrange_method_numerical(self):
        w = 320
        h = 180
        k = 1.5
        diagonals = [[0, 1, 2], [2, 1, 0], [1, 0, 2]]
        for diag in diagonals:
            N = len(diag)
            B, dim = r.do_diagonal_rectangulation(diag)
            dim[0, :] *= w
            dim[1, :] *= h
            E = r.build_rectangulation_equations(B)
            # Number of lambda values will be same as number of eqs (N+1)
            initial_est = np.ones(dim.size + N + 1)
            initial_est[0:dim.size] = dim.reshape(dim.size)

            def dfunc(X):
                return rl.get_derivative_from_eqs(X, E, w, h, k)

            X = fsolve(dfunc, initial_est)
            self.assertAlmostEqual(0.,
                                   rl.get_optimization_f_val(X, E, w, h, k))
Example #7
0
def get_best_rect_for_window_old(N, k, w, h):
    # Check all permutations
    # TODO filter duplicates
    sol_best = np.zeros(0)
    B_best = None
    dev_from_k = 0.15
    dev_from_k = 0.5
    seq_first = [r for r in range(0, N)]
    # XXX
    # seq_first = [1, 4, 2, 0, 3]
    for seq in itertools.permutations(seq_first):
        B, dim = rr.do_diagonal_rectangulation(seq)
        E = rr.build_rectangulation_equations(B)
        print(seq)
        print(B)
        sol_sympy = solve_rectangle_eqs(E, w, h, k)
        sol = np.zeros((2, N))
        # Only one solution
        print(sol_sympy)
        full_sol = True
        for i in range(N):
            if not isinstance(sol_sympy.args[0][i], sp.Float) or \
               not isinstance(sol_sympy.args[0][N + i], sp.Float):
                print('Not fully determined:', sol_sympy)
                full_sol = False
                break
            sol[0, i] = sol_sympy.args[0][i]
            sol[1, i] = sol_sympy.args[0][N + i]
        # sol = get_matrix_from_solution(N, sol_sympy)
        if full_sol and check_proportions_in_range(k, sol, dev_from_k):
            if sol_best.size == 0 or \
               compare_rectangulations_size(sol, sol_best, w, h):
                sol_best = sol
                B_best = B
        rr.draw_resized_rectangles(B, sol, w, h)

    return B_best, sol_best