Пример #1
0
def test_objective_to_coeffs():
    vars = [x, y, z]
    assert_array_equal(objective_to_coeffs(1.2 + x - 3.4 * y, vars, 'max'),
                       array([-1.0, 3.4, 0.0]))

    except_ok = False
    try:
        objective_to_coeffs(1.2 + x * y, vars)
    except ValueError:
        except_ok = True
    assert except_ok
Пример #2
0
def to_sdpa_sparse(objective_func,
                   lmis,
                   variables,
                   objective_type='minimize',
                   split_blocks=True,
                   comment=None):
    """Put problem (objective and LMIs) into SDPA sparse format."""
    obj_coeffs = lmi_sdp.objective_to_coeffs(objective_func, variables,
                                             objective_type)
    lmi_coeffs = lmi_to_coeffs(lmis, variables, split_blocks)

    s = lmi_sdp.sdp._sdpa_header(obj_coeffs, lmi_coeffs, comment)

    def _print_sparse(x, b, m, sign=1):
        s = ''
        shape = m.shape
        for i in range(shape[0]):
            for j in range(i, shape[1]):
                e = sign * m[i, j]
                if e != 0:
                    s += '%d %d %d %d %s\n' % (x, b, i + 1, j + 1, str(e))
        return s

    for b in range(len(lmi_coeffs)):
        s += _print_sparse(0, b + 1, lmi_coeffs[b][1], sign=-1)
    for x in range(len(obj_coeffs)):
        for b in range(len(lmi_coeffs)):
            s += _print_sparse(x + 1, b + 1, lmi_coeffs[b][0][x])

    return s
def to_sdpa_sparse(objective_func, lmis, variables, objective_type='minimize',
                   split_blocks=True, comment=None):
    """Put problem (objective and LMIs) into SDPA sparse format."""
    obj_coeffs = lmi_sdp.objective_to_coeffs(objective_func, variables,
                                             objective_type)
    lmi_coeffs = lmi_to_coeffs(lmis, variables, split_blocks)

    s = lmi_sdp.sdp._sdpa_header(obj_coeffs, lmi_coeffs, comment)

    def _print_sparse(x, b, m, sign=1):
        s = ''
        shape = m.shape
        for i in range(shape[0]):
            for j in range(i, shape[1]):
                e = sign*m[i, j]
                if e != 0:
                    s += '%d %d %d %d %s\n' % (x, b, i+1, j+1, str(e))
        return s

    for b in range(len(lmi_coeffs)):
        s += _print_sparse(0, b+1, lmi_coeffs[b][1], sign=-1)
    for x in range(len(obj_coeffs)):
        for b in range(len(lmi_coeffs)):
            s += _print_sparse(x+1, b+1, lmi_coeffs[b][0][x])

    return s
Пример #4
0
def to_cvxopt(objective_func,
              lmis,
              variables,
              objective_type='minimize',
              split_blocks=True):
    """Prepare objective and LMI to be used with cvxopt SDP solver.

    Parameters
    ----------
    objective_func: symbolic linear expression
    lmi: symbolic LMI or Matrix, or a list of them
    variables: list of symbols
        The variable symbols which form the LMI/SDP space.
    objective_type: 'maximize' or 'minimize', defaults to 'minimize'
    split_blocks: bool
        If set to True, function tries to subdivide each LMI into
        smaller diagonal blocks

    Returns
    -------
    c, Gs, hs: parameters ready to be input to cvxopt.solvers.sdp()
    """
    if cvxopt is None:
        raise lmi_sdp.sdp.NotAvailableError(to_cvxopt.__name__)

    obj_coeffs = lmi_sdp.objective_to_coeffs(objective_func, variables,
                                             objective_type)
    lmi_coeffs = lmi_to_coeffs(lmis, variables, split_blocks)

    c = matrix(obj_coeffs)

    Gs = []
    hs = []

    for (LMis, LM0) in lmi_coeffs:
        Gs.append(
            matrix([(-LMi).flatten().astype(float).tolist() for LMi in LMis]))
        hs.append(matrix(LM0.astype(float).tolist()))

    return c, Gs, hs
def to_cvxopt(objective_func, lmis, variables, objective_type='minimize',
              split_blocks=True):
    """Prepare objective and LMI to be used with cvxopt SDP solver.

    Parameters
    ----------
    objective_func: symbolic linear expression
    lmi: symbolic LMI or Matrix, or a list of them
    variables: list of symbols
        The variable symbols which form the LMI/SDP space.
    objective_type: 'maximize' or 'minimize', defaults to 'minimize'
    split_blocks: bool
        If set to True, function tries to subdivide each LMI into
        smaller diagonal blocks

    Returns
    -------
    c, Gs, hs: parameters ready to be input to cvxopt.solvers.sdp()
    """
    if cvxopt is None:
        raise NotAvailableError(to_cvxopt.__name__)

    obj_coeffs = lmi_sdp.objective_to_coeffs(objective_func, variables,
                                             objective_type)
    lmi_coeffs = lmi_to_coeffs(lmis, variables, split_blocks)

    c = matrix(obj_coeffs)

    Gs = []
    hs = []

    for (LMis, LM0) in lmi_coeffs:
        Gs.append(matrix([(-LMi).flatten().astype(float).tolist()
                          for LMi in LMis]))
        hs.append(matrix(LM0.astype(float).tolist()))

    return c, Gs, hs