Optimization (minimization) procedure Parameters ---------- objective_function: callable A callable to optimize (minimize) executor: Executor An executor object, with method :code:`submit(callable, *args, **kwargs)` and returning a Future-like
(
self,
objective_function: tp.Callable[..., tp.Loss],
executor: tp.Optional[tp.ExecutorLike] = None,
batch_mode: bool = False,
verbosity: int = 0,
constraint_violation: tp.Any = None,
)
| 609 | """ |
| 610 | |
| 611 | def minimize( |
| 612 | self, |
| 613 | objective_function: tp.Callable[..., tp.Loss], |
| 614 | executor: tp.Optional[tp.ExecutorLike] = None, |
| 615 | batch_mode: bool = False, |
| 616 | verbosity: int = 0, |
| 617 | constraint_violation: tp.Any = None, |
| 618 | ) -> p.Parameter: |
| 619 | """Optimization (minimization) procedure |
| 620 | |
| 621 | Parameters |
| 622 | ---------- |
| 623 | objective_function: callable |
| 624 | A callable to optimize (minimize) |
| 625 | executor: Executor |
| 626 | An executor object, with method :code:`submit(callable, *args, **kwargs)` and returning a Future-like object |
| 627 | with methods :code:`done() -> bool` and :code:`result() -> float`. The executor role is to dispatch the execution of |
| 628 | the jobs locally/on a cluster/with multithreading depending on the implementation. |
| 629 | Eg: :code:`concurrent.futures.ProcessPoolExecutor` |
| 630 | batch_mode: bool |
| 631 | when :code:`num_workers = n > 1`, whether jobs are executed by batch (:code:`n` function evaluations are launched, |
| 632 | we wait for all results and relaunch n evals) or not (whenever an evaluation is finished, we launch |
| 633 | another one) |
| 634 | verbosity: int |
| 635 | print information about the optimization (0: None, 1: fitness values, 2: fitness values and recommendation) |
| 636 | constraint_violation: list of functions or None |
| 637 | each function in the list returns >0 for a violated constraint. |
| 638 | |
| 639 | Returns |
| 640 | ------- |
| 641 | ng.p.Parameter |
| 642 | The candidate with minimal value. :code:`ng.p.Parameters` have field :code:`args` and :code:`kwargs` which can |
| 643 | be directly used on the function (:code:`objective_function(*candidate.args, **candidate.kwargs)`). |
| 644 | |
| 645 | Note |
| 646 | ---- |
| 647 | for evaluation purpose and with the current implementation, it is better to use batch_mode=True |
| 648 | """ |
| 649 | # pylint: disable=too-many-branches |
| 650 | if self.budget is None: |
| 651 | raise ValueError("Budget must be specified") |
| 652 | if executor is None: |
| 653 | executor = utils.SequentialExecutor() # defaults to run everything locally and sequentially |
| 654 | if self.num_workers > 1: |
| 655 | self._warn( |
| 656 | f"num_workers = {self.num_workers} > 1 is suboptimal when run sequentially", |
| 657 | errors.InefficientSettingsWarning, |
| 658 | ) |
| 659 | assert executor is not None |
| 660 | tmp_runnings: tp.List[tp.Tuple[p.Parameter, tp.JobLike[tp.Loss]]] = [] |
| 661 | tmp_finished: tp.Deque[tp.Tuple[p.Parameter, tp.JobLike[tp.Loss]]] = deque() |
| 662 | # go |
| 663 | sleeper = ngtools.Sleeper() # manages waiting time depending on execution time of the jobs |
| 664 | remaining_budget = self.budget - self.num_ask |
| 665 | first_iteration = True |
| 666 | # |
| 667 | while remaining_budget or self._running_jobs or self._finished_jobs: |
| 668 | # # # # # Update optimizer with finished jobs # # # # # |