|
| 1 | +""" |
| 2 | +Copyright 2025 CVXPY developers |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +from typing import Tuple |
| 18 | + |
| 19 | +import cvxpy as cp |
| 20 | +from cvxpy import problems |
| 21 | +from cvxpy.expressions.expression import Expression |
| 22 | +from cvxpy.reductions.canonicalization import Canonicalization |
| 23 | +from cvxpy.reductions.expr2smooth.canonicalizers import CANON_METHODS as smooth_canon_methods |
| 24 | +from cvxpy.reductions.inverse_data import InverseData |
| 25 | + |
| 26 | + |
| 27 | +class Expr2smooth(Canonicalization): |
| 28 | + """Reduce Expressions to an equivalent smooth program |
| 29 | +
|
| 30 | + This reduction takes as input (minimization) expressions and converts |
| 31 | + them into smooth expressions. |
| 32 | + """ |
| 33 | + def __init__(self, problem=None, quad_obj: bool = False) -> None: |
| 34 | + super(Canonicalization, self).__init__(problem=problem) |
| 35 | + self.smooth_canon_methods = smooth_canon_methods |
| 36 | + self.quad_obj = quad_obj |
| 37 | + |
| 38 | + def accepts(self, problem): |
| 39 | + """A problem is always accepted""" |
| 40 | + return True |
| 41 | + |
| 42 | + def apply(self, problem): |
| 43 | + """Converts an expr to a smooth program""" |
| 44 | + inverse_data = InverseData(problem) |
| 45 | + |
| 46 | + # smoothen objective function |
| 47 | + canon_objective, canon_constraints = self.canonicalize_tree( |
| 48 | + problem.objective, True) |
| 49 | + |
| 50 | + # smoothen constraints |
| 51 | + for constraint in problem.constraints: |
| 52 | + # canon_constr is the constraint re-expressed in terms of |
| 53 | + # its canonicalized arguments, and aux_constr are the constraints |
| 54 | + # generated while canonicalizing the arguments of the original |
| 55 | + # constraint |
| 56 | + canon_constr, aux_constr = self.canonicalize_tree( |
| 57 | + constraint, False) |
| 58 | + canon_constraints += aux_constr + [canon_constr] |
| 59 | + inverse_data.cons_id_map.update({constraint.id: canon_constr.id}) |
| 60 | + |
| 61 | + new_problem = problems.problem.Problem(canon_objective, |
| 62 | + canon_constraints) |
| 63 | + return new_problem, inverse_data |
| 64 | + |
| 65 | + def canonicalize_tree(self, expr, affine_above: bool) -> Tuple[Expression, list]: |
| 66 | + """Recursively canonicalize an Expression. |
| 67 | +
|
| 68 | + Parameters |
| 69 | + ---------- |
| 70 | + expr : The expression tree to canonicalize. |
| 71 | + affine_above : The path up to the root node is all affine atoms. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + A tuple of the canonicalized expression and generated constraints. |
| 76 | + """ |
| 77 | + # TODO don't copy affine expressions? |
| 78 | + affine_atom = type(expr) not in self.smooth_canon_methods |
| 79 | + canon_args = [] |
| 80 | + constrs = [] |
| 81 | + for arg in expr.args: |
| 82 | + canon_arg, c = self.canonicalize_tree(arg, affine_atom and affine_above) |
| 83 | + canon_args += [canon_arg] |
| 84 | + constrs += c |
| 85 | + canon_expr, c = self.canonicalize_expr(expr, canon_args, affine_above) |
| 86 | + constrs += c |
| 87 | + return canon_expr, constrs |
| 88 | + |
| 89 | + def canonicalize_expr(self, expr, args, affine_above: bool) -> Tuple[Expression, list]: |
| 90 | + """Canonicalize an expression, w.r.t. canonicalized arguments. |
| 91 | +
|
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + expr : The expression tree to canonicalize. |
| 95 | + args : The canonicalized arguments of expr. |
| 96 | + affine_above : The path up to the root node is all affine atoms. |
| 97 | +
|
| 98 | + Returns |
| 99 | + ------- |
| 100 | + A tuple of the canonicalized expression and generated constraints. |
| 101 | + """ |
| 102 | + # Constant trees are collapsed, but parameter trees are preserved. |
| 103 | + if isinstance(expr, Expression) and ( |
| 104 | + expr.is_constant() and not expr.parameters()): |
| 105 | + return expr, [] |
| 106 | + |
| 107 | + if type(expr) in self.smooth_canon_methods: |
| 108 | + return self.smooth_canon_methods[type(expr)](expr, args) |
| 109 | + |
| 110 | + return expr.copy(args), [] |
| 111 | + |
| 112 | +def example_max(): |
| 113 | + # Define variables |
| 114 | + x = cp.Variable(1) |
| 115 | + y = cp.Variable(1) |
| 116 | + |
| 117 | + objective = cp.Minimize(-cp.maximum(x,y)) |
| 118 | + |
| 119 | + constraints = [x - 14 == 0, y - 6 == 0] |
| 120 | + |
| 121 | + problem = cp.Problem(objective, constraints) |
| 122 | + return problem |
| 123 | + |
| 124 | +prob = example_max() |
| 125 | +new_problem, inverse = Expr2smooth(prob).apply(prob) |
| 126 | +print(new_problem) |
0 commit comments