예제 #1
0
dims_bb = [
    nn,
] * 11

aa = [nr.rand(_) for _ in dims_aa]
aa = [_ / sum(_) for _ in aa]

bb = [nr.rand(_) for _ in dims_bb]
bb = [_ / sum(_) for _ in bb]

cc = []
for _a in aa:
    for _b in bb:
        cc += [_a + _b]

emd_aa = greedy_primal_dual(aa)
emd_bb = greedy_primal_dual(bb)
emd_cc = greedy_primal_dual(cc)
print()
print(
    'Does obj(A) + obj(B) == obj(A+B)?',
    abs(emd_aa['primal objective'] + emd_bb['primal objective'] -
        emd_cc['dual objective']) < 1e-10)
print()
print('A')
print('  |A|:', len(aa))
print('  primal obj:                  ', emd_aa['primal objective'])
print('  dual   obj:                  ', emd_aa['dual objective'])
print('B')
print('  |B|:', len(bb))
print('  primal obj:                  ', emd_bb['primal objective'])
예제 #2
0
    hh = cvxopt.matrix([cvxopt.matrix(np.zeros(cc.size)), cvxopt.matrix(beq)])
    bb = cvxopt.matrix(sum(sum_aa) / len(aa) * 1.)
    AA_eq = cvxopt.matrix(1., (1, cc.size))

    GG = cvxopt.sparse(GG)

    ret_cvx = cvx_solvers.lp(cvxopt.matrix(cc), GG, hh, A=AA_eq, b=bb)
    return ret_cvx


if __name__ == '__main__':
    dims = [
        4,
    ] * 6
    aa = [np.random.random(d) for d in dims]
    aa = [_ / sum(_) for _ in aa]

    # solve the primal and dual problems explicity
    emd_aa = greedy_primal_dual(aa)
    cvx_aa = cvxprimal(aa)
    dul_aa = cvxdual(aa)

    print('=' * 50)
    print('Objectives')
    print('  greedy primal obj      : %6.4f' % emd_aa['primal objective'])
    print('  greedy dual   obj      : %6.4f' % emd_aa['dual objective'])
    print('  cvxopt primal obj      : %6.4f' % cvx_aa['primal objective'])
    print('  cvxopt dual   obj      : %6.4f' % cvx_aa['dual objective'])
    print('  cvxopt dual primal obj : %6.4f' % -dul_aa['primal objective'])
    print('  cvxopt dual dual obj   : %6.4f' % -dul_aa['dual objective'])