Append a point and its target value to the known data. Parameters ---------- params : np.ndarray a single point, with len(x) == self.dim. target : float target function value constraint_value : float or np.ndarray or None
(
self, params: ParamsType, target: float, constraint_value: float | NDArray[Float] | None = None
)
| 422 | return x |
| 423 | |
| 424 | def register( |
| 425 | self, params: ParamsType, target: float, constraint_value: float | NDArray[Float] | None = None |
| 426 | ) -> None: |
| 427 | """Append a point and its target value to the known data. |
| 428 | |
| 429 | Parameters |
| 430 | ---------- |
| 431 | params : np.ndarray |
| 432 | a single point, with len(x) == self.dim. |
| 433 | |
| 434 | target : float |
| 435 | target function value |
| 436 | |
| 437 | constraint_value : float or np.ndarray or None |
| 438 | Constraint function value |
| 439 | |
| 440 | Raises |
| 441 | ------ |
| 442 | NotUniqueError: |
| 443 | if the point is not unique |
| 444 | |
| 445 | Notes |
| 446 | ----- |
| 447 | runs in amortized constant time |
| 448 | |
| 449 | Examples |
| 450 | -------- |
| 451 | >>> target_func = lambda p1, p2: p1 + p2 |
| 452 | >>> pbounds = {"p1": (0, 1), "p2": (1, 100)} |
| 453 | >>> space = TargetSpace(target_func, pbounds) |
| 454 | >>> len(space) |
| 455 | 0 |
| 456 | >>> x = np.array([0, 0]) |
| 457 | >>> y = 1 |
| 458 | >>> space.register(x, y) |
| 459 | >>> len(space) |
| 460 | 1 |
| 461 | """ |
| 462 | x = self._as_array(params) |
| 463 | |
| 464 | if x in self: |
| 465 | if self._allow_duplicate_points: |
| 466 | self.n_duplicate_points = self.n_duplicate_points + 1 |
| 467 | |
| 468 | print( |
| 469 | Fore.RED + f"Data point {x} is not unique. {self.n_duplicate_points}" |
| 470 | " duplicates registered. Continuing ..." + Fore.RESET |
| 471 | ) |
| 472 | else: |
| 473 | error_msg = ( |
| 474 | f"Data point {x} is not unique. You can set" |
| 475 | ' "allow_duplicate_points=True" to avoid this error' |
| 476 | ) |
| 477 | raise NotUniqueError(error_msg) |
| 478 | |
| 479 | # if x is not within the bounds of the parameter space, warn the user |
| 480 | if self._bounds is not None and not np.all((self._bounds[:, 0] <= x) & (x <= self._bounds[:, 1])): |
| 481 | for key in self.keys: |