def test_assign_opt4():
    P0 = np.array([[
        32.2839240000000, 3.21131500000000, 32.2839240000000, 32.2839240000000,
        32.2839240000000, 32.2839240000000, 32.2839240000000, 22.2960390000000,
        32.2839240000000, np.inf, np.inf, np.inf
    ],
                   [
                       32.2839240000000, 1.92542300000000, 32.2839240000000,
                       32.2839240000000, 32.2839240000000, 32.2839240000000,
                       32.2839240000000, 11.4484150000000, 32.2839240000000,
                       6.16967300000000, 6.16967300000000, 6.16967300000000
                   ],
                   [
                       0, 32.2839240000000, 32.2504870000000, 32.2839240000000,
                       32.2839240000000, 32.2839240000000, 32.2839240000000,
                       32.2839240000000, 32.2839240000000, 6.16967300000000,
                       6.16967300000000, 6.16967300000000
                   ]])
    (S0, C0) = g_sub.assign_opt(P0)

    exp_C0 = 9.380988
    exp_S0 = np.array([1, 9, 0])

    test.assert_allclose(C0, exp_C0)
    test.assert_array_equal(S0, exp_S0)
def test_assign_opt1():
    P0 = np.array([
        26.1142509467173, 26.1142509467173, 26.1142509467173, 26.1142509467173,
        0
    ])
    (S0, C0) = g_sub.assign_opt(P0)

    exp_C0 = 0
    exp_S0 = np.array([4])

    test.assert_allclose(C0, exp_C0)
    test.assert_array_equal(S0, exp_S0)
def test_assign_opt3():
    P0 = np.array(
        [[np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf, np.inf],
         [
             np.inf, 32.9419076165403, 32.9419076165403, 32.9419076165403,
             32.9419076165403, 32.9419076154638, np.inf, 6.82765666982308
         ]])
    (S0, C0) = g_sub.assign_opt(P0)

    exp_C0 = 6.82765666982308
    exp_S0 = np.array([-1, 7])

    test.assert_allclose(C0, exp_C0)
    test.assert_array_equal(S0, exp_S0)
def test_assign_opt2():
    P0 = np.array([[
        32.9419076165403, 32.9419076165403, 32.9419076165403, 17.4668722633371,
        32.9419076165403, 32.9419076165403, 6.82765666982308, 6.82765666982308
    ],
                   [
                       0, 32.9419076165403, 32.9419076165403, 32.9419076165403,
                       32.9419076165403, 32.9419076154638, 6.82765666982308,
                       6.82765666982308
                   ]])
    (S0, C0) = g_sub.assign_opt(P0)

    exp_C0 = 6.82765666982308
    exp_S0 = np.array([6, 0])

    test.assert_allclose(C0, exp_C0)
    test.assert_array_equal(S0, exp_S0)
示例#5
0
def __murty_helper(p0, m):
    (s0, c0) = subs.assign_opt(p0)
    s0 = s0.T

    if m == 1:
        return (s0.reshape((1, s0.size)), np.array([c0]))

    (n_rows, n_cols) = p0.shape
    # preallocate arrays
    blk_sz = 1000
    ans_lst_P = np.zeros((n_rows, n_cols, blk_sz))
    ans_lst_S = np.zeros((n_rows, blk_sz), dtype=int)
    ans_lst_C = np.nan * np.ones(blk_sz)

    ans_lst_P[:, :, 0] = p0
    ans_lst_S[:, 0] = s0.T
    ans_lst_C[0] = c0
    ans_nxt_ind = 1

    assigns = np.nan * np.ones((n_rows, m))
    costs = np.zeros(m)

    for ii in range(0, m):
        # if cleared break
        if np.isnan(ans_lst_C).all():
            assigns = assigns[:, 0:ans_nxt_ind]
            costs = costs[0:ans_nxt_ind]
            break

        # find lowest cost solution
        idx_top = np.nanargmin(ans_lst_C[0:ans_nxt_ind])
        assigns[:, ii] = ans_lst_S[:, idx_top]
        costs[ii] = ans_lst_C[idx_top]

        P_now = ans_lst_P[:, :, idx_top]
        S_now = ans_lst_S[:, idx_top]

        ans_lst_C[idx_top] = np.nan

        for (aw, aj) in enumerate(S_now):
            if aj >= 0:
                P_tmp = P_now.copy()
                if aj <= n_cols - n_rows - 1:
                    P_tmp[aw, aj] = np.inf
                else:
                    P_tmp[aw, (n_cols - n_rows):] = np.inf

                (S_tmp, C_tmp) = subs.assign_opt(P_tmp)

                S_tmp = S_tmp.T
                if (S_tmp >= 0).all():
                    # allocate more space as needed
                    if ans_nxt_ind >= len(ans_lst_C):
                        ans_lst_P = np.concatenate(
                            (ans_lst_P, np.zeros((n_rows, n_cols, blk_sz))),
                            axis=2)
                        ans_lst_S = np.concatenate(
                            (ans_lst_S, np.zeros((n_rows, blk_sz), dtype=int)),
                            axis=1)
                        ans_lst_C = np.hstack(
                            (ans_lst_C, np.nan * np.ones(blk_sz)))

                    ans_lst_P[:, :, ans_nxt_ind] = P_tmp
                    ans_lst_S[:, ans_nxt_ind] = S_tmp
                    ans_lst_C[ans_nxt_ind] = C_tmp
                    ans_nxt_ind += 1

                    v_tmp = P_now[aw, aj]
                    P_now[aw, :] = np.inf
                    P_now[:, aj] = np.inf
                    P_now[aw, aj] = v_tmp

    return (assigns.T, costs)