Ejemplo n.º 1
0
    def add_multi_gen(self, pop, f_evals, opt_hv):
        self.gen_num += 1
        self.gens.append(None)
        self.fevals.append(f_evals)

        hv = multi_utils.hypervolume(pop)

        os = GenStats(min=opt_hv - hv, max=opt_hv - hv, mean=opt_hv - hv)
        fs = GenStats(min=hv, max=hv, mean=hv)

        self.obj_stats.append(os)
        self.fit_stats.append(fs)

        if self.write_immediately:
            with open(self.flog_name, 'a') as f:
                f.write(f'{f_evals} {fs.max} {fs.mean} {fs.min}\n')
            with open(self.olog_name, 'a') as f:
                f.write(f'{f_evals} {os.max} {os.mean} {os.min}\n')

        if self.gen_num % self.print_frequency == 0:
            print(f'{f_evals:8} {os.min:8.2f} {os.mean:8.2f} {os.max:8.2f}')
Ejemplo n.º 2
0
        fit = mf.get_function_by_name(fit_name)
        opt_hv = mf.get_opt_hypervolume(fit_name)
        mutate_ind = Mutation(step_size=MUT_STEP)
        xover = functools.partial(crossover, cross=one_pt_cross, cx_prob=CX_PROB)
        mut = functools.partial(mutation, mut_prob=MUT_PROB, mutate=mutate_ind)

        # run the algorithm `REPEATS` times and remember the best solutions from 
        # last generations
    
        best_inds = []
        for run in range(REPEATS):
            # initialize the log structure
            log = utils.Log(OUT_DIR, EXP_ID + '.' + fit_name , run, 
                            write_immediately=True, print_frequency=5)
            # create population
            pop = create_pop(POP_SIZE, cr_ind)
            # run evolution - notice we use the pool.map as the map_fn
            pop = evolutionary_algorithm(pop, MAX_GEN, fit, [xover, mut], tournament_selection_NSGA2, mutate_ind, map_fn=map, log=log, opt_hv=opt_hv)
            # remember the best individual from last generation, save it to file
            best_inds.append(mu.hypervolume(pop))
            
            # if we used write_immediately = False, we would need to save the 
            # files now
            # log.write_files()

        # print an overview of the best individuals from each run
        for i, bi in enumerate(best_inds):
            print(f'Run {i}: objective = {opt_hv - bi}')

        # write summary logs for the whole experiment
        utils.summarize_experiment(OUT_DIR, EXP_ID + '.' + fit_name)