Exemplo n.º 1
0
def test_nsga2():
    NDIM = 5
    BOUND_LOW, BOUND_UP = 0.0, 1.0
    MU = 16
    NGEN = 100

    toolbox = base.Toolbox()
    toolbox.register("attr_float", random.uniform, BOUND_LOW, BOUND_UP)
    toolbox.register("individual", tools.initRepeat,
                     creator.__dict__[INDCLSNAME], toolbox.attr_float, NDIM)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    toolbox.register("evaluate", benchmarks.zdt1)
    toolbox.register("mate",
                     tools.cxSimulatedBinaryBounded,
                     low=BOUND_LOW,
                     up=BOUND_UP,
                     eta=20.0)
    toolbox.register("mutate",
                     tools.mutPolynomialBounded,
                     low=BOUND_LOW,
                     up=BOUND_UP,
                     eta=20.0,
                     indpb=1.0 / NDIM)
    toolbox.register("select", tools.selNSGA2)

    pop = toolbox.population(n=MU)
    fitnesses = toolbox.map(toolbox.evaluate, pop)
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    pop = toolbox.select(pop, len(pop))
    for gen in range(1, NGEN):
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= 0.9:
                toolbox.mate(ind1, ind2)

            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        pop = toolbox.select(pop + offspring, MU)

    hv = hypervolume(pop, [11.0, 11.0])
    # hv = 120.777 # Optimal value

    assert hv > HV_THRESHOLD, "Hypervolume is lower than expected %f < %f" % (
        hv, HV_THRESHOLD)

    for ind in pop:
        assert not (any(numpy.asarray(ind) < BOUND_LOW)
                    or any(numpy.asarray(ind) > BOUND_UP))
Exemplo n.º 2
0
def main(seed=1):
    random.seed(seed)

    NGEN = 100
    MU = 100
    CXPB = 0.9

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"

    pop = toolbox.population()

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, [x for x in invalid_ind])
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    # Begin the generational process
    for gen in range(1, NGEN):
        # Vary the population
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)

            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
            # print("Indiv num:{} fit: {}\n".format(ind, fit))

        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)
        record = stats.compile(pop)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        print(logbook.stream)

    # print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))

    return pop, logbook
Exemplo n.º 3
0
def nsgaii(model, mu, ngen, cxpb, mutpb):
    assert mu % 4 == 0, "Error: mu is not divisible by 4"

    logging.debug("Working on model " + model.name + " @ NSGAII.py::nsgaii")
    toolbox = base.Toolbox()
    toolbox.register('mate', tools.cxOnePoint)
    toolbox.register('mutate',
                     tools.mutPolynomialBounded,
                     low=0,
                     up=1.0,
                     eta=20.0,
                     indpb=1.0 / model.decNum)
    toolbox.register('select', tools.selNSGA2)

    pop_df = model.init_random_pop(mu)
    model.eval_pd_df(pop_df, normalized=True)

    pop = model.pd_to_deap(pop_df)
    pop = toolbox.select(pop, len(pop))

    for gen in range(1, ngen):
        # _show_pop(sorted(pop, key=lambda i: i[0]))
        logging.debug("Running at gen " + str(gen))
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        t_offspring = list()

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= cxpb:
                toolbox.mate(ind1, ind2)
                del ind1.fitness.values
                del ind2.fitness.values

            if random.random() <= mutpb:
                toolbox.mutate(ind1)
                toolbox.mutate(ind2)
                del ind1.fitness.values
                del ind2.fitness.values

        # saving new configurations
        if not ind1.fitness.valid:
            t_offspring.append(ind1)
        if not ind1.fitness.valid:
            t_offspring.append(ind2)

        # eval
        offspring_df = model.deap_to_pd(t_offspring)
        model.eval_pd_df(offspring_df, normalized=True)
        offspring = model.pd_to_deap(offspring_df)

        pop = toolbox.select(pop + offspring, mu)

    toolbox.unregister('mate')
    toolbox.unregister('mutate')
    toolbox.unregister('select')

    res = sortLogNondominated(pop, k=10,
                              first_front_only=True)  # k value is non-sense
    return model.deap_to_pd(res)
Exemplo n.º 4
0
def main(seed=None):
    random.seed(seed)

    NGEN = 250
    MU = 100
    CXPB = 0.9

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", tools.mean)
    stats.register("std", tools.std)
    stats.register("min", min)
    stats.register("max", max)

    column_names = ["gen", "evals"]
    column_names.extend(stats.functions.keys())
    logger = tools.EvolutionLogger(column_names)
    logger.logHeader()

    pop = toolbox.population(n=MU)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    stats.update(pop)
    logger.logGeneration(evals=len(invalid_ind), gen=0, stats=stats)

    # Begin the generational process
    for gen in range(1, NGEN):
        # Variate the population
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)

            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)
        stats.update(pop)
        logger.logGeneration(evals=len(invalid_ind), gen=gen, stats=stats)

    return pop
Exemplo n.º 5
0
def main(seed=None):
    random.seed(seed)

    NGEN = 250
    MU = 100
    CXPB = 0.9

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean, axis=0)
    stats.register("std", numpy.std, axis=0)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"

    pop = toolbox.population(n=MU)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    # Begin the generational process
    for gen in range(1, NGEN):
        # Vary the population
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)

            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)
        record = stats.compile(pop)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        print(logbook.stream)

    return pop, logbook
Exemplo n.º 6
0
def select_parents(pop, toolbox, n_selected, algorithm):
    if algorithm == 'nsga2' or algorithm == 'rmota' or algorithm == 'mmota':
        offspring = tools.selTournamentDCD(pop, len(pop))
    elif algorithm == 'dtas':
        offspring = toolbox.select(pop, n_selected)
    else:
        raise exceptions.UnrecognizedAlgorithmException
    return offspring
Exemplo n.º 7
0
def action(model, mu, ngen, cxpb, mutpb):
    toolbox = model.toolbox

    toolbox.register('mate', tools.cxOnePoint)
    toolbox.register('mutate',
                     tools.mutPolynomialBounded,
                     low=0,
                     up=1.0,
                     eta=20.0,
                     indpb=1.0 / model.decsNum)
    toolbox.register('select', tools.selNSGA2)

    stats = tools.Statistics(lambda ind: ind)
    # PF0 = list()
    # with open(
    #         os.path.dirname(os.path.abspath(__file__)) + '/../Metrics/PF_0/' +
    #         model.name + '.txt', 'r') as f:
    #     for l in f:
    #         e = l.strip('\n').split(' ')
    #         e = [float(i) for i in e]
    #         PF0.append(e)
    # stats.register("gd_gs_pfs_hv", self_stats, model=model, PF0=PF0)

    logbook = tools.Logbook()
    # logbook.header = "gen", "gd_gs_pfs_hv"

    pop = random_pop(model, mu)
    for p in pop:
        model.eval(p)

    pop = toolbox.select(pop, len(pop))
    t = time.time()
    for gen in range(1, ngen):
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [copy.deepcopy(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= cxpb:
                toolbox.mate(ind1, ind2)

            if random.random() <= mutpb:
                toolbox.mutate(ind1)
                toolbox.mutate(ind2)

            model.eval(ind1)
            model.eval(ind2)

        pop = toolbox.select(pop + offspring, mu)
        # print(gen)
        record = stats.compile(pop)
        logbook.record(gen=gen, **record)
        print(logbook.stream)
        print(time.time() - t)

    for p in pop:
        model.eval(p, normalized=False)
    return pop
Exemplo n.º 8
0
    def main(seed=None):
        random.seed(seed)

        NGEN = ngen
        MU = npop
        CXPB = p_cross

        stats = tools.Statistics(lambda ind: ind.fitness.values)
        stats.register("min", np.min, axis=0)
        stats.register("max", np.max, axis=0)

        logbook = tools.Logbook()
        logbook.header = "gen", "evals", "min", "max"

        pop = toolbox.population(n=MU)
        pop_ini = pop[:]

        invalid_ind = [ind for ind in pop if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        pop = toolbox.select(pop, len(pop))

        record = stats.compile(pop)
        logbook.record(gen=0, evals=len(invalid_ind), **record)
        if PRINT:
            print(logbook.stream)

        for gen in range(1, NGEN):

            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [toolbox.clone(ind) for ind in offspring]

            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                if random.random() <= CXPB:
                    toolbox.mate(ind1, ind2)

                toolbox.mutate(ind1)
                toolbox.mutate(ind2)
                del ind1.fitness.values, ind2.fitness.values

            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit

            pop = toolbox.select(pop + offspring, MU)
            record = stats.compile(pop)
            logbook.record(gen=gen, evals=len(invalid_ind), **record)
            if PRINT:
                print(logbook.stream)

#        print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))

        return pop, pop_ini, logbook
Exemplo n.º 9
0
def main():
    temp = Chromosome(0)

    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("Individual", list, fitness=creator.FitnessMin)

    MU = 20
    NGEN = 100
    CXPB = 0.95

    toolbox = base.Toolbox()
    toolbox.register("individual", initialisation, creator.Individual)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    toolbox.register("evaluate", calculateFitness)
    toolbox.register("select", tools.selNSGA2)
    toolbox.register("mate", tools.cxOnePoint)
    toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.05)

    pop = toolbox.population(n=MU)
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    plotter = []
    for i in range(NGEN):
        pop = toolbox.select(pop, len(pop))
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)

            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)
        plotter.append(mean([k.fitness.values[0] for k in pop]))

    plt.plot(plotter)
    plt.xlabel('Number of iterations')
    plt.ylabel('Fitness of the population')
    plt.show()
Exemplo n.º 10
0
    def multiobjective(pop, toolbox, ngen, cxpb, stats, halloffame):
        """
        This code is based of the NSGA-II example from:
        https://github.com/DEAP/deap/blob/master/examples/ga/nsga2.py
        """
        logbook = tools.Logbook()
        logbook.header = 'gen', 'evals', 'std', 'min', 'avg', 'max'

        invalid_ind = [ind for ind in pop if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # This is just to assign the crowding distance to the individuals
        # no actual selection is done
        pop = toolbox.select(pop, len(pop))

        halloffame.update(pop)
        record = stats.compile(pop)
        logbook.record(gen=0, evals=len(invalid_ind), **record)
        print(logbook.stream)

        # Begin the generational process
        for gen in range(1, ngen):

            # Vary the population
            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [toolbox.clone(ind) for ind in offspring]

            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                if random.random() <= cxpb:
                    toolbox.mate(ind1, ind2)

                toolbox.mutate(ind1)
                toolbox.mutate(ind2)
                del ind1.fitness.values, ind2.fitness.values

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit

            # Select the next generation population
            pop = toolbox.select(pop + offspring, len(pop))
            halloffame.update(pop)
            record = stats.compile(pop)
            logbook.record(gen=gen, evals=len(invalid_ind), **record)
            print(logbook.stream)

        return pop, logbook
Exemplo n.º 11
0
    def run(self):
        self.setup()
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in self.pop if not ind.fitness.valid]
        fitnesses = list((self.toolbox.map(self.toolbox.evaluate,
                                           invalid_ind)))
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # This is just to assign the crowding distance to the individuals
        # no actual selection is done
        self.pop = self.toolbox.select(self.pop, len(self.pop))

        record = self.stats.compile(self.pop)
        self.logbook.record(gen=0, evals=len(invalid_ind), **record)
        print(self.logbook.stream)

        # Begin the generational process
        for i_generation in range(1, self.n_generation):
            # Vary the population
            offspring = tools.selTournamentDCD(self.pop, len(self.pop))
            offspring = [self.toolbox.clone(ind) for ind in offspring]

            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                if random.random() <= self.crossover_probability:
                    self.toolbox.mate(ind1, ind2)

                self.toolbox.mutate(ind1)
                self.toolbox.mutate(ind2)
                del ind1.fitness.values, ind2.fitness.values

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = self.toolbox.map(self.toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit

            # Select the next generation population
            self.pop = self.toolbox.select(self.pop + offspring,
                                           self.n_population)
            record = self.stats.compile(self.pop)
            self.logbook.record(gen=i_generation,
                                evals=len(invalid_ind),
                                **record)
            print(self.logbook.stream)

        print("Final population hypervolume is %f" %
              hypervolume(self.pop, [11.0, 11.0]))
        return self.pop, self.logbook
Exemplo n.º 12
0
def EMO(gen):
    INDIV = 100
    CXPB = 0.9
    NGEN = gen

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    pop = toolbox.population(n=INDIV)

    # applying fitness to people with invalid fitnesses
    invalid_individual = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_individual)
    for ind, fit in zip(invalid_individual, fitnesses):
        ind.fitness.values = fit

    # assign crowding distance
    pop = toolbox.select(pop, len(pop))

    # go through generations
    for gen in range(1, NGEN):

        # selection using dominance
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        # applying crossover
        for indiv1, indiv2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(indiv1, indiv2)

            #mutates based on indp prob (1/dimensions)
            toolbox.mutate(indiv1)
            toolbox.mutate(indiv2)
            del indiv1.fitness.values, indiv2.fitness.values

        # evaluating fitness of individuals with invalid fitnesses
        invalid_individual = [
            ind for ind in offspring if not ind.fitness.valid
        ]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_individual)
        for ind, fit in zip(invalid_individual, fitnesses):
            ind.fitness.values = fit

        # Chossing a population for the next generation
        pop = toolbox.select(pop + offspring, INDIV)

    return pop
Exemplo n.º 13
0
def test_nsga2():
    NDIM = 5
    BOUND_LOW, BOUND_UP = 0.0, 1.0
    MU = 16
    NGEN = 100

    toolbox = base.Toolbox()
    toolbox.register("attr_float", random.uniform, BOUND_LOW, BOUND_UP)
    toolbox.register("individual", tools.initRepeat, creator.__dict__[INDCLSNAME], toolbox.attr_float, NDIM)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    toolbox.register("evaluate", benchmarks.zdt1)
    toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0)
    toolbox.register("mutate", tools.mutPolynomialBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0, indpb=1.0/NDIM)
    toolbox.register("select", tools.selNSGA2)

    pop = toolbox.population(n=MU)
    fitnesses = toolbox.map(toolbox.evaluate, pop)
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    pop = toolbox.select(pop, len(pop))
    for gen in range(1, NGEN):
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]
        
        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= 0.9:
                toolbox.mate(ind1, ind2)
            
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values
        
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        pop = toolbox.select(pop + offspring, MU)

    hv = hypervolume(pop, [11.0, 11.0])
    # hv = 120.777 # Optimal value

    assert hv > HV_THRESHOLD, "Hypervolume is lower than expected %f < %f" % (hv, HV_THRESHOLD)
Exemplo n.º 14
0
    def runGenerations(self):
        # Running algorithm for given number of generations
        for gen in range(self.num_gen):
            print(f"{20*'#'} Currently Evaluating {gen} Generation {20*'#'}")

            # Selecting individuals
            # Selecting offsprings from the population, about 1/2 of them
            self.offspring = tools.selTournamentDCD(self.pop, len(self.pop))
            self.offspring = [
                self.toolbox.clone(ind) for ind in self.offspring
            ]

            # Performing , crossover and mutation operations according to their probabilities
            for ind1, ind2 in zip(self.offspring[::2], self.offspring[1::2]):
                # Mating will happen 80% of time if cross_prob is 0.8
                if random.random() <= self.cross_prob:
                    # print("Mating happened")
                    self.toolbox.mate(ind1, ind2)

                    # If cross over happened to the individuals then we are deleting those individual
                    #   fitness values, This operations are being done on the offspring population.
                    del ind1.fitness.values, ind2.fitness.values
                self.toolbox.mutate(ind1)
                self.toolbox.mutate(ind2)

            # Calculating fitness for all the invalid individuals in offspring
            self.invalid_ind = [
                ind for ind in self.offspring if not ind.fitness.valid
            ]
            self.fitnesses = self.toolbox.map(self.toolbox.evaluate,
                                              self.invalid_ind)
            for ind, fit in zip(self.invalid_ind, self.fitnesses):
                ind.fitness.values = fit

            # Recalcuate the population with newly added offsprings and parents
            # We are using NSGA2 selection method, We have to select same population size
            self.pop = self.toolbox.select(self.pop + self.offspring,
                                           self.pop_size)

            # Recording stats in this generation
            recordStat(self.invalid_ind, self.logbook, self.pop, self.stats,
                       gen + 1)

        print(f"{20 * '#'} End of Generations {20 * '#'} ")
Exemplo n.º 15
0
def NSGA2(name):
    model = DimacsModel(name)
    toolbox = model.toolbox
    toolbox.register('mutate', model.bit_flip_mutate)
    toolbox.register('select', tools.selNSGA2)
    toolbox.register('mate', model.cxTwoPoint)
    # get the initial generation from the result of sway
    # and evaluate them
    pop = load_sway_results(model)
    toolbox.map(model.eval_ind, pop)

    # the parameters for NSGA-II as follows
    MU = 100
    NGEN = 100
    CXPB = 0.9

    # start the NSGA2 algorithms
    for _ in range(100-len(pop)):
        pop.append(random.choice(pop))
    random.shuffle(pop)

    for gen in range(1, NGEN):
        # vary the population
        tools.emo.assignCrowdingDist(pop)
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        map(model.eval, offspring)
        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)

    valid_pop = [i for i in pop if i.fitness.values[0] <=0]
    hv, spread, _, size, _ = stat_basing_on_pop(valid_pop, False)

    print(hv, spread, size)
    return hv, spread, size
Exemplo n.º 16
0
def selTournamentDCD(problem, individuals):
    
    # Evaluate any new guys
    for individual in individuals:
        if not individual.valid:
            individual.evaluate()
            
    # Format a population data structure usable by DEAP's package
    dIndividuals = deap_format(problem, individuals)
    
    # Assign crowding distance
    tools.emo.assignCrowdingDist(dIndividuals)
    
    # Select elites
    selectees = tools.selTournamentDCD(dIndividuals, len(individuals))
    
    # Update beginning population data structure
    selectedIndices = [i for i,sel in enumerate(selectees)]
    return [individuals[s] for s in selectedIndices], len(individuals)
Exemplo n.º 17
0
def selTournamentDCD(problem, individuals):

    # Evaluate any new guys
    for individual in individuals:
        if not individual.valid:
            individual.evaluate()

    # Format a population data structure usable by DEAP's package
    dIndividuals = deap_format(problem, individuals)

    # Assign crowding distance
    tools.emo.assignCrowdingDist(dIndividuals)

    # Select elites
    selectees = tools.selTournamentDCD(dIndividuals, len(individuals))

    # Update beginning population data structure
    selectedIndices = [i for i, sel in enumerate(selectees)]
    return [individuals[s] for s in selectedIndices], len(individuals)
Exemplo n.º 18
0
    def ZDT1_Selection(self, pop):

        #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        # Vary the population
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        # # Select the next generation individuals
        # offspring = toolbox.select(pop, len(pop))
        # # Clone the selected individuals
        # offspring = list(map(toolbox.clone, offspring))

        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]): #get every single pair
            if random.random() < self.CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values
        for mutant in offspring:
            if random.random() < self.MUTPB:
                #print mutant
                toolbox.mutate(mutant)
                #print mutant
                del mutant.fitness.values



        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]

        # print("  Cross or Mutation: %s" % len(invalid_ind))
        fitnesses = map(toolbox.evaluate, invalid_ind)

        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit


        # Select the next generation population
        off = toolbox.select(pop + offspring, self.num_population)
        pop[:] = off
Exemplo n.º 19
0
	def varCustom(population, toolbox, lambda_, cxpb=CXPB):
		offspring = []

		for _ in xrange(lambda_):
			op_choice = random.random()
			if op_choice < cxpb: # produce offspring via crossover
				if method == 'NSGA2': 
					parents = map(toolbox.clone, tools.selTournamentDCD(population, 2))
				elif method == 'SPEA2': 
					parents = map(toolbox.clone, tools.selTournamentSPEA2(population, 2))
				ind1, ind2 = parents[0], parents[1]
				ind1, ind2 = toolbox.mate(ind1, ind2)
				del ind1.fitness.values
				offspring.append(ind1)
			else:
				ind1 = toolbox.clone(random.choice(population))
				#ind1, = toolbox.mutate(ind1)
				del ind1.fitness.values
				offspring.append(ind1)

		for i in range(len(offspring)): # produce offspring via mutation
			offspring[i], = toolbox.mutate(offspring[i])

		return offspring
Exemplo n.º 20
0
def main(seed=None):
    random.seed(seed)

    NGEN = 250
    MU = 100

    log_interval = 25

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean, axis=0)
    stats.register("std", np.std, axis=0)
    stats.register("min", np.min, axis=0)
    stats.register("max", np.max, axis=0)
    stats.register("median", np.median, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max", "median"

    pop = toolbox.population(n=MU)
    hof = tools.ParetoFront()

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)
    hof.update(pop)

    basepath = os.path.dirname(os.path.abspath(__file__))
    log_dir = '{}/logs/{}/'.format(basepath, time.strftime('%y%m%d-%H%M%S'))
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    # log initial population
    os.makedirs('{}0'.format(log_dir))
    for i, agent in enumerate(pop):
        agent[0].save_weights('{}0/{}_weights.csv'.format(log_dir, i), overwrite=True)
    log_population(pop, '{}0'.format(log_dir))

    # Begin the generational process
    for gen in range(1, NGEN+1):
        # Get Offspring
        # first get pareto front
        pareto_fronts = tools.sortNondominated(pop, len(pop))
        selection = pareto_fronts[0]
        len_pareto = len(pareto_fronts[0])

        rest = list(chain(*pareto_fronts[1:]))
        if len(rest) % 4:
            rest.extend(random.sample(selection, 4 - (len(rest) % 4)))

        selection.extend(tools.selTournamentDCD(rest, len(rest)))
        offspring = [toolbox.mutate(toolbox.clone(ind)) for ind in selection[:len(pop)]]

        # Revaluate the individuals in last population
        fitnesses = toolbox.map(toolbox.evaluate, pop)
        for ind, fit in zip(pop, fitnesses):
            ind.fitness.values = fit

        # Evaluate the new offspring
        fitnesses = toolbox.map(toolbox.evaluate, offspring)
        for ind, fit in zip(offspring, fitnesses):
            ind.fitness.values = fit

        # Update the hall of fame with the generated individuals
        hof.update(offspring)

        plot_population(pop, offspring, lim = [[10,120],[0,0],[0,4]])

        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)
        pareto_fronts = tools.sortNondominated(pop, len(pop))
        plot_selection(pop, pareto_front_size=len(pareto_fronts[0]), lim = [[10,120],[0,0],[0,4]])
        
        record = stats.compile(pop)
        logbook.record(gen=gen, evals=len(offspring)+len(pop), **record)
        print(logbook.stream)

        if gen % log_interval == 0 or gen == NGEN:
            os.makedirs('{}{}'.format(log_dir, gen))
            for i, agent in enumerate(pop):
                agent[0].save_weights('{}{}/{}_weights.csv'.format(log_dir, gen, i), overwrite=True)
            log_population(pop, '{}{}'.format(log_dir, gen))

    with open('{}/gen_stats.txt'.format(log_dir), 'w') as fp:
        np.savetxt(fp, logbook, fmt="%s")

    plot_population(pop)
    print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0, 11.0]))

    os.makedirs('{}hof'.format(log_dir))
    for i, agent in enumerate(hof):
        agent[0].save_weights('{}hof/{}_weights.csv'.format(log_dir, i), overwrite=True)
    log_population(hof, '{}hof'.format(log_dir))

    return pop, logbook
Exemplo n.º 21
0
def search(toolbox, seed=None, gens=500, mu=200, verbose=False):
    random.seed(seed)

    CXPB = 0.9  # pravdepodobnost krizenia?

    stats = tools.Statistics(lambda ind: ind.fitness.values)

    # stats.register("avg", numpy.mean, axis=0)
    # stats.register("std", numpy.std, axis=0)
    # stats.register("min", np.min, axis=0)
    # stats.register("max", np.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "hypervolume"

    # vytvorime inicialnu populaciu
    pop = toolbox.population(n=mu)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # Choose worst solution as reference point for hypervolume calculation
    ref = np.max([x.fitness.values for x in pop], axis=0) + 1

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))
    record = stats.compile(pop)
    best_hypervolume = hypervolume(pop, ref)
    record['hypervolume'] = best_hypervolume
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    output(logbook.stream, verbose)

    best_hypervolume_population = pop

    # Begin the generational process
    for gen in range(1, gens):
        # Vary the population
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)

            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop = toolbox.select(pop + offspring, mu, nd='log')
        record = stats.compile(pop)

        current_hypervolume = hypervolume(pop, ref)

        record['hypervolume'] = current_hypervolume

        if current_hypervolume > best_hypervolume:
            best_hypervolume = current_hypervolume
            best_hypervolume_population = pop

        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        output(logbook.stream, verbose)

    return pop, logbook, best_hypervolume_population, best_hypervolume
Exemplo n.º 22
0
    def _run_geneneration(self,
                          pop,
                          crossover_rate,
                          mutation_rate,
                          toolbox,
                          reporting_interval,
                          allele_dict,
                          evaluate_population,
                          keys,
                          single_obj,
                          stats_callback,
                          caching,
                          **kwargs):
        '''
        
        Helper function for runing a single generation.
        
        :param pop:
        :param crossover_rate:
        :param mutation_rate:
        :param toolbox:
        :param reporting_interval:
        :param allele_dict:
        :param evaluate_population:
        :param keys:
        :param single_obj:
        
        
        '''
        # Variate the population
        pop_size = len(pop)
        a = pop[0:closest_multiple_of_four(len(pop))]
        if single_obj:
            offspring = toolbox.select(pop, pop_size, min(pop_size, 10))
        else:
            offspring = tools.selTournamentDCD(a, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]
        
        no_name=False
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            # Apply crossover 
            if random.random() < crossover_rate:
                keys = sorted(child1.keys())
                
                try:
                    keys.pop(keys.index("name"))
                except ValueError:
                    no_name = True
                
                child1_temp = [child1[key] for key in keys]
                child2_temp = [child2[key] for key in keys]
                toolbox.crossover(child1_temp, child2_temp)

                if not no_name:
                    for child, child_temp in zip((child1, child2), 
                                             (child1_temp,child2_temp)):
                        name = ""
                        for key, value in zip(keys, child_temp):
                            child[key] = value
                            name += " "+str(child[key])
                        child['name'] = name 
                else:
                    for child, child_temp in zip((child1, child2), 
                                             (child1_temp,child2_temp)):
                        for key, value in zip(keys, child_temp):
                            child[key] = value
                
            #apply mutation
            toolbox.mutate(child1, mutation_rate, allele_dict, keys, 0.05)
            toolbox.mutate(child2, mutation_rate, allele_dict, keys, 0.05)

            for entry in (child1, child2):
                del entry.fitness.values
            
            if caching:
                for entry in (child1, child2):
                    try:
                        ind = stats_callback.tried_solutions[entry]
                    except KeyError:
                        del entry.fitness.values
                        continue
                    
                    entry.fitness = ind.fitness 
       
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        evaluate_population(invalid_ind, reporting_interval, toolbox, self)

        # Select the next generation population
        if single_obj:
            pop = offspring
        else:
            pop = toolbox.select(pop + offspring, pop_size)

        return pop
Exemplo n.º 23
0
def main(archive=[], archivestats={}, **kwargs):
    for key, val in kwargs.items():
        print(f"{key} : {val}")
    seed = kwargs['seed']
    random.seed(seed)
    np.random.seed(seed)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean, axis=0)
    stats.register("std", np.std, axis=0)
    stats.register("min", np.min, axis=0)
    stats.register("max", np.max, axis=0)
    NGEN = kwargs['NGEN']
    MU = 100  #100
    CXPB = 0.9
    eta = 20
    n_selected = MU if (kwargs['algorithm'] == 'nsga2'
                        or kwargs['algorithm'] == 'rmota') else int(
                            (MU * 0.8) / 2)
    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"

    nNodes = kwargs['nNodes']
    network_creator = topologies.network_topologies[kwargs['network_creator']]
    nTasks = kwargs['nTasks']
    task_creator = topologies.task_topologies[kwargs['task_creator']]
    energy_list = kwargs['energy_list_sim']
    networkGraph = network_creator(energy_list=energy_list, **kwargs)
    taskGraph = task_creator(networkGraph, **kwargs)

    aliveGraph = network_creator(energy_list=energy_list, **kwargs)
    #remove_dead_nodes(aliveGraph, energy_list, **kwargs)
    try:
        pop, toolbox = setup_run(aliveGraph,
                                 taskGraph,
                                 eta,
                                 archive=archive,
                                 archivestats=archivestats,
                                 **kwargs)
    except exceptions.NoValidNodeException:
        raise exceptions.NetworkDeadException
    except exceptions.NetworkDeadException as e:
        raise e
    except Exception as e:
        print(f"Error during ea setup: {e}")
        raise e
    pop.sort(key=lambda x: x.fitness.values)

    #Evaluate the individuals with an invalid fitness
    #invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    mapped = zip(pop, itertools.repeat(kwargs))
    #fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, itertools.repeat([networkGraph,taskGraph, [energy]*25]))
    continue_run = checkIfAlive(**kwargs)
    if not continue_run:
        raise exceptions.NetworkDeadException
    try:
        fitnesses = toolbox.map(evaluate_wrapper, mapped)
    except exceptions.NoValidNodeException:
        raise exceptions.NetworkDeadException
    except exceptions.NetworkDeadException as e:
        raise e
    except Exception as e:
        print(f"Error during ea gen: {e}")
        raise e
    for ind, fit in zip(pop, fitnesses):
        if kwargs['algorithm'] == 'nsga2' or kwargs['algorithm'] == 'rmota':
            ind.fitness.values = fit[:2]
        elif kwargs['algorithm'] == 'dtas':
            ind.latency = fit[1]
            ind.fitness.values = fit[0],
        else:
            print("unrecognized algorithm")
            return -1
        ind.received = fit[2]
        ind.energy = fit[3]
        ind.node_status = fit[4]

# # This is just to assign the crowding distance to the individuals
# # no actual selection is done
    if kwargs['algorithm'] == 'nsga2' or kwargs['algorithm'] == 'rmota':
        pop = toolbox.select(pop, len(pop))
    if kwargs['algorithm'] == 'rmota':
        archive, archivestats = toolbox.selArchive(pop, archive, archivestats,
                                                   popSize)

    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(pop), **record)
    print(logbook.stream)
    # Begin the generational process
    for gen in range(1, NGEN):
        # Vary the population
        if kwargs['algorithm'] == 'nsga2' or kwargs['algorithm'] == 'rmota':
            offspring = tools.selTournamentDCD(pop, len(pop))
        else:
            offspring = toolbox.select(pop, n_selected)

        offspring = [toolbox.clone(ind) for ind in offspring]
        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            #    if kwargs['verbose']:
            #        print(f"parents: {ind1}  ,  {ind2}")
            #    if random.random() <= CXPB:
            #        toolbox.mate(ind1, ind2)
            #    if kwargs['verbose']:
            #       print(f"children: {ind1}  ,  {ind2}")
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            #    if kwargs['verbose']:
            #       print(f"mutated: {ind1}  ,  {ind2}")
            del ind1.fitness.values, ind2.fitness.values
        # Evaluate the individuals with an invalid fitness
        for ind in offspring:
            repair_individual(ind, aliveGraph, taskGraph,
                              kwargs['network_status'])
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        mapped = zip(invalid_ind, itertools.repeat(kwargs))
        #fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, itertools.repeat([networkGraph,taskGraph,[energy]*25]))
        try:
            fitnesses = toolbox.map(evaluate_wrapper, mapped)
        except exceptions.NoValidNodeException:
            raise exceptions.NetworkDeadException
        except exceptions.NetworkDeadException as e:
            raise e
        except Exception as e:
            print(f"error during ea loop: {e}")
            raise e
        for ind, fit in zip(invalid_ind, fitnesses):
            if kwargs['algorithm'] == 'nsga2' or kwargs['algorithm'] == 'rmota':
                ind.fitness.values = fit[:2]
            elif kwargs['algorithm'] == 'dtas':
                ind.latency = fit[1]
                ind.fitness.values = fit[0],
            else:
                print("unrecognized algorithm")
                return -1
            ind.received = fit[2]
            ind.energy = fit[3]
            ind.node_status = fit[4]
        # Select the next generation population
        pop = toolbox.select(pop + offspring, len(pop))
        if kwargs['algorithm'] == 'rmota':
            archive, archivestats = toolbox.selArchive(pop, archive,
                                                       archivestats, popSize)
        record = stats.compile(pop)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        print(logbook.stream)

    #print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))
    if kwargs['algorithm'] == 'nsga2' or kwargs['algorithm'] == 'rmota':
        pfront = sortEpsilonNondominated(pop, len(pop))[0]
        best = tools.selBest(pfront, 1)
    else:
        best = tools.selBest(pop, 1)
    if kwargs['algorithm'] == 'nsga2' or kwargs['algorithm'] == 'dtas':
        archive = []
    #print("---")
    #print(pop)
    #print("---")
    #print(logbook)
    #print("---")
    #print(best)
    return pop, logbook, best, archive, archivestats
Exemplo n.º 24
0
    def run_nsga2evo(self, num_run, gen_record, ngen=51,seed=None, population=None, ):
        random.seed(seed)

        NGEN = ngen
        MU = 100


        stats = tools.Statistics(lambda ind: ind.fitness.values)
        # stats.register("avg", numpy.mean, axis=0)
        # stats.register("std", numpy.std, axis=0)
        stats.register("min", np.min, axis=0)
        stats.register("max", np.max, axis=0)

        logbook = tools.Logbook()
        logbook.header = "gen", "evals", "std", "min", "avg", "max"

        if population == None:
            try:
                pop = self.toolbox.population_restart()
            except FileNotFoundError:
                pop = self.toolbox.population(n=MU)
        else:
            pop = population

        # Evaluate the individuals with an invalid fitness
        queue = multiprocessing.Queue()
        invalid_ind = [ind for ind in pop if not ind.fitness.valid]
        fitnesses = self.toolbox.map(self.toolbox.evaluate, invalid_ind, repeat(queue))
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # This is just to assign the crowding distance to the individuals
        # no actual selection is done
        pop = self.toolbox.select(pop, len(pop))

        record = stats.compile(pop)
        logbook.record(gen=0, evals=len(invalid_ind), **record)
        # print(logbook.stream)

        # Begin the generational process
        for gen in range(1, NGEN):
            # Vary the population

            # print('Cloning Population')
            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [self.toolbox.clone(ind) for ind in offspring]
            # print('Cloning Completed')

            # print('Mutating Offspring')
            for ind1 in offspring:
                self.toolbox.mutate(ind1)
                del ind1.fitness.values
            # print('Mutation Completed')

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            # print('Evaluating Fitness')
            fitnesses = self.toolbox.map(self.toolbox.evaluate, invalid_ind, repeat(queue))
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit
            # print("Fitness Evaluation Completed")

            # Select the next generation population
            # print('Selecting Population')
            pop = self.toolbox.select(pop + offspring, MU)
            # print('Selection Completed')
            record = stats.compile(pop)
            logbook.record(gen=gen, evals=len(invalid_ind), **record)
            # print(logbook.stream)

        # print('Evolution Completed')
        filename = str(num_run) + '_' + str(gen_record) + '.csv'
        filepath = os.path.join(self.results_folder, filename)
        with open(filepath, 'w') as csvfile:
            # print('Writing to ' + filename)
            writer = csv.writer(csvfile, delimiter=',')
            for p in pop:
                writer.writerow(p)
        # print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))

        return pop, logbook
Exemplo n.º 25
0
    def fit(self):
        creator.create('FitnessMax', base.Fitness, weights=(1.0, ))
        creator.create('Individual', list, fitness=creator.FitnessMax)

        toolbox = base.Toolbox()

        BOUND_LOW, BOUND_UP = 0.000001, 1.0

        NDIM = self._fitness_obj.param_count

        toolbox.register('attr_float', self._uniform, BOUND_LOW, BOUND_UP,
                         NDIM)
        toolbox.register('individual', tools.initIterate, creator.Individual,
                         toolbox.attr_float)
        toolbox.register('population', tools.initRepeat, list,
                         toolbox.individual)

        toolbox.register('evaluate', self._fitness_obj.fitness)
        toolbox.register('mate',
                         tools.cxSimulatedBinaryBounded,
                         low=BOUND_LOW,
                         up=BOUND_UP,
                         eta=20.0)
        toolbox.register('mutate',
                         tools.mutPolynomialBounded,
                         low=BOUND_LOW,
                         up=BOUND_UP,
                         eta=20.0,
                         indpb=1.0 / NDIM)
        toolbox.register("select", tools.selNSGA2)

        NGEN = int(self._task_info['NGEN']) + 1
        pop_size = int(self._task_info['population'])
        CXPB = float(self._task_info['CXPB'])

        stats = tools.Statistics(lambda ind: ind.fitness.values)
        stats.register('min', np.min, axis=0)
        stats.register('max', np.max, axis=0)
        stats.register('mean', np.mean, axis=0)
        stats.register('std', np.std, axis=0)

        logbook = tools.Logbook()
        logbook.header = 'gen', 'evals', 'std', 'min', 'avg', 'max'

        pop = toolbox.population(n=pop_size)

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in pop if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        pop = toolbox.select(pop, len(pop))

        # Begin the generational process
        for gen in range(1, NGEN):

            # Vary the population
            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [toolbox.clone(ind) for ind in offspring]

            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                if random.random() <= CXPB:
                    toolbox.mate(ind1, ind2)

                toolbox.mutate(ind1)
                toolbox.mutate(ind2)
                del ind1.fitness.values, ind2.fitness.values

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit

            # Select the next generation population
            pop = toolbox.select(pop + offspring, pop_size)
            record = stats.compile(pop)

            # storage each generation result
            self._ga_results_list.append('%s\n' % (record['max'][0]))

            logbook.record(gen=gen, evals=len(invalid_ind), **record)
            if self._show_opts_history:
                print(logbook.stream)

        best_ind = tools.selBest(pop, 1)[0]
        best_material = self._fitness_obj.recovery_material(best_ind)
        return best_material
    def train(self):
        ###################################
        # 1. Initialize the population
        # toolbox.population은 creator.Individual n개를 담은 list를 반환. (=> population)
        ###################################
        now = datetime.datetime.now()
        now_str = now.strftime('%Y-%m-%d %H:%M:%S')
        print("[GA] Initialion starts ...")
        logging.info("[GA] Initialion starts at " + now_str)
        init_start_time = time.time()

        GA_history_list = []

        start_gen = 1

        train_loader, val_loader = get_train_valid_loader_CIFAR10(
            self.data_path,
            self.args_train.batch_size,
            valid_size=0.2,
            shuffle=True,
            num_workers=self.args_train.workers,
            pin_memory=True)

        # (train_log 읽어와서 특정 generation부터 이어서 train 하지 않고) 처음부터 train 하는 경우
        # population initializatino 부터 GA search 시작
        if self.TRAIN_FROM_LOGS == False:
            print("Training start!")
            logging.info("Training start!")
            pop = self.toolbox.population(n=self.pop_size)
            ###################################
            # 2. Evaluate the population
            ###################################
            invalid_ind = [ind for ind in pop]

            for idx, ind in enumerate(invalid_ind):
                eval_time_for_1_chromo = time.time()
                fitness, ind_model = evaluate_one_chromo(
                    ind,
                    args_train=self.args_train,
                    train_loader=train_loader,
                    val_loader=val_loader,
                    stage_pool_path_list=self.stage_pool_path_list,
                    data_path=self.data_path,
                    log_file_name=self.log_file_name)

                eval_time_for_1_chromo = time.time() - eval_time_for_1_chromo
                print(
                    '\t\t [eval_time_for_1_chromo: %.3fs]' %
                    eval_time_for_1_chromo, idx, 'th chromo is evaluated.')
                logging.info(
                    '\t\t [eval_time_for_1_chromo: %.3fs] %03d th chromo is evaluated.'
                    % (eval_time_for_1_chromo, idx))
                ind.fitness.values = fitness
                GA_history_list.append([ind, fitness])

            # log 기록 - initialize (= 0th generation)
            self.train_log[0] = GA_history_list

            self.save_log()

            # This is just to assign the crowding distance to the individuals
            # no actual selection is done
            pop = self.toolbox.select(pop, len(pop))

        # train_log 읽어와서 중간부터 이어서 train 하는 경우
        elif self.TRAIN_FROM_LOGS == True:
            print("Training start!")
            print("Read train_log...")
            logging.info("Training start!")
            logging.info("Read train_log...")

            # train_log 읽어오기
            with open(self.REAL_TRAIN_LOG_PATH) as train_log_json_file:
                data = json.load(
                    train_log_json_file)  # hp(=hyperparameter), train_log 있음

            train_log_past = data['train_log']
            niter = len(train_log_past)  # 기록 상 총 init 횟수
            npop = len(train_log_past['0'])

            start_gen = niter  # niter = 11 이면, log 상에 0 ~ 10번까지 기록되어있는 것.

            # self.train_log 에 읽어온 로그 넣어놓기 (OrderedDict())
            for i in range(niter):
                self.train_log[str(i)] = train_log_past[str(i)]

            self.save_log()

            # population 읽어오기
            # train_log 에서 last population 읽어오기
            last_population = train_log_past[str(int(niter) - 1)]

            # last population으로 population 만들기
            pop = self.toolbox.population_load(last_population)

            # fitness values 도 읽어오기
            for i in range(len(last_population)):
                pop[i].fitness.values = last_population[i][1]

            pop = self.toolbox.select(pop, len(pop))

        now = datetime.datetime.now()
        now_str = now.strftime('%Y-%m-%d %H:%M:%S')
        print("Initialization is finished at", now_str)
        logging.info("Initialion is finished at " + now_str)

        init_time = time.time() - init_start_time
        logging.info("Initialization time = " + str(init_time) + "s")
        print()

        ###################################
        # 3. Begin GA
        ###################################
        # Begin the generational process
        for gen in range(start_gen, self.ngen + 1):  # self.ngen 남은 횟수를 이어서 돌리기
            # 3.1. log 기록
            now = datetime.datetime.now()
            now_str = now.strftime('%Y-%m-%d %H:%M:%S')
            print(str(gen) + "th generation starts at" + now_str)
            logging.info(str(gen) + "th generation starts at" + now_str)

            start_gen_time = time.time()

            # 3.2. Offspring pool 생성 후, crossover(=mate) & mutation
            # Vary the population
            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [self.toolbox.clone(ind) for ind in offspring]

            # ::2, 1::2 즉, 짝수번째 크로모좀과 홀수번쨰 크로모좀들 차례로 선택하면서 cx, mut 적용
            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                self.toolbox.mate(ind1, ind2, self.cxpb)

                self.toolbox.mutate(ind1, mutpb=self.mutpb)
                self.toolbox.mutate(ind2, mutpb=self.mutpb)
                del ind1.fitness.values, ind2.fitness.values

            # 3.3. Evaluation
            # Evaluate the individuals with an invalid fitness
            print("\t Evaluation...")
            start_time = time.time()

            # fitness values (= accuracy, flops) 모음
            GA_history_list = []

            invalid_ind = [ind for ind in offspring]

            for idx, ind in enumerate(invalid_ind):
                eval_time_for_1_chromo = time.time()
                fitness, ind_model = evaluate_one_chromo(
                    ind,
                    args_train=self.args_train,
                    train_loader=train_loader,
                    val_loader=val_loader,
                    stage_pool_path_list=self.stage_pool_path_list,
                    data_path=self.data_path,
                    log_file_name=self.log_file_name)

                # <= evaluate() returns  (-prec, flops), NN_model
                eval_time_for_1_chromo = time.time() - eval_time_for_1_chromo
                print(
                    '\t\t [eval_time_for_1_chromo: %.3fs]' %
                    eval_time_for_1_chromo, idx, 'th chromo is evaluated.')
                logging.info(
                    '\t\t [eval_time_for_1_chromo: %.3fs] %03d th chromo is evaluated.'
                    % (eval_time_for_1_chromo, idx))

                ind.fitness.values = fitness
                GA_history_list.append([ind, fitness])

            # log 기록
            self.train_log[gen] = GA_history_list
            self.save_log()

            eval_time_for_one_generation = time.time() - start_time
            print("\t Evaluation ends (Time : %.3f)" %
                  eval_time_for_one_generation)

            # Select the next generation population
            pop = self.toolbox.select(pop + offspring, self.pop_size)

            gen_time = time.time() - start_gen_time
            print('\t [gen_time: %.3fs]' % gen_time, gen,
                  'th generation is finished.')

            logging.info(
                '\t Gen [%03d/%03d] -- evals: %03d, evals_time: %.4fs, gen_time: %.4fs'
                % (gen, self.ngen, len(invalid_ind),
                   eval_time_for_one_generation, gen_time))
Exemplo n.º 27
0
def nsga2Algorithm(population,
                   toolbox,
                   cxpb,
                   mutpb,
                   ngen,
                   stats=None,
                   halloffame=None,
                   verbose=__debug__):

    logbook = tools.Logbook()
    logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    if halloffame is not None:
        halloffame.update(population)

    numOfIndividuals = len(population)

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    population = toolbox.select(population, len(population))

    record = stats.compile(population) if stats else {}
    logbook.record(gen=0, evals=len(invalid_ind), **record)

    if verbose:
        print(logbook.stream)
    # Begin the generational process

    removed = 0
    for gen in range(1, ngen + 1):
        # Vary the population

        # Dodawanie osobników aż populacja będzie podzielna przez 4
        while (len(population) % 4 != 0):
            population.append(population[random.randint(
                0,
                len(population) - 1)])

        offspring = tools.selTournamentDCD(population, len(population))

        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= cxpb:
                ind1, ind2 = toolbox.mate(ind1, ind2)

            if random.random() <= mutpb:
                toolbox.mutate(ind1)
            if random.random() <= mutpb:
                toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        if halloffame is not None:
            removed += halloffame.update(offspring)

        # Select the next generation population
        population = toolbox.select(population + offspring, numOfIndividuals)
        record = stats.compile(population) if stats else {}
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        if verbose:
            print(logbook.stream)
    return population, logbook, removed
Exemplo n.º 28
0
    def test010_(self):
        print("**** TEST {} ****".format(whoami()))
        NDIM = 30
        BOUND_LOW, BOUND_UP = 0.0, 1.0
        BOUND_LOW_STR, BOUND_UP_STR = '0.0', '1.0' 
        RES_STR = '0.01'
        NGEN = 250
        POPSIZE = 40
        MU = 100
        CXPB = 0.9
        
        # Create variables
        var_names = [str(num) for num in range(NDIM)]
        myLogger.setLevel("CRITICAL")
        basis_set = [Variable.from_range(name, BOUND_LOW_STR, RES_STR, BOUND_UP_STR) for name in var_names]
        myLogger.setLevel("DEBUG")
        
        # Create DSpace
        thisDspace = DesignSpace(basis_set)
        
        # Create OSpace
        objective_names = ('obj1','obj3')
        objective_goals = ('Max', 'Min')
        this_obj_space = ObjectiveSpace(objective_names, objective_goals)
        mapping = Mapping(thisDspace, this_obj_space)
                
        # Statistics and logging
        stats = tools.Statistics(lambda ind: ind.fitness.values)
        stats.register("avg", np.mean, axis=0)
        stats.register("std", np.std, axis=0)
        stats.register("min", np.min, axis=0)
        stats.register("max", np.max, axis=0)        
        logbook = tools.Logbook()
        logbook.header = "gen", "evals", "std", "min", "avg", "max"
        
        creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))
        
        toolbox = base.Toolbox()
        
        #--- Eval
        toolbox.register("evaluate", benchmarks.mj_zdt1_decimal)
        
        #--- Operators
        toolbox.register("mate", tools.cxSimulatedBinaryBounded, 
                         low=BOUND_LOW, up=BOUND_UP, eta=20.0)
        toolbox.register("mutate", tools.mutPolynomialBounded, low=BOUND_LOW, up=BOUND_UP, 
                         eta=20.0, indpb=1.0/NDIM)
        toolbox.register("select", tools.selNSGA2)
        
        # Create the population
        mapping.assign_individual(Individual2)
        mapping.assign_fitness(creator.FitnessMin)
        pop = mapping.get_random_population(POPSIZE)
        
        # Evaluate first pop        
        invalid_ind = [ind for ind in pop if not ind.fitness.valid]
        toolbox.map(toolbox.evaluate, invalid_ind)
        logging.debug("Evaluated {} individuals".format(len(invalid_ind)))
        
        # Check that they are evaluated
        invalid_ind = [ind for ind in pop if not ind.fitness.valid]
        assert not invalid_ind
        
        pop = toolbox.select(pop, len(pop))
        logging.debug("Crowding distance applied to initial population of {}".format(len(pop)))
        
        myLogger.setLevel("CRITICAL")
        for gen in range(1, NGEN):
            # Vary the population
            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [toolbox.clone(ind) for ind in offspring]
            logging.debug("Selected and cloned {} offspring".format(len(offspring)))
            
            #print([ind.__hash__() for ind in offspring])
            #for ind in offspring:
            #    print()
            pairs = zip(offspring[::2], offspring[1::2])
            for ind1, ind2 in pairs:
                if random.random() <= CXPB:
                    toolbox.mate(ind1, ind2)
                    
                toolbox.mutate(ind1)
                toolbox.mutate(ind2)
                del ind1.fitness.values, ind2.fitness.values
            logging.debug("Operated over {} pairs".format(len(pairs)))

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            processed_ind = toolbox.map(toolbox.evaluate, invalid_ind)
            logging.debug("Evaluated {} individuals".format(len(processed_ind)))
            
            #raise
            #for ind, fit in zip(invalid_ind, fitnesses):
            #    ind.fitness.values = fit
        
            # Select the next generation population
            pop = toolbox.select(pop + offspring, MU)
            record = stats.compile(pop)
            logbook.record(gen=gen, evals=len(invalid_ind), **record)
            print(logbook.stream)
        
        ###
        with open(r"C:\Users\jon\git\deap1\examples\ga\pareto_front\zdt1_front.json") as optimal_front_data:
            optimal_front = json.load(optimal_front_data)
        # Use 500 of the 1000 points in the json file
        optimal_front = sorted(optimal_front[i] for i in range(0, len(optimal_front), 2))
                
        pop.sort(key=lambda x: x.fitness.values)
        print(stats)
        print("Convergence: ", convergence(pop, optimal_front))
        print("Diversity: ", diversity(pop, optimal_front[0], optimal_front[-1]))

        
        front = np.array([ind.fitness.values for ind in pop])
        optimal_front = np.array(optimal_front)
        plt.scatter(optimal_front[:,0], optimal_front[:,1], c="r")
        print(front)
        plt.scatter(front[:,0], front[:,1], c="b")
        plt.axis("tight")
        #plt.savefig('C:\ExportDir\test1.png')
        plt.savefig('C:\\ExportDir\\out.pdf', transparent=True, bbox_inches='tight', pad_inches=0)
        #plt.show()
        
        
Exemplo n.º 29
0
def nsga2vm(seed=None):
    random.seed(seed)
    
    # first weight means smaller better, second weight means bigger is better
    creator.create("FitnessMin", base.Fitness, weights=(-1.0, 1.0, -1.0))
    creator.create("Individual", array, typecode='i', fitness=creator.FitnessMin)
    
    toolbox = base.Toolbox()
    # register methods to toolbox
    toolbox.register("attr_int", generate_param, INIT_RANDOM)
    toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.attr_int)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    toolbox.register("evaluate", interactive_benchmark, ap_env=ap_env)
    toolbox.register("mate", cxSimulatedBinaryBoundedINT, eta=ETA_MATE)
    toolbox.register("mutate", mutPolynomialBoundedINT, eta=ETA_MUTATE, indpb=1.0/NDIM)
    toolbox.register("select", tools.selNSGA2, nd='log')

    # use multithreading
    if CPU_THREAD > 1:
        pool = multiprocessing.Pool(processes = CPU_THREAD)
        toolbox.register("map", pool.map)

    pop = toolbox.population(n=MU)
    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    # display xxxx/4000 and remaining time
    progress = ProgressDisplay(NGEN)

    # Begin the generational process
    for gen in range(NGEN):
        if gen == EXIT_INCUBATION_NGEN:
            ap_env.is_incubating = False
        if gen % 10 == 0:
            progress.display_progress(gen)
            if WORKLOAD_SIGMA > 0:
                ap_env.workload_shuffle_noise(WORKLOAD_SIGMA)
            if IDLELOAD_SIGMA > 0:
                ap_env.idleload_shuffle_noise(IDLELOAD_SIGMA)

        # Vary the population
        offspring = tools.selTournamentDCD(pop, N_OFFSPRING)
        offspring = [toolbox.clone(ind) for ind in offspring]
        
        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)
    
    if CPU_THREAD > 1:
        pool.close()
        pool.join()
        pool.terminate()
    return pop
Exemplo n.º 30
0
def main(cfg):
    """Main workflow of NSGA-II based Scenario analysis."""
    random.seed()
    pop_size = cfg.nsga2_npop
    gen_num = cfg.nsga2_ngens
    rule_cfg = cfg.bmps_rule
    rule_mth = cfg.rule_method
    cx_rate = cfg.nsga2_rcross
    mut_perc = cfg.nsga2_pmut
    mut_rate = cfg.nsga2_rmut
    sel_rate = cfg.nsga2_rsel
    ws = cfg.nsga2_dir
    worst_econ = cfg.worst_econ
    worst_env = cfg.worst_env
    # available gene value list
    possible_gene_values = list(cfg.bmps_params.keys())
    possible_gene_values.append(0)
    units_info = cfg.units_infos
    slppos_tagnames = cfg.slppos_tagnames
    suit_bmps = cfg.slppos_suit_bmps
    gene_to_unit = cfg.gene_to_slppos
    unit_to_gene = cfg.slppos_to_gene

    print_message('Population: %d, Generation: %d' % (pop_size, gen_num))
    print_message('BMPs configure method: %s' % ('rule-based' if rule_cfg else 'random-based'))

    # create reference point for hypervolume
    ref_pt = numpy.array([worst_econ, worst_env]) * multi_weight * -1

    stats = tools.Statistics(lambda sind: sind.fitness.values)
    stats.register('min', numpy.min, axis=0)
    stats.register('max', numpy.max, axis=0)
    stats.register('avg', numpy.mean, axis=0)
    stats.register('std', numpy.std, axis=0)

    logbook = tools.Logbook()
    logbook.header = 'gen', 'evals', 'min', 'max', 'avg', 'std'

    pop = toolbox.population(cfg, n=pop_size)
    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]

    try:
        # parallel on multiprocesor or clusters using SCOOP
        from scoop import futures
        fitnesses = futures.map(toolbox.evaluate, [cfg] * len(invalid_ind), invalid_ind)
        # print('parallel-fitnesses: ', fitnesses)
    except ImportError or ImportWarning:
        # serial
        fitnesses = toolbox.map(toolbox.evaluate, [cfg] * len(invalid_ind), invalid_ind)
        # print('serial-fitnesses: ', fitnesses)

    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit[:2]
        ind.id = fit[2]

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, pop_size)
    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print_message(logbook.stream)

    # Begin the generational process
    output_str = '### Generation number: %d, Population size: %d ###\n' % (gen_num, pop_size)
    print_message(output_str)
    UtilClass.writelog(cfg.logfile, output_str, mode='replace')

    for gen in range(1, gen_num + 1):
        output_str = '###### Generation: %d ######\n' % gen
        print_message(output_str)
        # Vary the population
        offspring = tools.selTournamentDCD(pop, int(pop_size * sel_rate))
        offspring = [toolbox.clone(ind) for ind in offspring]
        # print_message('Offspring size: %d' % len(offspring))
        if len(offspring) >= 2:  # when offspring size greater than 2, mate can be done
            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                if random.random() <= cx_rate:
                    if rule_cfg:
                        toolbox.mate_rule(slppos_tagnames, ind1, ind2)
                    else:
                        toolbox.mate_rdn(ind1, ind2)
                if rule_cfg:
                    toolbox.mutate_rule(units_info, gene_to_unit, unit_to_gene, slppos_tagnames,
                                        suit_bmps, ind1,
                                        perc=mut_perc, indpb=mut_rate, method=rule_mth)
                    toolbox.mutate_rule(units_info, gene_to_unit, unit_to_gene, slppos_tagnames,
                                        suit_bmps, ind2,
                                        perc=mut_perc, indpb=mut_rate, method=rule_mth)
                else:
                    toolbox.mutate_rdm(possible_gene_values, ind1, perc=mut_perc, indpb=mut_rate)
                    toolbox.mutate_rdm(possible_gene_values, ind2, perc=mut_perc, indpb=mut_rate)
                del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        invalid_ind_size = len(invalid_ind)
        # print_message('Evaluate pop size: %d' % invalid_ind_size)
        try:
            from scoop import futures
            fitnesses = futures.map(toolbox.evaluate, [cfg] * invalid_ind_size, invalid_ind)
        except ImportError or ImportWarning:
            fitnesses = toolbox.map(toolbox.evaluate, [cfg] * invalid_ind_size, invalid_ind)

        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit[:2]
            ind.id = fit[2]

        # Select the next generation population
        pop = toolbox.select(pop + offspring, pop_size)

        hyper_str = 'Gen: %d, hypervolume: %f\n' % (gen, hypervolume(pop, ref_pt))
        print_message(hyper_str)
        UtilClass.writelog(cfg.hypervlog, hyper_str, mode='append')

        record = stats.compile(pop)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        print_message(logbook.stream)

        # Create plot
        front = numpy.array([ind.fitness.values for ind in pop])
        plot_pareto_front(front, ['Economic effectiveness', 'Environmental effectiveness'],
                          ws, gen, 'Pareto frontier of Scenarios Optimization')
        # save in file
        output_str += 'scenario\teconomy\tenvironment\tgene_values\n'
        for indi in pop:
            output_str += '%d\t%f\t%f\t%s\n' % (indi.id, indi.fitness.values[0],
                                                indi.fitness.values[1],
                                                str(indi))
        UtilClass.writelog(cfg.logfile, output_str, mode='append')

        # Delete SEIMS output files, and BMP Scenario database of current generation
        delete_model_outputs(cfg.model_dir, cfg.hostname, cfg.port, cfg.bmp_scenario_db)

    return pop, logbook
Exemplo n.º 31
0
def main(seed=None):
   random.seed(seed)

   NGEN = 2
   MU = 4
   CXPB = 0.6
   pop = toolbox.population(n=MU)

   stats = tools.Statistics(lambda ind: ind.fitness.values)
   stats.register("min", numpy.min, axis=0)
   stats.register("max", numpy.max, axis=0)
   stats.register("pop", copy.deepcopy)

   history = tools.History()
   # Decorate the variation operators
   #toolbox.register("variate", variate, mate=toolbox.mate, mutate=toolbox.mutate)
   #toolbox.decorate("variate", history.decorator)
   toolbox.decorate("mate", history.decorator)
   toolbox.decorate("mutate", history.decorator)

   fitnesses = toolbox.map(toolbox.evaluate, pop)
   for ind, fit in zip(pop, fitnesses):
       ind.fitness.values = fit
   plt.figure(figsize=(10,4))
   plt.subplot(1,2,1)
   for ind in pop: plt.plot(ind[0], ind[1], 'k.', ms=3)
   plt.xlabel('$x_1$');plt.ylabel('$x_2$');plt.title('Decision space');
   plt.subplot(1,2,2)
   for ind in pop: plt.plot(ind.fitness.values[0], ind.fitness.values[1], 'k.', ms=3)
   plt.xlabel('$f_1(\mathbf{x})$');plt.ylabel('$f_2(\mathbf{x})$');
   plt.xlim((0.5,3.6));plt.ylim((0.5,3.6)); plt.title('Objective space');
   plt.savefig("objective.png", dpi=200)

   logbook = tools.Logbook()
   logbook.header = "gen", "evals", "fitness", "size", "pop","ind"
   pickle.dump(logbook, open('nsga_ii-results.pickle', 'wb'),
           pickle.HIGHEST_PROTOCOL)

   hof = tools.ParetoFront()

   # Evaluate the individuals with an invalid fitness
   invalid_ind = [ind for ind in pop if not ind.fitness.valid]
   fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
   for ind, fit in zip(invalid_ind, fitnesses):
       ind.fitness.values = fit

   #hof.update(pop)

   # This is just to assign the crowding distance to the individualis
   # no actual selection is done
   pop = toolbox.select(pop, len(pop))

   record = stats.compile(pop)
   logbook.record(gen=0, evals=len(invalid_ind), **record)
   print(logbook.stream)

   # Begin the generational process
   for gen in range(1, NGEN):
       # Vary the population
       offspring = tools.selTournamentDCD(pop, len(pop))
       offspring = [toolbox.clone(ind) for ind in offspring]

       for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
           if random.random() <= CXPB:
               toolbox.mate(ind1, ind2)

           toolbox.mutate(ind1)
           toolbox.mutate(ind2)
           del ind1.fitness.values, ind2.fitness.values

       # Evaluate the individuals with an invalid fitness
       invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
       fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
       for ind, fit in zip(invalid_ind, fitnesses):
           ind.fitness.values = fit

       print "Evaluated %i individuals" % len(invalid_ind)

       pop = toolbox.select(pop+offspring, len(offspring))
       hof.update(pop)

       # Select the next generation population
       pop = toolbox.select(pop + offspring, MU)
       record = stats.compile(pop)
       logbook.record(gen=gen, evals=len(invalid_ind), **record)
       print(logbook.stream)

       plt.close("all")
       front = numpy.array([ind.fitness.values for ind in pop])
       plt.figure(figsize=(10,10))
       #fig,ax = plt.subplots(1,gen)
       plt.scatter(front[:,0], front[:,1], c="b")
       #locals()["ax"+str(gen)]=plt.scatter(front[:,0], front[:,1], c="b")
       #plt.tight_layout()
       plt.xlabel("RT(Time)")
       plt.ylabel("Memory usage, Mb")
       plt.savefig("front_gen"+str(gen)+".png", dpi=200)

   print("Pareto individuals are:")
   for ind in hof:
       print ind, ind.fitness.values
   print("XXXXXXXXXX Making plots XXXXXXXXXXXXX")

   #fig = plt.figure(figsize=(10,10))
   #ax = fig.gca()
   #ax.set_xlabel('RT')
   #ax.set_ylabel('Memory')
   #anim = animation.FuncAnimation(fig, lambda i: animate(i, logbook),
    #                           frames=len(logbook), interval=1,
    #                           blit=True)
   #anim.save('nsgaii-geantv.mp4', fps=15, bitrate=-1, dpi=500)
   #anim.save('populations.gif', writer='imagemagick')

   #print("XXXXXXXXXXXXXXXXXXXXXXX")


   print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))

   print("XXXXXXXXXXX Making more plots XXXXXXXXXXXX")
   fronts_s = tools.emo.sortLogNondominated(pop, len(pop))
   plot_colors = ('b','r', 'g', 'm', 'y', 'k', 'c')
   fig, ax = plt.subplots(1, figsize=(10,10))
   for i,inds in enumerate(fronts_s):
       par = [toolbox.evaluate(ind) for ind in inds]
       df = pd.DataFrame(par)
       df.plot(ax=ax, kind='scatter', label='Front ' + str(i+1),
                 x=df.columns[0], y=df.columns[1],
                 color=plot_colors[i % len(plot_colors)])
   plt.xlabel('$f_1(\mathbf{x})$');plt.ylabel('$f_2(\mathbf{x})$');
   plt.savefig("front.png", dpi=200)
Exemplo n.º 32
0
def NSGA2(model=errors, NDIM=15, GEN=20, eta=0.5):
    creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))
    creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin)
    toolbox = base.Toolbox()
    BOUND_LOW, BOUND_UP = 1.0, 20.0
    toolbox.register("attr_float", uniform, BOUND_LOW, BOUND_UP, NDIM)
    toolbox.register("r_list", tools.initIterate, creator.Individual, toolbox.attr_float)
    toolbox.register("population", tools.initRepeat, list, toolbox.r_list)
    toolbox.register("evaluate", model)
    toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=BOUND_LOW, up=BOUND_UP, eta=eta)
    toolbox.register("mutate", tools.mutPolynomialBounded, low=BOUND_LOW, up=BOUND_UP, eta=eta, indpb=1.0/NDIM)
    toolbox.register("select", tools.selNSGA2)
    NGEN = GEN
    MU = 100
    CXPB = 0.9

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean, axis=0)
    stats.register("std", numpy.std, axis=0)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"

    pop = toolbox.population(n=MU)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))
    min_val=[]
    record = stats.compile(pop)
    with open('./Pareto_Fronts/NSGA2-'+model.__name__+"-"+str(GEN)+"-"+str(eta)+'.txt', 'w') as f:
        f.write("%s %s\n" % (str(record.get('min')[0]), str(record.get('min')[1])))
        min_val.append(sum(record.get('min')))
        logbook.record(gen=0, evals=len(invalid_ind), **record)
        #print(logbook.stream)

        # Begin the generational process
        for gen in range(1, NGEN):
            # Vary the population
            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [toolbox.clone(ind) for ind in offspring]

            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                if random.random() <= CXPB:
                    toolbox.mate(ind1, ind2)

                toolbox.mutate(ind1)
                toolbox.mutate(ind2)
                del ind1.fitness.values, ind2.fitness.values

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit

            # Select the next generation population
            pop = toolbox.select(pop + offspring, MU)
            record = stats.compile(pop)
            min_val.append(sum(record.get('min')))
            f.write("%s %s\n" % (str(record.get('min')[0]), str(record.get('min')[1])))
            logbook.record(gen=gen, evals=len(invalid_ind), **record)
            #print(logbook.stream)

    return min_val
    def train(self):
        ###################################
        # 1. Initialize the population.  (toolbox.population은 creator.Individual n개를 담은 list를 반환. (=> population)
        ###################################
        now = datetime.datetime.now()
        now_str = now.strftime('%Y-%m-%d %H:%M:%S')
        print("[GA] Initialion starts ...")
        logging.info("[GA] Initialion starts at " + now_str)
        init_start_time = time.time()

        # fitness values (= accuracy, flops) 모음
        GA_history_list = []    # (choromosome, accuracy, flops) 이렇게 담아놓기
                                # e.g.  [ [[1,3,4], 40, 1000], [[2,6,10], 30%, 2000], ...  ]
        start_gen = 1

        # train_log 읽어와서 중간부터 이어서 train 하지 않고, 처음부터 train 하는 경우
        if self.TRAIN_FROM_LOGS == False:
            pop = self.toolbox.population(n=self.pop_size)
            ###################################
            # 2. Evaluate the population (with an invalid fitness)
            ###################################
            invalid_ind = [ind for ind in pop]

            # pop size = 20 고정
            # 크로모좀 4개씩 gpu 하나씩 배정해서 training 하기
            for i in [0, 4, 8, 12, 16]:
                # i ~ i+1
                # i ~ (i+3) 크로모좀을 각각 training 하기 - 먼저 끝났으면 기다리도록.
                # 로그도 다르게 떨궈야하네.

                eval_time_for_4_chromo = time.time()

                # 각각 training
                # i ~ i+3
                fitness_dict = evaluate_Multiprocess(ind_list=[invalid_ind[i], invalid_ind[i+1], invalid_ind[i+2], invalid_ind[i+3] ],
                                                      args_train=self.args_train,
                                                     stage_pool_path_list=self.stage_pool_path_list,
                                                     data_path=self.data_path,
                                                     channels=self.args_train.channels,
                                                     log_file_name=self.log_file_name)

                # <= evaluate() returns  (-prec, flops), NN_model
                eval_time_for_4_chromo = time.time() - eval_time_for_4_chromo
                print('\t\t [eval_time_for_4_chromo: %.3fs]' % eval_time_for_4_chromo, i, '~', (i+3), 'chromo is evaluated.')
                logging.info('\t\t [eval_time_for_4_chromo: %.3fs] %03d ~ %03d th chromo is evaluated.' % (eval_time_for_4_chromo, i, i+3))

                for fit_idx, chromo_idx in zip([0, 1, 2, 3], [i, i+1, i+2, i+3]):
                    fitness_for_idx_chromo = fitness_dict[fit_idx]
                    invalid_ind[chromo_idx].fitness.values = fitness_for_idx_chromo
                    GA_history_list.append([invalid_ind[chromo_idx], fitness_for_idx_chromo])
                    

            ## log 기록 - initialize (= 0th generation)
            self.train_log[0] = GA_history_list

            self.save_log()

            # This is just to assign the crowding distance to the individuals
            # no actual selection is done
            pop = self.toolbox.select(pop, len(pop))

            
        # train_log 읽어와서 중간부터 이어서 train 하는 경우
        # [Reference] Seeding a population => https://deap.readthedocs.io/en/master/tutorials/basic/part1.html
        elif self.TRAIN_FROM_LOGS == True:
            print("################# [KYY-check] Read train_log from the middle #################")
            logging.info("################# [KYY-check] Read train_log from the middle #################")
            
            # train_log 읽어오기
            with open(self.REAL_TRAIN_LOG_PATH) as train_log_json_file:
                data = json.load(train_log_json_file)  # hp(=hyperparameter), train_log 있음

            train_log_past = data['train_log']
            niter = len(train_log_past)  # 기록 상 총 init 횟수
            npop = len(train_log_past['0'])
            
            start_gen = niter  # niter = 11 이면, log 상에 0 ~ 10번까지 기록되어있는 것.
            
            # self.train_log 에 읽어온 로그 넣어놓기 (OrderedDict())
            for i in range(niter):
                self.train_log[str(i)] = train_log_past[str(i)]

            self.save_log()
            
            # population 읽어오기
            # train_log 에서 last population 읽어오기
            last_population = train_log_past[str(int(niter)-1)]   # list of [chromosome, [-val_accuracy, flops]]

            # last population으로 population 만들기
            pop = self.toolbox.population_load(last_population)

            # fitness values 도 읽어오기
            for i in range(len(last_population)):    
                pop[i].fitness.values = last_population[i][1]  # [-val_accuracy, flops]
            
            # This is just to assign the crowding distance to the individuals
            # no actual selection is done
            pop = self.toolbox.select(pop, len(pop))
            
        now = datetime.datetime.now()
        now_str = now.strftime('%Y-%m-%d %H:%M:%S')
        print("Initialization is finished at", now_str)
        logging.info("Initialion is finished at " + now_str)

        init_time = time.time() - init_start_time
        logging.info("Initialization time = " + str(init_time) + "s")
        print()
        
        if self.RANDOM_SEARCH == False:
            ###################################
            # Begin GA
            ###################################
            # Begin the generational process
            for gen in range(start_gen, self.ngen+1):  # self.ngen 남은 횟수를 이어서 돌리기
                ##### 3.1. log 기록
                now = datetime.datetime.now()
                now_str = now.strftime('%Y-%m-%d %H:%M:%S')
                print("#####", gen, "th generation starts at", now_str)
                logging.info("#####" + str(gen) + "th generation starts at" + now_str)

                start_gen_time = time.time()

                ##### 3.2. Offspring pool 생성 후, crossover(=mate) & mutation
                # Vary the population
                offspring = tools.selTournamentDCD(pop, len(pop))
                offspring = [self.toolbox.clone(ind) for ind in offspring]

                # ::2, 1::2 즉, 짝수번째 크로모좀과 홀수번쨰 크로모좀들 차례로 선택하면서 cx, mut 적용
                # e.g. 0번, 1번 ind를 cx, mut   // 2번, 3번 ind를 cx, mut // ...
                for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                    # crossover (=mate)
                    self.toolbox.mate(ind1, ind2, self.cxpb)
                    
                    # mutation
                    self.toolbox.mutate(ind1, mutpb=self.mutpb)
                    self.toolbox.mutate(ind2, mutpb=self.mutpb)
                    del ind1.fitness.values, ind2.fitness.values

                ##### 3.3. Evaluation
                # Evaluate the individuals with an invalid fitness
                print("\t Evaluation...")
                start_time = time.time()

                # fitness values (= accuracy, flops) 모음
                GA_history_list = []

                invalid_ind = [ind for ind in offspring]
                # pop size = 20 고정
                # 크로모좀 4개씩 gpu 하나씩 배정해서 training 하기
                for i in [0, 4, 8, 12, 16]:
                    # i ~ i+1
                    # i ~ (i+3) 크로모좀을 각각 training 하기 - 먼저 끝났으면 기다리도록.
                    # 로그도 다르게 떨궈야하네.

                    eval_time_for_4_chromo = time.time()

                    # 각각 training
                    # i ~ i+3
                    fitness_dict = evaluate_Multiprocess(ind_list=[invalid_ind[i], invalid_ind[i+1], invalid_ind[i+2], invalid_ind[i+3] ],
                                                          args_train=self.args_train,
                                                         stage_pool_path_list=self.stage_pool_path_list,
                                                         data_path=self.data_path,
                                                         log_file_name=self.log_file_name)

                    # <= evaluate() returns  (-prec, flops), NN_model
                    eval_time_for_4_chromo = time.time() - eval_time_for_4_chromo
                    print('\t\t [eval_time_for_4_chromo: %.3fs]' % eval_time_for_4_chromo, i, '~', (i+3), 'chromo is evaluated.')
                    logging.info('\t\t [eval_time_for_4_chromo: %.3fs] %03d ~ %03d th chromo is evaluated.' % (eval_time_for_4_chromo, i, i+3))

                    for fit_idx, chromo_idx in zip([0, 1, 2, 3], [i, i+1, i+2, i+3]):
                        fitness_for_idx_chromo = fitness_dict[fit_idx]
                        invalid_ind[chromo_idx].fitness.values = fitness_for_idx_chromo
                        GA_history_list.append([invalid_ind[chromo_idx], fitness_for_idx_chromo])


                # 전체 크로모좀 evaluation 했으니 cuda cache 한번 clear 해주기
                torch.cuda.empty_cache()

                ## log 기록
                self.train_log[gen] = GA_history_list

                self.save_log()

                eval_time_for_one_generation = time.time() - start_time
                print("\t Evaluation ends (Time : %.3f)" % eval_time_for_one_generation)

                ##### Select the next generation population
                pop = self.toolbox.select(pop + offspring, self.pop_size)

                gen_time = time.time() - start_gen_time
                print('\t [gen_time: %.3fs]' % gen_time, gen, 'th generation is finished.')

                logging.info('\t Gen [%03d/%03d] -- evals: %03d, evals_time: %.4fs, gen_time: %.4fs' % (
                    gen, self.ngen, len(invalid_ind), eval_time_for_one_generation, gen_time))

        elif self.RANDOM_SEARCH == True:
            ###################################
            # Begin Random Search
            ###################################
            print("###################################")
            print("########## Random Search ##########")
            print("###################################")            
            # Begin the generational process
            for gen in range(start_gen, self.ngen+1):  # self.ngen 남은 횟수를 이어서 돌리기
                ##### 3.1. log 기록
                now = datetime.datetime.now()
                now_str = now.strftime('%Y-%m-%d %H:%M:%S')
                print("#####", gen, "th generation starts at", now_str)
                logging.info("#####" + str(gen) + "th generation starts at" + now_str)

                start_gen_time = time.time()
                
                # Random initialization
                offspring = self.toolbox.population(n=self.pop_size)

                ## Evaluation
                # Evaluate the individuals with an invalid fitness
                print("\t Evaluation...")
                start_time = time.time()

                # fitness values (= accuracy, flops) 모음
                GA_history_list = []

                invalid_ind = [ind for ind in offspring]
                # pop size = 20 고정
                # 크로모좀 4개씩 gpu 하나씩 배정해서 training 하기
                for i in [0, 4, 8, 12, 16]:
                    # i ~ i+1
                    # i ~ (i+3) 크로모좀을 각각 training 하기 - 먼저 끝났으면 기다리도록.
                    # 로그도 다르게 떨궈야하네.

                    eval_time_for_4_chromo = time.time()

                    # 각각 training
                    # i ~ i+3
                    fitness_dict = evaluate_Multiprocess(ind_list=[invalid_ind[i], invalid_ind[i+1], invalid_ind[i+2], invalid_ind[i+3] ],
                                                          args_train=self.args_train,
                                                         stage_pool_path_list=self.stage_pool_path_list,
                                                         data_path=self.data_path,
                                                         log_file_name=self.log_file_name)

                    # <= evaluate() returns  (-prec, flops), NN_model
                    eval_time_for_4_chromo = time.time() - eval_time_for_4_chromo
                    print('\t\t [eval_time_for_4_chromo: %.3fs]' % eval_time_for_4_chromo, i, '~', (i+3), 'chromo is evaluated.')
                    logging.info('\t\t [eval_time_for_4_chromo: %.3fs] %03d ~ %03d th chromo is evaluated.' % (eval_time_for_4_chromo, i, i+3))

                    for fit_idx, chromo_idx in zip([0, 1, 2, 3], [i, i+1, i+2, i+3]):
                        fitness_for_idx_chromo = fitness_dict[fit_idx]
                        invalid_ind[chromo_idx].fitness.values = fitness_for_idx_chromo
                        GA_history_list.append([invalid_ind[chromo_idx], fitness_for_idx_chromo])


                # 전체 크로모좀 evaluation 했으니 cuda cache 한번 clear 해주기
                torch.cuda.empty_cache()

                ## log 기록
                self.train_log[gen] = GA_history_list

                self.save_log()

                eval_time_for_one_generation = time.time() - start_time
                print("\t Evaluation ends (Time : %.3f)" % eval_time_for_one_generation)

                gen_time = time.time() - start_gen_time
                print('\t [gen_time: %.3fs]' % gen_time, gen, 'th generation is finished.')

                logging.info('\t Gen [%03d/%03d] -- evals: %03d, evals_time: %.4fs, gen_time: %.4fs' % (
                    gen, self.ngen, len(invalid_ind), eval_time_for_one_generation, gen_time))            
Exemplo n.º 34
0
def main(problem: Problem = None, seed=None):
    config = problem.config
    random.seed(seed)

    # DEAP framework setup
    # We define a bi-objective fitness function.
    # 1. Maximize the sparseness minus an amount due to the distance between members
    # 2. Minimize the distance to the decision boundary
    creator.create("FitnessMulti",
                   base.Fitness,
                   weights=config.fitness_weights)
    creator.create("Individual",
                   problem.deap_individual_class(),
                   fitness=creator.FitnessMulti)

    toolbox = base.Toolbox()
    problem.toolbox = toolbox
    # We need to define the individual, the evaluation function (OOBs), mutation
    # toolbox.register("individual", tools.initIterate, creator.Individual)
    toolbox.register("individual", problem.deap_generate_individual)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    toolbox.register("evaluate", problem.deap_evaluate_individual)
    toolbox.register("mutate", problem.deap_mutate_individual)
    toolbox.register("select", tools.selNSGA2)

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    # stats.register("avg", numpy.mean, axis=0)
    # stats.register("std", numpy.std, axis=0)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)
    stats.register("avg", numpy.mean, axis=0)
    stats.register("std", numpy.std, axis=0)
    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "min", "max", "avg", "std"

    # Generate initial population.
    log.info("### Initializing population....")
    pop = toolbox.population(n=config.POPSIZE)

    # Evaluate the initial population.
    # Note: the fitness functions are all invalid before the first iteration since they have not been evaluated.
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]

    problem.pre_evaluate_members(invalid_ind)

    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    problem.archive.process_population(pop)

    # This is just to assign the crowding distance to the individuals (no actual selection is done).
    pop = toolbox.select(pop, len(pop))

    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    # Initialize the archive.
    problem.on_iteration(0, pop, logbook)

    # Begin the generational process
    for gen in range(1, config.NUM_GENERATIONS):
        # invalid_ind = [ind for ind in pop]

        # problem.pre_evaluate_members(invalid_ind)

        # Vary the population
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [ind.clone() for ind in offspring]

        problem.reseed(pop, offspring)

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        to_eval = offspring + pop
        invalid_ind = [ind for ind in to_eval]

        problem.pre_evaluate_members(invalid_ind)

        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        problem.archive.process_population(offspring + pop)
        # Select the next generation population
        pop = toolbox.select(pop + offspring, config.POPSIZE)
        record = stats.compile(pop)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        print(logbook.stream)
        problem.on_iteration(gen, pop, logbook)
    return pop, logbook
Exemplo n.º 35
0
    def train(self):
        ###################################
        # 1. Initialize the population.  (toolbox.population은 creator.Individual n개를 담은 list를 반환. (=> population)
        ###################################
        now = datetime.datetime.now()
        now_str = now.strftime('%Y-%m-%d %H:%M:%S')
        print("[GA] Initialion starts ...")
        logging.info("[GA] Initialion starts at " + now_str)
        init_start_time = time.time()

        pop = self.toolbox.population(n=self.pop_size)

        # fitness values (= accuracy, flops) 모음
        GA_history_list = []  # (choromosome, accuracy, flops) 이렇게 담아놓기
        # e.g.  [ [[1,3,4], 40, 1000], [[2,6,10], 30%, 2000], ...  ]

        ###################################
        # 2. Evaluate the population (with an invalid fitness)
        ###################################
        invalid_ind = [ind for ind in pop]

        for idx, ind in enumerate(invalid_ind):
            fitness, ind_model = evaluate_v2(
                ind,
                args_train=self.args_train,
                stage_pool_path_list=self.stage_pool_path_list,
                data_path=self.data_path,
                log_file_name=self.log_file_name)
            ind.fitness.values = fitness
            GA_history_list.append([ind, fitness])

        ## log 기록 - initialize (= 0th generation)
        self.train_log[0] = GA_history_list

        self.save_log()

        # This is just to assign the crowding distance to the individuals
        # no actual selection is done
        pop = self.toolbox.select(pop, len(pop))

        now = datetime.datetime.now()
        now_str = now.strftime('%Y-%m-%d %H:%M:%S')
        print("Initialization is finished at", now_str)
        logging.info("Initialion is finished at " + now_str)

        init_time = time.time() - init_start_time
        logging.info("Initialization time = " + str(init_time) + "s")
        print()

        ###################################
        # 3. Begin GA
        ###################################
        # Begin the generational process
        for gen in range(1, self.ngen):
            ##### 3.1. log 기록
            now = datetime.datetime.now()
            now_str = now.strftime('%Y-%m-%d %H:%M:%S')
            print("#####", gen, "th generation starts at", now_str)
            logging.info("#####" + str(gen) + "th generation starts at" +
                         now_str)

            start_gen = time.time()

            ##### 3.2. Offspring pool 생성 후, crossover(=mate) & mutation
            # Vary the population
            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [self.toolbox.clone(ind) for ind in offspring]

            # ::2, 1::2 즉, 짝수번째 크로모좀과 홀수번쨰 크로모좀들 차례로 선택하면서 cx, mut 적용
            # e.g. 0번, 1번 ind를 cx, mut   // 2번, 3번 ind를 cx, mut // ...
            for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
                if random.random() <= self.cxpb:
                    self.toolbox.mate(ind1, ind2)

                self.toolbox.mutate(ind1, indpb=self.mutpb)
                self.toolbox.mutate(ind2, indpb=self.mutpb)
                del ind1.fitness.values, ind2.fitness.values

            ##### 3.3. Evaluation
            # Evaluate the individuals with an invalid fitness
            print("\t Evaluation...")
            start_time = time.time()

            # fitness values (= accuracy, flops) 모음
            GA_history_list = []

            invalid_ind = [ind for ind in offspring]

            for idx, ind in enumerate(invalid_ind):
                fitness, ind_model = evaluate_v2(
                    ind,
                    args_train=self.args_train,
                    stage_pool_path_list=self.stage_pool_path_list,
                    data_path=self.data_path,
                    log_file_name=self.log_file_name)
                # <= evaluate() returns  (-prec, flops), NN_model

                ind.fitness.values = fitness
                GA_history_list.append([ind, fitness])

            ## log 기록
            self.train_log[gen] = GA_history_list

            self.save_log()

            eval_time_for_one_generation = time.time() - start_time
            print("\t Evaluation ends (Time : %.3f)" %
                  eval_time_for_one_generation)

            ##### Select the next generation population
            pop = self.toolbox.select(pop + offspring, self.pop_size)

            gen_time = time.time() - start_gen
            print('\t [gen_time: %.3fs]' % gen_time, gen,
                  'th generation is finished.')

            logging.info(
                '\t Gen [%03d/%03d] -- evals: %03d, evals_time: %.4fs, gen_time: %.4fs'
                % (gen, self.ngen, len(invalid_ind),
                   eval_time_for_one_generation, gen_time))
Exemplo n.º 36
0
def main(rand_seed=None):
    random.seed(rand_seed)

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("min", np.min, axis=0)
    stats.register("max", np.max, axis=0)
    stats.register("avg", np.mean, axis=0)
    stats.register("std", np.std, axis=0)
    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "min", "max", "avg", "std"

    # Generate initial population.
    print("### Initializing population ....")
    population = toolbox.population(n=POPSIZE)

    # Evaluate the individuals with an invalid fitness.
    # Note: the fitnesses are all invalid before the first iteration since they have not been evaluated
    invalid_ind = [ind for ind in population]

    # fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, itertools.repeat(population))
    # Note: the sparseness is calculated wrt the archive. It can be calculated wrt population+archive
    # Therefore, we pass to the evaluation method the current archive.
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind,
                            itertools.repeat(archive.get_archive()))
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # Update archive with the individuals on the decision boundary.
    for ind in population:
        if ind.misclass < 0:
            archive.update_archive(ind)

    print("### Number of Individuals generated in the initial population: " +
          str(Individual.COUNT))

    # This is just to assign the crowding distance to the individuals (no actual selection is done).
    population = toolbox.select(population, len(population))

    record = stats.compile(population)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    # Begin the generational process
    for gen in range(1, NGEN):
        # Vary the population.
        offspring = tools.selTournamentDCD(population, len(population))
        offspring = [toolbox.clone(ind) for ind in offspring]

        # Reseeding
        if len(archive.get_archive()) > 0:
            seed_range = random.randrange(1, RESEEDUPPERBOUND)
            candidate_seeds = archive.archived_seeds
            for i in range(seed_range):
                population[len(population) - i -
                           1] = reseed_individual(candidate_seeds)

        # Mutation.
        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals
        # NOTE: all individuals in both population and offspring are evaluated to assign crowding distance.
        invalid_ind = [ind for ind in population + offspring]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind,
                                itertools.repeat(archive.get_archive()))

        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        for ind in population + offspring:
            if ind.fitness.values[1] < 0:
                archive.update_archive(ind)

        # Select the next generation population
        population = toolbox.select(population + offspring, POPSIZE)

        if gen % 300 == 0:
            archive.create_report(x_test, Individual.SEEDS, gen)

        # Update the statistics with the new population
        record = stats.compile(population)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        print(logbook.stream)

    print(logbook.stream)

    return population
def main(seed=None, play=0, NGEN=40, MU=4 * 10):
    # random.seed(seed)

    # MU has to be a multiple of 4. period.
    CXPB = 0.9

    stats = tools.Statistics(lambda ind: ind.fitness.values[1])
    # stats.register("avg", numpy.mean, axis=0)
    # stats.register("std", numpy.std, axis=0)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"
    toolbox.register("evaluate", minimize_src)
    time1 = time.time()
    pop_src = toolbox.population(n=MU)
    time2 = time.time()
    print("After population initialisation", time2 - time1)
    print(type(pop_src))
    # print("population initialized")
    # network_obj = Neterr(indim, outdim, n_hidden, np.random)
    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop_src if not ind.fitness.valid]

    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit
    time3 = time.time()
    print("After feedforward", time3 - time2)
    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop_src = toolbox.select(pop_src, len(pop_src))
    # print( "first population selected, still outside main loop")
    # print(pop)
    record = stats.compile(pop_src)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)
    maxi = 0
    stri = ''
    flag = 0
    # Begin the generational process
    # print(pop.__dir__())
    time4 = time.time()
    for gen in range(1, NGEN):

        # Vary the population
        if gen == 1:
            time6 = time.time()
        if gen == NGEN - 1:
            time7 = time.time()
        print()
        print("here in gen no.", gen)
        offspring = tools.selTournamentDCD(pop_src, len(pop_src))
        offspring = [toolbox.clone(ind) for ind in offspring]
        if play:
            if play == 1:
                pgen = NGEN * 0.1
            elif play == 2:
                pgen = NGEN * 0.9

            if gen == int(pgen):
                print("gen:", gen, "doing clustering")
                to_bp_lis = cluster.give_cluster_head(offspring,
                                                      int(MU * bp_rate))
                assert (to_bp_lis[0] in offspring)
                print("doing bp")
                [
                    item.modify_thru_backprop(indim,
                                              outdim,
                                              network_obj_src.rest_setx,
                                              network_obj_src.rest_sety,
                                              epochs=10,
                                              learning_rate=0.1,
                                              n_par=10) for item in to_bp_lis
                ]

                # Evaluate the individuals with an invalid fitness
                invalid_ind = [
                    ind for ind in offspring if not ind.fitness.valid
                ]
                fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
                for ind, fit in zip(invalid_ind, fitnesses):
                    ind.fitness.values = fit
        if gen == 1:
            time8 = time.time()
        if gen == NGEN - 1:
            time9 = time.time()
        dum_ctr = 0
        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            flag = 0
            if random.random() <= CXPB:
                ind1, ind2 = toolbox.mate(ind1, ind2, gen)
                ind1 = creator.Individual(indim, outdim, ind1)
                ind2 = creator.Individual(indim, outdim, ind2)
                flag = 1
            maxi = max(maxi, ind1.node_ctr, ind2.node_ctr)
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)

            offspring[dum_ctr] = ind1
            offspring[dum_ctr + 1] = ind2
            del offspring[dum_ctr].fitness.values, offspring[dum_ctr +
                                                             1].fitness.values
            dum_ctr += 2
        if gen == 1:
            print("1st gen after newpool", time.time() - time8)
        if gen == NGEN - 1:
            print("last gen after newpool", time.time() - time9)

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop_src = toolbox.select(pop_src + offspring, MU)

        record = stats.compile(pop_src)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        anost = logbook.stream
        liso = [item.rstrip() for item in anost.split("\t")]
        mse = float(liso[3])

        print(anost)
        stri += anost + '\n'
        print("generation done")
        # file_ob.write(str(logbook.stream))
        # print(len(pop))
        # file_ob.close()
    time5 = time.time()
    print("Overall time", time5 - time4)
    # print(stri)
    print(
        ' ------------------------------------src done------------------------------------------- '
    )
    fronts = tools.sortNondominated(pop_src, len(pop_src))
    pareto_front = fronts[0]
    print(pareto_front)
    st = '\n\n'
    pareto_log_fileo = open(
        "./log_folder/log_pareto_just_src_nll_mse_misc_com" + str(NGEN) +
        ".txt", "a")
    for i in range(len(pareto_front)):
        print(pareto_front[i].fitness.values)
        st += str(pareto_front[i].fitness.values)
        pareto_log_fileo.write(st + '\n')
    pareto_log_fileo.close()

    return pop_src, logbook
Exemplo n.º 38
0
def multirun(datasetName):
    # init_population,init_ari,datamat,datalabels = ini_Cluster(kNumber=6) #多种聚类算法产生初始种群
    # datamat,datalabels = loadDataset("../dataset/glass.data")
    path = '../dataset/'+datasetName
    datamat,datalabels = loadDataset(path)
    print 'data ready'
    # datalabels_to_float = list(map(lambda x: float(x), datalabels))

    sampledData, remainedData, sampledIndex, remainedIndex= data_sample(datamat,1,10)
    print 'sampledData ready'
    #
    pop_kmeans = rsnn(sampledData, remainedData, sampledIndex, remainedIndex,'kmeans')
    print 'kmeans end'
    max_nmi1 = -inf
    for ind1 in pop_kmeans:
        nmi1 = normalized_mutual_info_score(datalabels, ind1)
        if nmi1 > max_nmi1:
            max_nmi1 = nmi1
    print '初始kmeans最大nmi为%s'%max_nmi1
    pop_ward = rsnn(sampledData, remainedData, sampledIndex, remainedIndex,'ward')
    print 'ward end'
    max_nmi2 = -inf
    for ind2 in pop_ward:
        nmi2 = normalized_mutual_info_score(datalabels, ind2)
        if nmi2 > max_nmi2:
            max_nmi2 = nmi2
    print '初始ward最大nmi为%s'%max_nmi2
    pop_complete = rsnn(sampledData, remainedData, sampledIndex, remainedIndex,'complete')
    print 'complete end'
    max_nmi3 = -inf
    for ind3 in pop_complete:
        nmi3 = normalized_mutual_info_score(datalabels, ind3)
        if nmi3 > max_nmi3:
            max_nmi3 = nmi3
    print '初始complete最大nmi为%s'%max_nmi3
    pop_average = rsnn(sampledData, remainedData, sampledIndex, remainedIndex,'average')
    print 'average end'
    max_nmi4 = -inf
    for ind4 in pop_average:
        nmi4 = normalized_mutual_info_score(datalabels, ind4)
        if nmi4 > max_nmi4:
            max_nmi4 = nmi4
    print '初始average最大nmi为%s'%max_nmi4
    pop = []
    pop.extend(pop_kmeans)
    pop.extend(pop_ward)
    pop.extend(pop_complete)
    pop.extend(pop_average)


    init_population = []
    for indiv1 in pop:
        ind1 = creator.Individual(indiv1)
        init_population.append(ind1)

    filter_pop = filter(lambda x:len(x)>0,init_population) ##去除初始化聚类失败的结果
    population = filter_pop #population是总的种群,后续的交叉算法的结果也要添加进来


    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, tile(datamat,(len(invalid_ind),1,1)),tile(population,(len(invalid_ind),1,1)),invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    population = toolbox.select(population, len(population))
    popeliteLen = len(population)
    for i in range(generation):
        print '第%s代'%i
        popElite = toolbox.select(population, popeliteLen) #top half from population

        # Vary the population
        parentSpring = tools.selTournamentDCD(population, len(population))
        parentSpring = [toolbox.clone(ind) for ind in parentSpring]
        newoffspring = []
        # applying crossover
        subpopArr=[]
        for subtimes in range(5): #这里循环10次,是因为subpop写死成4次,乘起来又产生40个个体,用于产生dsce运算产生40个结果,视作交叉
            subpopOneArr = getSubPop(parentSpring)
            subpopArr.extend(subpopOneArr)
        for subpop in subpopArr:
            transMatrix, popClusterArr_3, popClusterArr_2, clusterNumArr = transformation(datamat, subpop)
            similiarMatrix, unionClusterArr_2 = measureSimilarity(transMatrix, popClusterArr_3, popClusterArr_2,
                                                              clusterNumArr, datamat, a1=0.8)
            dictCownP = assign(similiarMatrix, 0.7)
            resultList = resultTransform(dictCownP, datamat)
            ind_ensemble = creator.Individual(resultList)
            newoffspring.append(ind_ensemble)


        # evaluating fitness of individuals with invalid fitnesses
        invalid_ind = [ind for ind in newoffspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, tile(datamat,(len(invalid_ind),1,1)),tile(newoffspring,(len(invalid_ind),1,1)),invalid_ind)#这里只用了未经处理的数据,没有用到真实类别
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Chossing a population for the next generation
        population = toolbox.select(popElite + newoffspring, popeliteLen)
    result1 = toolbox.nondominated(population,len(population))
    ari_arr = []
    max_ari = -inf
    for ind in result1[0]:
        ari = adjusted_rand_score(datalabels, ind)
        ari_arr.append(ari)
        if ari > max_ari:
            max_ari = ari
    nmi_arr = []
    max_nmi = -inf
    print 'nmi值'
    for ind in result1[0]:
        nmi = normalized_mutual_info_score(datalabels, ind)
        nmi_arr.append(nmi)
        if nmi > max_nmi:
            max_nmi = nmi
    print '最大nmi值为:%s' % max_nmi
    return max_nmi,max_ari
Exemplo n.º 39
0
def main_spea2(algorithm="SPEA2", seed=None, NGEN=100, MU=100):
    random.seed(seed)
    #NGEN       # Generation
    #MU     # Population Size
    CXPB = 0.9

    NDIM = 30
    BOUND_LOW = (B_RP[0], S_RP[0], B_IP[0], S_IP[0], Tb[0], TbB[0], Ts[0],
                 TsB[0], bCinc[0], bBinc[0], sCdec[0], sBdec[0], Kb[0], Ks[0],
                 Offset[0])
    BOUND_UP = (B_RP[1], S_RP[1], B_IP[1], S_IP[1], Tb[1], TbB[1], Ts[1],
                TsB[1], bCinc[1], bBinc[1], sCdec[1], sBdec[1], Kb[1], Ks[1],
                Offset[1])

    toolbox.register("mate",
                     tools.cxSimulatedBinaryBounded,
                     low=BOUND_LOW,
                     up=BOUND_UP,
                     eta=0.0)
    toolbox.register("mutate",
                     tools.mutPolynomialBounded,
                     low=BOUND_LOW,
                     up=BOUND_UP,
                     eta=0.0,
                     indpb=1.0 / NDIM)

    toolbox.register("select", tools.selSPEA2)
    toolbox.register("evaluate", evaluateInd)

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean, axis=0)
    stats.register("std", numpy.std, axis=0)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"

    pop = toolbox.population(n=MU)
    #algorithm = "NSGA2"
    print "Algorithm = ", algorithm
    print "Generation = ", NGEN
    print "Population Size = ", MU
    referencePoint = (2.0, 0, 0)
    hv = InnerHyperVolume(referencePoint)
    #front = [(1.0, 0.5128205128205128, 195.0),(1.0, 2.7732997481108312, 397.0),(1.0, 0.7787356321839081, 348.0),(1.0, 2.7732997481108312, 397.0),(1.0, 0.5339233038348082, 339.0),(1.0, 0.5128205128205128, 195.0),(1.0, 0.5128205128205128, 195.0),(1.0, 0.5128205128205128, 195.0)]

    global identifier
    statfile_purchase = genFileName(algorithm, "purchase", NGEN, MU,
                                    identifier)  # Stat Generation
    insert(statfile_purchase,
           algorithm + "_gen" + str(NGEN) + "_pop" + str(MU))

    statfile_utility = genFileName(algorithm, "utility", NGEN, MU,
                                   identifier)  # Stat Generation
    insert(statfile_utility, algorithm + "_gen" + str(NGEN) + "_pop" + str(MU))

    statfile_time = genFileName(algorithm, "time", NGEN, MU,
                                identifier)  # Stat Generation
    insert(statfile_time, algorithm + "_gen" + str(NGEN) + "_pop" + str(MU))
    """
    print ("Initial Population")
    for p in pop:
	print p
    """

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    # Begin the generational process
    for gen in range(1, NGEN):
        # Vary the population
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)

            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        oldpop = pop
        oldrecord = record
        #map(lambda x:insert(statfile_purchase,str(x.fitness.values[0])),oldpop)
        #map(lambda x:insert(statfile_utility,str(x.fitness.values[1])),oldpop)
        #map(lambda x:insert(statfile_time,str(x.fitness.values[2])),oldpop)

        #Only final generation required
        #volume = hv.compute([x.fitness.values for x in oldpop if x.fitness.values[0]==1.0])
        #print volume

        pop = toolbox.select(pop + offspring, MU)
        record = stats.compile(pop)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        print(logbook.stream)

        if stop_early(oldpop, pop, gen, oldrecord, record):
            print("Stopping generation early, no purchases")
            break

    map(lambda x: insert(statfile_purchase, str(x.fitness.values[0])), pop)
    insertnl(statfile_purchase)
    map(lambda x: insert(statfile_utility, str(x.fitness.values[1])), pop)
    insertnl(statfile_utility)
    map(lambda x: insert(statfile_time, str(x.fitness.values[2])), pop)
    insertnl(statfile_time)

    r = redis.StrictRedis(host='152.46.19.201', port=6379, db=0)
    r.flushall()  # Remove all keys once done

    return pop, logbook
Exemplo n.º 40
0
def mainNSGA(seed=None):
    with open("evaluated_inds.log", "w") as myfile:
        myfile.write("===Evaluated genomes===:\n")

    with open("runtime_pareto.log", "w") as myfile:
        myfile.write("===runtime_pareto===:\n")

    with open("runtime_time.log", "w") as myfile:
        myfile.write("===runtime_time===:\n")

    ga_data = {}

    toolbox.register("evaluate", evalOneMax)
    toolbox.register("mate", tools.cxTwoPoint)
    toolbox.register("mutate",
                     tools.mutUniformInt,
                     low=knobs_low,
                     up=knobs_up,
                     indpb=0.05)
    toolbox.register("select", tools.selNSGA2)
    random.seed(seed)
    #MU = 40
    #CXPB = 0.8
    #MUTPB = 0.8
    #NGEN = 30

    MU = 80
    CXPB = 0.8
    MUTPB = 0.8
    NGEN = 30

    pop = toolbox.population(n=MU)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    # Begin the generational process
    for gen in range(1, NGEN):
        start = time.time()
        # Vary the population
        print(" ======Beginning %i th generation======: " % gen)
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)
                del ind1.fitness.values, ind2.fitness.values
            if random.random() <= MUTPB:
                toolbox.mutate(ind1)
                toolbox.mutate(ind2)
                del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)

        fitness_list = []
        fronts_lists = tools.sortNondominated(pop,
                                              len(pop),
                                              first_front_only=True)[0]
        fronts = []

        for i in range(len(fronts_lists)):
            if fronts_lists[i] not in fronts:
                fronts.append(fronts_lists[i])
                fitness_list.append(fronts_lists[i].fitness.values)
        print " Pareto front is:"
        ga_data[gen] = {"fitness": fitness_list, "front": fronts}
        with open("runtime_pareto.log", "a") as myfile:
            myfile.write(str(ga_data[gen]) + "\n")
        end = time.time()
        with open("runtime_time.log", "a") as myfile:
            myfile.write(str(end - start) + "\n")
        pprint.pprint(fitness_list)
        pprint.pprint(fronts)

        print("  Evaluated %i individuals\n" % len(invalid_ind))

    jsonConfigFile = "./ga_data.json"
    with open(jsonConfigFile, "w") as jFile:
        json.dump(ga_data, jFile, indent=4, separators=(',', ': '))

    #with open(jsonConfigFile) as jFile:
    #   ga_data = json.load(jFile)
    #   pprint.pprint(ga_data)

    print("-- End of (successful) evolution --")

    return pop
Exemplo n.º 41
0
def NSGA(funcs_l, weights, var, sigma, MU=24, NGEN=20, wide_search=1.5):
    IND_SIZE = len(var)
    creator.create("MaFitness", base.Fitness, weights=weights)
    creator.create("Individual", list, fitness=creator.MaFitness)
    toolbox = base.Toolbox()

    eval_funcs = lambda x: tuple([f(x) for f in funcs_l])
    toolbox.register("evaluate", eval_funcs)
    S.model = var
    c = S.extract_genotype()
    init_func = lambda c, sigma, size: np.random.normal(c, sigma, size)
    bound_max = list(wide_search * c)
    bound_min = list(-wide_search * c)
    toolbox.register("attr_float", init_func, c, sigma, len(var))
    toolbox.register("individual", tools.initIterate, creator.Individual,
                     toolbox.attr_float)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    paretofront = tools.ParetoFront()
    #toolbox.register("mutate", tools.mutGaussian, mu=c, sigma=sigma, indpb=1.0 / IND_SIZE)
    toolbox.register("mutate",
                     tools.mutPolynomialBounded,
                     low=bound_min,
                     up=bound_max,
                     eta=20.0,
                     indpb=1.0 / IND_SIZE)
    toolbox.register("mate",
                     tools.cxSimulatedBinaryBounded,
                     low=bound_min,
                     up=bound_min,
                     eta=20.0)

    toolbox.register("select", tools.selNSGA2)
    CXPB = 0.6
    turing_spot = tools.Statistics(lambda ind: ind.fitness.values[0])
    rectanglitude = tools.Statistics(lambda ind: ind.fitness.values[1])

    mstats = tools.MultiStatistics(Rectanglitude=rectanglitude,
                                   Turing_Spot=turing_spot)
    mstats.register("avg", np.mean, axis=0)
    mstats.register("max", np.max, axis=0)
    mstats.register("min", np.min, axis=0)

    logbook = tools.Logbook()
    pop = toolbox.population(n=MU)
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit
    pop = toolbox.select(pop, len(pop))
    record = mstats.compile(pop)
    #print("Record  =" ,record)
    logbook.record(**record)
    print(logbook.stream)
    # Begin the generational process
    records = {}
    for gen in range(1, NGEN):
        # Vary the population
        if (gen % 5 == 0):
            S.Swarm.controller.withVisiblite(True)
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]
        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)
        record = mstats.compile(pop)
        logbook.record(g=gen, **record)
        records[gen] = record
        print(logbook.stream)

    return pop, paretofront, records
Exemplo n.º 42
0
def NSGA2(model):
    print "Model: ", model.__name__

    creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0))
    creator.create("Individual", array.array, typecode='d', fitness=creator.FitnessMin)
    toolbox = base.Toolbox()
    model = model()
    BOUND_LOW, BOUND_UP = model.domMin, model.domMax

    # Functions zdt4 has bounds x1 = [0, 1], xn = [-5, 5], with n = 2, ..., 10
    # BOUND_LOW, BOUND_UP = [0.0] + [-5.0]*9, [1.0] + [5.0]*9

    # Functions zdt1, zdt2, zdt3 have 30 dimensions, zdt4 and zdt6 have 10
    NDIM = model.decs

    def uniform(low, up, size=None):
        try:
            return [random.uniform(a, b) for a, b in zip(low, up)]
        except TypeError:
            return [random.uniform(a, b) for a, b in zip([low] * size, [up] * size)]

    toolbox.register("attr_float", uniform, BOUND_LOW, BOUND_UP, NDIM)
    toolbox.register("r_list", tools.initIterate, creator.Individual, toolbox.attr_float)
    toolbox.register("population", tools.initRepeat, list, toolbox.r_list)

    toolbox.register("evaluate", model.getObjs)
    toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0)
    toolbox.register("mutate", tools.mutPolynomialBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0, indpb=1.0 / NDIM)
    toolbox.register("select", tools.selNSGA2)
    min_val = []
    # random.seed(seed)

    NGEN = 25
    MU = 100
    CXPB = 0.9

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean, axis=0)
    stats.register("std", numpy.std, axis=0)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"

    pop = toolbox.population(n=MU)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

        # This is just to assign the crowding distance to the individuals
        # no actual selection is done
    pop = toolbox.select(pop, len(pop))
    record = stats.compile(pop)
    min_val.append(sum(record.get('min')))
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    # Begin the generational process
    for gen in range(1, NGEN):
        # Vary the population
        offspring = tools.selTournamentDCD(pop, len(pop))
        offspring = [toolbox.clone(ind) for ind in offspring]

        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= CXPB:
                toolbox.mate(ind1, ind2)

            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values

        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop = toolbox.select(pop + offspring, MU)
        record = stats.compile(pop)
        min_val.append(sum(record.get('min')))
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        print(logbook.stream)

        # print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))

    best = pop[0]
    for i in pop:
        if sum(model.getObjs(i)) < sum(model.getObjs(best)):
            best = i
    print "Best solution = "
    for i in best:
        print i

    return min_val
Exemplo n.º 43
0
def main():
    dolphin_dir = find_dolphin_dir()
    pad_path = dolphin_dir + '/Pipes/p3'
    mw_path = dolphin_dir + '/MemoryWatcher/MemoryWatcher'
    if dolphin_dir is None:
        print('Could not find dolphin config dir.')
        return

    state = p3.state.State()
    sm = p3.state_manager.StateManager(state)
    write_locations(dolphin_dir, sm.locations())

    fox = p3.fox.Fox()

    # Weights: (Minimize damage on self, Maximize damage dealt)
    creator.create("FitnessOptima", base.Fitness, weights=(-1.0, 1.0))
    creator.create("Individual", list, fitness=creator.FitnessOptima)

    toolbox = base.Toolbox()

    BOUND_LOW, BOUND_UP = -1.0, 1.0
    NDIM = 30

    def uniform(low, up, size=None):
        try:
            return [random.uniform(a, b) for a, b in zip(low, up)]
        except TypeError:
            return [
                random.uniform(a, b) for a, b in zip([low] * size, [up] * size)
            ]

    toolbox.register("attr_real", random.uniform, -1, 1)
    toolbox.register("individual", tools.initRepeat, creator.Individual,
                     toolbox.attr_real, c.nnet['n_weights'])
    toolbox.register("population",
                     tools.initRepeat,
                     list,
                     toolbox.individual,
                     n=c.game['n_agents'])

    toolbox.register("evaluate", evalANN)

    toolbox.register("mate",
                     tools.cxSimulatedBinaryBounded,
                     low=BOUND_LOW,
                     up=BOUND_UP,
                     eta=20.0)
    toolbox.register("mutate",
                     tools.mutPolynomialBounded,
                     low=BOUND_LOW,
                     up=BOUND_UP,
                     eta=20.0,
                     indpb=1.0 / NDIM)
    toolbox.register("select", tools.selNSGA2)

    CXPB, MU, NGEN = 0.9, 12, 2000

    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean, axis=0)
    stats.register("std", np.std, axis=0)
    stats.register("min", np.min, axis=0)
    stats.register("max", np.max, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "std", "min", "avg", "max"

    # pop = toolbox.population()

    # to load in trained weights:
    with open("population.txt", "r") as infile:
        pop = json.load(infile)

    # Add agents to the population
    for ind in pop:
        ann = nnet(c.nnet['n_inputs'], c.nnet['n_h_neurons'],
                   c.nnet['n_outputs'], ind)
        fox.add_agent(ann)

    print('Start dolphin now. Press ^C to stop p3.')
    with p3.pad.Pad(pad_path) as pad, p3.memory_watcher.MemoryWatcher(
            mw_path) as mw:
        fox.agent = 11
        run(fox, state, sm, mw, pad, pop,
            toolbox)  # run agent once to get past "ready, GO!" lag
        fox.agent = 0
        run(fox, state, sm, mw, pad, pop, toolbox)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]
    fitnesses = toolbox.evaluate(fox.agents)

    for ind, fit in zip(invalid_ind, fitnesses[0]):
        ind.fitness.values = tuple(fit)

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, len(pop))

    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    try:
        # Begin the generational process
        while fox.generation < NGEN:
            fox.generation += 1
            fox.reset()

            # Selection
            # offspring = toolbox.select(pop, len(pop))
            offspring = tools.selTournamentDCD(pop, len(pop))
            offspring = [toolbox.clone(ind) for ind in offspring]

            # Mating
            for child1, child2 in zip(offspring[::2], offspring[1::2]):
                if random.random() <= CXPB:
                    toolbox.mate(child1, child2)
                # Mutation
                toolbox.mutate(child1)
                toolbox.mutate(child2)
                del child1.fitness.values, child2.fitness.values

            # Add offspring to NNs
            for ind in offspring:
                ann = nnet(c.nnet['n_inputs'], c.nnet['n_h_neurons'],
                           c.nnet['n_outputs'], ind)
                fox.add_agent(ann)

            # Run game with offspring
            with p3.pad.Pad(pad_path) as pad, p3.memory_watcher.MemoryWatcher(
                    mw_path) as mw:
                run(fox, state, sm, mw, pad, offspring, toolbox)

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = toolbox.evaluate(fox.agents)
            for ind, fit in zip(invalid_ind, fitnesses[0]):
                ind.fitness.values = tuple(fit)

            # Select the next generation population
            pop = toolbox.select(pop + offspring, MU)

            # Collect stats
            record = stats.compile(pop)
            logbook.record(gen=fox.generation,
                           evals=len(invalid_ind),
                           **record)
            print(logbook.stream)
            if (fox.generation % 1 == 0):
                statistics(pop, logbook, fox.generation)

        print("Training complete")

    except KeyboardInterrupt:
        print('Stopped')
Exemplo n.º 44
0
Arquivo: nsga2.py Projeto: gaohr/SEIMS
def main(num_Gens, size_Pops, cx, seed=None):

    # toolbox.register("attr_float", iniPops)
    # toolbox.register("individual", tools.initIterate, creator.Individuals, toolbox.attr_float)
    # toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    # toolbox.register("evaluate", calBenefitandCost)
    # toolbox.register("mate", tools.cxOnePoint)
    # toolbox.register("mutate", mutModel, indpb=MutateRate)
    # toolbox.register("select", tools.selNSGA2)

    random.seed(seed)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)
    # stats.register("avg", numpy.mean, axis=0)
    # stats.register("std", numpy.std, axis=0)

    logbook = tools.Logbook()
    logbook.header = "gen", "evals", "min", "max"
    
    pop = toolbox.population(n=size_Pops)
    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in pop if not ind.fitness.valid]

    try:
        # parallel on multiprocesor or clusters using SCOOP
        from scoop import futures
        fitnesses = futures.map(toolbox.evaluate, invalid_ind)
        # print "parallel-fitnesses: ",fitnesses
    except ImportError or ImportWarning:
        # serial
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        # print "serial-fitnesses: ",fitnesses

    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # This is just to assign the crowding distance to the individuals
    # no actual selection is done
    pop = toolbox.select(pop, size_Pops)
    record = stats.compile(pop)
    logbook.record(gen=0, evals=len(invalid_ind), **record)
    print(logbook.stream)

    # Begin the generational process
    for gen in range(1, num_Gens):
        printInfo("###### Iteration: %d ######" % gen)
        # Vary the population
        offspring = tools.selTournamentDCD(pop, int(size_Pops * SelectRate))
        offspring = [toolbox.clone(ind) for ind in offspring]
        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
            if random.random() <= cx:
                toolbox.mate(ind1, ind2)
            toolbox.mutate(ind1)
            toolbox.mutate(ind2)
            del ind1.fitness.values, ind2.fitness.values
        
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        try:
            # parallel on multiprocesor or clusters using SCOOP
            from scoop import futures
            fitnesses = futures.map(toolbox.evaluate, invalid_ind)
        except ImportError or ImportWarning:
            # serial
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)

        # invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        # fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        # Select the next generation population
        pop = toolbox.select(pop + offspring, size_Pops)
        record = stats.compile(pop)
        logbook.record(gen=gen, evals=len(invalid_ind), **record)
        # print "\nlogbook.stream: ", logbook.stream

        if gen % 1 == 0:
            # Create plot
            createPlot(pop, model_Workdir, num_Gens, size_Pops, gen)
            # save in file
            outputStr = "### Generation number: %d, Population size: %d ###" % (num_Gens, size_Pops) + LF
            outputStr += "### Generation_%d ###" % gen + LF
            outputStr += "cost\tbenefit\tscenario" + LF
            for indi in pop:
                outputStr += str(indi.fitness.values[0]) + "\t" + str(indi.fitness.values[1]) + "\t" \
                             + str(indi) + LF
            outfilename = model_Workdir + os.sep + "NSGAII_OUTPUT" + os.sep + "Gen_" \
                           + str(GenerationsNum) + "_Pop_" + str(PopulationSize) + os.sep + "Gen_" \
                           + str(GenerationsNum) + "_Pop_" + str(PopulationSize) + "_resultLog.txt"
            WriteLog(outfilename, outputStr, MODE='append')
    printInfo("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0]))
    return pop, logbook