def run(self, n=1, initial_configurations=[], nb_iter_to_generate_img=-1): start_run = time.time() with Timeout(int(self.time_budget - (start_run - time.time()))): try: self.logger.info("Run default configuration") self.env.run_default_configuration() if len(initial_configurations) > 0: self.logger.info("Run initial configurations") else: self.logger.info("No initial configuration to run.") for i in range(n): if time.time() - self.env.start_time < self.time_budget: res, config = self.MCT_SEARCH() if res > self.bestscore: self.bestscore = res self.bestconfig = config else: return 0 if nb_iter_to_generate_img == -1 or i % nb_iter_to_generate_img == 0: self.tree.draw_tree( os.path.join(self.exec_dir, "images")) self.print_tree("tree_{0}".format(i)) except Timeout.Timeout: self.logger.info("Budget exhausted.") return 0
def run(self, n=1, intial_configuration=[], generate_image_path=""): start_run = time.time() with Timeout(int(self.time_budget - (start_run - time.time()))): try: self.env.run_default_configuration() self.env.check_time() if len(intial_configuration) > 0: executed_config = self.env.run_default_all() id_class = self.create_node_for_algorithm() score_each_cl = self.env.run_initial_configuration( intial_configuration, executed_config) for cl, vals in score_each_cl.items(): if len(vals) > 0: [self.BACKUP(id_class[cl], s) for s in vals] else: self.env.check_time() self.env.run_main_configuration() for i in range(n): if time.time() - self.env.start_time < self.time_budget: self.MCT_SEARCH() if self.exec_dir != "": img_dir = os.path.join(self.exec_dir, "images") if not os.path.exists(img_dir): os.makedirs(img_dir) self.tree.draw_tree( os.path.join(img_dir, "step_%s" % i)) else: return 0 gc.collect() except Timeout.Timeout: return 0
def run(self, nb_simulation=1, initial_configurations=[], step_to_generate_img=-1): """Run MCTS algorithm Parameters: ---------- nb_simulation: int number of MCTS simulation to run (default is 10) initial_configurations: list of object set of configuration to start with (default is []) step_to_generate_img: int or None set of initial configuration (default -1, generate image for each MCTS iteration) Do not generate images if None. Returns: ---------- int 1 if timeout else 0 """ start_run = time.time() with Timeout(int(self.time_budget - (start_run - time.time()))): try: self.logger.info("Run default configuration") self.env.run_default_configuration() if len(initial_configurations) > 0: self.logger.info("Run initial configurations") else: self.logger.info("No initial configuration to run.") for i in range(nb_simulation): if time.time() - self.env.start_time < self.time_budget: res, config = self.MCT_SEARCH() if res > self.best_score: self.best_score = res self.best_config = config else: return 0 if step_to_generate_img == -1 or i % step_to_generate_img == 0: self.tree.draw_tree( os.path.join(self.exec_dir, "images")) self.print_tree("tree_{0}".format(i)) except Timeout.Timeout: self.logger.info("Budget exhausted.") return 1
def run(self, n = 1, initial_configurations = [], nb_iter_to_generate_img = ""): start_run = time.time() with Timeout(int(self.time_budget - (start_run - time.time()))): try: self.logger.info("Run default configuration") self.bestconfig, self.bestscore = self.env.run_default_configuration() self.env.check_time() if len(initial_configurations) > 0: self.logger.info("Run initial configurations") executed_config = self.env.run_default_all() score_each_cl = self.env.run_initial_configuration(initial_configurations, executed_config) else: self.logger.info("Run random intialization.") self.env.check_time() score_each_cl = self.env.run_main_configuration() id_class = self.create_node_for_algorithm() for cl, vals in score_each_cl.items(): if len(vals) > 0: [self.BACKUP(id_class[cl], s) for s in vals] for i in range(n): if time.time() - self.env.start_time < self.time_budget: res, config = self.MCT_SEARCH() if res > self.bestscore: self.bestscore = res self.bestconfig = config else: self.logger.info("Budget exhausted.") return 0 if nb_iter_to_generate_img == -1 or i % nb_iter_to_generate_img == 0: self.tree.draw_tree( os.path.join(self.exec_dir, "images")) except Timeout.Timeout: self.logger.info("Budget exhausted.") return 0
def check_time(self): if time.time() - self.start_time < 3600: return True else: raise Timeout.Timeout("Finished")