Exemple #1
0
def _constraint_solver(parameter: p.Parameter, budget: int) -> p.Parameter:
    """Runs a suboptimization to solve the parameter constraints"""
    parameter_without_constraint = parameter.copy()
    parameter_without_constraint._constraint_checkers.clear()
    opt = registry["OnePlusOne"](parameter_without_constraint,
                                 num_workers=1,
                                 budget=budget)
    for _ in range(budget):
        cand = opt.ask()
        # Our objective function is minimum for the point the closest to
        # the original candidate under the constraints.
        penalty = sum(
            utils._float_penalty(func(cand.value))
            for func in parameter._constraint_checkers)

        # TODO: this may not scale well with dimension
        distance = np.tanh(
            np.sum(cand.get_standardized_data(reference=parameter)**2))
        # TODO: because of the return whenever constraints are satisfied, the first case never arises
        loss = distance if penalty <= 0 else penalty + distance + 1.0
        opt.tell(cand, loss)
        if penalty <= 0:  # constraints are satisfied
            break
    data = opt.recommend().get_standardized_data(
        reference=parameter_without_constraint)
    return parameter.spawn_child().set_standardized_data(data)
Exemple #2
0
 def __init__(self, reference: p.Parameter) -> None:
     self.reference = reference.spawn_child()
     self.reference.freeze()
     # initial check
     parameter = self.reference.spawn_child()
     parameter.set_standardized_data(
         np.linspace(-1, 1, self.reference.dimension))
     expected = parameter.get_standardized_data(reference=self.reference)
     self._ref_arrays = self.list_arrays(self.reference)
     arrays = self.list_arrays(parameter)
     check = np.concatenate([
         x.get_standardized_data(reference=y)
         for x, y in zip(arrays, self._ref_arrays)
     ],
                            axis=0)
     self.working = True
     if not np.allclose(check, expected):
         self.working = False
         self._warn()