Skip to content

Commit

Permalink
Make MinimizeMaximize inherit from Canonical
Browse files Browse the repository at this point in the history
  • Loading branch information
phschiele committed Feb 23, 2023
1 parent f7c8d0d commit 23a1a71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 3 additions & 4 deletions dsp/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import cvxpy as cp
import numpy as np
from cvxpy.atoms.atom import Atom
from cvxpy.constraints.constraint import Constraint
from cvxpy.problems.objective import Objective
from cvxpy.utilities import Canonical
Expand All @@ -20,10 +19,10 @@
from dsp.saddle_extremum import SaddleExtremum


class MinimizeMaximize:
class MinimizeMaximize(Canonical):
def __init__(self, expr: cp.Expression) -> None:
self._validate_arguments(expr)
self.expr = Atom.cast_to_const(expr)
self.args = [cp.Expression.cast_to_const(expr)]

@staticmethod
def _validate_arguments(expr: cp.Expression | float | int) -> None:
Expand All @@ -45,7 +44,7 @@ def value(self) -> float:
class SaddlePointProblem(cp.Problem):
def __init__(
self,
minmax_objective: MinimizeMaximize | Objective,
minmax_objective: MinimizeMaximize,
constraints: list[Constraint] | None = None,
minimization_vars: Iterable[cp.Variable] | None = None,
maximization_vars: Iterable[cp.Variable] | None = None,
Expand Down
14 changes: 14 additions & 0 deletions tests/test_minimizemaximize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cvxpy as cp
import pytest

import dsp
from dsp.problem import MinimizeMaximize


Expand All @@ -17,3 +18,16 @@ def test_value():
def test_invalid_argument():
with pytest.raises(TypeError):
MinimizeMaximize("hello")


def test_properties():
x = cp.Variable()
y = cp.Variable()
f = dsp.saddle_inner(x, y)

obj = MinimizeMaximize(f)
assert obj.is_dsp()
assert obj.value is None
assert obj.variables() == [x, y]
assert obj.parameters() == []
assert obj.atoms() == [dsp.saddle_inner]

0 comments on commit 23a1a71

Please sign in to comment.