def fetch_new_runhistory(self, is_init=False): if is_init: n_del = self.Model.delete().where(self.Model.origin < 0).execute() if n_del > 0: self.logger.info(f"Delete {n_del} invalid records in run_history database.") query = self.Model.select().where(self.Model.origin >= 0) else: query = self.Model.select().where(self.Model.pid != os.getpid()).where(self.Model.origin >= 0) for model in query: run_id = model.run_id config_id = model.config_id config = model.config config_bin = model.config_bin config_origin = model.config_origin cost = model.cost time = model.time instance_id = model.instance_id seed = model.seed status = model.status additional_info = model.additional_info origin = model.origin timestamp = model.timestamp try: config = pickle.loads(config_bin) except Exception as e: self.logger.error(f"{e}\nUsing config json instead to build Configuration.") config = Configuration(self.config_space, values=config, origin=config_origin) try: additional_info = json.loads(additional_info) except Exception as e: self.logger.error(f"{e}\nSet default to additional_info.") additional_info = {} self.runhistory.add(config, cost, time, StatusType(status), instance_id, seed, additional_info, DataOrigin(origin)) self.timestamp = datetime.datetime.now()
def fetch_new_runhistory(self, is_init=False): if is_init: query = self.Model.select().where(self.Model.origin >= 0) else: query = self.Model.select().where(self.Model.pid != os.getpid()).where(self.Model.origin >= 0) config_cost=[] for model in query: config_id = model.config_id config = model.config config_bit = model.config_bit config_origin = model.config_origin cost = model.cost time = model.time instance_id = model.instance_id seed = model.seed status = model.status additional_info = model.additional_info origin = model.origin timestamp = model.timestamp try: config = pickle.loads(config_bit) except: config = Configuration(self.config_space, values=json.loads(config), origin=config_origin) try: additional_info = json.loads(additional_info) except Exception: pass if not self.runhistory.ids_config.get(config_id): config_cost.append([config,cost]) self.runhistory.add(config, cost, time, StatusType(status), instance_id, seed, additional_info, DataOrigin(origin)) self.timestamp = datetime.datetime.now() return config_cost
def load_json(self, fn: str, cs: ConfigurationSpace): """Load and runhistory in json representation from disk. Overwrites current runhistory! Parameters ---------- fn : str file name to load from cs : ConfigSpace instance of configuration space """ try: txt = self.file_system.read_txt(fn) all_data = json.loads(txt, object_hook=StatusType.enum_hook) except Exception as e: self.logger.warning( 'Encountered exception %s while reading runhistory from %s. ' 'Not adding any runs!', e, fn, ) return config_origins = all_data.get("config_origins", {}) self.ids_config = {} self.ids_config = {(id_): Configuration(cs, values=values, origin=config_origins.get(id_, None)) for id_, values in all_data["configs"].items()} self.config_ids = { config: id_ for id_, config in self.ids_config.items() } self._n_id = len(self.config_ids) # important to use add method to use all data structure correctly for k, v in all_data["data"]: id_ = (k[0]) if id_ in self.ids_config: self.add(config=self.ids_config[id_], cost=float(v[0]), time=float(v[1]), status=StatusType(v[2]), instance_id=k[1], seed=int(k[2]), additional_info=v[3])