def intensify(self, challengers: typing.List[Configuration], incumbent: Configuration, run_history: RunHistory, aggregate_func: typing.Callable, time_bound: float = float(MAXINT), log_traj: bool = True): """Running intensification to determine the incumbent configuration. *Side effect:* adds runs to run_history Implementation of Procedure 2 in Hutter et al. (2011). Parameters ---------- challengers : typing.List[Configuration] promising configurations incumbent : Configuration best configuration so far run_history : RunHistory stores all runs we ran so far aggregate_func: typing.Callable aggregate error across instances time_bound : float, optional (default=2 ** 31 - 1) time in [sec] available to perform intensify log_traj: bool whether to log changes of incumbents in trajectory Returns ------- incumbent: Configuration() current (maybe new) incumbent configuration inc_perf: float empirical performance of incumbent configuration """ self.start_time = time.time() self._ta_time = 0 if time_bound < self._min_time: raise ValueError("time_bound must be >= %f" % (self._min_time)) self._num_run = 0 self._chall_indx = 0 # Line 1 + 2 if isinstance(challengers, ChallengerList): challengers = challengers.challengers L1 = 10 L2 = 10000 r = np.random.uniform() if 0.05 < r < 0.2 and len(challengers) > L1: target = np.random.randint(1, L1) challengers[target], challengers[0] = challengers[0], challengers[ target] if r < 0.05 and len(challengers) > L2: target = np.random.randint(L1, L2) challengers[target], challengers[0] = challengers[0], challengers[ target] for challenger in challengers: if not run_history.db.appointment_config(challenger): continue if challenger == incumbent: self.logger.debug( "Challenger was the same as the current incumbent; Skipping challenger" ) continue self.logger.debug("Intensify on %s", challenger) if hasattr(challenger, 'origin'): self.logger.debug("Configuration origin: %s", challenger.origin) try: # Lines 3-7 self._add_inc_run(incumbent=incumbent, run_history=run_history) # Lines 8-17 incumbent = self._race_challenger( challenger=challenger, incumbent=incumbent, run_history=run_history, aggregate_func=aggregate_func, log_traj=log_traj) if self.always_race_against and \ challenger == incumbent and \ self.always_race_against != challenger: self.logger.debug( "Race against constant configuration after incumbent change." ) incumbent = self._race_challenger( challenger=self.always_race_against, incumbent=incumbent, run_history=run_history, aggregate_func=aggregate_func, log_traj=log_traj) except BudgetExhaustedException: # We return incumbent, SMBO stops due to its own budget checks inc_perf = run_history.get_cost(incumbent) self.logger.debug("Budget exhausted; Return incumbent") return incumbent, inc_perf tm = time.time() if self._chall_indx >= self.min_chall: if self._num_run > self.run_limit: self.logger.debug( "Maximum #runs for intensification reached") break if not self.use_ta_time_bound and tm - self.start_time - time_bound >= 0: self.logger.debug( "Wallclock time limit for intensification reached (" "used: %f sec, available: %f sec)" % (tm - self.start_time, time_bound)) break elif self._ta_time - time_bound >= 0: self.logger.debug( "TA time limit for intensification reached (" "used: %f sec, available: %f sec)" % (self._ta_time, time_bound)) break # output estimated performance of incumbent inc_runs = run_history.get_runs_for_config(incumbent) inc_perf = aggregate_func(incumbent, run_history, inc_runs) self.logger.info( "Updated estimated cost of incumbent on %d runs: %.4f" % (len(inc_runs), inc_perf)) self.stats.update_average_configs_per_intensify( n_configs=self._chall_indx) return incumbent, inc_perf
from dsmac.runhistory.runhistory import RunHistoryDB from dsmac.runhistory.utils import get_id_of_config from dsmac.tae.execute_ta_run import StatusType if __name__ == '__main__': from ConfigSpace import ConfigurationSpace import joblib from dsmac.optimizer.objective import average_cost from dsmac.runhistory.runhistory import RunHistory runhistory = RunHistory(average_cost,db_args="test.db") cs: ConfigurationSpace = joblib.load("/home/tqc/PycharmProjects/auto-pipeline/test/php.bz2") runhistory.load_json( "/home/tqc/PycharmProjects/auto-pipeline/test/test_runhistory/default_dataset_name/smac_output/runhistory.json", cs) all_configs = (runhistory.get_all_configs()) config = all_configs[0] config_id = get_id_of_config(config) cost = runhistory.get_cost(config) db = RunHistoryDB(cs, runhistory, "test.db") db.delete_all() ans = db.appointment_config(config) print(ans) db.insert_runhistory(config, cost, 0.1, StatusType.SUCCESS) db2 = RunHistoryDB(cs, runhistory, "test.db") db2.insert_runhistory(all_configs[1], runhistory.get_cost(all_configs[1]), 0.1, StatusType.SUCCESS) db.fetch_new_runhistory()