def transform_sum_largest(expr): x = only_arg(expr) k = expr.k q = epi_var(expr, "sum_largest") t = epi_var(expr, "sum_largest_t", size=dims(x)) obj = expression.add(expression.sum_entries(t), expression.multiply(expression.scalar_constant(k), q)) constr = [ expression.leq_constraint(x, expression.add(t, q)), expression.leq_constraint(expression.scalar_constant(0), t) ] return obj, constr
def transform_sum_largest(expr): x = only_arg(expr) k = expr.k q = epi_var(expr, "sum_largest") t = epi_var(expr, "sum_largest_t", size=dims(x)) obj = expression.add( expression.sum_entries(t), expression.multiply(expression.scalar_constant(k), q)) constr = [ expression.leq_constraint(x, expression.add(t, q)), expression.leq_constraint(expression.scalar_constant(0), t)] return obj, constr
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
def transform_quad_over_lin(expr): assert len(expr.arg) == 2 x, y = expr.arg assert dim(y) == 1 t = epi_var(expr, "qol", size=(1,1)) return t, [ expression.soc_constraint( expression.add(y, t), expression.vstack( expression.add(y, expression.negate(t)), expression.reshape( expression.multiply(expression.scalar_constant(2), x), dim(x), 1))), expression.leq_constraint(expression.scalar_constant(0), y)]
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
def transform_quad_over_lin(expr): assert len(expr.arg) == 2 x, y = expr.arg assert dim(y) == 1 t = epi_var(expr, "qol", size=(1, 1)) return t, [ expression.soc_constraint( expression.add(y, t), expression.hstack( expression.add(y, expression.negate(t)), expression.reshape( expression.multiply(expression.scalar_constant(2), x), 1, dim(x)))), expression.leq_constraint(expression.scalar_constant(0), y) ]
def transform_problem(problem, params): prox_rules = PROX_RULES + BASE_RULES # Epigraph/cone rules if params.use_epigraph: prox_rules.append(epigraph) prox_rules.append(prox_non_negative) prox_rules.append(transform_cone) f_exprs = list(transform_expr(prox_rules, problem.objective)) for constr in problem.constraint: f_exprs += list(transform_expr(prox_rules, constr)) return expression.Problem(objective=expression.add(*f_exprs))
def transform_hstack(expr): m = dim(expr, 0) n = dim(expr, 1) offset = 0 add_args = [] for arg in expr.arg: ni = dim(arg, 1) add_args.append( expression.linear_map( linear_map.right_matrix_product(linear_map.index(slice(offset, offset + ni), n), m), transform_expr(arg) ) ) offset += ni return expression.add(*add_args)
def transform_hstack(expr): m = dim(expr, 0) n = dim(expr, 1) offset = 0 add_args = [] for arg in expr.arg: ni = dim(arg, 1) add_args.append( expression.linear_map( linear_map.right_matrix_product( linear_map.index(slice(offset, offset + ni), n), m), transform_expr(arg))) offset += ni return expression.add(*add_args)
def transform_vstack(expr): m = dim(expr, 0) n = dim(expr, 1) offset = 0 add_args = [] for arg in expr.arg: mi = dim(arg, 0) add_args.append( expression.linear_map( linear_map.left_matrix_product( linear_map.transpose( linear_map.index(slice(offset, offset + mi), m)), n), transform_expr(arg))) offset += mi return expression.add(*add_args)
def transform_vstack(expr): m = dim(expr, 0) n = dim(expr, 1) offset = 0 add_args = [] for arg in expr.arg: mi = dim(arg, 0) add_args.append( expression.linear_map( linear_map.left_matrix_product( linear_map.transpose( linear_map.index(slice(offset, offset+mi), m)), n), transform_expr(arg))) offset += mi return expression.add(*add_args)
def transform_problem(problem): f_exprs = list(transform_expr(problem.objective)) for constr in problem.constraint: f_exprs += list(transform_expr(constr)) return expression.Problem(objective=expression.add(*f_exprs))
def transform_add(expr): return expression.add(*[promote(transform_expr(e), dim(expr)) for e in expr.arg])
def gm(t, x, y): return expression.soc_elemwise_constraint( expression.add(x, y), expression.add(x, expression.negate(y)), expression.multiply(expression.scalar_constant(2), t))
def problem(self): return expression.Problem( objective=expression.add(*(f.expr for f in self.nodes(FUNCTION))), constraint=[f.expr for f in self.nodes(CONSTRAINT)])
def transform_add(expr): return expression.add( *[promote(transform_expr(e), dim(expr)) for e in expr.arg])