예제 #1
0
 def testSampleHyperspherePositiveQuadrant(self):
     for d in range(1, 10):
         self.assertTrue(
             sample_hypersphere_positive_quadrant(d).norm().isclose(
                 torch.tensor([1.0], dtype=torch.double)),
             "sampled hypersphere point's norm is not 1.0",
         )
예제 #2
0
def _extract_random_scalarization_settings(
    objective_weights: Tensor,
    outcome_constraints: Optional[Tuple[Tensor, Tensor]] = None,
    **kwargs: Any,
) -> Tensor:
    """Generate a random weighting based on scalarization settings."""
    use_random_scalarization = kwargs.get("random_scalarization", False)
    random_weights = None
    if use_random_scalarization:
        # Pareto Optimization incompatible with outcome constraints.
        if outcome_constraints is not None:
            raise ValueError(
                "Random scalarization for pareto frontier exploration "
                "is incompatible with outcome constraints. Remove one.")
        # Set distribution and sample weights.
        distribution = kwargs.get("random_scalarization_distribution", SIMPLEX)
        if distribution == SIMPLEX:
            random_weights = sample_simplex(len(objective_weights))
        elif distribution == HYPERSPHERE:
            random_weights = sample_hypersphere_positive_quadrant(
                len(objective_weights))

    if random_weights is not None:
        objective_weights = torch.mul(objective_weights, random_weights)
    return objective_weights