Exemple #1
0
def transform_norm_p(expr):
    p = expr.p
    x = only_arg(expr)
    t = epi_var(expr, "norm_p", size=(1,1))

    if p == float("inf"):
        return t, [expression.leq_constraint(x, t),
                   expression.leq_constraint(expression.negate(x), t)]

    if p == 1:
        return transform_expr(expression.sum_entries(expression.abs_val(x)))

    if p == 2:
        return t, [expression.soc_constraint(t, x)]

    r = epi_var(expr, "norm_p_r", size=dims(x))
    t1 = expression.multiply(expression.ones(*dims(x)), t)

    if p < 0:
        p, _ = power_tools.pow_neg(p)
        p = Fraction(p)
        constrs = gm_constrs(t1, [x, r], (-p/(1-p), 1/(1-p)))
    elif 0 < p < 1:
        p, _ = power_tools.pow_mid(p)
        p = Fraction(p)
        constrs = gm_constrs(r, [x, t1], (p, 1-p))
    elif p > 1:
        abs_x, constrs = transform_expr(expression.abs_val(x))
        p, _ = power_tools.pow_high(p)
        p = Fraction(p)
        constrs += gm_constrs(abs_x, [r, t1], (1/p, 1-1/p))

    constrs.append(expression.eq_constraint(expression.sum_entries(r), t))
    return t, constrs
Exemple #2
0
def transform_norm_p(expr):
    p = expr.p
    x = only_arg(expr)
    t = epi_var(expr, "norm_p")

    if p == float("inf"):
        return t, [
            expression.leq_constraint(x, t),
            expression.leq_constraint(expression.negate(x), t)
        ]

    if p == 1:
        return transform_expr(expression.sum_entries(expression.abs_val(x)))

    if p == 2:
        if not expr.has_axis:
            return t, [
                expression.soc_constraint(t, expression.reshape(x, 1, dim(x)))
            ]
        if expr.axis == 0:
            return t, [
                expression.soc_constraint(expression.reshape(t, dim(x, 1), 1),
                                          expression.transpose(x))
            ]
        if expr.axis == 1:
            return t, [expression.soc_constraint(t, x)]

    r = epi_var(expr, "norm_p_r", size=dims(x))
    t1 = expression.multiply(expression.ones(*dims(x)), t)

    if p < 0:
        p, _ = power_tools.pow_neg(p)
        p = Fraction(p)
        constrs = gm_constrs(t1, [x, r], (-p / (1 - p), 1 / (1 - p)))
    elif 0 < p < 1:
        p, _ = power_tools.pow_mid(p)
        p = Fraction(p)
        constrs = gm_constrs(r, [x, t1], (p, 1 - p))
    elif p > 1:
        abs_x, constrs = transform_expr(expression.abs_val(x))
        p, _ = power_tools.pow_high(p)
        p = Fraction(p)
        constrs += gm_constrs(abs_x, [r, t1], (1 / p, 1 - 1 / p))

    constrs.append(expression.eq_constraint(expression.sum_entries(r), t))
    return t, constrs
Exemple #3
0
def transform_huber(expr):
    n = epi_var(expr, "huber_n")
    s = epi_var(expr, "huber_s")

    # n**2 + 2*M*|s|
    t, constr = transform_expr(
        expression.add(
            expression.power(n, 2),
            expression.multiply(expression.scalar_constant(2 * expr.M),
                                expression.abs_val(s))))
    # x == s + n
    x = only_arg(expr)
    constr.append(expression.eq_constraint(x, expression.add(s, n)))
    return t, constr
Exemple #4
0
def transform_huber(expr):
    n = epi_var(expr, "huber_n")
    s = epi_var(expr, "huber_s")

    # n**2 + 2*M*|s|
    t, constr = transform_expr(
        expression.add(
            expression.power(n, 2),
            expression.multiply(
                expression.scalar_constant(2*expr.M),
                expression.abs_val(s))))
    # x == s + n
    x = only_arg(expr)
    constr.append(expression.eq_constraint(x, expression.add(s, n)))
    return t, constr