コード例 #1
0
    def run(self, max_hours_runtime=29, max_gens=3000, num_random_individuals=1, num_env_cycles=0,
            directory="tests_data", name="TestRun", max_eval_time=60, time_to_try_again=30, checkpoint_every=100,
            save_vxa_every=100, save_pareto=False, save_nets=False, save_lineages=False,
            continued_from_checkpoint=False, batch_size=None, update_survivors_age=True, max_fitness=None):

        if self.autosuspended:
            sub.call("rm %s/AUTOSUSPENDED" % directory, shell=True)

        self.autosuspended = False
        self.max_gens = max_gens  # can add additional gens through checkpointing

        print_log = PrintLog()
        print_log.add_timer("evaluation")
        self.start_time = print_log.timers["start"]  # sync start time with logging

        if not continued_from_checkpoint:  # generation zero
            self.directory = directory
            self.name = name
            self.num_random_inds = num_random_individuals
            self.num_env_cycles = num_env_cycles

            initialize_folders(self.pop, self.directory, self.name, save_nets, save_lineages=save_lineages)
            sub.call("touch {}/RUNNING".format(self.directory), shell=True)

        while self.pop.gen < max_gens:

            if self.pop.gen % checkpoint_every == 0:
                print_log.message("Saving checkpoint at generation {0}".format(self.pop.gen + 1), timer_name="start")
                self.save_checkpoint(self.directory, self.pop.gen)

            if self.elapsed_time(units="h") > max_hours_runtime or self.pop.best_fit_so_far == max_fitness:
                self.autosuspended = True
                print_log.message("Autosuspending at generation {0}".format(self.pop.gen + 1), timer_name="start")
                self.save_checkpoint(self.directory, self.pop.gen)
                sub.call("touch {0}/AUTOSUSPENDED && rm {0}/RUNNING".format(self.directory), shell=True)
                break

            print_log.message("Creating folders structure for this generation")
            make_gen_directories(self.pop, self.directory, save_vxa_every, save_nets)

            # mutation
            print_log.message("Mutation starts")
            new_solutions = self.make_children()
            new_children = [self.encoding(t) for t in new_solutions]
            print_log.message("Mutation ends: successfully generated %d new children." % (len(new_children)))

            # replace old pop
            print_log.message("Replacing population with new designs")
            self.pop.individuals = []
            for mat in new_children:
                self.pop.add_random_individual()
                for old_net in self.pop[-1].genotype.networks:
                    if old_net.output_node_names[0] == "material":   # TODO: make this dynamic
                        old_net.values = mat
                self.pop[-1].genotype.express()
            print_log.message("New population size is %d" % len(self.pop))

            # evaluate fitness
            print_log.message("Starting fitness evaluation", timer_name="start")
            print_log.reset_timer("evaluation")
            self.update_env()
            self.evaluate(self.sim, self.env[self.curr_env_idx], self.pop, print_log, save_vxa_every,
                          self.directory, self.name, max_eval_time, time_to_try_again, save_lineages, batch_size)
            print_log.message("Fitness evaluation finished", timer_name="evaluation")  # record total eval time in log

            if self.solver is not None:  # update solver
                self.solver.tell([ind.fitness for ind in self.pop])

            # print population to stdout and save all individual data
            print_log.message("Saving statistics")
            write_gen_stats(self.pop, self.directory, self.name, save_vxa_every, save_pareto, save_nets,
                            save_lineages=save_lineages)

            self.pop.gen += 1

        if not self.autosuspended:  # print end of run stats
            print_log.message("Finished {0} generations".format(self.pop.gen + 1))
            print_log.message("DONE!", timer_name="start")
            sub.call("touch {0}/RUN_FINISHED && rm {0}/RUNNING".format(self.directory), shell=True)
コード例 #2
0
ファイル: algorithms.py プロジェクト: DanielLinsc/evosoro
    def run(self,
            max_hours_runtime=29,
            max_gens=3000,
            num_random_individuals=1,
            num_env_cycles=0,
            directory="tests_data",
            name="TestRun",
            max_eval_time=60,
            time_to_try_again=10,
            checkpoint_every=100,
            save_vxa_every=100,
            save_pareto=True,
            save_nets=False,
            save_lineages=True,
            continued_from_checkpoint=False):

        if self.autosuspended:
            sub.call("rm %s/AUTOSUSPENDED" % directory, shell=True)

        self.autosuspended = False
        self.max_gens = max_gens  # can add additional gens through checkpointing

        print_log = PrintLog()
        print_log.add_timer("evaluation")
        self.start_time = print_log.timers[
            "start"]  # sync start time with logging

        # sub.call("clear", shell=True)

        if not continued_from_checkpoint:  # generation zero
            self.directory = directory
            self.name = name
            self.num_random_inds = num_random_individuals
            self.num_env_cycles = num_env_cycles
            # global fitness_list
            # fitness_list += [self.pop.best_fit_so_far]
            global RUN_NAME
            global RUN_DIR
            RUN_NAME = name
            RUN_DIR = directory

            initialize_folders(self.pop,
                               self.directory,
                               self.name,
                               save_nets,
                               save_lineages=save_lineages)
            make_gen_directories(self.pop, self.directory, save_vxa_every,
                                 save_nets)
            sub.call("touch {}/RUNNING".format(self.directory), shell=True)
            self.evaluate(self.sim, self.env[self.curr_env_idx], self.pop,
                          print_log, save_vxa_every, self.directory, self.name,
                          max_eval_time, time_to_try_again, save_lineages)
            self.select(
                self.pop, directory, name
            )  # only produces dominated_by stats, no selection happening (population not replaced)
            write_gen_stats(self.pop,
                            self.directory,
                            self.name,
                            save_vxa_every,
                            save_pareto,
                            save_nets,
                            save_lineages=save_lineages)

        while self.pop.gen < max_gens:

            if self.pop.gen % checkpoint_every == 0:
                print_log.message(
                    "Saving checkpoint at generation {0}".format(self.pop.gen +
                                                                 1),
                    timer_name="start")
                self.save_checkpoint(self.directory, self.pop.gen)
            if self.elapsed_time(units="h") > max_hours_runtime:
                self.autosuspended = True
                print_log.message(
                    "Autosuspending at generation {0}".format(self.pop.gen +
                                                              1),
                    timer_name="start")
                self.save_checkpoint(self.directory, self.pop.gen)
                sub.call("touch {0}/AUTOSUSPENDED && rm {0}/RUNNING".format(
                    self.directory),
                         shell=True)
                break

            self.pop.gen += 1
            print_log.message("Creating folders structure for this generation")
            make_gen_directories(self.pop, self.directory, save_vxa_every,
                                 save_nets)

            # update ages
            self.pop.update_ages()

            # mutation
            print_log.message("Mutation starts")
            new_children = self.mutate(self.pop, print_log=print_log)
            print_log.message(
                "Mutation ends: successfully generated %d new children." %
                (len(new_children)))

            # combine children and parents for selection
            print_log.message("Now creating new population")
            self.pop.append(new_children)
            for _ in range(self.num_random_inds):
                print_log.message("Random individual added to population")
                self.pop.add_random_individual()
            print_log.message("New population size is %d" % len(self.pop))

            # evaluate fitness
            print_log.message("Starting fitness evaluation",
                              timer_name="start")
            print_log.reset_timer("evaluation")
            self.update_env()
            self.evaluate(self.sim, self.env[self.curr_env_idx], self.pop,
                          print_log, save_vxa_every, self.directory, self.name,
                          max_eval_time, time_to_try_again, save_lineages)
            print_log.message(
                "Fitness evaluation finished",
                timer_name="evaluation")  # record total eval time in log

            # perform selection by pareto fronts
            new_population = self.select(self.pop, directory, name)

            # adding individuals if the pop is too small
            if len(self.pop) < self.pop.pop_size:
                for _ in range(self.pop.pop_size - len(self.pop)):
                    print_log.message(
                        "Random individual added to population because too many were deleted"
                    )
                    self.pop.add_random_individual()
                print_log.message("New population size is %d" % len(self.pop))

            # print population to stdout and save all individual data
            print_log.message("Saving statistics")
            write_gen_stats(self.pop,
                            self.directory,
                            self.name,
                            save_vxa_every,
                            save_pareto,
                            save_nets,
                            save_lineages=save_lineages)
            # replace population with selection
            self.pop.individuals = new_population
            print_log.message("Population size reduced to %d" % len(self.pop))

        if not self.autosuspended:  # print end of run stats
            print_log.message("Finished {0} generations".format(self.pop.gen +
                                                                1))
            print_log.message("DONE!", timer_name="start")
            sub.call("touch {0}/RUN_FINISHED && rm {0}/RUNNING".format(
                self.directory),
                     shell=True)
コード例 #3
0
    def run(self, max_hours_runtime=29, max_gens=350, num_random_individuals=1, num_env_cycles=0,
            directory="tests_data", name="TestRun",
            max_eval_time=60, time_to_try_again=30,
            checkpoint_every=100, save_vxa_every=100, save_pareto=True,
            save_nets=False, save_lineages=False, continued_from_checkpoint=False,
            batch_size=None, update_survivors_age=True,
            max_fitness=None):

        if self.autosuspended:
            sub.call("rm run_1/AUTOSUSPENDED" % directory, shell=True) #sub.call("rm %s/AUTOSUSPENDED" % directory, shell=True)


        self.autosuspended = False
        self.max_gens = max_gens  # can add additional gens through checkpointing


        #Class that init a dictionary that contain important times
        print_log = PrintLog() #here you have the default times: start and last_call
        print_log.add_timer("evaluation") #add "evaluation" time in the dict list
        self.start_time = print_log.timers["start"]  # sync start time with logging

        # sub.call("clear", shell=True)

        #If you are starting from generation zero
        if not continued_from_checkpoint:  # generation zero
            self.directory = directory #the name of directoty given to this run. example: run_(seed)
            self.name = name #the name given to this run. exemplo "Swimmers"
            self.num_random_inds = num_random_individuals #parameter about the number of new random individuals added in each generations. typical example = 1
            self.num_env_cycles = num_env_cycles #parameter to change environment. typical example = 1 (same environment always)

            #Function used to create the folders where the simulations outputs will be saved: 
            initialize_folders(self.pop, self.directory, self.name, save_nets, save_lineages=save_lineages)

            #saves networks and vxa file for each generation if save_vxa_every and save_nets = True
            make_gen_directories(self.pop, self.directory, save_vxa_every, save_nets)

            #Create RUNNING file
            sub.call("touch {}/RUNNING".format(self.directory), shell=True)

            #Evaluate fitness of all individuals of the population in Voxelyze
            #In evaluation, the functions read_voxlyze_results AND write_voxelyze_file are used
            #So it is the place where the contact with Voxelyze is done.
            self.evaluate(self.sim, self.env[self.curr_env_idx], self.pop, print_log, save_vxa_every, self.directory,
                          self.name, max_eval_time, time_to_try_again, save_lineages, batch_size)
            
                 
            self.select(self.pop)  # only produces dominated_by stats, no selection happening (population not replaced)
            write_gen_stats(self.pop, self.directory, self.name, save_vxa_every, save_pareto, save_nets,
                            save_lineages=save_lineages)

        while self.pop.gen < max_gens:

            if self.pop.gen % checkpoint_every == 0: #CHECK_POINT_EVERY = 1 saves always
                print_log.message("Saving checkpoint at generation {0}".format(self.pop.gen+1), timer_name="start")
                self.save_checkpoint(self.directory, self.pop.gen)

            if self.elapsed_time(units="h") > max_hours_runtime or self.pop.best_fit_so_far == max_fitness:
                self.autosuspended = True
                print_log.message("Autosuspending at generation {0}".format(self.pop.gen+1), timer_name="start")
                self.save_checkpoint(self.directory, self.pop.gen)
                sub.call("touch {0}/AUTOSUSPENDED && rm {0}/RUNNING".format(self.directory), shell=True)
                break

            self.pop.gen += 1
            print_log.message("Creating folders structure for this generation")
            make_gen_directories(self.pop, self.directory, save_vxa_every, save_nets)

            # update ages
            self.pop.update_ages(update_survivors_age)

            # mutation
            print_log.message("Mutation starts")
            new_children = self.mutate(self.pop, print_log=print_log)
            print_log.message("Mutation ends: successfully generated %d new children." % (len(new_children)))

            # combine children and parents for selection
            print_log.message("Now creating new population")
            self.pop.append(new_children) #Add the new children to population
            for _ in range(self.num_random_inds):
                print_log.message("Random individual added to population")
                self.pop.add_random_individual() #add new random individual to population
            print_log.message("New population size is %d" % len(self.pop))

            # evaluate fitness
            print_log.message("Starting fitness evaluation", timer_name="start")
            print_log.reset_timer("evaluation")
            self.update_env()
            self.evaluate(self.sim, self.env[self.curr_env_idx], self.pop, print_log, save_vxa_every, self.directory,
                          self.name, max_eval_time, time_to_try_again, save_lineages, batch_size)
            print_log.message("Fitness evaluation finished", timer_name="evaluation")  # record total eval time in log

            # perform selection by pareto fronts
            new_population = self.select(self.pop)

            # print population to stdout and save all individual data
            print_log.message("Saving statistics")
            write_gen_stats(self.pop, self.directory, self.name, save_vxa_every, save_pareto, save_nets,
                            save_lineages=save_lineages)

            # replace population with selection
            self.pop.individuals = new_population
            print_log.message("Population size reduced to %d" % len(self.pop))

        if not self.autosuspended:  # print end of run stats
            print_log.message("Finished {0} generations".format(self.pop.gen + 1))
            print_log.message("DONE!", timer_name="start")
            sub.call("touch {0}/RUN_FINISHED && rm {0}/RUNNING".format(self.directory), shell=True)