Skip to content

Commit be57b28

Browse files
typing: Fix ty invalid-attribute-override on SPSMapping (#828)
1 parent c9d1a82 commit be57b28

1 file changed

Lines changed: 35 additions & 30 deletions

File tree

‎tests/trajectories/_objectives.py‎

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,23 @@ def __repr__(self) -> str:
2828
return f"{self.__class__.__name__}({self.n_values})"
2929

3030

31+
class SPSMapping(ABC):
32+
"""Strong Pareto stationary mapping of an objective."""
33+
34+
@abstractmethod
35+
def __call__(self, w: Tensor) -> Tensor:
36+
"""
37+
Map a vector with (strictly) positive coordinates to the corresponding strongly pareto
38+
stationary point.
39+
"""
40+
41+
3142
class WithSPSMappingMixin(ABC):
3243
"""Mixin adding the possibility to get the Strong Pareto stationary mapping."""
3344

34-
class SPSMapping(ABC):
35-
@abstractmethod
36-
def __call__(self, w: Tensor) -> Tensor:
37-
"""
38-
Map a vector with (strictly) positive coordinates to the corresponding strongly pareto
39-
stationary point.
40-
"""
41-
4245
@property
4346
@abstractmethod
44-
def sps_mapping(self) -> "WithSPSMappingMixin.SPSMapping":
47+
def sps_mapping(self) -> SPSMapping:
4548
pass
4649

4750

@@ -73,21 +76,22 @@ def quad(x: Tensor, A: Tensor, u: Tensor) -> Tensor:
7376
x_minus_u = x - u
7477
return x_minus_u @ A @ x_minus_u
7578

76-
class SPSMapping(WithSPSMappingMixin.SPSMapping):
77-
def __init__(self, As: list[Tensor], us: list[Tensor]) -> None:
78-
self.As = As
79-
self.us = us
79+
@property
80+
def sps_mapping(self) -> "QuadraticSPSMapping":
81+
return QuadraticSPSMapping(self.As, self.us)
8082

81-
def __call__(self, w: Tensor) -> Tensor:
82-
G = torch.stack([weight * A for weight, A in zip(w, self.As, strict=False)]).sum(dim=0)
83-
b = torch.stack(
84-
[weight * A @ u for weight, A, u in zip(w, self.As, self.us, strict=False)]
85-
).sum(dim=0)
86-
return torch.linalg.lstsq(G, b, driver="gelsd").solution
8783

88-
@property
89-
def sps_mapping(self) -> "QuadraticFunction.SPSMapping":
90-
return self.SPSMapping(self.As, self.us)
84+
class QuadraticSPSMapping(SPSMapping):
85+
def __init__(self, As: list[Tensor], us: list[Tensor]) -> None:
86+
self.As = As
87+
self.us = us
88+
89+
def __call__(self, w: Tensor) -> Tensor:
90+
G = torch.stack([weight * A for weight, A in zip(w, self.As, strict=False)]).sum(dim=0)
91+
b = torch.stack(
92+
[weight * A @ u for weight, A, u in zip(w, self.As, self.us, strict=False)]
93+
).sum(dim=0)
94+
return torch.linalg.lstsq(G, b, driver="gelsd").solution
9195

9296

9397
class HomogenousQuadraticFunction(QuadraticFunction):
@@ -113,13 +117,14 @@ def __call__(self, x: Tensor) -> Tensor:
113117
def jacobian(self, x: Tensor) -> Tensor:
114118
return torch.diag(torch.stack([2 * x[0], 2 * x[1]]))
115119

116-
class SPSMapping(WithSPSMappingMixin.SPSMapping):
117-
def __init__(self, n_values: int) -> None:
118-
self.n_values = n_values
120+
@property
121+
def sps_mapping(self) -> "ElementWiseQuadraticSPSMapping":
122+
return ElementWiseQuadraticSPSMapping(self.n_values)
119123

120-
def __call__(self, w: Tensor) -> Tensor: # noqa: ARG002
121-
return torch.zeros(self.n_values)
122124

123-
@property
124-
def sps_mapping(self) -> "ElementWiseQuadratic.SPSMapping":
125-
return self.SPSMapping(self.n_values)
125+
class ElementWiseQuadraticSPSMapping(SPSMapping):
126+
def __init__(self, n_values: int) -> None:
127+
self.n_values = n_values
128+
129+
def __call__(self, w: Tensor) -> Tensor: # noqa: ARG002
130+
return torch.zeros(self.n_values)

0 commit comments

Comments
 (0)