Skip to content

Commit

Permalink
Added repr methods and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kstone40 committed Aug 22, 2024
1 parent 3f591cc commit 9a244e8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
27 changes: 20 additions & 7 deletions obsidian/objectives/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Product_Objective(Objective):
Args:
ind (tuple[int]): The indices of objectives to be used in a product
weights (tuple[float]): The weights corresponding to indexed objectives
weights (list[float | int], optional): The weights corresponding to
indexed objectives. Defaults to ``(1,)*len(ind)``.
const (float | int): A constant value that can be added to the product
new_dim (bool, optional): Whether to create a new objective dimension
from this product. Default is ``False``. Setting to ``True`` will
Expand All @@ -42,18 +43,25 @@ class Product_Objective(Objective):
"""
def __init__(self,
ind: tuple[int],
weights: tuple[float],
weights: list[float | int] | None = None,
const: float | int = 0,
new_dim: bool = True) -> None:
super().__init__(new_dim)
# Always MOO if dim is being added, always SOO otherwise
if weights is None:
weights = [1]*len(ind)
if len(weights) != len(ind):
raise ValueError('The length of weights and indices must be the same')
self.register_buffer('ind', torch.tensor(ind, dtype=torch.int))
self.register_buffer('weights', torch.tensor(weights, dtype=TORCH_DTYPE))
self.register_buffer('const', torch.tensor(const, dtype=TORCH_DTYPE))
self.register_buffer('new_dim', torch.tensor(new_dim, dtype=torch.bool))


def __repr__(self):
"""String representation of object"""
return f'{self.__class__.__name__} (ind={self.ind.tolist()}, \
weights={self.weights.tolist()}, const={self.const})'

def forward(self,
samples: Tensor,
X: Tensor | None = None) -> Tensor:
Expand All @@ -75,10 +83,10 @@ class Divide_Objective(Objective):
Args:
ind_num (int): The index of the objective to be used in the numerator
w_num (float | int, optional): The weights corresponding to numerator
w_num (float | int, optional): The weight corresponding to numerator
objective. Defaults to ``1``.
ind_denom (int): The index of the objective to be used in the denominator
w_denom (float | int, optional): The weights corresponding to denominator
w_denom (float | int, optional): The weight corresponding to denominator
objective. Defaults to ``1``.
const (float | int): A constant value that can be added to the quotient
new_dim (bool, optional): Whether to create a new objective dimension
Expand All @@ -103,6 +111,11 @@ def __init__(self,
self.register_buffer('const', torch.tensor(const, dtype=TORCH_DTYPE))
self.register_buffer('new_dim', torch.tensor(new_dim, dtype=torch.bool))

def __repr__(self):
"""String representation of object"""
return f'{self.__class__.__name__} (num={self.w_num} * {self.ind_num}, \
denom={self.w_denom} * {self.ind_denom}, const={self.const})'

def forward(self,
samples: Tensor,
X: Tensor | None = None) -> Tensor:
Expand All @@ -129,7 +142,7 @@ class Feature_Objective(Objective):
X_space (ParamSpace): The parameter space.
ind (tuple[int]): The indices of the parameters in the real space
to be used as features.
coeff (tuple[float | int], optional): The coefficients corresponding to each feature.
coeff (list[float | int], optional): The coefficients corresponding to each feature.
Defaults to ``[0]``.
Raises:
Expand All @@ -140,7 +153,7 @@ class Feature_Objective(Objective):
def __init__(self,
X_space: ParamSpace,
ind: tuple[int],
coeff: tuple[float | int]) -> None:
coeff: list[float | int]) -> None:

super().__init__(mo=True)

Expand Down
10 changes: 8 additions & 2 deletions obsidian/tests/test_objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
Objective_Sequence,
Utopian_Distance,
Index_Objective,
Bounded_Target
Bounded_Target,
Product_Objective,
Divide_Objective
)

from obsidian.tests.utils import DEFAULT_MOO_PATH, equal_state_dicts
Expand All @@ -32,9 +34,13 @@

test_objs = [Identity_Objective(mo=len(target) > 1),
Scalar_WeightedNorm(weights=[1, 1]),
Feature_Objective(X_space, ind=(0,), coeff=(1,)),
Feature_Objective(X_space, ind=(0,), coeff=[1]),
Objective_Sequence([Utopian_Distance([1], target[0]), Index_Objective()]),
Bounded_Target(bounds=[(0, 1)]*len(target), targets=target),
Objective_Sequence([Product_Objective(ind=(0,), weights=None, const=1, new_dim=True),
Divide_Objective(0, 1, new_dim=True),
Divide_Objective(0, 1, new_dim=False)]),
Product_Objective(ind=(0,), weights=[1], const=1, new_dim=False),
None]

utopian = Utopian_Distance(utopian=[10, 10], targets=target)
Expand Down

0 comments on commit 9a244e8

Please sign in to comment.