Example #1
0
File: NS.py Project: salehiac/BR-NS
    def __init__(self,
                 archive,
                 nov_estimator,
                 mutator,
                 problem,
                 selector,
                 n_pop,
                 n_offspring,
                 agent_factory,
                 visualise_bds_flag,
                 map_type="scoop",
                 logs_root="/tmp/ns_log/",
                 compute_parent_child_stats=False):
        """
        archive               Archive           object implementing the Archive interface. Can be None if novelty is LearnedNovelty1d/LearnedNovelty2d
        nov_estimator         NoveltyEstimator  object implementing the NoveltyEstimator interface. 
        problem               Problem           object that provides 
                                                     - __call__ function taking individual_index returning (fitness, behavior_descriptors, task_solved_or_not)
                                                     - a dist_thresh (that is determined from its bds) which specifies the minimum distance that should separate a point x from
                                                       its nearest neighbour in the archive+pop in order for the point to be considered as novel. It is also used as a threshold on novelty
                                                       when updating the archive.
                                                    - optionally, a visualise_bds function.
        mutator               Mutator
        selector              function
        n_pop                 int 
        n_offspring           int           
        agent_factory         function          
        visualise_bds_flag    int               
        map_type              string            different options for sequential/parallel mapping functions. supported values currently are 
                                                "scoop" distributed map from futures.map
                                                "std"   buildin python map
        logs_root             str               the logs diretory will be created inside logs_root
        """
        self.archive = archive
        if archive is not None:
            self.archive.reset()

        self.nov_estimator = nov_estimator
        self.problem = problem

        self.map_type = map_type
        self._map = futures.map if map_type == "scoop" else map
        print(
            colored("[NS info] Using map_type " + map_type,
                    "green",
                    attrs=["bold"]))

        self.mutator = mutator
        self.selector = selector

        self.n_offspring = n_offspring
        self.agent_factory = agent_factory

        initial_pop = [self.agent_factory() for i in range(n_pop)]
        initial_pop = self.generate_new_agents(initial_pop, generation=0)

        self._initial_pop = copy.deepcopy(initial_pop)

        assert n_offspring >= len(
            initial_pop), "n_offspring should be larger or equal to n_pop"

        self.visualise_bds_flag = visualise_bds_flag

        if os.path.isdir(logs_root):
            self.logs_root = logs_root
            self.log_dir_path = MiscUtils.create_directory_with_pid(
                dir_basename=logs_root + "/NS_log_",
                remove_if_exists=True,
                no_pid=False)
            print(
                colored("[NS info] NS log directory was created: " +
                        self.log_dir_path,
                        "green",
                        attrs=["bold"]))
        else:
            raise Exception(
                "Root dir for logs not found. Please ensure that it exists before launching the script."
            )

        self.task_solvers = {}

        self.compute_parent_child_stats = compute_parent_child_stats

        self.save_archive_to_file = True
        self.disable_tqdm = False