def _instantiate_config(self, config): """Check before dispatching experiment whether configuration corresponds to a executable experiment environment. 1. Check `refers` and instantiate `Adapter` objects from it. 2. Try to build parameter space from user arguments. 3. Check whether configured algorithms correspond to [known]/valid implementations of the ``Algorithm`` class. Instantiate these objects. 4. Check if experiment `is_done`, prompt for larger `max_trials` if it is. (TODO) """ # Just overwrite everything else given for section, value in config.items(): if section not in self.__slots__: log.info( "Found section '%s' in configuration. Experiments " "do not support this option. Ignoring.", section) continue if section.startswith('_'): log.info( "Found section '%s' in configuration. " "Cannot set private attributes. Ignoring.", section) continue # Copy sub configuration to value confusing side-effects # Only copy at this level, not `config` directly to avoid TypeErrors if config contains # non-serializable objects (copy.deepcopy complains otherwise). if isinstance(value, dict): value = copy.deepcopy(value) setattr(self, section, value) try: space_builder = SpaceBuilder() space = space_builder.build_from(config['metadata']['user_args']) if not space: raise ValueError( "Parameter space is empty. There is nothing to optimize.") # Instantiate algorithms self.algorithms = PrimaryAlgo(space, self.algorithms) except KeyError: pass if self.refers and not isinstance(self.refers.get('adapter'), BaseAdapter): self.refers['adapter'] = Adapter.build(self.refers['adapter']) if not self.producer.get('strategy'): self.producer = { 'strategy': Strategy(of_type="MaxParallelStrategy") } elif not isinstance(self.producer.get('strategy'), BaseParallelStrategy): self.producer = { 'strategy': Strategy(of_type=self.producer['strategy']) }
def _instantiate_strategy(config=None, ignore_unavailable=False): """Instantiate the strategy object Parameters ---------- config: dict, optional Configuration of the strategy. If None of empty, system's defaults are used (orion.core.config.producer.strategy). ignore_unavailable: bool, optional If True and algorithm is not available (plugin not installed), return the configuration. Otherwise, raise Factory error from PrimaryAlgo """ if not config: config = orion.core.config.experiment.strategy if isinstance(config, str): strategy_type = config config = {} else: config = copy.deepcopy(config) strategy_type, config = next(iter(config.items())) try: strategy = Strategy(of_type=strategy_type, **config) except NotImplementedError as e: if not ignore_unavailable: raise e log.warning(str(e)) log.warning("Strategy will not be instantiated.") strategy = {strategy_type: config} return strategy
def test_handle_corrupted_trials(caplog, strategy, corrupted_trial): """Verify that corrupted trials are handled properly""" with caplog.at_level(logging.WARNING, logger="orion.core.worker.strategy"): lie = Strategy(strategy).lie(corrupted_trial) match = "Trial `{}` has an objective but status is not completed".format( corrupted_trial.id) assert match in caplog.text assert lie is not None assert lie.value == corrupted_trial.objective.value
def _instantiate_strategy(config=None): """Instantiate the strategy object Parameters ---------- config: dict, optional Configuration of the strategy. If None of empty, system's defaults are used (orion.core.config.producer.strategy). """ if not config: config = orion.core.config.experiment.strategy if isinstance(config, str): strategy_type = config config = {} else: strategy_type, config = next(iter(config.items())) return Strategy(of_type=strategy_type, **config)
def test_create_meanparallel(self): """Test creating a MeanParallelStrategy class""" strategy = Strategy('MeanParallelStrategy') assert isinstance(strategy, MeanParallelStrategy)
def test_create_noparallel(self): """Test creating a NoParallelStrategy class""" strategy = Strategy('NoParallelStrategy') assert isinstance(strategy, NoParallelStrategy)
def test_handle_uncorrupted_trials(caplog, strategy, incomplete_trial): """Verify that no warning is logged if trial is valid""" with caplog.at_level(logging.WARNING, logger="orion.core.worker.strategy"): Strategy(strategy).lie(incomplete_trial) assert "Trial `{}` has an objective but status is not completed" not in caplog.text