|
26 | 26 | from math import copysign |
27 | 27 | from numpy.random import default_rng |
28 | 28 |
|
29 | | -from .samplers import LatinHypercubeSampler |
30 | | - |
31 | 29 | try: |
32 | 30 | from tqdm.auto import tqdm as _tqdm |
33 | 31 | _tqdm = partial(_tqdm, leave=False) |
@@ -1257,6 +1255,7 @@ def optimize(self, *, |
1257 | 1255 | return_optimization: bool = False, |
1258 | 1256 | random_state: Optional[int] = None, |
1259 | 1257 | n_initial_points: Optional[int] = None, |
| 1258 | + n_workers: int = 1, |
1260 | 1259 | **kwargs) -> Union[pd.Series, |
1261 | 1260 | Tuple[pd.Series, pd.Series], |
1262 | 1261 | Tuple[pd.Series, pd.Series, dict]]: |
@@ -1546,7 +1545,8 @@ def _optimize_openbox() -> Union[pd.Series, |
1546 | 1545 | Tuple[pd.Series, pd.Series], |
1547 | 1546 | Tuple[pd.Series, pd.Series, dict]]: |
1548 | 1547 | try: |
1549 | | - from openbox import space as sp, Optimizer |
| 1548 | + from openbox import space as sp, Optimizer, ParallelOptimizer |
| 1549 | + from .samplers import LatinHypercubeSampler |
1550 | 1550 | except ImportError: |
1551 | 1551 | raise ImportError("Need package 'openbox' for method='openbox'. " |
1552 | 1552 | "pip install openbox") from None |
@@ -1609,20 +1609,31 @@ def eval_run(config: sp.Configuration): |
1609 | 1609 | logging.warning(f'Only {len(valid_initial_configs)}/{len(initial_configs)} valid configurations are generated for initial design strategy "Latin Hypercube". ') |
1610 | 1610 | num_random_config = n_initial_points - len(valid_initial_configs) |
1611 | 1611 | valid_initial_configs += Advisor.sample_random_configs(space, num_random_config, excluded_configs=valid_initial_configs) |
1612 | | - opt = Optimizer( |
1613 | | - eval_run, |
1614 | | - space, |
1615 | | - num_constraints=1, |
1616 | | - num_objectives=1, |
1617 | | - surrogate_type='auto', |
1618 | | - acq_type='auto', |
1619 | | - acq_optimizer_type='auto', |
1620 | | - max_runs=max_tries, |
1621 | | - task_id='soc', |
1622 | | - random_state=random_state, |
1623 | | - initial_configurations=valid_initial_configs, |
1624 | | - initial_runs=initial_runs, |
1625 | | - ) |
| 1612 | + |
| 1613 | + params = { |
| 1614 | + "objective_function": eval_run, |
| 1615 | + "config_space": space, |
| 1616 | + "num_constraints": 1, |
| 1617 | + "num_objectives": 1, |
| 1618 | + "surrogate_type": 'auto', |
| 1619 | + "acq_type": 'auto', |
| 1620 | + "acq_optimizer_type": 'auto', |
| 1621 | + "max_runs": max_tries, |
| 1622 | + "task_id": 'soc', |
| 1623 | + "random_state": random_state, |
| 1624 | + "initial_configurations": valid_initial_configs, |
| 1625 | + "initial_runs": initial_runs, |
| 1626 | + } |
| 1627 | + |
| 1628 | + if n_workers == 1: |
| 1629 | + opt = Optimizer(**params) |
| 1630 | + else: |
| 1631 | + opt = ParallelOptimizer( |
| 1632 | + parallel_strategy="async", |
| 1633 | + batch_size=n_workers, |
| 1634 | + batch_strategy="default", |
| 1635 | + **params |
| 1636 | + ) |
1626 | 1637 | history = opt.run() |
1627 | 1638 | optimal_configurations = history.get_incumbents() |
1628 | 1639 | if not len(optimal_configurations): |
|
0 commit comments