def get_gen_stats(toolbox, population):
    """
        Returns the stats for a single generation
    """
    best = tools.selBest(population, k=1)[0].fitness.values[0]
    worst = tools.selWorst(population, k=1)[0].fitness.values[0]
    return {'best': float(best), 'worst': float(worst)}
Esempio n. 2
0
        print("#  gMax %s" % gMax)
        print("#  gAvg %s" % gMean)
        print("#  gStd %s" % gStd)
        print "==" * 40

        timeNow = time.time()
        print "GAOUT: |" + "GEN[%s] lMin[%s] lMax[%s] lMean[%s] lStd[%.2f] timeUsed[%s]" % \
                (g, lMin, lMax, lMean, lStd, timeNow-timeDjDone)
        print "GAOUT: |" + "GEN[%s] gMin[%s] gMax[%s] gMean[%s] gStd[%.2f] timeUsed[%s]" % \
                (g, gMin, gMax, gMean, gStd, timeNow-timeDjDone)
        print "GAOUT: |" + "GEN[%s] Min[%s] Max[%s] Mean[%s] Std[%.2f] timeUsed[%s]" % \
                (g, Min, Max, mean, std, timeNow-timeDjDone)
        # immigrantion
        gBest = tools.selBest(gPop, 1)[0] 
        lBest = tools.selBest(lPop, 1)[0]
        lWorst = tools.selWorst(lPop, 1)[0]

        if gBest.fitness.values[0] < lBest.fitness.values[0]:
            lPop.append(gBest)
        elif gBest.fitness.values[0] < lWorst.fitness.values[0]:
            lPop.append(gBest)
        else:      
            gPop.append(lBest)

    timeGaDone = time.time()
    lPopBest = tools.selBest(lPop, 1)[0]
    print "lPopBest"
    showInd(lPopBest)
    gPopBest = tools.selBest(gPop, 1)[0]
    print "gPopBest"
    showInd(gPopBest)
    def start(self):
        # copy of the population
        pop = list(self.population)

        statistics = []

        generation = 0
        convergence = False
        while not convergence and generation < self.config.number_generations:

            # Log the fitness values of the generation
            statistics.append(np.sort(np.array([i.fitness.values for i in pop])))

            # Elitism - select elite
            elite = tools.selBest(pop, int(self.config.elitism * self.population_size))
            elite = list(map(self.toolbox.clone, elite))

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

            # Apply crossover on the offspring
            for child1, child2 in zip(offspring[::2], offspring[1::2]):
                if random.random() < self.config.crossover_rate:
                    self.toolbox.mate(child1, child2)
                    del child1.fitness.values
                    del child2.fitness.values

            # Apply mutation on the offspring
            for mutant in offspring:
                if random.random() < self.config.mutation_rate:
                    self.toolbox.mutate(mutant)
                    del mutant.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

            # Replace population with offsprings
            pop[:] = offspring

            # Elitism - Replace the K worst individuals by the elite
            worsts = tools.selWorst(pop, len(elite))
            for ind_replace in worsts:
                pop.remove(ind_replace)
            for ind in elite:
                pop.append(ind)

            # check for convergence
            if generation >= self.config.generations_to_converge:
                best_init = statistics[generation - self.config.generations_to_converge][-1]
                best_end = statistics[generation][-1]

                if abs(best_end - best_init) < self.config.threshold_converge:
                    convergence = True
            generation += 1

        statistics_matrix = np.zeros(len(statistics) * self.population_size).reshape(len(statistics), self.population_size)
        for i in range(len(statistics)):
            statistics_matrix[i, :] = statistics[i][:, 0]

        return pop, statistics_matrix
 #mutpb = probabilidade de mutação
 offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1)
 
 #Reavalia
 avg_h = 0
 fits = toolbox.map(toolbox.evaluate, offspring)
 for fit, ind in zip(fits, offspring):
     avg_h += fit[0]
     ind.fitness.values = fit
     
 #nova população
 population[:] = offspring
     
 #pega melhor e pior indivíduos para montar o gráfico
 top = tools.selBest(population, k=1)
 worst = tools.selWorst(population, k=1)
 
 avg_h = avg_h/len(population)
 top_h = nqueen_fitness(top[0])[0]
 worst_h = nqueen_fitness(worst[0])[0]
 plotlosses.update({'top': top_h,
                    'average': avg_h,
                    'worst': worst_h})
 plotlosses.send()
 
 #Avalia critério de parada
 if(nqueen_fitness(top[0])[0] == 0): 
     print(top[0])
     resultado = binToDec(top[0],log_N)
     #dataframe
     eixos = [i for i in range(N)]
 def get_worst_individual_from_pop(indivuduals):
     return tools.selWorst(indivuduals, 1)
Esempio n. 6
0
def genetic(rts, cpus):

    def func_X(a, b):
        """ length of messages transmitted from a towards b or from b towards a """
        comm_load = 0
        if "p" in a:
            for p in a["p"]:
                if p["id"] == b["id"]:
                    comm_load += p["payload"]
        # This part consideres incoming msgs from other tasks (so that task is the successor, b->a)
        # if "p" in b:
        #    for p in b["p"]:
        #        if p["id"] == a["id"]:
        #            comm_load += p["payload"]
        return comm_load

    def func_Xc(cpu_h, cpu_k):
        """ length of all messages (in bytes) to be transmitted between processors h and k through the network """
        summ = 0
        for task_h in cpu_h["tasks"]:
            for task_j in cpu_k["tasks"]:
                summ += func_X(task_h, task_j)
        return summ

    def func_Y(i, rts):
        """ load of the communication control network that the task i produces """
        comm_load = 0
        other_tasks = [t for t in rts if t is not i]
        for j in other_tasks:
            comm_load += func_X(i, j)
        return comm_load

    def func_Vp(cpus):
        """ total amount of information to be transferred over the network """
        summ = 0
        for cpu in cpus.values():
            other_cpus = [c for c in cpus.values() if c is not cpu]
            for other_cpu in other_cpus:
                summ += func_Xc(cpu, other_cpu)
        return summ

    def func_B(rts):
        """ Total amount of data to be transferred between predecessor and successors throught the network """
        summ = 0
        for task in rts:
            summ += func_Y(task, rts)
        return summ

    def func_cost_p(rts, cpus):
        return func_Vp(cpus) / func_B(rts)

    def get_cpu_alloc(individual):
        cpus_alloc = dict()
        for cpu_id in cpus.keys():
            cpus_alloc[cpu_id] = {"tasks": [], "uf": 0}  # tasks assigned to this cpu

        # A stack is assembled containing the tasks ordered by the value of the gene in decreasing order.
        task_stack = []
        for task_id, gene in enumerate(individual):
            task_stack.append((gene, rts[task_id]))
        task_stack.sort(key=lambda t: t[0], reverse=True)  # sort by gene value

        # clear previous task assignation
        #for cpu in cpus_alloc.values():
        #    cpu["tasks"].clear()

        # aux list  -- for easy sorting
        cpu_stack = [cpu for cpu in cpus_alloc.values()]

        # partition
        for _, max_task in task_stack:
            if "cpu" in max_task:
                cpu_id = max_task["cpu"]
                cpus_alloc[cpu_id]["tasks"].append(max_task)
            else:
                # create auxiliary stack with all task j that communicate with i
                aux_stack = []

                # add the succesors
                if "p" in max_task:
                    for p in max_task["p"]:
                        for task in rts:
                            if task["id"] == p["id"]:
                                aux_stack.append((func_Y(task, rts), task))

                # add other tasks that communicate with the task (the task will be the succesor)
                # for task in [t for t in rts if t is not max_task]:
                #    if "p" in task:
                #        for p in task["p"]:
                #           if p["id"] == max_task["id"]:
                #                aux_stack.append((func_Y(task, rts), task))

                cpu_a = None

                # order by func_y
                if aux_stack:
                    aux_stack.sort(key=lambda t: t[0], reverse=True)
                    aux_max_task = aux_stack[0]

                    # find the cpu at which the aux_max_task is allocated
                    for cpu in cpus_alloc.values():
                        if aux_max_task in cpu["tasks"]:
                            cpu_a = cpu

                # if not aux_stack or cpu_a is None:
                if cpu_a is None:
                    # update uf factors and allocate task to cpu with min uf
                    for cpu in cpus_alloc.values():
                        cpu["uf"] = sum([t["uf"] for t in cpu["tasks"]])
                    cpu_stack.sort(key=lambda c: c["uf"])
                    cpu_stack[0]["tasks"].append(max_task)
                else:
                    cpu_a["tasks"].append(max_task)

        # return the task allocation performed using the chromosome
        return cpus_alloc

    def cost(individual):
        # apply the cost function to the chromosome based in the cpu allocation produced
        return func_cost_p(rts, get_cpu_alloc(individual))

    def init_population(individual, rts, n):
        # generate initial population
        p_list = []

        # generate first chromosome
        chromosome = []
        for task in rts:
            g = func_Y(task, rts) * int(random.uniform(0, 1) * len(rts))
            chromosome.append(g)
        p_list.append(chromosome)

        # remaining chromosomes
        for _ in range(n - 1):
            new_chromosome = []
            nu = max(chromosome) / 10
            for g1 in chromosome:
                g2 = abs(g1 + int(random.uniform(-nu, nu)))
                new_chromosome.append(g2)
            p_list.append(new_chromosome)

        return [individual(c) for c in p_list]

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

    # Defines each individual as a list
    creator.create("Individual", list, fitness=creator.FitnessMin)

    toolbox = base.Toolbox()

    # Initialize the population
    toolbox.register("population", init_population, creator.Individual, rts)

    # Applies a gaussian mutation of mean mu and standard deviation sigma on the input individual. The indpb argument
    # is the probability of each attribute to be mutated.
    toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.01)

    # Use map to pass values
    toolbox.register("evaluate", cost)

    # Generate the initial population (first generation)
    population = toolbox.population(n=6)

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

    for _ in range(120):  # generations

        # Select the k worst individuals among the input individuals.
        population_worst = tools.selWorst(population, int(len(population) / 2))

        # Perform a roulette selection and apply a crossover to the selected individuals
        for _ in range(len(population_worst)):
            pair = tools.selRoulette(population_worst, 2)  # roulette
            tools.cxOnePoint(pair[0], pair[1])  # one point crossover

        # Mutate
        for c in population_worst:
            toolbox.mutate(c)
            del c.fitness.values  # delete the fitness value

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

    # print the final population
    print_population(population)

    # memory constraint verification
    for i, ind in enumerate(population):
        valid_cpu = True
        ch_cpus = get_cpu_alloc(ind)
        for cpuid, cpu in ch_cpus.items():
            if cpus[cpuid]["capacity"] < sum([t["r"] for t in cpu["tasks"]]):
                valid_cpu = False
        if valid_cpu:
            print_results(i, rts, ch_cpus)
        else:
            print("Chromosome {0} -- Invalid assignation found.".format(i))
Esempio n. 7
0
def evolution(Original, populationSize, NGEN, removeUserPB, removePermissionPB,
              addUserPB, addPermissionPB, numberOfTrialItems, freq,
              numberTopRoleModels):
    print("Prepare evolutionary algorithm...")
    time = []
    results = defaultdict(list)
    genStart = 0
    population = []

    # Create Logbook
    logbook = tools.Logbook()

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

    userSize = int(Original.shape[0])
    permissionSize = int(Original.shape[1])

    # Toolbox
    toolbox = base.Toolbox()
    # Chromosome generator
    toolbox.register("chromosome",
                     init.generateChromosome,
                     userSize=userSize,
                     permissionSize=permissionSize)
    # Structure initializers
    toolbox.register("individual", tools.initRepeat, creator.Individual,
                     toolbox.chromosome, 1)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    # Genetic Operators
    #toolbox.register("evaluate", evalFunc, userSize=userSize, permissionSize=permissionSize, orig=Original)
    toolbox.register("mate", operators.mateFunc, r=0.5)
    toolbox.register("mutate",
                     operators.mutFunc,
                     removeUserPB=removeUserPB,
                     removePermissionPB=removePermissionPB,
                     addUserPB=addUserPB,
                     addPermissionPB=addPermissionPB,
                     userSize=userSize,
                     permissionSize=permissionSize)
    toolbox.register("select", tools.selTournament, tournsize=5)
    #toolbox.register("select", tools.selBest, k=10)

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

    # Creating the population
    if (not population):
        print("Generate new population of " + str(populationSize) +
              " individuals")
        population = toolbox.population(n=populationSize)
        print(population)

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

    # EVALUATION
    #fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    trials = eval.evaluation(population, numberOfTrialItems, Original)

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

    # Log statistics for first generation
    if ((len(logbook) == 0)
            or (logbook.pop(len(logbook) - 1)["gen"] != genStart)):
        record = stats.compile(population)
        logbook.record(gen=genStart, evals=trials, **record)
        print("Generation " + str(genStart) + ":\t" + str(logbook.stream))

    # Begin the evolution
    print("Start evolution...")
    start = datetime.datetime.now()
    print("Start time: " + str(start))
    #hof = tools.HallOfFame(maxsize=1)

    generation = genStart + 1
    stop = False
    print("Start evolution with Generation " + str(genStart))
    while ((not stop) and (generation <= genStart + NGEN)):
        #--------------------------------------------------------------------------------------------------------
        # Neurons are then ranked by average fitness. Each neuron in the top quartile is recombined with a
        # higher-ranking neuron using 1-point crossover and mutation at low levels to create the offspring to
        # replace the lowest-ranking half of the population.
        #--------------------------------------------------------------------------------------------------------

        half = int(len(population) / 2)
        quartile = int(len(population) / 4)

        top_half_offspring = tools.selBest(population, half)
        # Clone the selected individuals
        top_half_offspring = [toolbox.clone(ind) for ind in top_half_offspring]
        top_quartile_offspring = tools.selBest(top_half_offspring, quartile)
        # Apply crossover on the offspring
        for child1, child2 in zip(top_quartile_offspring[::2],
                                  top_quartile_offspring[1::2]):
            toolbox.mate(child1, child2)
            del child1.fitness.values
            del child2.fitness.values

        low_half_offspring = tools.selWorst(population, half)
        # Clone the selected individuals
        low_half_offspring = [toolbox.clone(ind) for ind in low_half_offspring]
        # Apply mutation on the offspring
        for mutant in low_half_offspring:
            toolbox.mutate(mutant)
            del mutant.fitness.values

        # Select the next generation population
        population = list(
            itertools.chain(list(top_half_offspring),
                            list(low_half_offspring)))

        # Evaluate the individuals with an invalid fitness
        #invalid_ind = [ind for ind in population if not ind.fitness.valid]
        trials = eval.evaluation(population, numberOfTrialItems, Original)

        # Add Fitness values to results
        if generation % freq == 0:
            for ind in population:
                results[generation].append(ind.fitness.values)
            # Log statistics for generation
            record = stats.compile(population)
            logbook.record(gen=generation, evals=trials, **record)
            print("Generation " + str(generation) + ":\t" +
                  str(logbook.stream))

        generation += 1

    end = datetime.datetime.now()
    timediff = end - start
    time.append(timediff.total_seconds())
    generation -= 1
    # Print final population
    #visual.printpopulation(population)
    print("==> Generation " + str(generation))
    print("DONE.\n")

    fileExt = "_SANE_" + str(len(population)) + "_" + str(
        generation) + "_" + str(numberOfTrialItems)

    return population, results, generation, time, tools.selBest(
        population, k=numberTopRoleModels), logbook, fileExt
Esempio n. 8
0
def GenAlg(CrossoverP, MutationP, PenAdapt, population_size, budget_size):
    random.seed(64)

    # create an initial population of n individuals
    pop = toolbox.population(n=population_size)

    # allocation de tous les individus à 0
    for individual in pop:
        init = [0] * len(individual[6::7])
        individual[6::7] = init
        #print(individual[6::7])

    # CXPB  is the probability with which two individuals
    #       are crossed
    #
    # MUTPB is the probability for mutating an individual
    CXPB = CrossoverP
    MUTPB = MutationP

    # ----------------------------------------------------PREMIERE EVAL---------------------------------------------
    def feasible(individual):
        """Feasibility function for the individual. Returns True if feasible False
    otherwise."""
        if sum(individual[0::7]) < budget_size:
            return True
        return False

    def evalFct(individual, adaptiveP):
        """Evaluation function for the individual."""
        prixInd = individual[0::7]
        audienceInd = individual[5::7]

        if feasible(individual):
            return sum(prixInd), mean(audienceInd)
        else:

            return sum(prixInd) - adaptiveP, mean(
                audienceInd) - adaptiveP / 1000

    toolbox.register("evaluate", evalFct, adaptiveP=PenAdapt)

    print("Start of evolution")

    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
        #print(ind.fitness.values)

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

    # Extracting all the fitnesses of
    fits = [ind.fitness.values[0] for ind in pop]

    # Variable keeping track of the number of generations
    g = 0
    comptDim = 0
    comptAug = 0
    comptDiv = 0

    # ----------------------------------------------------DEBUT EVOLUTION---------------------------------------------
    while g < 100:
        # A new generation
        g = g + 1
        print("-- Generation %i --" % g)
        #print(pop)
        i = 0
        for inf in pop:
            print("Individuals score: %s, %s, %s" %
                  (sum(inf[0::7]), mean(inf[5::7]), evalFct(inf, PenAdapt)))
            if feasible(inf) is False:
                #print(inf)
                i = i + 1

        print("  incompatibles: %s" % i)

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

        # ----------------------------------------------------CROSSOVER----------------------------------------------------
        for child1, child2 in zip(offspring[::2], offspring[1::2]):

            # cross two individuals with probability CXPB
            if random.random() < CXPB:
                #print("before crossover %s, %s: " % (child1, child2))
                toolbox.mate(child1, child2)
                #print("after crossover %s, %s: " % (child1, child2))

                # fitness values of the children
                # must be recalculated later
                del child1.fitness.values
                del child2.fitness.values
# ----------------------------------------------------MUTATION----------------------------------------------------
        for mutant in offspring:
            alloc = mutant[6::7]
            # mutate an individual with probability MUTPB
            for y in range(len(alloc)):
                if random.random() < MUTPB:
                    #print("mutation a eu lieu ! avant: %s" % mutant[6::7])
                    if alloc[y] == 1:
                        #print("avant c'etait un %s" % alloc[y])
                        alloc[y] = 0
                        #print("maintenant c'est un %s" % alloc[y])
                    else:
                        #print("avant c'etait un %s" % alloc[y])
                        alloc[y] = 1
                        #print("maintenant c'est un %s" % alloc[y])
                    mutant[6::7] = alloc
                    #print("mutation a eu lieu ! apres: %s" % mutant[6::7])
                    del mutant.fitness.values
# ----------------------------------------------------RECALCUL FITNESS---------------------------------------------
# Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

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

        # The population is entirely replaced by the offspring
        pop[:] = offspring
        best_ind = tools.selBest(pop, 1)[0]
        print("Best individual has %s, %s" %
              (sum(best_ind[0::7]), mean(best_ind[5::7])))

        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]

        BestOne = tools.selBest(pop, 5)
        #print("  Best 3 solutions %s" % BestOne)
        # --------------------------------------------------------FEEDBACK---------------------------------------------------
        # si les 3 best sont faisables
        if all(feasible(i) for i in BestOne):
            PenAdapt = PenAdapt / 2
            print(" Pénalité diminuée ! %s" % PenAdapt)
            comptDim = comptDim + 1

        # si les 3 best sont infaisables
        elif not any(feasible(i) for i in BestOne):
            PenAdapt = PenAdapt + 5
            print(" Pénalité augmentée! %s" % PenAdapt)
            comptAug = comptAug + 1

        # si il y a de la diversité
        else:
            print(" Aucun changement de pénalité ! %s" % PenAdapt)
            comptDiv = comptDiv + 1


# ----------------------------------------------------MODIF PENALITE'---------------------------------------------------

# fitness function avec pénalité adaptative

        def adaptiveEval(individual, adaptiveP):
            prixInd = individual[0::7]
            audienceInd = individual[5::7]

            if feasible(individual):
                return sum(prixInd), mean(audienceInd)
            else:

                return sum(prixInd) - adaptiveP, mean(
                    audienceInd) - adaptiveP / 100000

        toolbox.register("evaluate", adaptiveEval, adaptiveP=PenAdapt)

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

    best_ind = tools.selBest(pop, 1)[0]
    worst_ind = tools.selWorst(pop, 1)[0]
    print(" Pénalité augmentée %s fois" % comptAug)
    print(" Pénalité diminuée %s fois" % comptDim)
    print(" Pénalité non changée %s fois" % comptDiv)
    print(" Pénalité %s" % PenAdapt)
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
    print("Best individual has %s, %s" %
          (sum(best_ind[0::7]), mean(best_ind[5::7])))
Esempio n. 9
0
def main():
    random.seed(64)
    
    pop = toolbox.population(n=100)
    CXPB, MUTPB, NGEN = 0.65, 0.08, 100
    # print("Start of evolution")
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    
    # print("  Evaluated %i individuals" % len(pop))
    
    # Begin the evolution
    evaluations = 0
    j = 0
    for g in range(NGEN):
    
    # while (evaluations < 4000):
        # print("-- Generation %i --" % g)
        
        # 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]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
    
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        
        # print("  Evaluated %i individuals" % len(invalid_ind))
        # The population is entirely replaced by the offspring
        best_ind = tools.selBest(pop, 1)[0]
        worst_ind = tools.selWorst(offspring, 1)[0]

        for i in range(len(offspring)):
            if (offspring[i] == worst_ind):
                offspring[i] = best_ind
                break

        pop[:] = offspring 
        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]
        length = len(pop)
        mean = sum(fits) / length

        j = j + 1

    # for i in range(len(pop)):         
    #     print (pop, 1)[0][i].fitness.values

    while True:
        try:            
            f = open(sys.argv[1], "a")
            flock(f, LOCK_EX | LOCK_NB)
            for i in range(len(pop)):            
                f.write(str((pop, 1)[0][i].fitness.values))
            f.write('\n')
            flock(f, LOCK_UN)
        except IOError:
            time.sleep(5)
            continue
        break
    
    best_ind = tools.selBest(pop, 1)[0]
    # print("Best individual is: %s with fitness value:  \n\n" % best_ind)

    # valor1, valor2 = 0, 0
    # for i in range(len(parte1)):
    #     # parte1[i] = individual[i]
    #     valor1 = valor1 + int((math.pow(2,i))*(best_ind[21-i]))
    # for i in range(len(parte1)):
    #     # parte2[i] = individual[i+22]
    #     valor2 = valor2 + int((math.pow(2,i))*(best_ind[43-i]))

    # valor1 = valor1 * 0.00004768372718899898
    # valor2 = valor2 * 0.00004768372718899898
    
    # valor1 = valor1 - 100
    # valor2 = valor2 - 100

    # x = valor1
    # y = valor2

    # print("Best individual is: %s with fitness value: %s \n\n" % ((x,y), best_ind.fitness.values[0]))

    # f = open(sys.argv[1], "a")
    # flock(f, LOCK_EX | LOCK_NB)
    # f.write(str((best_ind[0],best_ind[1])))
    # f.write(' ')
    # f.write(str((best_ind.fitness.values[0])))
    # f.write('\n')
    # flock(f, LOCK_UN)
    f.close()
Esempio n. 10
0
def selectTeams(individuals, k):
    return  tools.selTournament(individuals, int(0.95*len(individuals)), tournsize=4) + \
            tools.selWorst(individuals, int(0.05*len(individuals)))
Esempio n. 11
0
        print("#  gMax %s" % gMax)
        print("#  gAvg %s" % gMean)
        print("#  gStd %s" % gStd)
        print "==" * 40

        timeNow = time.time()
        print "GAOUT: |" + "GEN[%s] lMin[%s] lMax[%s] lMean[%s] lStd[%.2f] timeUsed[%s]" % \
                (g, lMin, lMax, lMean, lStd, timeNow-timeDjDone)
        print "GAOUT: |" + "GEN[%s] gMin[%s] gMax[%s] gMean[%s] gStd[%.2f] timeUsed[%s]" % \
                (g, gMin, gMax, gMean, gStd, timeNow-timeDjDone)
        print "GAOUT: |" + "GEN[%s] Min[%s] Max[%s] Mean[%s] Std[%.2f] timeUsed[%s]" % \
                (g, Min, Max, mean, std, timeNow-timeDjDone)
        # immigrantion
        gBest = tools.selBest(gPop, 1)[0]
        lBest = tools.selBest(lPop, 1)[0]
        lWorst = tools.selWorst(lPop, 1)[0]

        if gBest.fitness.values[0] < lBest.fitness.values[0]:
            lPop.append(gBest)
        elif gBest.fitness.values[0] < lWorst.fitness.values[0]:
            lPop.append(gBest)
        else:
            gPop.append(lBest)

    timeGaDone = time.time()
    lPopBest = tools.selBest(lPop, 1)[0]
    print "lPopBest"
    showInd(lPopBest)
    gPopBest = tools.selBest(gPop, 1)[0]
    print "gPopBest"
    showInd(gPopBest)
Esempio n. 12
0
def selectTeams(individuals, k):
    return  tools.selTournament(individuals, int(0.95*len(individuals)), tournsize=4) + \
            tools.selWorst(individuals, int(0.05*len(individuals)))
Esempio n. 13
0
def main():
    # random.seed(64)
    CXPB, MUTPB, NGEN = 0.9, 0.1, 100
    ano = 2010
    
    joint_log_likelihood, total_size, menor_lat, menor_long, expectations, N_ano, N = dados_observados_R(ano)
 
    global mi
    mi = float(N_ano)/float(N)
    pop = toolbox.population(n=500)
    
    # fitnesses = list(map(toolbox.evaluate, pop))
    # for ind, fit in zip(pop, fitnesses):
    #     ind.fitness.values = fit
    
    while(ano <= 2010):
        global mi
        mi = float(N_ano)/float(N)
        # Evaluate the entire population
        fitnesses = list(map(toolbox.evaluate, pop))
        for ind, fit in zip(pop, fitnesses):
            ind.fitness.values = fit
        
        # Begin the evolutionck())
        for g in range(NGEN):
            print("-- Generation %i --" % g)
            # Select the next generation individuals
            offspring = toolbox.select(pop, len(pop))
            # Clone the selected individuals
            offspring = list(map(toolbox.clone, offspring))
            print("Start of evolution")
            # Apply crossover and mutation on the offspring
            for child1, child2 in zip(offspring[::2], offspring[1::2]):
                
                if random.random() < CXPB:
                    toolbox.mate(child1, child2)
                    del child1.fitness.values
                    del child2.fitness.values
            for mutant in offspring:
                if random.random() < MUTPB:
                    toolbox.mutate(mutant, indpb=0.05)
                    del mutant.fitness.values
        
            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = list(map(toolbox.evaluate, invalid_ind))
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit
            
            print("  Evaluated %i individuals" % len(invalid_ind))
            
            # The population is entirely replaced by the offspring, but the last pop best_ind

            best_ind = tools.selBest(pop, 1)[0]
            worst_ind = tools.selWorst(offspring, 1)[0]
            
            for i in range(len(offspring)):
                if (offspring[i] == worst_ind):
                    offspring[i] = best_ind
                    break

            pop[:] = offspring    
            # fim loop GERACAO

        ano += 1

        if(ano <= 2010):
            joint_log_likelihood, total_size, menor_lat, menor_long, expectations, N_ano, N = dados_observados_R(ano)
        global mi
        mi = float(N_ano)/float(N)
        
        pop = toolbox.population(n=50)
        fitnesses = list(map(toolbox.evaluate, pop))
        for ind, fit in zip(pop, fitnesses):
            ind.fitness.values = fit

        best_ind = tools.selBest(pop, 1)[0]
        for i in range(len(best_ind)):
            global quant_por_grupo
            quant_por_grupo[i] = poisson_press(best_ind[i], mi) - 1 
 
        print quant_por_grupo
    # The population is entirely replaced by a sorted offspring
    pop[:] = offspring
    pop[:] = tools.selBest(pop, len(pop))

    # Remove worst individuals, if keeping the best of them
    if keep_best:
        pop[keep_best:] = pop[:-keep_best]
        pop[:keep_best] = best_individuals

    # Show worst and best
    bestInd = tools.selBest(pop, 1)[0]
    print(
        f"Best individual: {bestInd} \n        Fitness: {bestInd.fitness.wvalues[0]}  Feasible: {bestInd.feasible}"
    )

    worstInd = tools.selWorst(pop, 1)[0]
    print(
        f"Worst individual: {worstInd} \n        Fitness: {worstInd.fitness.wvalues[0]}  Feasible: {worstInd.feasible}"
    )

    print(
        f"Best of all individuals: {bestOfAll} \n        Fitness: {bestOfAll.fitness.wvalues[0]}  Feasible: {bestOfAll.feasible}"
    )

    # Statistics
    fits = [sum(ind.fitness.wvalues) for ind in pop]

    length = len(pop)
    mean = sum(fits) / length
    sum2 = sum(x * x for x in fits)
    std = abs(sum2 / length - mean**2)**0.5
Esempio n. 15
0
def main(CrossoverP, MutationP, PenAdapt):
    random.seed(64)

    # create an initial population of 300 individuals (where
    # each individual is a list of integers)
    pop = toolbox.population(n=10)

    # CXPB  is the probability with which two individuals
    #       are crossed
    #
    # MUTPB is the probability for mutating an individual
    CXPB = CrossoverP
    MUTPB = MutationP

    def evalFct(individual, adaptiveP):
        """Evaluation function for the individual."""
        x1 = individual[0]
        x2 = individual[1]
        x3 = individual[2]

        if feasible(individual):
            return (x1 - 25)**2 * sin(x2) * (x3 / 3),
        else:
            #
            return ((x1 - 25)**2 * sin(x2) * (x3 / 3)) / adaptiveP,

    toolbox.register("evaluate", evalFct, adaptiveP=PenAdapt)

    print("Start of evolution")

    # Evaluate the entire population ---WIP---
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
        #print(ind.fitness.values)

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

    # Extracting all the fitnesses of
    fits = [ind.fitness.values[0] for ind in pop]

    # Variable keeping track of the number of generations
    g = 0
    comptDim = 0
    comptAug = 0
    comptDiv = 0

    # Begin the evolution
    while g < 100:
        # A new generation
        g = g + 1
        print("-- Generation %i --" % g)
        #print(pop)
        i = 0
        for inf in pop:
            if feasible(inf) is False:
                #print(inf)
                i = i + 1
        print("  incompatibles: %s" % i)

        # 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]):

            # cross two individuals with probability CXPB
            if random.random() < CXPB:
                toolbox.mate(child1, child2)

                # fitness values of the children
                # must be recalculated later
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:

            # mutate an individual with probability MUTPB
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

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

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

        # The population is entirely replaced by the offspring
        pop[:] = offspring

        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]

        length = len(pop)
        mean = sum(fits) / length
        #sum2 = sum(x * x for x in fits)
        #std = abs(sum2 / length - mean ** 2) ** 0.5

        i = 0

        BestOne = tools.selBest(pop, 5)
        print("  Min %s" % min(fits))
        print("  Max %s" % max(fits))
        print("  Avg %s" % mean)
        #print("  Std %s" % std)
        print("  Best 3 solutions %s" % BestOne)

        # si les 3 best sont faisables
        if all(feasible(i) for i in BestOne):
            PenAdapt = PenAdapt / 12
            print(" Pénalité diminuée ! %s" % PenAdapt)
            comptDim = comptDim + 1

        # si les 3 best sont infaisables
        elif not any(feasible(i) for i in BestOne):
            PenAdapt = PenAdapt * 2
            print(" Pénalité augmentée! %s" % PenAdapt)
            comptAug = comptAug + 1

        # si il y a de la diversité
        else:
            print(" Aucun changement de pénalité ! %s" % PenAdapt)
            comptDiv = comptDiv + 1

        # fitness function avec pénalité adaptative

        def adaptiveEval(individual, adaptiveP):
            x1 = individual[0]
            x2 = individual[1]
            x3 = individual[2]

            if feasible(individual):
                return (x1 - 25)**2 * sin(x2) * (x3 / 3),
            else:
                #
                return ((x1 - 25)**2 * sin(x2) * (x3 / 3)) / adaptiveP,

        toolbox.register("evaluate", adaptiveEval, adaptiveP=PenAdapt)

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

    best_ind = tools.selBest(pop, 1)[0]
    worst_ind = tools.selWorst(pop, 1)[0]
    print(" Pénalité augmentée %s fois" % comptAug)
    print(" Pénalité diminuée %s fois" % comptDim)
    print(" Pénalité non changée %s fois" % comptDiv)
    print(" Pénalité %s" % PenAdapt)
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
    print("Worst individual is %s, %s" % (worst_ind, worst_ind.fitness.values))
Esempio n. 16
0
def ga(treino_, teste_):
    treino = iniciarDados(treino_)
    teste = iniciarDados(teste_)

    MINIMO = 10**10
    MAXIMO = -1 * MINIMO

    for lista in treino_:
        if (min(lista) < MINIMO):
            MINIMO = min(lista)
        if (max(lista) > MAXIMO):
            MAXIMO = max(lista)

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

    toolbox = base.Toolbox()

    toolbox.register("atribuirValor", random.uniform, MINIMO,
                     MAXIMO + abs(MAXIMO - MINIMO) / 2)

    toolbox.register("individual", tools.initRepeat, creator.Individual,
                     toolbox.atribuirValor, K * TAM)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    toolbox.register("evaluate", avaliarFitness, pontos=treino)
    toolbox.register("evaluateTeste", avaliarFitness, pontos=teste)
    toolbox.register("mate", tools.cxTwoPoint)
    toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.5)
    toolbox.register("select", tools.selTournament, tournsize=3)

    pop = toolbox.population(n=250)

    NGEN = 100
    CXPB = .75
    MUTPB = .1

    # Avaliando os individuos.
    # Para cada par (ind, fit), atribua o valor fitness para o respectivo individuo.

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

    # Evolucao:
    for g in range(NGEN):
        print("\n-- Generation %i --" % g)

        # Selecao da proxima geracao.
        offspring = toolbox.select(pop, len(pop))
        # Duplica os selecionados.

        offspring = list(map(toolbox.clone, offspring))

        # Crossover na populacao, cada par e composto por um elemento impar e um par da lista.
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            # Levando em conta o sucesso definido para crossover...
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values

        # Chance de mutacao.
        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        # Verificando se os fitness sao validos, e corrigindo caso contrario.
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

        pop[:] = tools.selBest(offspring + tools.selBest(pop, 2), len(pop))
        # pop[:] = offspring

        # Array com todos os fitness para estatisticas.
        fits = [ind.fitness.values[0] for ind in pop]

        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x * x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5

        print("  Min: ", round(min(fits), 2))  # Valor maximo.
        print("  Max: ", round(max(fits), 2))  # Valor minimo.
        print("  Avg: ", round(mean, 2))  # Valor medio.
        print("  Std: ", round(std, 2))  # Desvio padrao da geracao.
        # print(" Melhor Individuo:\n", tools.selBest(pop, 1))
        # print(" Pior Individuo:\n", tools.selWorst(pop, 1))

        if (CXPB > .60):
            CXPB -= .001

        if (MUTPB < .20):
            MUTPB += .001

    fitnesses = list(map(toolbox.evaluateTeste, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    fits = [ind.fitness.values[0] for ind in pop]

    length = len(pop)
    mean = sum(fits) / length
    sum2 = sum(x * x for x in fits)
    std = abs(sum2 / length - mean**2)**0.5

    print("\n\nAplicando os centroides nos dados de teste: ")
    print("  Min: ", round(min(fits), 2))  # Valor maximo.
    print("  Max: ", round(max(fits), 2))  # Valor minimo.
    print("  Avg: ", round(mean, 2))  # Valor medio.
    print("  Std: ", round(std, 2))  # Desvio padrao da geracao.
    print(" Melhor Individuo:\n", tools.selBest(pop, 1))
    print(" Pior Individuo:\n", tools.selWorst(pop, 1))
Esempio n. 17
0
def run_pso(instance_name,
            particle_size,
            pop_size,
            max_iteration,
            cognitive_coef,
            social_coef,
            s_limit=3,
            plot=False,
            save=False,
            logs=False):

    instance = load_problem_instance(instance_name)

    if instance is None:
        return

    if plot:
        plot_instance(instance_name=instance_name,
                      customer_number=particle_size)

    creator.create("FitnessMax", base.Fitness, weights=(1.0, ))
    creator.create("Particle",
                   list,
                   fitness=creator.FitnessMax,
                   speed=list,
                   smin=None,
                   smax=None,
                   best=None)

    toolbox = base.Toolbox()
    toolbox.register("particle",
                     generate_particle,
                     size=particle_size,
                     val_min=1,
                     val_max=particle_size,
                     s_min=-s_limit,
                     s_max=s_limit)
    toolbox.register("population", tools.initRepeat, list, toolbox.particle)
    toolbox.register("update",
                     update_particle,
                     phi1=cognitive_coef,
                     phi2=social_coef)
    toolbox.register('evaluate', calculate_fitness, data=instance)

    pop = toolbox.population(n=pop_size)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean)
    stats.register("std", numpy.std)
    stats.register("min", numpy.min)
    stats.register("max", numpy.max)

    logbook = tools.Logbook()
    logbook.header = ["gen", "evals"] + stats.fields

    best = None
    iter_num = 0
    previous_best = 0

    print('### EVOLUTION START ###')
    start = time.time()

    for g in range(max_iteration):

        fit_count = 0
        for part in pop:
            part.fitness.values = toolbox.evaluate(part)
            if part.fitness.values[0] > previous_best:
                previous_best = part.fitness.values[0]
                iter_num = g + 1
            elif part.fitness.values[0] == previous_best:
                fit_count += 1

        if fit_count > int(numpy.ceil(pop_size * 0.15)):
            rand_pop = toolbox.population(n=pop_size)
            for part in rand_pop:
                part.fitness.values = toolbox.evaluate(part)
            some_inds = tools.selRandom(
                rand_pop, int(numpy.ceil(pop_size * 0.1)))  # random pop here
            mod_pop = tools.selWorst(pop, int(numpy.ceil(pop_size * 0.9)))
        else:
            some_inds = tools.selBest(pop, int(numpy.ceil(
                pop_size * 0.05)))  # elite pop here
            mod_pop = tools.selRandom(pop, int(numpy.ceil(pop_size * 0.95)))

        mod_pop = list(map(toolbox.clone, mod_pop))

        for part in mod_pop:
            if not part.best or part.best.fitness < part.fitness:
                part.best = creator.Particle(part)
                part.best.fitness.values = part.fitness.values
            if not best or best.fitness < part.fitness:
                best = creator.Particle(part)
                best.fitness.values = part.fitness.values

        for part in mod_pop:
            toolbox.update(part, best)

        mod_pop.extend(some_inds)
        pop[:] = mod_pop

        # Gather all the stats in one list and print them
        logbook.record(gen=g + 1, evals=len(pop), **stats.compile(pop))
        print(logbook.stream)

    end = time.time()
    print('### EVOLUTION END ###')
    best_ind = tools.selBest(pop, 1)[0]
    print(f'Best individual: {best_ind}')
    route = create_route_from_ind(best_ind, instance)
    print_route(route)
    print(f'Fitness: { round(best_ind.fitness.values[0],2) }')
    print(f'Total cost: { round(calculate_fitness(best_ind, instance)[1],2) }')
    print(f'Found in (iteration): { iter_num }')
    print(f'Execution time (s): { round(end - start,2) }')
    # print(f'{round(best_ind.fitness.values[0], 2)} & {round(calculate_fitness(best_ind, instance)[1],2)} & {iter_num} & {round(end - start, 2)}')

    if plot:
        plot_route(route=route, instance_name=instance_name)

    return route
Esempio n. 18
0
def genetic_algorithm(pop):
    "Implementacion del GA en una poblacion dada (pop)"

    print "Start of the Genetic Algorithm"

    list_max = []  # lista de maximas de las generaciones de esta simulacion
    list_ave = []  # lista de promedio de las generaciones de esta simulacion
    list_imp_rate = [
    ]  # lista de mejora del fitness percentual a traves de las
    # generaciones
    prev_best_fitness = 0
    improv_rate = 0
    valid_solu_flag_in = 0
    valid_solu_flag_out = 0
    valid_gen = -1

    ##        Inicio de GA

    # Evaluacion de toda la poblacion
    fitnesses = list(map(toolbox.evaluate, pop))
    #        print pop
    for ind, fit in zip(pop, fitnesses):
        #            print fit
        ind.fitness.values = fit

#==============================================================================
#     # Inicio de Evolucion
#==============================================================================
    for g in range(param.NGEN):

        gen_best = []

        # Seleccion de inviduos para la proxima generacion
        offspring = toolbox.select1(pop, int(
            param.POPU *
            (1 - param.ELIT_RATE)))  # Se aplicaran operadores geneticos
        offspring_aux = toolbox.select2(pop, int(param.POPU *
                                                 param.ELIT_RATE))  # Elitismo
        # Clonacion de los individuos elegidos
        offspring = list(map(toolbox.clone, offspring))

        # Operadores geneticos
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            #                print child1, child2
            if random.random() < param.CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            if random.random() < param.MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        offspring = offspring + offspring_aux  # Se agrega elitismo

        # Se evaluan individuos con fitness invalidos, y se calcula los valores de ellos
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit

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

# Se reemplaza la poblacion con los hijos
        pop[:] = offspring
        # Se juntan los fitness de la poblacion en una lista
        fits = [ind.fitness.values[0] for ind in pop]

        length = len(pop)
        mean = sum(fits) / length
        #            sum2 = sum(x*x for x in fits)
        #            std = abs(sum2 / length - mean**2)**0.5

        list_max.append(max(fits))
        list_ave.append(mean)

        if g > 0:
            improv_rate = (max(fits) -
                           prev_best_fitness) * 100 / prev_best_fitness

#            print g, max(fits),improv_rate
        list_imp_rate.append(improv_rate)
        prev_best_fitness = max(fits)
        gen_best = tools.selBest(pop, 1)[0]

        ## Se verifica si el mejor individuo de la generacion es valido
        if valid_solu_flag_out == 0 and g > 0:
            for idx, indiv in enumerate(
                    gen_best):  # verificacion en matriz de rutas validas
                if idx != (len(gen_best) - 1):
                    if int(param.arr_allowed_routes[param.arr_subgroup[
                            gen_best[idx]]][param.arr_subgroup[gen_best[
                                idx + 1]]]) == 1:
                        valid_solu_flag_in = 0
                        break
                    else:
                        valid_solu_flag_in = 1
            if valid_solu_flag_in == 1:
                valid_solu_flag_out = 1
                valid_gen = g
#                 print "valid gen", valid_gen

##    print("-- End of (successful) evolution --")
    best_ind = tools.selBest(pop, 1)[0]
    worst_ind = tools.selWorst(pop, 1)[0]

    ##    print evaluation(best_ind), max(fits)

    last_pop = pop

    #==============================================================================
    #     ## Se verifica si el mejor individuo de la generacion es valido
    #         if valid_solu_flag == 0:
    #             for idx, indiv in enumerate(best_ind): # verificacion en matriz de rutas validas
    #                 if idx != (len(best_ind)-1):
    #                     if int(arr_allowed_routes[best_ind[idx]][best_ind[idx+1]]) != 1:
    #                         valid_solu_flag = 1
    #
    #         if valid_solu_flag == 1:
    #             print g
    #             valid_gen = g
    #
    #         n_ruta_inval = 0 # contador de rutas invalidas en individuo
    #
    #         for idx, indiv in enumerate(best_ind): # verificacion en matriz de rutas validas
    #             if idx != (len(best_ind)-1):
    #                 if int(arr_allowed_routes[best_ind[idx]][best_ind[idx+1]]) == 1:
    #                     n_ruta_inval += 1
    #
    #         print n_ruta_inval
    #==============================================================================

    #    Se retornan indicadores del GA
    return (best_ind, max(fits), worst_ind, list_max, list_ave, list_imp_rate,
            valid_gen, last_pop)  #, pop, list_max
Esempio n. 19
0
    def optimize(self, ngen=NGEN):
        # random.seed(64)

        # create an initial population of 300 individuals (where
        # each individual is a list of integers)
        pop = self.toolbox.population(n=NPOP)

        # エリート保存個体
        hof = tools.HallOfFame(1)

        # CXPB  is the probability with which two individuals
        #       are crossed
        #
        # MUTPB is the probability for mutating an individual
        # CXPB, MUTPB = 0.5, 0.2

        print("Start of evolution")

        # Evaluate the entire population
        fitnesses = list(map(self.toolbox.evaluate, pop))
        for ind, fit in zip(pop, fitnesses):
            ind.fitness.values = fit

        # エリート個体の更新
        hof.update(pop)

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

        # Extracting all the fitnesses of
        fits = [ind.fitness.values[0] for ind in pop]

        # Variable keeping track of the number of generations
        g = 0

        # Begin the evolution
        while g < ngen:
            # A new generation
            g = g + 1
            print("-- Generation %i --" % g)

            # Select the next generation individuals
            if self.select_num:
                # 選択個体数指定の場合
                offspring = self.toolbox.select(pop, self.select_num)
            else:
                offspring = self.toolbox.select(pop, len(pop))

            # Clone the selected individuals
            offspring = list(map(self.toolbox.clone, offspring))

            # Apply crossover and mutation on the offspring
            for child1, child2 in zip(offspring[::2], offspring[1::2]):

                # cross two individuals with probability CXPB
                if random.random() < CXPB:
                    self.toolbox.mate(child1, child2)

                    # fitness values of the children
                    # must be recalculated later
                    del child1.fitness.values
                    del child2.fitness.values

            for mutant in offspring:

                # mutate an individual with probability MUTPB
                if random.random() < MUTPB:
                    self.toolbox.mutate(mutant)
                    del mutant.fitness.values

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

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

            # The population is entirely replaced by the offspring
            if self.select_num:
                # 選択個体数指定の場合
                pop.extend(offspring)
                rmv = tools.selWorst(pop, self.select_num)
                for r in rmv:
                    pop.remove(r)
                # print("len(pop)", len(pop))
                # print("len(offspring)", len(offspring))
            else:
                pop[:] = offspring

            # エリート個体の更新
            hof.update(pop)

            # Gather all the fitnesses in one list and print the stats
            fits = [ind.fitness.values[0] for ind in pop]

            length = len(pop)
            mean = sum(fits) / length
            sum2 = sum(x * x for x in fits)
            std = abs(sum2 / length - mean**2)**0.5

            print("  Min %s" % min(fits))
            print("  Max %s" % max(fits))
            print("  Avg %s" % mean)
            print("  Std %s" % std)

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

        # ベストパラメータ=最適解に最も近づいた値
        # best_ind = tools.selBest(pop, 1)[0]
        best_ind = hof.items[0]
        print("Best individual is %s, %s" %
              (best_ind, best_ind.fitness.values))
        best_parameters = self.getX(best_ind)
        print(best_parameters)

        if self.cb_end:
            # 終了コールバック:Unityアプリへベストパラメータを通知する
            self.cb_end(best_parameters)
    avg_list.append(mean)
    max_list.append(g_max)
    min_list.append(g_min)

    print("  Min %s" % g_min)
    print("  Max %s" % g_max)
    print("  Avg %s" % mean)
    print("  Std %s" % std)

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

best_ind = tools.selBest(pop, 1)[0]
print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))

worst_ind = tools.selWorst(pop, 1)[0]
print("Worst individual is %s, %s" % (worst_ind, worst_ind.fitness.values))

paretoFront = tools.ParetoFront()
paretoFront.update(pop)
print("Pareto Front:")
for p in paretoFront:
    print("%s, %s" % (p, p.fitness.values))

plt.plot(gen, avg_list, label="average")
plt.plot(gen, min_list, label="minimum")
plt.plot(gen, max_list, label="maximum")
plt.xlabel("Generation")
plt.ylabel("Fitness")
plt.legend(loc="upper right")
plt.show()
Esempio n. 21
0
def cluster_GA(
    nPool,
    eleNames,
    eleNums,
    eleRadii,
    generations,
    calc,
    filename,
    log_file,
    CXPB=0.5,
    singleTypeCluster=False,
    use_dask=False,
    use_vasp=False,
    al_method=None,
    al_learner_params=None,
    train_config=None,
    optimizer=BFGS,
    use_vasp_inter=False,
):
    """
    DEAP Implementation of the GIGA Geneting Algorithm for nanoclusters

    nPool : Total number of clusters present in the initial pool
    eleNames : List of element symbols present in the cluster
    eleNums : List of the number of atoms of each element present in the cluster
    eleRadii : List of radii of each element present in the cluster
    generations : Total number of generations to run the genetic algorithm
    calc : The calculator used to perform relaxations (must be an ase calculator object)
    filename : Name of the file to be used to generate ase traj and db files
    log_file : Name of the log file
    CXPB : probability of a crossover operation in a given generation
    singleTypeCluster : Default = False, set to True if only 1 element is present in cluster
    use_Dask : Default = False, set to True if using dask (Refer examples on using dask)
    use_vasp : Default = False, set to True if using inbuilt vasp optimizer to run GA code (not supported with active learning)
    al_method : Default = None, accepts values 'online' or 'offline'
    al_learner_params : Default = None, refer examples or https://github.com/ulissigroup/al_mlp for sample set up
    trainer_config : Default = None, refer examples or https://github.com/ulissigroup/al_mlp for sample set up
    optimizer : Default = BFGS, ase optimizer to be used
    use_vasp_inter : Default = False, whether to use vasp interactive mode or not
    """
    def calculate(atoms):
        """
        Support function to assign the type of minimization to e performed (pure vasp, using ase optimizer or using active learning)
        """
        if al_method is not None:

            atoms_min, parent_calls, parent_dataset = minimize_al(
                atoms,
                calc,
                eleNames,
                al_learner_params,
                train_config,
                dataset_parent,
                optimizer,
                al_method,
            )
        else:
            if use_vasp == True:
                atoms_min = minimize_vasp(atoms, calc)
            else:
                atoms_min = minimize(atoms, calc, optimizer, use_vasp_inter)
        return atoms_min, parent_calls, parent_dataset

    if al_method is not None:
        al_method = al_method.lower()

    # Creating DEAP types
    creator.create("FitnessMax", base.Fitness, weights=(1.0, ))
    creator.create("Individual", list, fitness=creator.FitnessMax)

    # Registration of the evolutionary tools in the toolbox
    toolbox = base.Toolbox()
    toolbox.register("poolfill", fillPool, eleNames, eleNums, eleRadii, calc)
    toolbox.register("individual", tools.initRepeat, creator.Individual,
                     toolbox.poolfill, 1)
    toolbox.register("evaluate", fitness_func)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

    # Registering mutations and crossover operators
    toolbox.register("mate", mate)
    toolbox.register("mutate_homotop", homotop)
    toolbox.register("mutate_rattle", rattle_mut)
    toolbox.register("mutate_rotate", rotate_mut)
    toolbox.register("mutate_twist", twist)
    toolbox.register("mutate_tunnel", tunnel)
    toolbox.register("mutate_partialinv", partialInversion)
    toolbox.register("mutate_skin", skin)
    toolbox.register("mutate_changecore", changeCore)

    # Registering selection operator
    toolbox.register("select", tools.selTournament)

    population = toolbox.population(n=nPool)
    # Creating a list of cluster atom objects from population
    pop_list = []
    for individual in population:
        pop_list.append(individual[0])
    write('init_pop_before_relax.traj', pop_list)

    #Initialize the parent dataset

    dataset_parent = []

    if use_dask == True:
        # distribute and run the calculations (requires dask and needs to be set up correctly)
        clus_bag = db.from_sequence(pop_list, partition_size=1)
        clus_bag_computed = clus_bag.map(calculate)
        lst_clus_min = clus_bag_computed.compute()

    else:
        lst_clus_min = list(map(calculate, pop_list))
        #print('lst_clus_min, parent_calls, dataset_parent')
        #print(lst_clus_min )
        #for i in range(len(lst_clus_min)):
        #print(lst_clus_min[i][0])
        #print(lst_clus_min[i][1])
        #print(lst_clus_min[i][2])

    for i, p in enumerate(population):
        p[0] = lst_clus_min[i][0]

    init_pop_list_after_relax = []
    for individual in population:
        init_pop_list_after_relax.append(individual[0])
    write('init_pop_after_relax.traj', init_pop_list_after_relax)
    with open(log_file, "a+") as fh:
        fh.write(
            f'Total clusters in the intital pool after relaxationi: {len(population)}'
            "\n")

    #parent_calls list if online learner
    parent_calls_initial_pool = []
    parent_data_initial_pool = []
    for i in range(len(lst_clus_min)):
        parent_calls_initial_pool.append(lst_clus_min[i][1])
        parent_data_initial_pool.append(lst_clus_min[i][2])
    #print(parent_calls_initial_pool)
    #print(parent_data_initial_pool)

    parent_data_initial_pool_flatten = [
        item for sublist in parent_data_initial_pool for item in sublist
    ]
    #print(len(parent_data_initial_pool_flatten))

    dataset_parent = parent_data_initial_pool_flatten[len(dataset_parent):]
    print('length of dataset_parent after initial pool relaxation')
    print(len(dataset_parent))
    #print(dataset_parent)
    print('parent calls after initial pool relaxation')
    print(parent_calls_initial_pool)
    print('Total parent calls after initial pool relaxation')
    print(sum(parent_calls_initial_pool))
    with open(log_file, "a+") as fh:
        fh.write(
            f'length of dataset_parent after initial pool relaxation: {len(dataset_parent)}'
            '\n')
        fh.write(
            f'parent calls after initial pool relaxation: {parent_calls_initial_pool}'
            '\n')
        fh.write(
            f'Total parent calls after initial pool relaxation: {sum(parent_calls_initial_pool)}'
            '\n')

    # Fitnesses (or Energy) values of the initial random population
    fitnesses = list(map(toolbox.evaluate, population))

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

    #Removing bad geometries
    population_filter = []
    for i, p in enumerate(population):
        if checkBonded(p[0]) == True:
            if checkOverlap(p[0]) == False:
                population_filter.append(p)
    population = copy.deepcopy(population_filter)

    init_pop_list_after_filter = []
    for individual in population:
        init_pop_list_after_filter.append(individual[0])
    write('init_pop_after_filter.traj', init_pop_list_after_filter)
    with open(log_file, "a+") as fh:
        fh.write(
            f'Total clusters in the intital pool after filtering: {len(population)}'
            "\n")

    fitnesses_init_pool = list(map(toolbox.evaluate, population))
    with open(log_file, "a+") as fh:
        fh.write("Energies (fitnesses) of the initial pool" "\n")
        for value in fitnesses_init_pool:
            fh.write("{} \n".format(value[0]))

    # Evolution of the Genetic Algorithm
    with open(log_file, "a+") as fh:
        fh.write("\n")
        fh.write("Starting Evolution" "\n")

    g = 0  # Generation counter

    init_pop_db = ase.db.connect("init_pop_{}.db".format(filename))
    for cl in population:
        write_to_db(init_pop_db, cl[0])

    bi = []

    while g < generations:
        mutType = None
        muttype_list = []
        g = g + 1
        with open(log_file, "a+") as fh:
            fh.write("{} {} \n".format("Generation", g))
        print('generation:', g)

        cm_pop = []
        if random.random() < CXPB:  # Crossover Operation
            mutType = "crossover"
            with open(log_file, "a+") as fh:
                fh.write("{} {} \n".format("mutType", mutType))

            # Crossover operation step.
            # The child clusters will be checked for bonding and similarity
            # between other child clusters.
            loop_count = 0
            while (
                    loop_count != 200
            ):  # Perform 200 possible crossovers or until unique crossovers match pool size
                clusters = toolbox.select(population, 2, 1)
                muttype_list.append(mutType)
                parent1 = copy.deepcopy(clusters[0])
                parent2 = copy.deepcopy(clusters[1])
                fit1 = clusters[0].fitness.values
                (f1, ) = fit1
                fit2 = clusters[1].fitness.values
                (f2, ) = fit2
                toolbox.mate(parent1[0], parent2[0], f1, f2)

                diff_list = []
                if checkBonded(parent1[0]) == True:
                    if loop_count == 0:
                        cm_pop.append(parent1)
                    else:
                        for c, cluster in enumerate(cm_pop):
                            diff = checkSimilar(cluster[0], parent1[0])
                            diff_list.append(diff)

                        if all(diff_list) == True:
                            cm_pop.append(parent1)
                loop_count = loop_count + 1

                if len(cm_pop) == nPool:
                    break

        else:  # Mutation Operation
            mutType = "mutations"
            with open(log_file, "a+") as fh:
                fh.write("{} {} \n".format("mutType", mutType))

                # Mutation opeation step
                # Each cluster in the population will undergo a randomly chosen mutation step
                # Mutated new clusters will be checked for bonding and similarity with other new clusters
            for m, mut in enumerate(population):
                mutant = copy.deepcopy(mut)
                if singleTypeCluster:
                    mutType = random.choice([
                        "rattle",
                        "rotate",
                        "twist",
                        "partialinv",
                        "tunnel",
                        "skin",
                        "changecore",
                    ])
                else:
                    mutType = random.choice([
                        "rattle",
                        "rotate",
                        "homotop",
                        "twist",
                        "partialinv",
                        "tunnel",
                        "skin",
                        "changecore",
                    ])

                muttype_list.append(mutType)

                if mutType == "homotop":
                    mutant[0] = toolbox.mutate_homotop(mutant[0])
                if mutType == "rattle":
                    mutant[0] = toolbox.mutate_rattle(mutant[0])
                if mutType == "rotate":
                    mutant[0] = toolbox.mutate_rotate(mutant[0])
                if mutType == "twist":
                    mutant[0] = toolbox.mutate_twist(mutant[0])
                if mutType == "tunnel":
                    mutant[0] = toolbox.mutate_tunnel(mutant[0])
                if mutType == "partialinv":
                    mutant[0] = toolbox.mutate_partialinv(mutant[0])
                if mutType == "skin":
                    mutant[0] = toolbox.mutate_skin(mutant[0])
                if mutType == "changecore":
                    mutant[0] = toolbox.mutate_changecore(mutant[0])

                diff_list = []
                if checkBonded(mutant[0]) == True:
                    for c, cluster in enumerate(cm_pop):
                        diff = checkSimilar(cluster[0], mutant[0])
                        diff_list.append(diff)

                    if all(diff_list) == True:
                        cm_pop.append(mutant)

            with open(log_file, "a+") as fh:
                fh.write("{} {} \n".format("mutType_list", muttype_list))

        mut_new_lst = []
        for mut in cm_pop:
            mut_new_lst.append(mut[0])
        write('mut_before_relax_gen' + str(g) + '.traj', mut_new_lst)

        # DASK Parallel relaxation of the crossover child/mutated clusters
        if use_dask == True:
            mut_bag = db.from_sequence(mut_new_lst, partition_size=1)
            mut_bag_computed = mut_bag.map(calculate)
            mut_new_lst_min = mut_bag_computed.compute()

        else:
            mut_new_lst_min = list(map(calculate, mut_new_lst))
            #print('mut_new_lst_min, parent_calls, dataset_parent')
            #print(mut_new_lst_min )
            #for i in range(len(mut_new_lst_min)):
            #print(mut_new_lst_min[i][0])
            #print(mut_new_lst_min[i][1])
            #print(mut_new_lst_min[i][2])

        for i, mm in enumerate(cm_pop):
            mm[0] = mut_new_lst_min[i][0]

        mut_list_after_relax = []
        for individual in cm_pop:
            mut_list_after_relax.append(individual[0])
        write('mut_after_relax_gen' + str(g) + '.traj', mut_list_after_relax)
        with open(log_file, "a+") as fh:
            fh.write(
                f'Total clusters relaxed in  Generation {g}: {len(cm_pop)}'
                "\n")

        #parent calls list if online learner
        parent_calls_mut_list = []
        parent_data_mut_list = []
        parent_data_mut_gen_images = []
        for i in range(len(mut_new_lst_min)):
            print(i)
            parent_calls_mut_list.append(mut_new_lst_min[i][1])
            parent_data_mut_list = mut_new_lst_min[i][2]
            #parent_data_mut_list_gen_list = parent_data_mut_list[len(dataset_parent] : ]
            #print('parent_data_mut_list')
            #print(parent_data_mut_list)
            print('len(parent_data_mut_list)')
            print(len(parent_data_mut_list))
            parent_data_mut_gen_images.append(
                parent_data_mut_list[len(dataset_parent):])
            print('len(parent_data_mut_gen_images)')
            #print(parent_data_mut_gen_images)
            print(len(parent_data_mut_gen_images))
            print('len(dataset_parent')
            print(len(dataset_parent))
            print('\n')
        parent_data_mut_gen_images_flatten = [
            item for sublist in parent_data_mut_gen_images for item in sublist
        ]

        print('len(parent_data_mut_gen_images_flatten)')
        print(len(parent_data_mut_gen_images_flatten))
        print('len of dataset parent before appending)')
        initial_total_images_dataset_parent = len(dataset_parent)
        print(initial_total_images_dataset_parent)

        dataset_parent.extend(parent_data_mut_gen_images_flatten)

        print('len of dataset parent after appending)')
        #print(dataset_parent)
        print(len(dataset_parent))
        print('parent calls list after relaxation in generations)')
        print(parent_calls_mut_list)
        parent_calls_mut_list_updated_gen = [
            (number - initial_total_images_dataset_parent)
            for number in parent_calls_mut_list
        ]
        print('parent calls list specific to this  generations')
        print(parent_calls_mut_list_updated_gen)

        with open(log_file, "a+") as fh:
            fh.write(
                f'Initial Total images in the parent dataset: {initial_total_images_dataset_parent}'
                '\n')
            fh.write(
                f'Parent calls list after relaxtions in generations: {parent_calls_mut_list} '
                '\n')
            fh.write(
                f'Parent calls  list specific to this  generations in each relaxation: {parent_calls_mut_list_updated_gen} '
                '\n')
            fh.write(
                f'Total Parent calls  specific to this  generations: {len(parent_data_mut_gen_images_flatten)} '
                '\n')
            fh.write(
                f'Total Parent calls  specific to this  generations: {sum(parent_calls_mut_list_updated_gen)} '
                '\n')
            fh.write(
                f'Final Total images in the parent dataset: {len(dataset_parent)}'
                '\n')

        fitnesses_mut = list(map(toolbox.evaluate, cm_pop))

        for ind, fit in zip(cm_pop, fitnesses_mut):
            ind.fitness.values = fit

        new_population = copy.deepcopy(population)
        # Relaxed clusters will be checked for bonded and similarity with the other
        # clusters in the population. If dissimilar, they will be added to the new population.
        for cm1, cmut1 in enumerate(cm_pop):
            new_diff_list = []
            if checkBonded(cmut1[0]) == True:
                if checkOverlap(cmut1[0]) == False:
                    for c2, cluster1 in enumerate(population):
                        diff = checkSimilar(cluster1[0], cmut1[0])
                        new_diff_list.append(diff)
                    if all(new_diff_list) == True:
                        new_population.append(cmut1)
                    else:
                        pass

        mut_list_after_filter = []
        for individual in new_population:
            mut_list_after_filter.append(individual[0])
        write('mut_after_filter_gen' + str(g) + '.traj', mut_list_after_filter)
        with open(log_file, "a+") as fh:
            fh.write(
                f'Total clusters flitered out in  Generation {g}: {len(cm_pop) + len(population) - len(new_population)}'
                "\n")
            fh.write(
                f'Total clusters in the pool after filtering in Generation {g}: {len(new_population)}'
                "\n")

        fitnesses_pool = list(map(toolbox.evaluate, new_population))

        with open(log_file, "a+") as fh:
            fh.write(
                "Energies (fitnesses) of the present pool before best 10 are selected"
                "\n")
            for value in fitnesses_pool:
                fh.write("{} \n".format(value[0]))

        # Selecting the lowest energy npool clusters from the new_population
        len_new_pop = len(new_population)
        if len_new_pop > nPool:
            best_n_clus = tools.selWorst(new_population, nPool)
        else:
            best_n_clus = new_population
        population = best_n_clus

        best_clus = tools.selWorst(population, 1)[0]
        with open(log_file, "a+") as fh:
            fh.write("{} {} \n".format("Lowest energy for this generation is",
                                       best_clus.fitness.values[0]))
            fh.write("\n Best cluster in this generation: \n")
            for atom in best_clus[0]:
                fh.write("{} {:12.8f} {:12.8f} {:12.8f} \n".format(
                    atom.symbol, atom.x, atom.y, atom.z))
            fh.write("\n")

        bi.append(best_clus[0])
        if g == 1:
            writer = TrajectoryWriter(filename + "_best.traj",
                                      mode="w",
                                      atoms=best_clus[0])
            writer.write()
        else:
            writer = TrajectoryWriter(filename + "_best.traj",
                                      mode="a",
                                      atoms=best_clus[0])
            writer.write()

    final_pop_db = ase.db.connect("final_pop_{}.db".format(filename))
    for clus in population:
        write_to_db(final_pop_db, clus[0])

    with open(log_file, "a+") as fh:
        fh.write("Global Minimum after {} Generations \n".format(g))
        for atom in best_clus[0]:
            fh.write("{} {:12.8f} {:12.8f} {:12.8f} \n".format(
                atom.symbol, atom.x, atom.y, atom.z))
    # Return the list of best clusters in every generations and the overall best cluster
    return bi, best_clus[0]
Esempio n. 22
0
def onGA(fleet: Fleet,
         hp: HyperParameters,
         critical_points: Dict,
         save_to: str = None,
         best_ind: IndividualType = None,
         savefig=False):
    customers_to_visit = {
        ev_id: fleet.vehicles[ev_id].assigned_customers
        for ev_id in fleet.vehicles_to_route
    }
    indices = block_indices(customers_to_visit, hp.r)

    # Fitness objects
    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("Individual",
                   list,
                   fitness=creator.FitnessMin,
                   feasible=False,
                   acceptable=False)

    # Toolbox
    toolbox = base.Toolbox()

    toolbox.register("individual",
                     random_individual,
                     indices=indices,
                     starting_points=critical_points,
                     customers_to_visit=customers_to_visit,
                     charging_stations=fleet.network.charging_stations,
                     allowed_charging_operations=hp.r)
    toolbox.register("evaluate",
                     fitness,
                     indices=indices,
                     critical_points=critical_points,
                     fleet=fleet,
                     hp=hp)
    toolbox.register("mate",
                     crossover,
                     indices=indices,
                     allowed_charging_operations=hp.r,
                     index=None)
    toolbox.register("mutate",
                     mutate,
                     indices=indices,
                     starting_points=critical_points,
                     customers_to_visit=customers_to_visit,
                     charging_stations=fleet.network.charging_stations,
                     allowed_charging_operations=hp.r,
                     index=None)
    toolbox.register("select",
                     tools.selTournament,
                     tournsize=hp.tournament_size)
    toolbox.register("select_worst", tools.selWorst)
    toolbox.register("decode",
                     decode,
                     indices=indices,
                     critical_points=critical_points,
                     fleet=fleet,
                     hp=hp)

    # BEGIN ALGORITHM
    t_init = time.time()

    # Random population
    if best_ind is None:
        pop = [
            creator.Individual(toolbox.individual())
            for i in range(hp.num_individuals)
        ]
    else:
        pop = [creator.Individual(best_ind)]
        pop += [
            creator.Individual(toolbox.individual())
            for i in range(hp.num_individuals - 1)
        ]

    # Evaluate the initial population and get fitness of each individual
    for k, ind in enumerate(pop):
        fit, feasible, acceptable = toolbox.evaluate(ind)
        ind.fitness.values = (fit, )
        ind.feasible = feasible
        ind.acceptable = acceptable

    print(f'  Evaluated {len(pop)} individuals')
    bestOfAll = tools.selBest(pop, 1)[0]
    print(
        f"Best individual  : {bestOfAll}\n Fitness: {bestOfAll.fitness.wvalues[0]} Feasible: {bestOfAll.feasible}"
    )

    # These will save statistics
    cs_capacity = fleet.network.nodes[
        fleet.network.charging_stations[0]].capacity
    opt_data = GenerationsData([], [], [], [], [], [], fleet, hp, bestOfAll,
                               bestOfAll.feasible, bestOfAll.acceptable,
                               len(fleet), cs_capacity)
    print("################  Start of evolution  ################")
    # Begin the evolution
    for g in range(hp.max_generations):
        # A new generation
        print(f"-- Generation {g}/{hp.max_generations} --")
        opt_data.generations.append(g)

        # Select the best individuals, if given
        if hp.elite_individuals:
            best_individuals = list(
                map(toolbox.clone, tools.selBest(pop, hp.elite_individuals)))

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

        # Mutation
        for mutant in offspring:
            if np.random.random() < hp.MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        # Crossover
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            if np.random.random() < hp.MUTPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values

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

        print(f'  Evaluated {len(invalid_ind)} individuals')

        # The population is entirely replaced by a sorted offspring
        pop[:] = offspring
        pop[:] = tools.selBest(pop, len(pop))

        # Insert best individuals from previous generation
        if hp.elite_individuals:
            pop[:] = best_individuals + pop[:-hp.elite_individuals]

        # Update best individual
        bestInd = tools.selBest(pop, 1)[0]
        if bestInd.fitness.wvalues[0] > bestOfAll.fitness.wvalues[0]:
            bestOfAll = bestInd

        # Real-time info
        print(
            f"Best individual  : {bestInd}\n Fitness: {bestInd.fitness.wvalues[0]} Feasible: {bestInd.feasible}"
        )

        worstInd = tools.selWorst(pop, 1)[0]
        print(
            f"Worst individual : {worstInd}\n Fitness: {worstInd.fitness.wvalues[0]} Feasible: {worstInd.feasible}"
        )

        print(
            f"Curr. best-of-all: {bestOfAll}\n Fitness: {bestOfAll.fitness.wvalues[0]} Feasible: {bestOfAll.feasible}"
        )

        # Statistics
        fits = [sum(ind.fitness.wvalues) for ind in pop]
        mean = np.average(fits)
        std = np.std(fits)

        print(f"Max {max(fits)}")
        print(f"Min {min(fits)}")
        print(f"Avg {mean}")
        print(f"Std {std}")

        opt_data.best_fitness.append(-max(fits))
        opt_data.worst_fitness.append(-min(fits))
        opt_data.average_fitness.append(mean)
        opt_data.std_fitness.append(std)
        opt_data.best_individuals.append(bestInd)

        print()

    t_end = time.time()
    print("################  End of (successful) evolution  ################")

    algo_time = t_end - t_init
    print('Algorithm time:', algo_time)

    fit, feasible, acceptable = toolbox.evaluate(bestOfAll)
    fit, feasible, acceptable = toolbox.evaluate(bestOfAll)
    routes = toolbox.decode(bestOfAll)

    opt_data.bestOfAll = bestOfAll
    opt_data.feasible = feasible
    opt_data.acceptable = acceptable
    opt_data.algo_time = algo_time
    opt_data.fleet = fleet

    if save_to:
        path = save_to
        try:
            os.mkdir(path)
        except FileExistsError:
            pass
        opt_data.save_opt_data(path, savefig=savefig)

    for r in routes.values():
        L = r[0][1]
        if sum([1 for Lk in L if Lk < 0]):
            print('negative')
    return routes, opt_data, toolbox
    plot = True if len(sys.argv) > 1 else False

    # Prepare the plot
    if plot: plt.ion()
    
    # Create the evolutionary algorithm toolbox
    toolbox = setupToolbox()    
    
    #===========================================================================
    # Create the initial population and start the Evolutionary Algorithm
    #===========================================================================
    npop = 50
    ngen = 50
    cxpb = 0.5
    mutpb = 0.3
    pop = toolbox.population(n=npop)
    pop, topOne = run(toolbox, pop, ngen=ngen, cxpb=cxpb, mutpb=mutpb, plot=plot)
    #===========================================================================

    
    # Print the top 10 of the resulting population     
    topTen = tools.selBest(topOne, k=10)
    for index, item in enumerate(topTen):
        print("#%d: %E %s (%s)" %(index,item.responseTime, str(item), item.generation))
    
    # Sort the best performing individuals by response time, and show in a new figure
    top = dict()
    top['Sorted'] = tools.selWorst(topOne, k=len(topOne))
    create_plot(pop,plot_name='populations.html')
    create_plot(top,plot_name='plot.html', add_gen=True) 
        
Esempio n. 24
0
def main(seed, exp_no, run_specs, test_set):
    # set the randomized seed for experimentation duplication
    random.seed(seed)
    # set experiment number
    experiment = exp_no
    # assign the test set for GP
    test_set = test_set

    # sets the specs for the run
    POP = run_specs['pop_size']
    GENS = run_specs['gens']
    HOF_SIZE = run_specs['hof_size']
    CROSS_PROB = run_specs['cross_prob']
    MUT_PROB = 1.0
    IND_MUT_PROB = run_specs['mut_rate']
    SELECTOR = run_specs['selector']
    ELITISM = run_specs['elitism']
    alg = run_specs['algorithm']
    REC_FITS = run_specs['rec_fits']
    SEEDED = run_specs['seeded_pop']

    # sets hof and fs on/off
    FS = run_specs['FS']
    HOF = run_specs['HOF']
    """____SET UP ANY LISTS FOR TRACKING DATA HERE___"""
    plot_best_fitness = []
    plot_average_fitness = []
    plot_worst_fitness = []

    blue_est_best_gp = []
    red_est_best_gp = []
    blue_est_avg_gp = []
    red_est_avg_gp = []

    # set evolutionary operators
    toolbox.register("mutate",
                     helpers.gaussian,
                     mu=0,
                     sigma=0.2,
                     indpb=IND_MUT_PROB)
    toolbox.register("mate", tools.cxOnePoint)
    toolbox.register("evaluate", helpers.evaluate)
    if SELECTOR == 'SUS':
        toolbox.register("select", tools.selStochasticUniversalSampling)
    elif SELECTOR == 'RANKED':
        toolbox.register("select", helpers.selRanked)

    # Initialise all the gene's to be used in the chromosome.
    toolbox.register("px_min", Gene, MIN_DISTANCE, MAX_DISTANCE, True)
    toolbox.register("px_max", Gene, MIN_DISTANCE, MAX_DISTANCE, False)
    toolbox.register("py_min", Gene, MIN_DISTANCE, MAX_DISTANCE, True)
    toolbox.register("py_max", Gene, MIN_DISTANCE, MAX_DISTANCE, False)
    toolbox.register("pz_min", Gene, MIN_DISTANCE, MAX_DISTANCE, True)
    toolbox.register("pz_max", Gene, MIN_DISTANCE, MAX_DISTANCE, False)
    toolbox.register("vx_min", Gene, MIN_VELOCITY, MAX_VELOCITY, True)
    toolbox.register("vx_max", Gene, MIN_VELOCITY, MAX_VELOCITY, False)
    toolbox.register("vy_min", Gene, MIN_VELOCITY, MAX_VELOCITY, True)
    toolbox.register("vy_max", Gene, MIN_VELOCITY, MAX_VELOCITY, False)
    toolbox.register("vz_min", Gene, MIN_VELOCITY, MAX_VELOCITY, True)
    toolbox.register("vz_max", Gene, MIN_VELOCITY, MAX_VELOCITY, False)
    toolbox.register("ax_min", Gene, MIN_ACCEL, MAX_ACCEL, True)
    toolbox.register("ax_max", Gene, MIN_ACCEL, MAX_ACCEL, False)
    toolbox.register("ay_min", Gene, MIN_ACCEL, MAX_ACCEL, True)
    toolbox.register("ay_max", Gene, MIN_ACCEL, MAX_ACCEL, False)
    toolbox.register("az_min", Gene, MIN_ACCEL, MAX_ACCEL, True)
    toolbox.register("az_max", Gene, MIN_ACCEL, MAX_ACCEL, False)

    # Initialize the structure of the chromosome
    # Sets tactic as a Tactic object and populates it with genes from gene template
    toolbox.register(
        "tactic",
        tools.initCycle,
        creator.Tactic,
        (toolbox.px_min, toolbox.px_max, toolbox.py_min, toolbox.py_max,
         toolbox.pz_min, toolbox.pz_max, toolbox.vx_min, toolbox.vx_max,
         toolbox.vy_min, toolbox.vy_max, toolbox.vz_min, toolbox.vz_max,
         toolbox.ax_min, toolbox.ax_max, toolbox.ay_min, toolbox.ay_max,
         toolbox.az_min, toolbox.az_max),
        n=CYCLES)

    # create the population initializer
    toolbox.register("population", tools.initRepeat, list, toolbox.tactic, POP)
    """<------------------------------------- START ---------------------------------------->"""

    if SEEDED == 1:
        blue_pop = pop_init.run()
        red_pop = pop_init.run()
        # Load the test set from the pickle file
        # with open("biased_blue_pop.pkl", "r") as cp_file:
        #     cp = cPickle.load(cp_file)
        # blue_pop = cp["population"]
        # with open("biased_red_pop.pkl", "r") as cp_file:
        #     cp = cPickle.load(cp_file)
        # red_pop = cp["population"]
    else:
        # initialize populations
        blue_pop = toolbox.population()
        red_pop = toolbox.population()

    # initialize hall of fame
    blue_hof = hof.HallOfFame(HOF_SIZE)
    red_hof = hof.HallOfFame(HOF_SIZE)

    # reset random seed to clock time
    algseed = datetime.now()
    random.seed(algseed)
    algseed = random.randint(0, 1001)
    random.seed(algseed)
    print 'Algorithm seed: ', algseed

    # get working folder for this set of runs
    folder_string = run_specs['folder']

    # Make new folder for experiemnt results
    save_path = os.getcwd()
    sub_folder_string = folder_string + str(exp_no) + '_Seed_' + str(
        seed) + '/'
    folder = os.path.join(save_path, sub_folder_string)
    if not os.path.isdir(folder):
        os.makedirs(folder)

    # create out file
    text_file = sub_folder_string + 'results.txt'
    sys.stdout = open(text_file, 'w')

    # create csv writer and file for fitness and initial/final population output
    csv_fits = sub_folder_string + 'relative_fitness.csv'
    fits = open(csv_fits, 'ab')
    fit_writer = csv.writer(fits)

    # create csv writer and file for detailed results for graph output
    # csv_pops = sub_folder_string + 'populations.csv'
    # pops = open(csv_pops, 'ab')
    # pops_writer = csv.writer(pops)

    # csv writer for outputting all fitnesses of blue population
    blue_pop_fits = sub_folder_string + 'blue_all_fits.csv'
    blue_fits_file = open(blue_pop_fits, 'ab')
    blue_fits_writer = csv.writer(blue_fits_file)

    # do the same for red ^^^
    red_pop_fits = sub_folder_string + 'red_all_fits.csv'
    red_fits_file = open(red_pop_fits, 'ab')
    red_fits_writer = csv.writer(red_fits_file)

    # create csv writer and file for GP data
    csv_fits = sub_folder_string + 'Gen_performance.csv'
    gp = open(csv_fits, 'ab')
    gp_writer = csv.writer(gp)
    # set up headings for graph csv
    fit_writer.writerow(
        ('Generation', 'BestB', 'AvgB', 'WorstB', 'BestR', 'AvgR', 'WorstR'))
    # set up GP headings
    gp_writer.writerow(
        ('Generation', 'BestB_GP', 'AvgB_GP', 'BestR_GP', 'AvgR_GP'))

    print 'Seed: ', seed

    i = 0
    while i <= GENS:
        i += 1
        print('--------------------------------' + 'Generation: ' + str(i) +
              '-----------------------------------')
        """ ----------------------------------- EVALUATE THE POPULATIONS --------------------------------------- """

        # Reset fitness scores to zero so they can be accumulated each generation
        for index, x in enumerate(blue_pop):
            x.fitness.values = (0.0, )

        for index, y in enumerate(red_pop):
            y.fitness.values = (0.0, )
        """<----------RELATIVE FITNESS EVALUATIONS--------->"""
        # Evaluate the populations and return with summed fitness scores of all engagements
        # THIS FITNESS SCORE NEEDS TO BE AVERAGED AFTER HOF EVALS
        blue_pop, red_pop = helpers.sum_pop_v_pop_evals(blue_pop, red_pop)
        """<----------HALL OF FAME EVALUATIONS--------->"""
        # check hof not empty, if not, loop through and evaluate
        # Only if HOF is turned on
        if HOF == 1:
            if len(red_hof) != 0:
                blue_pop = helpers.sum_pop_v_hof_evals(blue_pop, red_hof)

            if len(blue_hof) != 0:
                red_pop = helpers.sum_pop_v_hof_evals(red_pop, blue_hof)
        """<-------AVERAGE THE FITNESS SCORES------>"""
        # average the accumulated fitness scores by size of population and also output fitnesses to csv
        blue_fits_row = []
        blue_fits_row.append(i)
        for index, x in enumerate(blue_pop):
            if HOF == 1:
                x.fitness.values = (x.fitness.values[0] /
                                    (POP + len(red_hof)), )
            else:
                x.fitness.values = (x.fitness.values[0] / POP, )
            blue_fits_row.append(x.fitness.values[0])

        red_fits_row = []
        red_fits_row.append(i)
        for index, y in enumerate(red_pop):
            if HOF == 1:
                y.fitness.values = (y.fitness.values[0] /
                                    (POP + len(blue_hof)), )
            else:
                y.fitness.values = (y.fitness.values[0] / POP, )
            red_fits_row.append(y.fitness.values[0])

        if REC_FITS == 1:
            blue_fits_writer.writerow(blue_fits_row)
            red_fits_writer.writerow(red_fits_row)
        """------------Generalisation Performance---------------------"""
        # EVERY 5TH GEN RECORD STATS
        # if i % 5 == 0 or i == 1:
        #     # CLONE THE POPS SO GP CAN BE CALCULATED WITHOUT AFFECTING FITNESS SCORES
        #     blue_copy = list(map(toolbox.clone, blue_pop))
        #     red_copy = list(map(toolbox.clone, red_pop))
        #     blue_copy = tools.selBest(blue_copy, 5)
        #     red_copy = tools.selBest(red_copy, 5)
        #     # Get generalisation performance of two populations
        #     blue_copy = helpers.sum_pop_v_hof_evals(blue_copy, test_set)
        #     for index_x, x in enumerate(blue_copy):
        #         x.fitness.values = (x.fitness.values[0] / len(test_set),)
        #
        #     red_copy = helpers.sum_pop_v_hof_evals(red_copy, test_set)
        #     for index_x, x in enumerate(red_copy):
        #         x.fitness.values = (x.fitness.values[0] / len(test_set),)
        #
        #     # create variables for estimated best and average GPs
        #     nBest_blue = tools.selBest(blue_copy, 5)
        #     blue_ebgp = tools.selBest(nBest_blue, 1)
        #     blue_est_best_gp.append(blue_ebgp[0].fitness.values[0])
        #     blue_eagp_tally = []
        #     for index, x in enumerate(nBest_blue):
        #         blue_eagp_tally.append(x.fitness.values[0])
        #     blue_avg = sum(blue_eagp_tally) / len(nBest_blue)
        #     blue_est_avg_gp.append(blue_avg)
        #
        #     nBest_red = tools.selBest(red_copy, 5)
        #     red_ebgp = tools.selBest(nBest_red, 1)
        #     red_est_best_gp.append(red_ebgp[0].fitness.values[0])
        #     red_eagp_tally = []
        #     for index, x in enumerate(nBest_red):
        #         red_eagp_tally.append(x.fitness.values[0])
        #     red_avg = sum(red_eagp_tally) / len(nBest_red)
        #     red_est_avg_gp.append(red_avg)
        """<----------OUTPUT INFO TO TEXT FILE---------->"""
        # BLUE
        best_blue_ind = tools.selBest(blue_pop, 1)[0]
        print 'Best Blue fitness score: ', best_blue_ind.fitness.values[0]
        print 'Best blue chromosome: ', helpers.list_to_string(best_blue_ind)

        worst_blue_ind = tools.selWorst(blue_pop, 1)[0]
        print "Worst Blue fitness: ", worst_blue_ind.fitness.values[0]

        # get the average fitness of the generation
        sum_fits = sum(ind.fitness.values[0] for ind in blue_pop)
        avg_blue_fitness = sum_fits / POP
        print "Generation average Blue fitness: ", avg_blue_fitness

        # if i % 5 == 0 or i == 1:
        #     print 'Performance Measures:'
        #     # output the GP and diversity of the generation
        #     print 'Blue Estimated Average GP: ', blue_avg
        #     print 'Blue Estimated Best GP: ', blue_ebgp[0].fitness.values[0]
        #
        #     #Print the 5 best GP inds
        #     print 'Top Blue GP performers'
        #     for index, x in enumerate(nBest_blue):
        #         print index+1, ':'
        #         print helpers.list_to_string(x), 'GP: ', x.fitness.values[0]

        # RED
        # get best chromosome and output
        best_red_ind = tools.selBest(red_pop, 1)[0]
        print "Best Red fitness: ", best_red_ind.fitness.values[0]
        print 'Best Red chromosome: ', helpers.list_to_string(best_red_ind)

        # get worst and display
        worst_red_ind = tools.selWorst(red_pop, 1)[0]
        print "Worst Red fitness: ", worst_red_ind.fitness.values[0]

        # get the average fitness of the generation
        sum_fits = sum(ind.fitness.values[0] for ind in red_pop)
        avg_red_fitness = sum_fits / POP
        print "Generation average red fitness: ", avg_red_fitness

        # if i % 5 == 0 or i == 1:
        #     print 'Performance Measures:'
        #     # output the GP and diversity of the generation
        #     print 'Red Estimated Average GP: ', red_avg
        #     print 'Red Estimated Best GP: ', red_ebgp[0].fitness.values[0]
        #
        #     # Print the 5 best GP inds
        #     print 'Top red GP performers'
        #     for index, x in enumerate(nBest_red):
        #         print index + 1, ':'
        #         print helpers.list_to_string(x), 'GP: ', x.fitness.values[0]

        # Write best avg & worst to fits_csv
        fit_writer.writerow(
            (i, best_blue_ind.fitness.values[0], avg_blue_fitness,
             worst_blue_ind.fitness.values[0], best_red_ind.fitness.values[0],
             avg_red_fitness, worst_red_ind.fitness.values[0]))

        # if i % 5 == 0 or i == 1:
        #     # save the GP values to the GP file
        #     gp_writer.writerow((i, blue_ebgp[0].fitness.values[0], blue_avg, red_ebgp[0].fitness.values[0], red_avg))

        # if i % 5 == 0 or i == 1:
        #     # Fill the dictionary using the dict(key=value[, ...]) constructor
        #     cp = dict(population=blue_pop, generation=i, rndstate=random.getstate())
        #
        #     with open(sub_folder_string + "zgeneration" + str(i) + ".pkl", "wb") as cp_file:
        #         cPickle.dump(cp, cp_file)

        if i == GENS:
            break
        """<------------HALL OF FAME UPDATE----------->"""
        blue_hof.update(tools.selBest(blue_pop, 1), i)
        red_hof.update(tools.selBest(red_pop, 1), i)
        """<------------FITNESS SHARING------------>"""
        """ NEEDS TO BE FIXED IN HELPERS TO USE NEW CHROMOSOME """
        #MUST BE LAST THING BEFORE EVOLUTION HAPPENS
        # if FS == 1:
        #     blue_pop = helpers.share_fitness(blue_pop, 0.2)
        #     red_pop = helpers.share_fitness(red_pop, 0.2)
        """<--------------------BEGIN EVOLUTION------------------>"""
        # Select offspring for next generation
        if ELITISM == 1:
            blue_offspring = toolbox.select(blue_pop, POP - 1)
            red_offspring = toolbox.select(red_pop, POP - 1)
        else:
            blue_offspring = toolbox.select(blue_pop, POP)
            red_offspring = toolbox.select(red_pop, POP)

        # Clone the offspring so we can evolve
        blue_offspring = list(map(toolbox.clone, blue_offspring))
        red_offspring = list(map(toolbox.clone, red_offspring))

        # CROSSOVER
        # Blue
        for child1, child2 in zip(blue_offspring[::2], blue_offspring[1::2]):
            if random.random() < CROSS_PROB:
                # MATE
                toolbox.mate(child1, child2)
            # Red
        for child1, child2 in zip(red_offspring[::2], red_offspring[1::2]):
            if random.random() < CROSS_PROB:
                # MATE
                toolbox.mate(child1, child2)

        # MUTATION
        for mutant in blue_offspring:
            if random.random() < MUT_PROB:
                # MUTATE
                for index, x in enumerate(mutant):
                    mutant[index].value = helpers.convert_range(
                        mutant[index].value, mutant[index].min,
                        mutant[index].max)

                toolbox.mutate(mutant)

                for index, x in enumerate(mutant):
                    mutant[index].value = helpers.change_back(
                        mutant[index].value, mutant[index].min,
                        mutant[index].max)
                    helpers.bounds_check(mutant[index])

        for mutant in red_offspring:
            if random.random() < MUT_PROB:
                # MUTATE
                for index, x in enumerate(mutant):
                    mutant[index].value = helpers.convert_range(
                        mutant[index].value, mutant[index].min,
                        mutant[index].max)

                toolbox.mutate(mutant)

                for index, x in enumerate(mutant):
                    mutant[index].value = helpers.change_back(
                        mutant[index].value, mutant[index].min,
                        mutant[index].max)
                    helpers.bounds_check(mutant[index])

        if ELITISM == 1:
            blue_offspring += tools.selBest(blue_pop, 1)
            red_offspring += tools.selBest(red_pop, 1)

        # REPLACE
        blue_pop[:] = blue_offspring
        red_pop[:] = red_offspring

        print(
            '-------------------------------------Hall Of Fame Regular----------------------------------------'
        )
        print "BLUE: "
        # for chromosomes in blue_hof:
        #     print 'Chromosome: ', helpers.list_to_string(chromosomes), 'Fitness: ', chromosomes.fitness.values
        if len(blue_hof) != 0:
            blue_hof.print_hof()

        print "RED: "
        # for chromosomes in red_hof:
        #     print 'Chromosome: ', helpers.list_to_string(chromosomes), 'Fitness: ', chromosomes.fitness.values
        if len(red_hof) != 0:
            red_hof.print_hof()

        print "Generation ", i, " complete."

    print(
        '-------------------------------------Hall Of Fame Regular----------------------------------------'
    )
    print "BLUE: "
    # for chromosomes in blue_hof:
    #     print 'Chromosome: ', helpers.list_to_string(chromosomes), 'Fitness: ', chromosomes.fitness.values
    if len(blue_hof) != 0:
        blue_hof.print_hof()

    print "RED: "
    # for chromosomes in red_hof:
    #     print 'Chromosome: ', helpers.list_to_string(chromosomes), 'Fitness: ', chromosomes.fitness.values
    if len(red_hof) != 0:
        red_hof.print_hof()

    print(
        '-------------------------------------Stats----------------------------------------'
    )
    print 'Seed: ', seed
    print 'Selection: ', SELECTOR
    print("Pop size: " + str(POP))
    print("Generations: " + str(GENS))
    print("Crossover Prob: " + str(CROSS_PROB))
    print("Mutation Prob: " + str(IND_MUT_PROB))
    print 'Elitism: ', ELITISM
    print 'Hall of Fame: ', HOF
    print 'Fitness Sharing: ', FS
    print 'Algorithm: ', alg

    # Print the 5 best GP inds
    # print 'Top 5 Blue GP performers:'
    # for index, x in enumerate(nBest_blue):
    #     print index + 1, ':'
    #     print helpers.list_to_string(x), 'GP: ', x.fitness.values[0]

    # Print the 5 best GP inds
    # print 'Top red GP performers:'
    # for index, x in enumerate(nBest_red):
    #     print index + 1,":"
    #     print helpers.list_to_string(x), 'GP: ', x.fitness.values[0]

    # pops_writer.writerow(('Final Blue population fitness scores',))
    # for index, x in enumerate(blue_pop):
    #     pops_writer.writerow(x.fitness.values)
    # pops_writer.writerow(('Final Red population fitness scores',))
    # for index, y in enumerate(red_pop):
    #     pops_writer.writerow(y.fitness.values)

    # blue_est_avg_gp = sum(blue_est_avg_gp) / len(blue_est_avg_gp)
    # red_est_avg_gp = sum(red_est_avg_gp) / len(red_est_avg_gp)
    #
    # blue_est_best_gp = sum(blue_est_best_gp) / len(blue_est_best_gp)
    # red_est_best_gp = sum(red_est_best_gp) / len(red_est_best_gp)

    fits.close()
    # pops.close()
    blue_fits_file.close()
    red_fits_file.close()

    # stats = {"blue_est_avg_gp": blue_est_avg_gp, "blue_est_best_gp": blue_est_best_gp,
    #          "red_est_avg_gp": red_est_avg_gp, "red_est_best_gp": red_est_best_gp}

    return  #stats
Esempio n. 25
0
def  cluster_GA(nPool,eleNames,eleNums,eleRadii,generations,calc,filename,log_file,CXPB = 0.5,singleTypeCluster = False):
    '''
    DEAP Implementation of the GIGA Geneting Algorithm for nanoclusters
    '''
        
    best_db = ase.db.connect("{}.db".format(filename))
	
    #Creating types
    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
    creator.create("Individual", list,fitness=creator.FitnessMax)

	#Registration of the evolutionary tools in the toolbox
    toolbox = base.Toolbox()
    toolbox.register("poolfill", fillPool,eleNames,eleNums,eleRadii,calc)
    toolbox.register("individual",tools.initRepeat,creator.Individual,toolbox.poolfill,1)
    toolbox.register("evaluate1", fitness_func1)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)

	#Registering mutations and crossover operators 
    toolbox.register("mate", mate)
    toolbox.register("mutate_homotop", homotop)
    toolbox.register("mutate_rattle", rattle_mut)
    toolbox.register("mutate_rotate", rotate_mut)
    toolbox.register("mutate_twist", twist)
    toolbox.register("mutate_tunnel", tunnel)
    toolbox.register("mutate_partialinv",partialInversion)
    toolbox.register("mutate_skin",skin)
    toolbox.register("mutate_changecore",changeCore)
        
    #Registering selection operator 
    toolbox.register("select", tools.selTournament)


    population = toolbox.population(n=nPool)

    #Creating a list of cluster atom objects from pouplation
    pop_list = []
    for individual in population:
	    pop_list.append(individual[0])


	#Dask Parallelization
    def calculate(atoms):
	    atoms_min = minimize(atoms,calc)
	    return atoms_min

	#distribute and run the calculations
    clus_bag = db.from_sequence(pop_list, partition_size = 1)
    clus_bag_computed = clus_bag.map(calculate)
    lst_clus_min = clus_bag_computed.compute()

	
    for i,p in enumerate(population):
	    p[0] = lst_clus_min[i]
	
    #Fitnesses (or Energy) values of the initial random population
    fitnesses = list(map(toolbox.evaluate1, population)) 
        
    with open(log_file, 'a+') as fh:
        fh.write('Energies (fitnesses) of the initial pool' '\n')
        for value in fitnesses:
            fh.write("{} \n".format(value[0]))
	
    for ind, fit in zip(population, fitnesses):
        ind.fitness.values = fit

    #Evolution of the Genetic Algorithm
    with open(log_file, 'a+') as fh:
        fh.write('\n')
        fh.write('Starting Evolution' '\n')
	
    g = 0
    init_pop_db = ase.db.connect("init_pop_{}.db".format(filename))
    for cl in population:
	    write_to_db(init_pop_db,cl[0])

    bi = []
    while g < generations:
        mutType = None
        muttype_list = []
        g = g + 1
        with open(log_file, 'a+') as fh:
            fh.write('{} {} \n'.format('Generation', g))

        cm_pop = []
        if random.random() < CXPB:  #Crossover Operation
            mutType = 'crossover'
            with open(log_file, 'a+') as fh:
                fh.write('{} {} \n'.format('mutType', mutType))
                                
            #Crossover operation step. 
            #The child clusters will be checked for bonding and similarity
            #between other child clusters. 
            loop_count = 0
            while  loop_count != 200:
                clusters = toolbox.select(population,2,1)
                muttype_list.append(mutType)
                parent1 = copy.deepcopy(clusters[0])
                parent2 = copy.deepcopy(clusters[1])
                fit1 = clusters[0].fitness.values
                f1, = fit1
                fit2 = clusters[1].fitness.values
                f2, = fit2
                toolbox.mate(parent1[0],parent2[0],f1,f2)

                diff_list = []
                if checkBonded(parent1[0]) == True:
                    if loop_count == 0:
                        cm_pop.append(parent1)
                    else:
                        for c,cluster in enumerate(cm_pop):
                            diff = checkSimilar(cluster[0],parent1[0])
                            diff_list.append(diff)

                        if all(diff_list) == True:
                            cm_pop.append(parent1)
                loop_count = loop_count+1
                if len(cm_pop) == nPool:
                    break

        else:   #Mutation Operation
            mutType = 'mutations'
            with open(log_file, 'a+') as fh:
                fh.write('{} {} \n'.format('mutType', mutType))
                                
                                #Mutation opeation step
                                #Each cluster in the population will undergo a randomly chosen mutation step
                                #Mutated new clusters will be checked for bonding and similarity with other new clusters
            for m,mut in enumerate(population):
                mutant = copy.deepcopy(mut)
                if singleTypeCluster:
                    mutType = random.choice(['rattle','rotate','twist','partialinv','tunnel','skin','changecore'])
                else:
                    mutType = random.choice(['rattle','rotate','homotop','twist','partialinv','tunnel','skin','changecore'])

                muttype_list.append(mutType)

                if mutType == 'homotop':
                    mutant[0] = toolbox.mutate_homotop(mutant[0])
                if mutType == 'rattle':
                    mutant[0] = toolbox.mutate_rattle(mutant[0])
                if mutType == 'rotate':
                    mutant[0] = toolbox.mutate_rotate(mutant[0])
                if mutType == 'twist':
                    mutant[0] = toolbox.mutate_twist(mutant[0])
                if mutType == 'tunnel':
                    mutant[0] = toolbox.mutate_tunnel(mutant[0])
                if mutType == 'partialinv':
                    mutant[0] = toolbox.mutate_partialinv(mutant[0])
                if mutType == 'skin':
                    mutant[0] = toolbox.mutate_skin(mutant[0])
                if mutType == 'changecore':
                    mutant[0] = toolbox.mutate_changecore(mutant[0])
					
                diff_list = []
                if checkBonded(mutant[0]) == True:
                    for c,cluster in enumerate(cm_pop):
                        diff = checkSimilar(cluster[0],mutant[0])
                        diff_list.append(diff)

                    if all(diff_list) == True:
                        cm_pop.append(mutant)
		                	
            with open(log_file, 'a+') as fh:
                fh.write('{} {} \n'.format('mutType_list', muttype_list))
				

        mut_new_lst = []
        for mut in cm_pop:
            mut_new_lst.append(mut[0])
                        
        #DASK Parallel relaxation of the crossover child/mutatted clusters
        mut_bag = db.from_sequence(mut_new_lst, partition_size = 1)
        mut_bag_computed = mut_bag.map(calculate)
        mut_new_lst_min = mut_bag_computed.compute()
			
        for o,mm in enumerate(cm_pop):
            mm[0] = mut_new_lst_min[o]

        fitnesses_mut = list(map(toolbox.evaluate1, cm_pop)) 

        for ind, fit in zip(cm_pop, fitnesses_mut):
            ind.fitness.values = fit

        new_population = copy.deepcopy(population)
                        
        #Relaxed clusters will be checked for bonded and similarity with the other
        #clusters in the population. If dissimilar, they will be added to the new population.
        for cm1,cmut1 in enumerate(cm_pop):
            new_diff_list = []
            if checkBonded(cmut1[0]) == True:
                for c2,cluster1 in enumerate(population):
                    diff = checkSimilar(cluster1[0],cmut1[0])
                    new_diff_list.append(diff)
                if all(new_diff_list) == True:
                    new_population.append(cmut1)
                else:
                    pass
			
        with open(log_file, 'a+') as fh:
            fh.write('{} {} \n'.format('Total number of clusters in the new population', len(new_population)))
        
        fitnesses_pool = list(map(toolbox.evaluate1, new_population)) 
        
        with open(log_file, 'a+') as fh:
            fh.write('Energies (fitnesses) of the present pool' '\n')
            for value in fitnesses_pool:
                fh.write("{} \n".format(value[0]))
        
        #Selecting the lowest energy npool clusters from the new_population
        best_n_clus = tools.selWorst(new_population,nPool)
        population = best_n_clus

        best_clus = tools.selWorst(population,1)[0]
        with open(log_file, 'a+') as fh:
            fh.write('{} {} \n'.format('Lowest energy cluster is', best_clus))
            fh.write('{} {} \n'.format('Lowest energy is',best_clus.fitness.values[0]))
            fh.write('\n')
        bi.append(best_clus[0])
        write_to_db(best_db,best_clus[0])

    final_pop_db = ase.db.connect("final_pop_{}.db".format(filename))
    for clus in population:
        write_to_db(final_pop_db,clus[0])

    return bi,best_clus[0]
Esempio n. 26
0
	def evolveSteady(self):
		"""
		A method to evolve a species of boid using a 
		steady-state genetic algorithm. Method authored by 
		Gianni Orlando. Edited by Ryan McArdle.

		:returns: the evolved species, and its fitness
		"""
		name = "Steady"

		self.prepareEvolution()

		self.eval_limit = self.eval_limit_steady

		# Initialize fitness goal and individual type
		creator.create("FitnessMax", base.Fitness, weights=(1.0,))
		creator.create("Individual", array.array, typecode='d',fitness=creator.FitnessMax)

        # Initialize individuals, populations, and evolution operators
		toolbox = base.Toolbox()
		toolbox.register("individual", self.initBoids, creator.Individual, *self.parameter_bounds)
		toolbox.register("population", tools.initRepeat, list, toolbox.individual)
		toolbox.register("evaluate", self.boidFitness)
		toolbox.register("mate", tools.cxOnePoint)
		toolbox.register("mutate", tools.mutGaussian, indpb=0.5, mu=25.5, sigma=12.5) # 50% chance for each value to mutate
		toolbox.register("mutate2", tools.mutShuffleIndexes, indpb=0.5) # 50% chance for each value to mutate
		toolbox.register("select", tools.selTournament, tournsize= 5)

		toolbox.decorate('mate', self.checkBounds(self.parameter_bounds))
		toolbox.decorate('mutate', self.checkBounds(self.parameter_bounds))
		toolbox.decorate('mutate2', self.checkBounds(self.parameter_bounds))

		stats = tools.Statistics(lambda ind: ind.fitness.values)
		stats.register("avg", np.mean)
		stats.register("std", np.std)
		stats.register("min", np.min)
		stats.register("max", np.max)
		self.logbook = tools.Logbook()
		self.logbook.header = 'gen','evals','min','max','avg','std'
		hof = tools.HallOfFame(1)

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

        # Evaluate the entire population
		seed = random.randint(1,1e10)
		## Evaluate the entire population for fitness in parallel
		if __name__=="__main__":
			with mp.Pool(self.num_processes) as pool:
				fitnesses = pool.starmap(self.boidFitness, [(boid.tolist(),seed,self.current_evals,self.eval_limit) for boid in pop])

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

        # Extracting all the fitnesses of 
		fits = [ind.fitness.values[0] for ind in pop]

        # Variable keeping track of the number of generations
		g = 0

		## Record the initial population
		self.current_evals += len(pop)
		record = stats.compile(pop)
		logbook = tools.Logbook()
		logbook.header = 'gen','evals','min','max','avg','std'
		logbook.record(gen=0, evals=self.current_evals, **record)
		print(logbook.stream)
		hof.update(pop)
		print(hof[0])
		print(hof[0].fitness.values[0])
		
		# Begin the evolution
		while hof[0].fitness.values[0] < 1.0 and self.current_evals < self.eval_limit:
            # A new generation
			g = g + 1

             # Gather all the fitnesses in one list and print the stats
			fits = [ind.fitness.values for ind in pop]

            # Select the next generation individuals
			offspring = pop
			bestIndv = tools.selBest(offspring,k=2)
			worstIndv = tools.selWorst(offspring,k=2)
            # Clone the selected individuals
			offspring = list(map(toolbox.clone, offspring))

            # Apply crossover the two individuals (tournmanet 
            # selection)
			parents = toolbox.select(offspring,2)
			parent1, parent2 = parents[0], parents[1]
			replace1 = offspring[offspring.index(worstIndv[0])]
			replace2 = offspring[offspring.index(worstIndv[1])]
			for child1, child2 in zip([parent1], [parent2]):
				if random.random() < self.CXPB:
					toolbox.mate(child1, child2)
					if random.random() < self.MUTPB:
						toolbox.mutate(child1)
						del child1.fitness.values
					if random.random() < self.MUTPB:
						toolbox.mutate(child2)
						del child2.fitness.values
					if random.random() < self.MUTPB:
						toolbox.mutate2(child1)
						del child1.fitness.values
					if random.random() < self.MUTPB:
						toolbox.mutate2(child2)
						del child1.fitness.values
					offspring[offspring.index(replace1)] = parent1
					offspring[offspring.index(replace2)] = parent2

			pop[:] = offspring

			# Evaluate the population with invalid fitnesses
			invalid_ind = [ind for ind in pop if not ind.fitness.valid]
			seed = random.randint(1,1e10)
			if __name__=="__main__":
				with mp.Pool(self.num_processes) as pool:
					fitnesses = pool.starmap(self.boidFitness, [(boid.tolist(),seed,self.current_evals,self.eval_limit) for boid in invalid_ind])

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

            # Extracting all the fitnesses of 
			fits = [ind.fitness.values[0] for ind in pop]

			##################################################
			## Record the new generation
			hof.update(pop)
			record = stats.compile(pop)
			self.current_evals += len(invalid_ind)
			logbook.record(gen=g, evals=self.current_evals, **record)
			print(logbook.stream)
			print(hof[0])
			print(hof[0].fitness.values[0])
			#bestFit, bestDetailFit, bestFitWeight = self.boidFitness(hof[0].tolist(),seed,self.current_evals-len(invalid_ind),self.eval_limit,detail=True)
			#print(bestDetailFit)
			##################################################

		self.plotAndRecord(logbook,hof,name)

		return hof[0]
Esempio n. 27
0
def main():
    # random.seed(64)

    pop = toolbox.population(n=500)
    CXPB, MUTPB, NGEN = 0.9, 0.1, 100
    
    print("Start of evolution")
    
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    
    # Begin the evolutionck())
    for g in range(NGEN):
        print("-- Generation %i --" % g)
        # Select the next generation individuals
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = list(map(toolbox.clone, offspring))

        fits = [ind.fitness.values[0] for ind in pop]
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x*x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5
        # Apply crossover and mutation on the offspring
        m = 50
        while (m > 0):
            operator1 = 0
            m = m - 1
            for child1, child2 in zip(offspring[::50-m], offspring[1::50-m]):
                if random.random() < CXPB:
                    toolbox.mate(child1, child2)
                    del child1.fitness.values
                    del child2.fitness.values
                    operator1 = 1
                break
            if(operator1 == 0):
                for mutant in offspring:
                    # if random.random() < MUTPB:
                    toolbox.mutate(mutant, indpb=0.05)
                    del mutant.fitness.values
                    break
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
        
        print("  Evaluated %i individuals" % len(invalid_ind))
        # The population is entirely replaced by the offspring, but the last pop best_ind

        best_ind = tools.selBest(pop, 1)[0]
        worst_ind = tools.selWorst(offspring, 1)[0]
        
        for i in range(len(offspring)):
            if (offspring[i] == worst_ind):
                offspring[i] = best_ind
                break

        pop[:] = offspring        
        MUTPB, NGEN = MUTPB + 0.3, NGEN - 0.3
        
    while True:
        try:            
            f = open(sys.argv[1], "a")
            flock(f, LOCK_EX | LOCK_NB)
            for i in range(len(pop)):            
                f.write(str((pop, 1)[0][i].fitness.values))
            f.write('\n')
            flock(f, LOCK_UN)
        except IOError:
            time.sleep(5)
            continue
        break
Esempio n. 28
0
def main():
    random.seed(32)
    
    pop = toolbox.population(n=N)#個体数
    NGEN = 100 #世代数
    CXPB = 0.5 #交叉確率
    MUTPB = 0.2 #突然変異確率

    print("Start of evolution")

    fitnesses = list(map(toolbox.evaluate, pop))
    
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    
    print("  Evaluated %i individuals" % len(pop))

    for g in range(NGEN):
        print("-- Generation %i --" % g)

        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))

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

        for mutant in offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values
                
        toolbox.newoperation(tools.selWorst(offspring,1)[0])
        del tools.selWorst(offspring,1)[0].fitness.values
                
                
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = 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[:] = offspring        
        fits = [ind.fitness.values[0] for ind in pop]


        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x*x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5

        print("  Min %s" % min(fits))
        print("  Max %s" % max(fits))
        print("  Avg %s" % mean)
        print("  Std %s" % std)
        

    print("-- End of (successful) evolution --")
    best_ind = tools.selBest(pop, 1)[0]
    
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
Esempio n. 29
0
def main():
    pd.set_option('display.max_columns', None)
    df = pd.read_csv("ReplicatedAcousticFeatures-ParkinsonDatabase.csv",
                     sep=',')

    y = df['Status']
    df.drop('Status', axis=1, inplace=True)
    df.drop('ID', axis=1, inplace=True)
    df.drop('Recording', axis=1, inplace=True)
    number_of_attributes = len(df.columns)
    print(number_of_attributes)

    mms = MinMaxScaler()
    df_norm = mms.fit_transform(df)

    clf = SVC()
    scores = model_selection.cross_val_score(clf,
                                             df_norm,
                                             y,
                                             cv=5,
                                             scoring='accuracy',
                                             n_jobs=-1)
    print(scores.mean())

    creator.create("FitnessMax", base.Fitness, weights=(-1.0, ))
    creator.create("Individual", list, fitness=creator.FitnessMax)
    toolbox = base.Toolbox()
    print(
        "Choose classificator: \n 1: decision tree \n 2: ADABoost \n 3: k-neighbours \n 4: random forest \n 5: SVC \n"
    )
    classifier = int(input())
    toolbox = choose_classifier(toolbox,
                                number_of_attributes,
                                df,
                                y,
                                classifier_id=classifier)

    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    plot_x, plot_value, plot_std, plot_avg = [], [], [], []
    print("It's a Genetic algorithm with couple of deap library examples")
    print("Please choose details:")

    print(
        "Choose selection: \n 0: Tournament \n 1: Random \n 2: Best \n 3: Worst \n 4: Roulette \n 5: Blend"
    )
    user_input_selection = input()
    if user_input_selection == '0':
        print("Choose tournament size: ")
        tournament_size = input()
        toolbox.register("select",
                         tools.selTournament,
                         tournsize=int(tournament_size))
    elif user_input_selection == '1':
        toolbox.register("select", tools.selRandom)
    elif user_input_selection == '2':
        toolbox.register("select", tools.selBest)
    elif user_input_selection == '3':
        toolbox.register("select", tools.selWorst)
    elif user_input_selection == '4':
        toolbox.register("select", tools.selRoulette)
    elif user_input_selection == '5':
        toolbox.register("select", tools.cxUniformPartialyMatched, indpb=0.9)
    else:
        toolbox.register("select", tools.selBest)

    print(
        "Choose crossover method: \n 0: One point \n 1: Two point \n 2: uniform \n 3: Messy One point"
    )
    user_input_mate = input()
    if user_input_mate == '0':
        toolbox.register("mate", tools.cxOnePoint)
    elif user_input_mate == '1':
        toolbox.register("mate", tools.cxTwoPoint)
    elif user_input_mate == '2':
        toolbox.register("mate", tools.cxUniform, indpb=0.1)
    elif user_input_mate == '3':
        toolbox.register("mate", tools.cxMessyOnePoint)
    else:
        toolbox.register("mate", tools.cxOnePoint)

    print(
        "Choose mutation method: \n 0: Gaussian \n 1: Uniform Int \n 2: Shuffle Indexes\n 3: Custom algorithm mutation"
    )
    user_input_mutation = input()
    if user_input_mutation == '0':
        toolbox.register("mutate",
                         tools.mutGaussian,
                         mu=5,
                         sigma=10,
                         indpb=0.9)
    if user_input_mutation == '1':
        toolbox.register("mutate", tools.mutUniformInt, low=-10, up=10)
    if user_input_mutation == '2':
        toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.9)
    if user_input_mutation == '3':
        if classifier == 1:
            toolbox.register("mutate", mutation_decision_tree)
        elif classifier == 2:
            toolbox.register("mutate", mutation_ADABoost)
        elif classifier == 3:
            toolbox.register("mutate", mutationKNeighbors)
        elif classifier == 4:
            toolbox.register("mutate", mutation_random_forest)
        elif classifier == 5:
            toolbox.register("mutate", mutation_svc)
    else:
        toolbox.register("mutate",
                         tools.mutGaussian,
                         mu=5,
                         sigma=10,
                         indpb=0.9)

    print("Set size of population: ")
    # user_input_population_size = input()
    # sizePopulation = int(user_input_population_size)
    sizePopulation = 50

    print("Set mutation probablility: ")
    # user_input_mutation_probability = input()
    # probabilityMutation = float(user_input_mutation_probability)
    probabilityMutation = 0.1

    print("Set crossover probability: ")
    # user_input_crossover_probability = input()
    # probabilityCrossover = float(user_input_crossover_probability)
    probabilityCrossover = 0.9
    print("Set number of generations: ")
    # user_input_number_of_generations = input()
    # numberIteration = int(user_input_number_of_generations)
    numberIteration = 50

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

    g = 0
    numberElitism = 1
    start_time = time.clock()
    while g < numberIteration:
        g = g + 1
        print("-- Generation %i --" % g)
        # Select the next generation individuals
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected individuals
        offspring = list(map(toolbox.clone, offspring))
        listElitism = []
        for x in range(0, numberElitism):
            listElitism.append(tools.selBest(pop, 1)[0])
        # Apply crossover and mutation on the offspring
        for child1, child2 in zip(offspring[::2], offspring[1::2]):
            # cross two individuals with probability CXPB
            if random.random() < probabilityCrossover:
                toolbox.mate(child1, child2)
                # fitness values of the children
                # must be recalculated later
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            # mutate an individual with probability MUTPB
            if random.random() < probabilityMutation:
                toolbox.mutate(mutant)
                del mutant.fitness.values
            # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = 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[:] = offspring + listElitism
        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x * x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5
        print(" Min %s" % min(fits))
        print(" Max %s" % max(fits))
        print(" Avg %s" % mean)
        print(" Std %s" % std)
        best_ind = tools.selWorst(pop, 1)[0]
        print("Best individual is %s, %s" %
              (best_ind, best_ind.fitness.values))
        plot_x.append(g)
        plot_avg.append(mean)
        plot_std.append(std)
        plot_value.append(best_ind.fitness.values)
    #
    timeOfAll = time.clock() - start_time
    print(f'Time: {timeOfAll}')
    print("-- End of (successful) evolution --")

    plt.figure()
    plt.plot(plot_value)
    plt.ylabel('Best values')
    plt.show()
    plt.figure()
    plt.plot(plot_avg)
    plt.ylabel('Averages')
    plt.show()
    plt.figure()
    plt.plot(plot_std)
    plt.ylabel('Standard deviation')
    plt.show()
Esempio n. 30
0
def main():
    #random.seed(64)
    # nopop=range(10,200,20)
    # nopop=nopop[randrange(len(nopop))]
    # pop = toolbox.population(nopop[randrange(len(nopop))])
    nopop=random.randint(10,299)
    pop=toolbox.population(nopop)
    CXPB=round(random.uniform(0,1),1)
    # CXPB=CXPB[randrange(len(CXPB))]
    MUTPB=round(random.uniform(0,1),1)
    NGEN = 500

    print("Start of evolution")

    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

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

    # Begin the evolution
    start = time.time()
    minval=[]
    maxval=[]
    config=[]
    for i in range(10):
        for g in range(NGEN):
            print("-- Generation %i --" % g)

        # 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]):
                if random.random() < CXPB:
                    toolbox.mate(child1, child2)
                    del child1.fitness.values
                    del child2.fitness.values

            for mutant in offspring:
                if random.random() < MUTPB:
                    toolbox.mutate(mutant)
                    del mutant.fitness.values

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

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

        # The population is entirely replaced by the offspring
            pop[:] = offspring

        # Gather all the fitnesses in one list and print the stats
            fits = [ind.fitness.values[0] for ind in pop]

            length = len(pop)
            mean = sum(fits) / length
            sum2 = sum(x*x for x in fits)
            std = abs(sum2 / length - mean**2)**0.5

            print("  Min %s" % min(fits))
            print("  Max %s" % max(fits))
            print("  Avg %s" % mean)
            print("  Std %s" % std)


            minval.append([min(fits),tools.selWorst(pop,1)])
            maxval.append([max(fits),tools.selBest(pop,1)[0]])

            if max(fits) == 128:
                print "Total:", time.time()-start
                print tools.selBest(pop, 1)[0]
                return


            if min(fits) == 0:
                config.append([CXPB, MUTPB, nopop, time.time()-start])
                break
        pop=[]

    print("-- End of (successful) evolution --")
    print "Total:", time.time()-start
    best_ind = tools.selBest(pop, 1)[0]
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
    print min(minval)
    # config=[CXPB,MUTPB,time.time()-start]
    # f=open("config"+str(nopop)+".txt",'w')
    f=open("config13.txt",'w')
    # for el in range(len(config)):
    #     simplejson.dump(config[el],f)
    #     simplejson.dump('/n' ,f)
    # f.close()
    for el in config:
        f.write("%s\n" % el)
Esempio n. 31
0
toolbox.register("evaluate", eval)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutUniformInt, low=0, up=[3,3,3,3,100,100,100,100] ,indpb=0.15)
toolbox.register("select", tools.selTournament, tournsize=3)

population = toolbox.population(n=80)

NGEN=25
best = []
for gen in range(NGEN):
    offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.2)
    fits = toolbox.map(toolbox.evaluate, offspring)
    for fit, ind in zip(fits, offspring):
        ind.fitness.values = fit
    best.append(traffic_sim(tools.selBest(population, k=1)[0]))
    print("best: " + str(traffic_sim(tools.selBest(population, k=1)[0])),tools.selBest(population, k=1) )
    print("worst: " + str(traffic_sim(tools.selWorst(population, k=1)[0])),tools.selWorst(population, k=1))
    population = toolbox.select(offspring, k=len(population))


passed_intersection, entered_intersection, wreck, avg_wait = zip(*best)
for value in zip(*best):
    plt.plot(range(0,NGEN),np.array(value))
plt.legend(["passed_intersection", "entered_intersection", "wreck", "avg_wait"])
plt.show()

finalBest = tools.selBest(population, k=1)[0]
traffic_vis.draw_intersection(finalBest)

Esempio n. 32
0
def genetic_algorithm(y, df, numberOfAttributes):
    tstart = time.time()
    creator.create("FitnessMax", base.Fitness, weights=(-1.0, ))
    creator.create("Individual", list, fitness=creator.FitnessMax)

    toolbox = base.Toolbox()
    toolbox.register('individual', SVCParameters, numberOfAttributes,
                     creator.Individual)
    toolbox.register("population", tools.initRepeat, list, toolbox.individual)
    toolbox.register("evaluate", SVCParametersFitness, y, df,
                     numberOfAttributes)
    toolbox.register("select", tools.selTournament, tournsize=3)
    toolbox.register("mate", tools.cxTwoPoint)
    toolbox.register("mutate", mutationSVC)

    sizePopulation = 100
    probabilityMutation = 0.15
    probabilityCrossover = 0.85
    numberIteration = 100

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

    g = 0
    numberElitism = 1
    bestlist = []
    meanlist = []
    stdlist = []
    while g < numberIteration:
        g = g + 1
        print("--- Epoch %i ---" % g)

        offspring = toolbox.select(pop, len(pop))
        offspring = list(map(toolbox.clone, offspring))

        for child1, child2 in zip(offspring[::2], offspring[1::2]):

            if random.random() < probabilityCrossover:
                toolbox.mate(child1, child2)

                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            if random.random() < probabilityMutation:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = 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[:] = offspring

        fits = [ind.fitness.values[0] for ind in pop]

        length = len(pop)
        mean1 = sum(fits) / length
        sum2 = sum(x * x for x in fits)
        std = abs(sum2 / length - mean1**2)**0.5

        print("  Min %s" % min(fits))
        print("  Max %s" % max(fits))
        print("  Avg %s" % mean1)
        print("  Std %s" % std)
        best_ind = tools.selWorst(pop, 1)[0]
        print("Best individual is %s, %s" %
              (best_ind, best_ind.fitness.values))
        bestlist.append(max(fits))
        meanlist.append(mean(bestlist))
        if g > 1:
            stdlist.append(stdev(bestlist))

    tend = time.time()
    t = tend - tstart
    toPlot(bestlist, meanlist, stdlist)
    return bestlist.index(max(bestlist)), max(bestlist), t
Esempio n. 33
0
def main():
    """Complete generational algorithm
    mapを使うとジェネレータが帰ってくる"""
    n=100
    pop = toolbox.population(n)
    CXPB, MUTPB, NGEN = 0.6, 0.3, 100
    elite = 0.16
    random_elite = 0.04

    #初期個体の評価
    fitnesses = toolbox.map(toolbox.evaluate, pop) #[(6782,),(2342,)...]になってる
    
    for ind, fit in zip(pop, fitnesses): #ここでfitnessesが遅延評価される
        #print ind, fit
        ind.fitness.values = fit

    #Select the next generation individuals
    for g in xrange(NGEN):
        
        #g世代の個体についてstatisticsで設定した値を記録
        logbook.add_dictionary(g)
        #すべての個体の適応度をリストにまとめる
        fits = [ind.fitness.values for ind in pop]

        # recordに渡す値は実行する関数と対応
        #関数の引数が複数の場合は(データ、引数)で渡す
        logbook.record(fits, (pop, 1))
        #上位16%の個体は残す
        bestoffspring = tools.selBest(pop, int(n*elite))
        top = tools.selBest(pop, 1)
        print top


        #下位84%の個体からランダムに4%の個体を選び、エリートに加える
        worstoffspring = tools.selWorst(pop, int(n-(n*elite)))
        random_ind = tools.selRandom(worstoffspring, int(n*random_elite))
        save_offspring = bestoffspring + random_ind

        #元のバージョンoffspring = [toolbox.clone(ind) for ind in pop]やめたほうがいい
        #averageがおかしくなる

        #エリート保存(20%)
        save_offspring = list(toolbox.map(toolbox.clone, save_offspring))

        #オペレータを適用する個体(80%)
        #ランダムバージョン
        operated_offspring = tools.selRandom(worstoffspring, 
                                             int(n - len(save_offspring)))

        all_offspring = list(toolbox.map(toolbox.clone, 
                                         save_offspring + operated_offspring))

        #print len(operated_offspring), len(save_offspring), len(all_offspring)
        
        #Apply crossover and mutation on the offspring
        # 偶数番目と奇数番目の個体を取り出して交差
        for child1, child2 in zip(operated_offspring[::2], 
                                  operated_offspring[1::2]):
            if random.random() < CXPB:
                toolbox.mate(child1, child2)
                del child1.fitness.values
                del child2.fitness.values

        for mutant in operated_offspring:
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
                del mutant.fitness.values

        #Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in operated_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
        
        #評価値に従って個体を選択
        #The population is entirely replaced by the offspring
        #pop = toolbox.select(pop + offspring, len(offspring))
        pop[:] = save_offspring + operated_offspring
        

    return logbook
Esempio n. 34
0
def main():
    #random.seed(64)
    # nopop=range(10,200,20)
    # nopop=nopop[randrange(len(nopop))]
    # pop = toolbox.population(nopop[randrange(len(nopop))])
    nopop=random.randint(10,299)
    pop=toolbox.population(nopop)
    CXPB=round(random.uniform(0,1),1)
    # CXPB=CXPB[randrange(len(CXPB))]
    MUTPB=round(random.uniform(0,1),1)
    NGEN = 500

    print("Start of evolution")

    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

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

    # Begin the evolution
    start = time.time()
    minval=[]
    maxval=[]
    config=[]
    for i in range(10):
        for g in range(NGEN):
            print("-- Generation %i --" % g)

        # 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]):
                if random.random() < CXPB:
                    toolbox.mate(child1, child2)
                    del child1.fitness.values
                    del child2.fitness.values

            for mutant in offspring:
                if random.random() < MUTPB:
                    toolbox.mutate(mutant)
                    del mutant.fitness.values

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

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

        # The population is entirely replaced by the offspring
            pop[:] = offspring

        # Gather all the fitnesses in one list and print the stats
            fits = [ind.fitness.values[0] for ind in pop]

            length = len(pop)
            mean = sum(fits) / length
            sum2 = sum(x*x for x in fits)
            std = abs(sum2 / length - mean**2)**0.5

            print("  Min %s" % min(fits))
            print("  Max %s" % max(fits))
            print("  Avg %s" % mean)
            print("  Std %s" % std)


            minval.append([min(fits),tools.selWorst(pop,1)])
            maxval.append([max(fits),tools.selBest(pop,1)[0]])

            if max(fits) == 128:
                print "Total:", time.time()-start
                print tools.selBest(pop, 1)[0]
                return


            if min(fits) == 0:
                config.append([CXPB, MUTPB, nopop, time.time()-start])
                break
        pop=[]

    print("-- End of (successful) evolution --")
    print "Total:", time.time()-start
    best_ind = tools.selBest(pop, 1)[0]
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
    print min(minval)
    # config=[CXPB,MUTPB,time.time()-start]
    # f=open("config"+str(nopop)+".txt",'w')
    f=open("config13.txt",'w')
    # for el in range(len(config)):
    #     simplejson.dump(config[el],f)
    #     simplejson.dump('/n' ,f)
    # f.close()
    for el in config:
        f.write("%s\n" % el)
Esempio n. 35
0
def main():
    #random.seed(12) #uncommet this for testing
    pop = toolbox.population(n=n_pop) #CREATE POPULATION
    #for i in pop: #prints initial population
    # print pop.index(i)+1, i
    
    print("Start of evolution")
    
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop)) #THIS LINE MUST BE UNDERSTOOD
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    #for i in pop:
    #    print i.fitness.values,

    print " "
    print(" Evaluated %i individuals" % len(pop))
    
    # Begin the evolution
    round_gen = 0
    for g in range(NGEN):
        try:
            k = g+1
            round_gen += 1

            global wildcardWeight
            if wcw_switching == True:
                if g%wcw_swapGen == 0:
                    wildcardWeight = wcw_b
                else:
                    wildcardWeight = wcw_a
            
            print wildcardWeight

            # Initialize new population
            offspring = []
            
            # Select the next generation individuals
            elites = toolbox.selectE(pop) # select elites for next gen
            #if show_elites == True:
            #    for idx, i in enumerate(elites):
            #        print "%3d" % idx, "fv: %.6f" % i.fitness.values, i
            

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

            # Apply crossover on the offspring individuals
            # first we shuffle list members positions.
            # Then we mate every two members next to one another
            random.shuffle(offspring)
            random.shuffle(offspring) 
            for child1, child2 in zip(offspring[::2], offspring[1::2]):
                if random.random() < CXPB:
                    toolbox.mate(child1, child2)
                    del child1.fitness.values
                    del child2.fitness.values

            # Apply mutation on the offsping individuals
            for idx, individual in enumerate(offspring):
                if random.random() < enterMutation: # no need bcuz MUTPB in def
                    mutor = toolbox.clone(individual)
                    #print dir(mutor)
                    #print "##IND##", individual
                    mutor = toolbox.mutate(individual)
                    #print "##MUT##", mutor
                    del mutor.fitness.values
                    offspring[idx] = mutor

        #mutant = toolbox.clone(ind1)
        #ind2, = tools.mutGaussian(mutant, mu=0.0, sigma=0.2, indpb=0.2)
        #del mutant.fitness.values

    #-- VIII -- Optimizers --------------------------------------------------------

        #    print "###", len(offspring)
            for i in elites:
                offspring.append(i)

            mutatedElites = 0

            if mutateElitesWildcards == True:
                
                for i in elites:
                    #print i
                    mutant = mutateWcardGene_rand(i)
                    if mutant != i:
                        mutatedElites += 1
                        #print mutant
                        offspring.append(mutant)
                        #print offspring[-1]

        #    print "###", len(offspring)

            weaklings = tools.selWorst(offspring, (baseWeaklings + len(elites) + mutatedElites))
            for i in weaklings:
                offspring.remove(i)

            #This could be used in the same way to eliminate individuals like weaklings but..
            #offspring = list(offspring for offspring,_ in itertools.groupby(offspring))

            n_lost = n_pop - len(offspring) #No. of individuals lost due to 
            for i in range(n_lost):         #duplication or weaklings weeded out
                new_ind = toolbox.individual()
                #print new_ind
                offspring.append(new_ind)   #we replace them

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


            
            
            #remove dulicates in offspring (not practical unless new indvs added)
            #offspring = list(offspring for offspring,_ in itertools.groupby(offspring))

    #- End VIII ---------------------------------------------------------------------

            # The population is entirely replaced by the offspring
            random.shuffle(offspring)
            pop[:] = offspring

    #-- IX --- Statistics and Each loop Outputs -------------------------------------

            # Gather all the fitnesses in one list and print the stats
            if show_stats == True:
                print("-- Generation %i --" % k)
                print(" Evaluated %i individuals" % len(invalid_ind))
                fits = [ind.fitness.values[0] for ind in pop]
                
                length = len(pop)
                mean = sum(fits) / length
                sum2 = sum(x*x for x in fits)
                std = abs(sum2 / length - mean**2)**0.5
                #mx = float(max(fits))
                #mxp = (mx*100) / n_inds

                print(" individuals: %s" % len(pop))
                print(" weaklings: %s" % len(weaklings))
                print(" elites: %s" % len(elites))
                print(" Mutated Elites: %s" % mutatedElites)
                print(" Audit data: %s lines" % len(auditData))
                #print(" Min %s" % min(fits))
                print(" Max %s" % max(fits))
                #print(" mxp %.3f %%" % mxp)
                print(" Avg %s" % mean)
                print(" Std %s \n" % std)
                if show_elites == True and elitesNo >= 6:
                    bestElites = tools.selBest(elites,20)
                    for idx, i in enumerate(bestElites):
                        print "%3d" % idx, "fv: %.14f" % i.fitness.values, i
                elif show_elites == True:
                    for idx, i in enumerate(elites):
                        print "%3d" % idx, "fv: %.14f" % i.fitness.values, i            
                print("------End Generation %s" % k)
                print "\n"
            #print fitnesses

        except KeyboardInterrupt:
            print "You hit Crt-C to prematurely exit the loop"
            break

    
#- End IX -------------------------------------------------------------------------

# for i in pop: #prints initial population
# print pop.index(i)+1, "fv=%s" % i.fitness.values, i

        #if max(fits) >= 0.8063:
        # break
    
# print("-- End of (as NGEN set) evolution --")
    #for ind in pop:
    #    del ind.fitness.values

    global wildcardPenalty
    #wildcardPenalty = False
    wildcardPenalty = True

    fitnesses = list(map(toolbox.evaluate, pop)) #re-evaluate fitness without wildcard penalty
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit  

    print "Best individuals are: " #% (best_ind, best_ind.fitness.values))
    bestInds = tools.selBest(pop, Result_numbers)

    for i, j in enumerate(bestInds):
        print "%3d" % i, "fv: %.14f" % j.fitness.values, j

    print "\n\n"
    #Remove duplicate individuals from the results
    bestInds.sort()
    bestInds = tools.selBest(bestInds, len(bestInds))
    bestInds = list(bestInds for bestInds,_ in itertools.groupby(bestInds))
    print "Best individuals (duplications removed) are: "
    for i, j in enumerate(bestInds):
        print "%3d" % i, "fv: %.14f" % j.fitness.values, j

    topknots = toolbox.selectE(bestInds)
    print "\n\n"
    print "Best individuals by attack types are: "
    for i, j in enumerate(topknots):
        if j.fitness.values[0] > 0.0:
            print "%9s" % j[14][0:16], "%3d" % i, "fv: %.14f" % j.fitness.values, j
            #print "%3d" % i, "fv: %.14f" % j.fitness.values, j

    print "We ran", round_gen, "rounds"
Esempio n. 36
0
def main():
    """ This function is an entry  point of the application.
    """
    # reading command-line argumets and options
    parser = optparse.OptionParser()
    parser.set_defaults(debug=False,xls=False)
    parser.add_option('--debug', action='store_true', dest='debug')
    parser.add_option('--verbose', action='store_true', dest='verbose')
    parser.add_option('--evaluateDirect', action='store_true', dest='evaluateDirect')
    parser.add_option('--evaluateLikelihood', action='store_true', dest='evaluateLikelihood')
    
    (options, args) = parser.parse_args()
    
    # obtaining execution paramters
    parameters = {'InputFile': 'xxx.in', 
                  'InputFormat': 'in',
                  'DolloK': 1,
                  'Alpha': 0.4,
                  'Beta': 0.00001,
                  'RandomSeed': 1528981076,
                  'PopulationSize': 90,
                  'EliteSize': 0,
                  'CrossoverProbability': 0.96,
                  'MutationProbability': 0.64,
                  'FineGrainedTournamentSize': 3.5,
                  'MaxNumberGenerations': 7}
    parameters = get_execution_parameters(options, args, parameters)
    print("Execution parameters: ", parameters);
    print("------------------------")
    
    # setting random seed
    random_seed_value = 0
    if( int(parameters['RandomSeed']) > 0 ):
        random_seed_value = int(parameters['RandomSeed'])
    else:
        random_seed_value = int(time.mktime(datetime.now().timetuple()))
    random.seed( random_seed_value )
    print("Random seed: ",random_seed_value);
    time_of_start = time.clock()
    time_of_start_generation = time.clock()
    print("Execution starts at time: ", time_of_start/1000.0)
    print("------------------------")
    
        
    # reading read elements from input file
    if( parameters['InputFormat'] == 'in' ):
        (labels, reads) = read_labels_scrs_format_in(options, parameters)
        if(options.debug or options.verbose):
            print("Mutation labels (from input file):\n", labels);
            print("Reads[Unknowns] (from input file):")
            for x in reads:
                print(x);
    else:
        print("Error: Input format is not right.")
        return
    if( options.verbose):
        current_time = time.clock()
        print("Parameters read after: ", 
              (current_time - time_of_start)/1000.0, " sec.")
        time_of_start = current_time

    if( options.debug or options.verbose):
        print("Start of evolution")
    # create fitness function
    creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
   
    # create strucute of the individual
    creator.create("Individual", DolloNode, fitness=creator.FitnessMin)
    
    # create toolbox for execution of the evolutionary algorithm
    toolbox = base.Toolbox()
    
    # register bolean attribute to toolbbox 
    toolbox.register("attr_bool", random.randint, 0, 1)

    # parameter k in Dollo model
    dollo_k = int(parameters['DolloK'])
    if( dollo_k < 0):
        print("Error: parameter DolloK should not be negative.")
        return
    # register individual creation to toolbbox 
    toolbox.register("individual", 
                     init_dollo_node_individual, 
                     creator.Individual, 
                     labels=labels, 
                     k=dollo_k)
      
    # register population to toolbbox 
    toolbox.register("population", 
                     tools.initRepeat, 
                     list, 
                     toolbox.individual)
 
    # probability of false positives and false negatives
    alpha = float(parameters['Alpha'])
    if( alpha < 0):
        print("Error: parameter Alpha should not be negative.")
        return
    if( alpha >= 1):
        print("Error: parameter Alpha should be less than 1.")
        return
    beta = float(parameters['Beta'])
    if( beta < 0):
        print("Error: parameter Beta should not be negative.")
        return
    if( beta >= 1):
        print("Error: parameter Beta should be less than 1.")
        return
    # register evaluation function
    toolbox.register("evaluate", 
                     evaluate_dollo_node_likelihood, 
                     reads,
                     alpha,
                     beta)
    if( options.evaluateDirect ):
        toolbox.register("evaluate", 
                     evaluate_dollo_node_direct, 
                     reads,
                     alpha,
                     beta)
        
    # register the crossover operator
    toolbox.register("mate", 
                     crossover_dollo_node_exchange_parent_indices,
                     labels, dollo_k)
    # probability with which two individuals are crossed
    crossover_probability = float(parameters['CrossoverProbability'])
    if( crossover_probability < 0):
        print("Error: parameter CrossoverProbability should not be negative.")
        return
    if( crossover_probability > 1):
        print("Error: parameter CrossoverProbability should not be greater than 1.")
        return
           
    # register a mutation operator 
    toolbox.register("mutate", 
                     mutation_dollo_node_combine, 
                     labels,
                     dollo_k)
    # probability for mutating an individual
    mutation_probability = float(parameters['MutationProbability'])
    if( mutation_probability < 0):
        print("Error: parameter MutationProbability should not be negative.")
        return
    if( mutation_probability > 1):
        print("Error: parameter MutationProbability should not be greater than 1.")
        return

    # create an initial population, where each individual is a tree
    population_size = int(parameters['PopulationSize'])
    if( population_size <= 0):
        print("Error: parameter PopulationSize should be positive.")
        return
    pop = toolbox.population(n=population_size)
    if( options.verbose):
        print("Population (size %d) - initial\n"%len(pop))
        print (pop)
 
    # operator for selecting individuals for breeding the next
    # generation
    fgt_size = float(parameters['FineGrainedTournamentSize'])
    if( fgt_size <= 0):
        print("Error: parameter FineGrainedTournamentSize should be positive.")
        return
    toolbox.register("select", 
                     tools.selTournamentFineGrained, 
                     fgtournsize=fgt_size)

    if( options.verbose):
        current_time = time.clock()
        print("Registering operators for deap after: ", 
              (current_time - time_of_start)/1000.0, " sec.")
        time_of_start = current_time
    
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    if( options.debug):
        print("Fitnesses of individuals in population - initial")
        print (fitnesses)
    
    # Assign fitness to individuals in population
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit
    
    # Variable for maximum number of generations    
    max_number_generations = int(parameters['MaxNumberGenerations'])
    if( max_number_generations < 0):
        print("Error: parameter MaxNumberGenerations should not be negative.")
        return
    # Variable keeping track of the number of generations
    generation = 0
  
    # Begin the evolution
    while True:
        if( options.debug or options.verbose):
            print("-- Generation %i --" % generation)
            current_time_generation = time.clock()
            print("New generation after: ",  
                  (current_time_generation - time_of_start_generation)/1000.0, " sec.")
            time_of_start_generation = current_time_generation
   
        if( options.debug or options.verbose):
            fits = [ind.fitness.values[0] for ind in pop]
            length = len(pop)
            mean = sum(fits) / length
            sum2 = sum(x*x for x in fits)
            std = abs(sum2 / length - mean**2)**0.5
            print("  Fitnesses: ", fits)
            print("  Min %s" % min(fits))
            print("  Max %s" % max(fits))
            print("  Avg %s" % mean)
            print("  Std %s" % std)
            best_in_generation = tools.selBest(pop, 1)[0]
            print("  Best individual: \n %s", best_in_generation)
            print("  Best individual fitness: ", best_in_generation.fitness.values[0]); 
        
        # A new generation
        generation += 1

        # size of the elite part of the population
        elite_size = int(parameters['EliteSize'])
        if( elite_size < 0):
            print("Error: parameter EliteSize should not be negative.")
            return
        if( elite_size > population_size):
            print("Error: parameter EliteSize should not be greather than PopulationSize.")
            return
        # elite individuals
        elite = tools.selBest(pop,elite_size)
        # clone elite individuals
        elite = list(map(toolbox.clone, elite))
        if(options.verbose):
            print("elite =", elite)
        # non-elite individuals
        non_elite =  tools.selWorst(pop, population_size-elite_size)
        # clone non-elite individuals
        non_elite = list(map(toolbox.clone, non_elite))
        if(options.verbose):
            print("non_elite =", non_elite)
         
        # Select the ofsprings
        offspring = toolbox.select(pop, len(pop))
        # Clone the selected offsprings
        offspring = list(map(toolbox.clone, offspring))
        if(options.verbose):
            print("offspring (after selection) \n =", offspring)
        if( options.verbose):
            current_time = time.clock()
            print("Selecting offsprings after: ", 
                  (current_time - time_of_start)/1000.0, " sec.")
            time_of_start = current_time
  
        # Apply crossover on the offsprings
        for i in range(0,len(offspring)):
            # cross two individuals with previously determined probability 
            if random.random() < crossover_probability:
                child1 = random.choice(offspring)
                index2 = random.choice(range(0,len(offspring)))
                child2 = offspring[index2]
                i = 0
                while(child2.tree_is_equal(child1) and i<=len(offspring)):
                    i += 1
                    child2 = offspring[(index2+i)%len(offspring)]
                toolbox.mate(child1, child2)
                # fitness values of the children
                # must be recalculated later
                del child1.fitness.values
                del child2.fitness.values
        if(options.verbose):
            print("offspring (after crossover) \n =", offspring)
        if( options.verbose):
            current_time = time.clock()
            print("Crossover after: ", 
                  (current_time - time_of_start)/1000.0, " sec.")
            time_of_start = current_time

        # Apply mutation on the offspring
        for mutant in offspring:
            # mutate an individual with previously determined probability 
            if random.random() < mutation_probability:
                toolbox.mutate(mutant)
                # fitness values of the mutant
                # must be recalculated later
                del mutant.fitness.values
        if(options.verbose):
            print("offspring (after mutation) \n =", offspring)
        if( options.verbose):
            current_time = time.clock()
            print("Mutation after: ", 
                  (current_time - time_of_start)/1000.0, " sec.")
            time_of_start = current_time
   
        # Evaluate the new individuals that has no fitness until now
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = map(toolbox.evaluate, invalid_ind)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
            
        # Pool of items that should be copies into new generation
        # offsprings are put into pool
        pool = offspring
        # non-elite individuals are put into pool
        for i in range(0, population_size-elite_size):
            pool.append(non_elite[i])
        if(options.verbose):
            print("pool (for new generation) \n =", pool)
      
        # Elite individuals are copied into population
        for i in range(0,elite_size):
            pop[i] = elite[i]
        if(options.verbose):
            print("population (elites are copied) \n =", pop)

        # Remainning part of population is filled individuals from pool, 
        # taking into account that there will be no
        # repetition of the individuals 
        for i in range(elite_size,population_size):
            pop[i] = None
        j=0
        for i in range(elite_size,population_size):
            while(j<len(pool) and contains(pop, pool[j])):
                j+=1
            if( j < len(pool)):
                pop[i] = pool[j]
            else:
                pop[i] = pool[i-elite_size]
        if(options.verbose):
            print("population (all items are copied) \n =", pop)
        if( options.verbose):
            current_time = time.clock()
            print("Evaluation of changed individuals after: ", 
                  (current_time - time_of_start)/1000.0, " sec.")
            time_of_start = current_time


        # Check if any of finishing criteria is meet
        # Criteria based on number of generations
        if( generation > max_number_generations ):
            break
        # Criteria based on standard deviation of fitness in population
        fits = [ind.fitness.values[0] for ind in pop]
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x*x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5 
        if( std <= 0):
            break
          
    if( options.debug or options.verbose):
        print("-- End of evolution --")
    if( options.verbose):
        print("Population (size %d) - at end\n"%len(pop))
        print (pop)  
    best_ind = tools.selBest(pop, 1)[0]
    print("Best individual is\n%s\n, with fitness %s" % (best_ind, best_ind.fitness.values))
    matrix_E=matrix_inferred_E(reads,alpha,beta,best_ind)
    generate_matrix(matrix_E[0],parameters['InputFile']+"_fitness_"+str(best_ind.fitness.values)+".txt")
       # " alpha:"+str(alpha)+" beta:" +str(beta)+" dollo_k"+str(dollo_k)+" pro_cro"+
       # str(crossover_probability)+" pro_mut"+str(mutation_probability)+" pop_size"+
       # str(population_size)+" fitness"+str(best_ind.fitness.values)+".txt")
    generate_digraph(best_ind,labels,"tree_DOT_"+parameters['InputFile']+"_fitness_"+
                    str(best_ind.fitness.values)+".gv")  
    if( options.verbose):        
        print("Efficiency of cashing for funcion dolo_closest_node_distance")
        print(dollo_closest_node_distance.cache_info())
        print("Efficiency of cashing for funcion dolo_closest_node_distance")
        print(sub_evaluate.cache_info())
       #print("Efficiency of cashing for funcion dolo_closest_node_distance")
        #print(EaNode.closest_node_in_tree.cache_info())
    return
Esempio n. 37
0
def main():
    #random.seed(12) #uncommet this for testing
    pop = toolbox.population(n=n_pop) #CREATE POPULATION
    #for i in pop: #prints initial population
    # print pop.index(i)+1, i
    
    print("Start of evolution")
    
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop)) #THIS LINE MUST BE UNDERSTOOD
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    #for i in pop:
    #    print i.fitness.values,

    print " "
    print(" Evaluated %i individuals" % len(pop))
    
    # Begin the evolution
    round_gen = 0
    for g in range(NGEN):
        try:
            k = g+1
            round_gen += 1

            global wildcardWeight
            if wcw_switching == True:
                if g%wcw_swapGen == 0:
                    wildcardWeight = wcw_b
                else:
                    wildcardWeight = wcw_a
            
            #print wildcardWeight

            # Initialize new population
            offspring = []
            
            # Select the next generation individuals
            elites = toolbox.selectE(pop) # select elites for next gen
            #if show_elites == True:
            #    for idx, i in enumerate(elites):
            #        print "%3d" % idx, "fv: %.6f" % i.fitness.values, i
            

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

            # Apply crossover on the offspring individuals
            # first we shuffle list members positions.
            # Then we mate every two members next to one another
            random.shuffle(offspring)
            random.shuffle(offspring) 
            for child1, child2 in zip(offspring[::2], offspring[1::2]):
                if random.random() < CXPB:
                    toolbox.mate(child1, child2)
                    del child1.fitness.values
                    del child2.fitness.values

            # Apply mutation on the offsping individuals
            for idx, individual in enumerate(offspring):
                if random.random() < enterMutation: # no need bcuz MUTPB in def
                    mutor = toolbox.clone(individual)
                    #print dir(mutor)
                    #print "##IND##", individual
                    mutor = toolbox.mutate(individual)
                    #print "##MUT##", mutor
                    del mutor.fitness.values
                    offspring[idx] = mutor

        #mutant = toolbox.clone(ind1)
        #ind2, = tools.mutGaussian(mutant, mu=0.0, sigma=0.2, indpb=0.2)
        #del mutant.fitness.values

    #-- VIII -- Optimizers --------------------------------------------------------

            supremes = []
            
            for i in uniq_attack:
                space = []
                jail = []
                #topgun = toolbox.empty_individual()
                for j in elites:
                    if j[-1] == i:
                        space.append(j)
                #space.sort()
                global ace
                ace = tools.selBest(space, 1)
                ace = ace[0] #THE BEST ONE of that attack type

                if fitnessDiff_opt == True:

                    for idx, ind in enumerate(space):            #it works now using jail[]
                    #    print idx ,ind.fitness.values, ind
                    #    print (ace.fitness.values[0] - ind.fitness.values[0]) <= fitnessDiff_value
                        if (((ace.fitness.values[0] - ind.fitness.values[0]) <= fitnessDiff_value) and (idx > 0)):
                            jail.append(ind)
                    #        print 1

                    for ind in jail:
                        space.remove(ind)


                if matchEliminate_opt == True:
                    jail = []
                    for idx, ind in enumerate(space):
                        if matchEliminate(ace, ind) and ind != ace:
                            jail.append(ind)

                    for ind in jail:
                        space.remove(ind)

                #space = tools.selBest(space, bestTopKnots)
                for i in space:
                    supremes.append(i)

            elites = supremes
#--------

        #    print "###", len(offspring)
            for i in elites:
                offspring.append(i)

            mutatedElites = 0

            if mutateElitesWildcards == True:
                
                for i in elites:
                    #print i
                    mutant = mutateWcardGene_rand(i)
                    if mutant != i:
                        mutatedElites += 1
                        #print mutant
                        offspring.append(mutant)
                        #print offspring[-1]

        #    print "###", len(offspring)

            weaklings = tools.selWorst(offspring, (baseWeaklings + len(elites) + mutatedElites))
            for i in weaklings:
                offspring.remove(i)

            #This could be used in the same way to eliminate individuals like weaklings but..
            #offspring = list(offspring for offspring,_ in itertools.groupby(offspring))

            n_lost = n_pop - len(offspring) #No. of individuals lost due to 
            for i in range(n_lost):         #duplication or weaklings weeded out
                new_ind = toolbox.individual()
                #print new_ind
                offspring.append(new_ind)   #we replace them

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


            
            
            #remove dulicates in offspring (not practical unless new indvs added)
            #offspring = list(offspring for offspring,_ in itertools.groupby(offspring))

    #- End VIII ---------------------------------------------------------------------

            # The population is entirely replaced by the offspring
            random.shuffle(offspring)
            pop[:] = offspring

    #-- IX --- Statistics and Each loop Outputs -------------------------------------

            # Gather all the fitnesses in one list and print the stats
            if show_stats == True:
                print("-- Generation %i --" % k)
                print(" Evaluated %i individuals" % len(invalid_ind))
                fits = [ind.fitness.values[0] for ind in pop]
                
                length = len(pop)
                mean = sum(fits) / length
                sum2 = sum(x*x for x in fits)
                std = abs(sum2 / length - mean**2)**0.5
                #mx = float(max(fits))

                print(" individuals: %s" % len(pop))
                print(" weaklings: %s" % len(weaklings))
                print(" elites: %s" % len(elites))
                print(" Mutated Elites: %s" % mutatedElites)
                print(" Audit data: %s lines" % len(auditData))
                #print(" Min %s" % min(fits))
                print(" Max %s" % max(fits))
                #print(" mxp %.3f %%" % mxp)
                print(" Avg %s" % mean)
                print(" Std %s \n" % std)
                if show_elites == True and elitesNo >= 6:
                    bestElites = tools.selBest(elites,20)
                    for idx, i in enumerate(bestElites):
                        print "%3d" % idx, "fv: %.14f" % i.fitness.values, i
                elif show_elites == True:
                    for idx, i in enumerate(elites):
                        print "%3d" % idx, "fv: %.14f" % i.fitness.values, i            
                print("------End Generation %s" % k)
                print "\n"
            #print fitnesses

        except KeyboardInterrupt:
            print "You hit Crt-C to prematurely exit the loop"
            break

    
#- End IX -------------------------------------------------------------------------

# for i in pop: #prints initial population
# print pop.index(i)+1, "fv=%s" % i.fitness.values, i

        #if max(fits) >= 0.8063:
        # break
    
# print("-- End of (as NGEN set) evolution --")
    #for ind in pop:
    #    del ind.fitness.values

    global wildcardPenalty
    #wildcardPenalty = False
    wildcardPenalty = True

    fitnesses = list(map(toolbox.evaluate, pop)) #re-evaluate fitness without wildcard penalty
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit  

    print "Best individuals are: " #% (best_ind, best_ind.fitness.values))
    bestInds = tools.selBest(pop, Result_numbers)

    for i, j in enumerate(bestInds):
        print "%3d" % i, "fv: %.14f" % j.fitness.values, j

    print "\n\n"
    #Remove duplicate individuals from the results
    #bestInds.sort()
    bestInds = tools.selBest(bestInds, len(bestInds))
    bestInds = list(bestInds for bestInds,_ in itertools.groupby(bestInds))
    print "Best individuals (duplications removed) are: "
    for i, j in enumerate(bestInds):
        print "%3d" % i, "fv: %.14f" % j.fitness.values, j

    #Show Best individuals by attack types
    bestAttkTypes = toolbox.selectE(bestInds)
    print "\n\n"
    print "Best individuals by attack types are: "
    for i, j in enumerate(bestAttkTypes):
        if j.fitness.values[0] > 0.0:
            print "%9s" % j[14][0:16], "%3d" % i, "fv: %.14f" % j.fitness.values, j
            #print "%3d" % i, "fv: %.14f" % j.fitness.values, j

    #topknots = bestAttkTypes #comment if topknot filter is used.
    
# TOPKNOTS Filter -------------------------------------------------------------------
    #uniq_attack
    topknots = []
    
    for i in uniq_attack:
        space = []
        jail = []
        for j in bestAttkTypes:
            if j[-1] == i:
                space.append(j)
        #space.sort()
        global topgun
        topgun = tools.selBest(space, 1)
        topgun = topgun[0] #THE BEST ONE of that attack type

        #print "\n\nBEFORE SPACE: "
        #for idx, i in enumerate(space):
        #    print idx, i.fitness.values, i

        #print "topgun of %s: " % topgun[-1], topgun
        #for idx, ind in enumerate(space):            #it works now using jail[]
        #    print idx ,ind.fitness.values, ind
        #    print (topgun.fitness.values[0] - ind.fitness.values[0]) <= 0.001
        #    if (((topgun.fitness.values[0] - ind.fitness.values[0]) <= 0.001) and (idx > 0)):
        #        jail.append(ind)
        #        print 1

        #for ind in jail:
        #    space.remove(ind)


        #jail = []
        #for idx, ind in enumerate(space):
        #    if matchEliminate(topgun, ind) and ind != topgun:
        #        jail.append(ind)

        #for ind in jail:
        #    space.remove(ind)


        #print "AFTER SPACE: "
        #for idx, i in enumerate(space):
        #    print idx, i.fitness.values, i

        space = tools.selBest(space, bestTopKnots)
        for i in space:
            topknots.append(i)
        
    print "\n\n"
    print "topknots individuals are: "
    for i, j in enumerate(topknots):
        if j.fitness.values[0] > 0.7:
            print "%9s" % j[14][0:16], "%3d" % i, "fv: %.14f" % j.fitness.values, j

 
# END TopKnots -------------------------------------------------------------------------------

    print "We ran", round_gen, "rounds"

    #Write result to rulesDump.rcd file
    rules = []
    rulesDumpFile = open('rulesDump.rcd', 'w+')
    for item in topknots:
        line = ""
        if item.fitness.values[0] > 0.7:
            for i in item:
                line = line.__add__(str(i) + ' ')
            
            rules.append(line)

    for idx, item in enumerate(rules):
        item = str(idx+1) + " " + item
        rulesDumpFile.write("%s\n" % item)
    rulesDumpFile.close()
Esempio n. 38
0
def main(seed, exp_no, run_specs, test_set):
    # set the randomized seed for experimentation duplication
    random.seed(seed)
    # set experiment number
    experiment = exp_no
    # assign the test set for GP
    test_set = test_set

    # sets the specs for the run
    POP = run_specs['pop_size']
    GENS = run_specs['gens']
    HOF_SIZE = run_specs['hof_size']
    CROSS_PROB = run_specs['cross_prob']
    MUT_PROB = 1.0
    IND_MUT_PROB = run_specs['mut_rate']
    SELECTOR = run_specs['selector']
    ELITISM = run_specs['elitism']
    alg = run_specs['algorithm']
    REC_FITS = run_specs['rec_fits']
    SEEDED = run_specs['seeded_pop']

    # sets hof and fs on/off
    FS = run_specs['FS']
    HOF = run_specs['HOF']
    """____SET UP ANY LISTS FOR TRACKING DATA HERE___"""
    stats_rel_b = []
    stats_bgp_b = []
    stats_agp_b = []
    stats_rel_r = []
    stats_bgp_r = []
    stats_agp_r = []

    # set evolutionary operators
    toolbox.register("mutate",
                     helpers.gaussian,
                     mu=0,
                     sigma=1.0,
                     indpb=IND_MUT_PROB)
    toolbox.register("mate", tools.cxOnePoint)
    toolbox.register("evaluate", helpers.evaluate)
    if SELECTOR == 'SUS':
        toolbox.register("select", helpers.selStochasticUniversalSampling)
    elif SELECTOR == 'RANKED':
        toolbox.register("select", helpers.selRanked)

    # create gene templates
    toolbox.register("turn_range", Gene, min_turn_range, max_turn_range)
    toolbox.register("turn_angle", Gene, min_turn_angle, max_turn_angle)
    toolbox.register("desired_displ", Gene, min_displ, max_displ)
    toolbox.register("conv_range", Gene, min_conv_range, max_conv_range)
    toolbox.register("no_closer_range", Gene, min_closer_range,
                     max_closer_range)
    # create chromosome template
    toolbox.register(
        "tactic",
        tools.initCycle,
        creator.Tactic,
        (toolbox.turn_range, toolbox.turn_angle, toolbox.desired_displ,
         toolbox.conv_range, toolbox.no_closer_range),
        n=CYCLES)
    # create population template
    toolbox.register("population", tools.initRepeat, list, toolbox.tactic, POP)

    # create the population initializer
    toolbox.register("population", tools.initRepeat, list, toolbox.tactic, POP)
    """<------------------------------------- START ---------------------------------------->"""

    if SEEDED == 1:
        print " no seeded pop"
    else:
        # initialize populations
        blue_pop = toolbox.population()
        red_pop = toolbox.population()

    # initialize hall of fame
    blue_hof = hof.HallOfFame(HOF_SIZE)
    red_hof = hof.HallOfFame(HOF_SIZE)

    # get working folder for this set of runs
    folder_string = run_specs['folder']

    # Make new folder for experiemnt results
    save_path = os.getcwd()
    sub_folder_string = folder_string + str(exp_no) + '_Seed_' + str(
        seed) + '/'
    folder = os.path.join(save_path, sub_folder_string)
    if not os.path.isdir(folder):
        os.makedirs(folder)

    # create out file
    text_file = sub_folder_string + 'results.txt'
    sys.stdout = open(text_file, 'w')

    # reset random seed to clock time
    algseed = datetime.now()
    random.seed(algseed)
    algseed = random.randint(0, 10001)
    random.seed(algseed)
    print 'Algorithm seed: ', algseed

    # create csv writer and file for fitness and initial/final population output
    csv_fits = sub_folder_string + 'relative_fitness.csv'
    fits = open(csv_fits, 'ab')
    fit_writer = csv.writer(fits)

    # csv writer for outputting all fitnesses of blue population
    blue_pop_fits = sub_folder_string + 'blue_all_fits.csv'
    blue_fits_file = open(blue_pop_fits, 'ab')
    blue_fits_writer = csv.writer(blue_fits_file)

    # do the same for red ^^^
    red_pop_fits = sub_folder_string + 'red_all_fits.csv'
    red_fits_file = open(red_pop_fits, 'ab')
    red_fits_writer = csv.writer(red_fits_file)

    blue_gp = sub_folder_string + 'blue_gp.csv'
    blue_gp_file = open(blue_gp, 'ab')
    blue_gp_writer = csv.writer(blue_gp_file)
    blue_gp_writer.writerow(['best', 'avg'])

    blue_chromosomes = sub_folder_string + 'blue_best.txt'
    blue_best_file = open(blue_chromosomes, 'w')

    red_gp = sub_folder_string + 'red_gp.csv'
    red_gp_file = open(red_gp, 'ab')
    red_gp_writer = csv.writer(red_gp_file)
    red_gp_writer.writerow(['best', 'avg'])

    red_chromosomes = sub_folder_string + 'red_best.txt'
    red_best_file = open(red_chromosomes, 'w')

    print "<-----INITIAL BLUE----->"
    for b, ind in enumerate(blue_pop):
        print "Chromosome ", str(b), ': '
        print helpers.list_to_string(ind)

    print "<-----INITIAL RED----->"
    for r, ind in enumerate(red_pop):
        print "Chromosome ", str(r), ': '
        print helpers.list_to_string(ind)

    # create csv writer and file for GP data
    # csv_fits = sub_folder_string + 'Gen_performance.csv'
    # gp = open(csv_fits, 'ab')
    # gp_writer = csv.writer(gp)
    # set up headings for graph csv
    fit_writer.writerow(
        ('Generation', 'BestB', 'AvgB', 'WorstB', 'BestR', 'AvgR', 'WorstR'))
    # set up GP headings
    # gp_writer.writerow(('Generation', 'BestB_GP', 'AvgB_GP', 'BestR_GP', 'AvgR_GP'))

    print(
        '-------------------------------------Stats----------------------------------------'
    )
    print 'Seed: ', seed
    print 'Selection: ', SELECTOR
    print("Pop size: " + str(POP))
    print("Generations: " + str(GENS))
    print("Crossover Prob: " + str(CROSS_PROB))
    print("Mutation Prob: " + str(IND_MUT_PROB))
    print 'Elitism: ', ELITISM
    print 'Hall of Fame: ', HOF
    print 'Fitness Sharing: ', FS
    print 'Algorithm: ', alg

    i = 0
    while i <= GENS:
        i += 1
        print('--------------------------------' + 'Generation: ' + str(i) +
              '-----------------------------------')
        """ ----------------------------------- EVALUATE THE POPULATIONS --------------------------------------- """

        # Reset fitness scores to zero so they can be accumulated each generation
        for index, x in enumerate(blue_pop):
            x.fitness.values = (0.0, )

        for index, y in enumerate(red_pop):
            y.fitness.values = (0.0, )
        """<----------RELATIVE FITNESS EVALUATIONS--------->"""
        # Evaluate the populations and return with summed fitness scores of all engagements
        # THIS FITNESS SCORE NEEDS TO BE AVERAGED AFTER HOF EVALS
        blue_pop, red_pop = helpers.sum_pop_v_pop_evals(blue_pop, red_pop)
        """<----------HALL OF FAME EVALUATIONS--------->"""
        # check hof not empty, if not, loop through and evaluate
        # Only if HOF is turned on
        if HOF == 1:
            if len(red_hof) != 0:
                blue_pop = helpers.sum_pop_v_hof_evals(blue_pop, red_hof)

            if len(blue_hof) != 0:
                red_pop = helpers.sum_pop_v_hof_evals(red_pop, blue_hof)
        """<-------AVERAGE THE FITNESS SCORES------>"""
        # average the accumulated fitness scores by size of population and also output fitnesses to csv
        blue_fits_row = []
        blue_fits_row.append(i)
        for index, x in enumerate(blue_pop):
            if HOF == 1:
                x.fitness.values = (x.fitness.values[0] /
                                    (POP + len(red_hof)), )
            else:
                x.fitness.values = (x.fitness.values[0] / POP, )
            blue_fits_row.append(x.fitness.values[0])

        red_fits_row = []
        red_fits_row.append(i)
        for index, y in enumerate(red_pop):
            if HOF == 1:
                y.fitness.values = (y.fitness.values[0] /
                                    (POP + len(blue_hof)), )
            else:
                y.fitness.values = (y.fitness.values[0] / POP, )
            red_fits_row.append(y.fitness.values[0])

        if REC_FITS == 1:
            blue_fits_writer.writerow(blue_fits_row)
            red_fits_writer.writerow(red_fits_row)
        """------------GET BEST---------------------"""

        blue_best_file.write("<------------ GEN " + str(i) +
                             "--------------->\n")
        red_best_file.write("<------------ GEN " + str(i) +
                            "--------------->\n")
        best_blue = tools.selBest(blue_pop, 5)
        best_red = tools.selBest(red_pop, 5)

        for index, b in enumerate(best_blue):
            blue_best_file.write("Chromsome [" + str(index) + "]: " + "(" +
                                 str(b.fitness.values[0]) + ")" +
                                 helpers.list_to_string(b) + "\n")

        for index, r in enumerate(best_red):
            red_best_file.write("Chromsome [" + str(index) + "]: " + "(" +
                                str(r.fitness.values[0]) + ")" +
                                helpers.list_to_string(r) + "\n")
        """ <---------------- Gen Perf ---------------->"""
        # cloned so the GP can be calculated without wiping relative scores
        blue_copy = list(map(toolbox.clone, best_blue))
        red_copy = list(map(toolbox.clone, best_red))

        # Reset fitness scores to zero so they can be accumulated each generation
        for index, x in enumerate(blue_copy):
            x.fitness.values = (0.0, )

        for index, y in enumerate(red_copy):
            y.fitness.values = (0.0, )

        blue_copy = helpers.sum_pop_v_hof_evals(blue_copy, test_set)
        red_copy = helpers.sum_pop_v_hof_evals(red_copy, test_set)
        sum_gp_b = 0.0
        sum_gp_r = 0.0
        for index_x, x in enumerate(blue_copy):
            x.fitness.values = (x.fitness.values[0] / len(test_set), )
            sum_gp_b += x.fitness.values[0]
        for index_y, y in enumerate(red_copy):
            y.fitness.values = (y.fitness.values[0] / len(test_set), )
            sum_gp_r += y.fitness.values[0]

        avg_gp_b = sum_gp_b / len(blue_copy)
        avg_gp_r = sum_gp_r / len(red_copy)
        best_gp_b = tools.selBest(blue_copy, 1)[0].fitness.values[0]
        best_gp_r = tools.selBest(red_copy, 1)[0].fitness.values[0]
        blue_gp_writer.writerow([best_gp_b, avg_gp_b])
        red_gp_writer.writerow([best_gp_r, avg_gp_r])

        del blue_copy
        del red_copy
        """<----------OUTPUT INFO TO TEXT FILE---------->"""
        # BLUE
        best_blue_ind = tools.selBest(blue_pop, 1)[0]
        print 'Best Blue fitness score: ', best_blue_ind.fitness.values[0]
        print 'Best blue chromosome: ', helpers.list_to_string(best_blue_ind)

        worst_blue_ind = tools.selWorst(blue_pop, 1)[0]
        print "Worst Blue fitness: ", worst_blue_ind.fitness.values[0]
        print "Worst Blue chromosome: ", helpers.list_to_string(worst_blue_ind)

        # get the average fitness of the generation
        sum_fits = sum(ind.fitness.values[0] for ind in blue_pop)
        avg_blue_fitness = sum_fits / POP
        print "Generation average Blue fitness: ", avg_blue_fitness

        # RED
        # get best chromosome and output
        best_red_ind = tools.selBest(red_pop, 1)[0]
        print "Best Red fitness: ", best_red_ind.fitness.values[0]
        print 'Best Red chromosome: ', helpers.list_to_string(best_red_ind)

        # get worst and display
        worst_red_ind = tools.selWorst(red_pop, 1)[0]
        print "Worst Red fitness: ", worst_red_ind.fitness.values[0]
        print "Worst Red chromosome: ", helpers.list_to_string(worst_red_ind)

        # get the average fitness of the generation
        sum_fits = sum(ind.fitness.values[0] for ind in red_pop)
        avg_red_fitness = sum_fits / POP
        print "Generation average red fitness: ", avg_red_fitness

        # Write best avg & worst to fits_csv
        fit_writer.writerow(
            (i, best_blue_ind.fitness.values[0], avg_blue_fitness,
             worst_blue_ind.fitness.values[0], best_red_ind.fitness.values[0],
             avg_red_fitness, worst_red_ind.fitness.values[0]))

        # if i % 5 == 0 or i == 1:
        #     # Fill the dictionary using the dict(key=value[, ...]) constructor
        #     cp = dict(population=blue_pop, generation=i, rndstate=random.getstate())
        #
        #     with open(sub_folder_string + "zgeneration" + str(i) + ".pkl", "wb") as cp_file:
        #         cPickle.dump(cp, cp_file)

        stats_rel_b.append(best_blue_ind.fitness.values[0])
        stats_bgp_b.append(best_gp_b)
        stats_agp_b.append(avg_gp_b)
        stats_rel_r.append(best_red_ind.fitness.values[0])
        stats_bgp_r.append(best_gp_r)
        stats_agp_r.append(avg_gp_r)

        if i > 10:
            stats_rel_b.pop(0)
            stats_bgp_b.pop(0)
            stats_agp_b.pop(0)
            stats_rel_r.pop(0)
            stats_bgp_r.pop(0)
            stats_agp_r.pop(0)

        # if i > GENS-10:
        #     stats_rel_b.append(best_blue_ind.fitness.values[0])
        #     stats_bgp_b.append(best_gp_b)
        #     stats_agp_b.append(avg_gp_b)
        #     stats_rel_r.append(best_red_ind.fitness.values[0])
        #     stats_bgp_r.append(best_gp_r)
        #     stats_agp_r.append(avg_gp_r)

        if i == GENS:
            break
        """<------------HALL OF FAME UPDATE----------->"""
        blue_hof.update(tools.selBest(blue_pop, 1), i)
        red_hof.update(tools.selBest(red_pop, 1), i)
        """<--------------------BEGIN EVOLUTION------------------>"""
        # Select offspring for next generation
        if ELITISM == 1:
            blue_offspring = toolbox.select(blue_pop, POP - 1, POP)
            red_offspring = toolbox.select(red_pop, POP - 1, POP)
        else:
            blue_offspring = toolbox.select(blue_pop, POP, POP)
            red_offspring = toolbox.select(red_pop, POP, POP)

        # Clone the offspring so we can evolve
        blue_offspring = list(map(toolbox.clone, blue_offspring))
        red_offspring = list(map(toolbox.clone, red_offspring))
        random.shuffle(blue_offspring)
        random.shuffle(red_offspring)

        # CROSSOVER
        blue_cross_count = 0
        # Blue
        for child1, child2 in zip(blue_offspring[::2], blue_offspring[1::2]):
            if random.random() < CROSS_PROB:
                # MATE
                toolbox.mate(child1, child2)
                blue_cross_count += 1
        # Red
        red_cross_count = 0
        for child1, child2 in zip(red_offspring[::2], red_offspring[1::2]):
            if random.random() < CROSS_PROB:
                # MATE
                toolbox.mate(child1, child2)
                red_cross_count += 1
        print "blue crossover count: ", str(blue_cross_count)
        print "red crossover count: ", str(red_cross_count)

        # MUTATION
        for mutant in blue_offspring:
            if random.random() < MUT_PROB:
                # MUTATE
                for index, x in enumerate(mutant):
                    mutant[index].value = helpers.convert_range(
                        mutant[index].value, mutant[index].min,
                        mutant[index].max)

                toolbox.mutate(mutant)

                for index, x in enumerate(mutant):
                    mutant[index].value = helpers.change_back(
                        mutant[index].value, mutant[index].min,
                        mutant[index].max)
                    helpers.bounds_check(mutant[index])

        for mutant in red_offspring:
            if random.random() < MUT_PROB:
                # MUTATE
                for index, x in enumerate(mutant):
                    mutant[index].value = helpers.convert_range(
                        mutant[index].value, mutant[index].min,
                        mutant[index].max)

                toolbox.mutate(mutant)

                for index, x in enumerate(mutant):
                    mutant[index].value = helpers.change_back(
                        mutant[index].value, mutant[index].min,
                        mutant[index].max)
                    helpers.bounds_check(mutant[index])

        if ELITISM == 1:
            elite_blue = toolbox.clone(tools.selBest(blue_pop, 1))
            elite_red = toolbox.clone(tools.selBest(red_pop, 1))
            blue_offspring += elite_blue
            red_offspring += elite_red
        # REPLACE
        blue_pop[:] = blue_offspring
        red_pop[:] = red_offspring

        print(
            '-------------------------------------Hall Of Fame Regular----------------------------------------'
        )
        print "BLUE: "
        # for chromosomes in blue_hof:
        #     print 'Chromosome: ', helpers.list_to_string(chromosomes), 'Fitness: ', chromosomes.fitness.values
        if len(blue_hof) != 0:
            blue_hof.print_hof()

        print "RED: "
        # for chromosomes in red_hof:
        #     print 'Chromosome: ', helpers.list_to_string(chromosomes), 'Fitness: ', chromosomes.fitness.values
        if len(red_hof) != 0:
            red_hof.print_hof()

        print "Generation ", i, " complete."

    print(
        '-------------------------------------Hall Of Fame Regular----------------------------------------'
    )
    print "BLUE: "
    # for chromosomes in blue_hof:
    #     print 'Chromosome: ', helpers.list_to_string(chromosomes), 'Fitness: ', chromosomes.fitness.values
    if len(blue_hof) != 0:
        blue_hof.print_hof()

    print "RED: "
    # for chromosomes in red_hof:
    #     print 'Chromosome: ', helpers.list_to_string(chromosomes), 'Fitness: ', chromosomes.fitness.values
    if len(red_hof) != 0:
        red_hof.print_hof()

    print(
        '-------------------------------------Stats----------------------------------------'
    )
    print 'Seed: ', seed
    print 'Selection: ', SELECTOR
    print("Pop size: " + str(POP))
    print("Generations: " + str(GENS))
    print("Crossover Prob: " + str(CROSS_PROB))
    print("Mutation Prob: " + str(IND_MUT_PROB))
    print 'Elitism: ', ELITISM
    print 'Hall of Fame: ', HOF
    print 'Fitness Sharing: ', FS
    print 'Algorithm: ', alg

    fits.close()
    blue_fits_file.close()
    red_fits_file.close()
    red_best_file.close()
    blue_best_file.close()
    blue_gp_file.close()
    red_gp_file.close()

    stats = [
        seed,
        sum(stats_rel_b) / 10,
        sum(stats_bgp_b) / 10,
        sum(stats_agp_b) / 10,
        sum(stats_rel_r) / 10,
        sum(stats_bgp_r) / 10,
        sum(stats_agp_r) / 10
    ]

    # stats = {"blue_est_avg_gp": blue_est_avg_gp, "blue_est_best_gp": blue_est_best_gp,
    #          "red_est_avg_gp": red_est_avg_gp, "red_est_best_gp": red_est_best_gp}

    return stats
Esempio n. 39
0
def ga(treino_, teste_):
	treino = iniciarDados(treino_)
	teste = iniciarDados(teste_)

	MINIMO = 10**10
	MAXIMO = -1*MINIMO

	for lista in treino_:
		if(min(lista) < MINIMO):
			MINIMO = min(lista)
		if(max(lista) > MAXIMO):			
			MAXIMO = max(lista)

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

	toolbox = base.Toolbox()

	toolbox.register("atribuirValor", random.uniform, MINIMO, MAXIMO + abs(MAXIMO - MINIMO)/2)

	toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.atribuirValor, K*TAM)
	toolbox.register("population", tools.initRepeat, list, toolbox.individual)

	toolbox.register("evaluate", avaliarFitness, pontos = treino)
	toolbox.register("evaluateTeste", avaliarFitness, pontos = teste)
	toolbox.register("mate", tools.cxTwoPoint)
	toolbox.register("mutate", tools.mutGaussian, mu = 0, sigma = 1, indpb = 0.5)
	toolbox.register("select", tools.selTournament, tournsize = 3)

	pop = toolbox.population(n = 250)

	NGEN = 100
	CXPB = .75
	MUTPB = .1

	# Avaliando os individuos.
	# Para cada par (ind, fit), atribua o valor fitness para o respectivo individuo.

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

	# Evolucao:
	for g in range(NGEN):
		print("\n-- Generation %i --" % g)

		# Selecao da proxima geracao.
		offspring = toolbox.select(pop, len(pop))
		# Duplica os selecionados.	

		offspring = list(map(toolbox.clone, offspring))

		# Crossover na populacao, cada par e composto por um elemento impar e um par da lista.
		for child1, child2 in zip(offspring[::2], offspring[1::2]):
			# Levando em conta o sucesso definido para crossover...
			if random.random() < CXPB:
				toolbox.mate(child1, child2)
				del child1.fitness.values
				del child2.fitness.values

		# Chance de mutacao.
		for mutant in offspring:
			if random.random() < MUTPB:
				toolbox.mutate(mutant)
				del mutant.fitness.values

		# Verificando se os fitness sao validos, e corrigindo caso contrario.
		invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
		fitnesses = map(toolbox.evaluate, invalid_ind)
		for ind, fit in zip(invalid_ind, fitnesses):
			ind.fitness.values = fit

		pop[:] = tools.selBest(offspring + tools.selBest(pop, 2), len(pop))
		# pop[:] = offspring

		# Array com todos os fitness para estatisticas.
		fits = [ind.fitness.values[0] for ind in pop]
		
		length = len(pop)
		mean = sum(fits) / length
		sum2 = sum(x*x for x in fits)
		std = abs(sum2 / length - mean**2)**0.5
		
		print("  Min: ", round(min(fits), 2))   # Valor maximo.
		print("  Max: ", round(max(fits), 2))   # Valor minimo.
		print("  Avg: ", round(mean, 2))        # Valor medio.
		print("  Std: ", round(std, 2))         # Desvio padrao da geracao.
		# print(" Melhor Individuo:\n", tools.selBest(pop, 1))
		# print(" Pior Individuo:\n", tools.selWorst(pop, 1))

		if(CXPB > .60):
			CXPB -= .001

		if(MUTPB < .20):
			MUTPB += .001

	fitnesses = list(map(toolbox.evaluateTeste, pop))
	for ind, fit in zip(pop, fitnesses):
		ind.fitness.values = fit

	fits = [ind.fitness.values[0] for ind in pop]

	length = len(pop)
	mean = sum(fits) / length
	sum2 = sum(x*x for x in fits)
	std = abs(sum2 / length - mean**2)**0.5
	
	print("\n\nAplicando os centroides nos dados de teste: ")
	print("  Min: ", round(min(fits), 2))   # Valor maximo.
	print("  Max: ", round(max(fits), 2))   # Valor minimo.
	print("  Avg: ", round(mean, 2))        # Valor medio.
	print("  Std: ", round(std, 2))         # Desvio padrao da geracao.
	print(" Melhor Individuo:\n", tools.selBest(pop, 1))
	print(" Pior Individuo:\n", tools.selWorst(pop, 1))
Esempio n. 40
0
def main():
    # random.seed(64)
    CXPB, MUTPB, NGEN = 0.9, 0.1, 100
    ano_int = 2000
    ano_str = str(ano_int)
    
    var_coord = 0.5
    joint_log_likelihood, total_size, total_obs, menor_lat, menor_long, vector_latlong, expectations, N_ano, N = dados_observados_R(var_coord, ano_str)
 
    global mi
    mi = float(N_ano)/float(N)
    pop = toolbox.population(n=500)
    
    # fitnesses = list(map(toolbox.evaluate, pop))
    # for ind, fit in zip(pop, fitnesses):
    #     ind.fitness.values = fit
    
    while(ano_int <= 2010):
        global mi
        mi = float(N_ano)/float(N)
        # Evaluate the entire population
        fitnesses = list(map(toolbox.evaluate, pop))
        for ind, fit in zip(pop, fitnesses):
            ind.fitness.values = fit
        
        # Begin the evolutionck())
        for g in range(NGEN):
            print("-- Generation %i --" % g)
            # Select the next generation individuals
            offspring = toolbox.select(pop, len(pop))
            # Clone the selected individuals
            offspring = list(map(toolbox.clone, offspring))
            print("Start of evolution")
            # Apply crossover and mutation on the offspring
            for child1, child2 in zip(offspring[::2], offspring[1::2]):
                
                if random.random() < CXPB:
                    toolbox.mate(child1, child2)
                    del child1.fitness.values
                    del child2.fitness.values
            for mutant in offspring:
                if random.random() < MUTPB:
                    toolbox.mutate(mutant, indpb=0.05)
                    del mutant.fitness.values
        
            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = list(map(toolbox.evaluate, invalid_ind))
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit
            
            print("  Evaluated %i individuals" % len(invalid_ind))
            
            # The population is entirely replaced by the offspring, but the last pop best_ind

            best_ind = tools.selBest(pop, 1)[0]
            worst_ind = tools.selWorst(offspring, 1)[0]
            
            for i in range(len(offspring)):
                if (offspring[i] == worst_ind):
                    offspring[i] = best_ind
                    break

            pop[:] = offspring    
            # fim loop GERACAO

        ano_int = ano_int + 1
        
        ano_str = str(ano_int)

        joint_log_likelihood, total_size, total_obs, menor_lat, menor_long, vector_latlong, expectations, N_ano, N = dados_observados_R(var_coord, ano_str)
        global mi
        mi = float(N_ano)/float(N)
        
        pop = toolbox.population(n=500)
        fitnesses = list(map(toolbox.evaluate, pop))
        for ind, fit in zip(pop, fitnesses):
            ind.fitness.values = fit

        best_ind = tools.selBest(pop, 1)[0]
        for i in range(len(best_ind)):
            global quant_por_grupo
            quant_por_grupo[i] = poisson_press(best_ind[i], mi)
 

        while True:
            try:            
                f = open(sys.argv[1], "a")
                flock(f, LOCK_EX | LOCK_NB)
                f.write(str(ano_int - 1))
                f.write('\n')
                for i in range(len((pop, 1)[0])):            
                    f.write(str((pop, 1)[0][i].fitness.values))
                f.write('\n')
                global quant_por_grupo
                f.write(str(quant_por_grupo))
                f.write('\n')
                f.write(str(best_ind.fitness.values))
                f.write('\n')
                flock(f, LOCK_UN)
                f.write('\n')
            except IOError:
                time.sleep(5)
                continue
            break
Esempio n. 41
0
    def run_algorithm(self, seed):
        # Set up custom hall of fame to keep track of the top chromosomes and their generations
        hall_of_fame = hof.HallOfFame(self.HOF_SIZE)
        # set up standard hall of fame that comes with DEAP
        hall_of_fame_with_dupes = tools.HallOfFame(self.HOF_SIZE)
        # Get date and time
        date_time = datetime.datetime.now().strftime("%m-%d_%I%M%p")

        # print out to file
        file_name = date_time + '.txt'
        sys.stdout = open(file_name, 'w')

        print 'Seed:', seed

        i = 0
        while i < self.GENERATIONS and not self.isConverged:
            i += 1
            print('--------------------------------' + 'Generation: ' +
                  str(i) + '-----------------------------------')
            # evaluate each chromosome in the population and assign its fitness score
            for index, x in enumerate(self.population):
                # update the chromosome, write out to JSON tactical file
                chromosome_parameters.update_chromosome(
                    x[0].value, x[1].value, x[2].value, x[3].value, x[4].value)
                # use Ace0 to evaluate the chromosome
                x.fitness.values = helper.evaluate(self.repetitions)

            # Select the best chromosome from this generation and display it
            best_chromosome = tools.selBest(self.population, 1)[0]
            print "Best chromosome is: ", helper.list_to_string(
                best_chromosome), best_chromosome.fitness.values

            # Select worst chromosome and display
            worst_chromosome = tools.selWorst(self.population, 1)[0]
            print "Worst chromosome is: ", helper.list_to_string(
                worst_chromosome), worst_chromosome.fitness.values

            # Get the over all fitness values
            sum_fits = sum(ind.fitness.values[0] for ind in self.population)
            average_fitness = sum_fits / self.POP
            print 'Generation average fitness: ', average_fitness

            # save best and average fitness to plot lists
            self.plot_best_fitness.append(best_chromosome.fitness.values)
            self.plot_average_fitness.append(average_fitness)
            self.plot_worst_fitness.append(worst_chromosome.fitness.values)

            # Update the hall of fame to track the best chromosomes from each generation
            hall_of_fame.update(self.population, i)
            hall_of_fame_with_dupes.update(self.population)

            # hall_of_fame.print_hof()

            # this is where we evolve the population
            # Select the next generation individuals
            offspring = self.toolbox.select(self.population,
                                            len(self.population))
            # Clone the selected individuals so we can apply crossover
            offspring = list(map(self.toolbox.clone, offspring))

            # Apply crossover on the offspring
            for child1, child2 in zip(offspring[::2], offspring[::-2]):
                if random.random() < self.CROSSOVER_PROBABILITY:
                    # mate the two children
                    self.toolbox.mate(child1, child2)

            # Apply mutation on the offspring
            for mutant in offspring:
                if random.random() < self.MUTATION_PROBABILITY:
                    # print 'Mutated Chromosome before: ', helper.list_to_string(mutant)
                    for index, x in enumerate(mutant):
                        mutant[index].value = helper.convert_range(
                            mutant[index].value, mutant[index].min,
                            mutant[index].max)

                    self.toolbox.mutate(mutant)

                    for index, x in enumerate(mutant):
                        mutant[index].value = helper.change_back(
                            mutant[index].value, mutant[index].min,
                            mutant[index].max)
                        helper.bounds_check(mutant[index])

                    # print 'Mutated Chromosome after: ', helper.list_to_string(mutant)

            # The population is entirely replaced by the offspring
            self.population[:] = offspring

            if float(best_chromosome.fitness.values[0]
                     ) - average_fitness < 0.0001:
                self.converge_tracker += 1
                if self.converge_tracker >= self.converge_tracker_max:
                    print 'CONVERGED'
                    self.isConverged = True
            else:
                self.converge_tracker = 0

            # # Elitism
            self.population[0] = hall_of_fame_with_dupes[0]

        print(
            '-------------------------------------Hall Of Fame Regular----------------------------------------'
        )
        for chromosomes in hall_of_fame_with_dupes:
            print 'Chromosome: ', helper.list_to_string(
                chromosomes), 'Fitness: ', chromosomes.fitness

        print(
            '-------------------------------------Hall Of Fame with Gen----------------------------------------'
        )
        hall_of_fame.print_hof()

        print(
            '-------------------------------------Stats----------------------------------------'
        )
        print("Pop size: " + str(self.POP))
        print("Generations: " + str(self.GENERATIONS))
        print("Crossover Prob: " + str(self.CROSSOVER_PROBABILITY))
        print("Mutation Prob: " + str(self.MUTATION_PROBABILITY))

        # Select the best chromosome from this generation and display it
        best_chromosome = tools.selBest(self.population, 1)[0]
        print "Best chromosome is: ", helper.list_to_string(
            best_chromosome), best_chromosome.fitness.values

        # Select worst chromosome and display
        worst_chromosome = tools.selWorst(self.population, 1)[0]
        print "Worst chromosome is: ", helper.list_to_string(
            worst_chromosome), worst_chromosome.fitness.values

        # Get the over all fitness values
        sum_fits = sum(ind.fitness.values[0] for ind in self.population)
        average_fitness = sum_fits / self.POP
        print 'Generation average fitness: ', average_fitness

        title = 'Seed: ' + str(seed)

        visualization.draw_plot(title, self.plot_average_fitness,
                                self.plot_best_fitness,
                                self.plot_worst_fitness,
                                'average per generation', 'best fitness',
                                'worst fitness', 'generation', 'fitness', 1,
                                250, 150, date_time)

        del creator.fitness
        del creator.Tactic
Esempio n. 42
0
def alphaGA(fleet: Fleet,
            hp: AlphaGA_HyperParameters,
            save_to: str = None,
            init_pop=None,
            savefig=False,
            plot_best_generation=False):
    # OBJECTS
    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("Individual",
                   list,
                   fitness=creator.FitnessMin,
                   feasible=False,
                   acceptable=False)

    # TOOLBOX
    toolbox = base.Toolbox()
    toolbox.register("individual",
                     random_individual,
                     num_customers=len(fleet.network.customers),
                     num_cs=len(fleet.network.charging_stations),
                     m=len(fleet),
                     r=hp.r)
    toolbox.register("evaluate", fitness, fleet=fleet, hp=hp)
    toolbox.register("mate",
                     crossover,
                     fleet=fleet,
                     hp=hp,
                     block_probability=(.33, .33, .33),
                     index=None)
    toolbox.register("mutate",
                     mutate,
                     fleet=fleet,
                     hp=hp,
                     block_probability=(.33, .33, .33),
                     index=None)
    toolbox.register("select",
                     tools.selTournament,
                     tournsize=hp.tournament_size)
    toolbox.register("select_worst", tools.selWorst)
    toolbox.register("decode", decode, fleet=fleet, hp=hp)

    # BEGIN ALGORITHM
    t_init = time.time()

    # Population from first candidates
    random_inds_num = int(hp.num_individuals / 3)
    init_size = len(init_pop)
    mutate_num = hp.num_individuals - init_size - random_inds_num
    pop = [creator.Individual(i) for i in init_pop]

    # Random population
    pop += [
        toolbox.mutate(toolbox.clone(pop[randint(0, init_size - 1)]))
        for i in range(mutate_num)
    ]
    pop += [
        creator.Individual(toolbox.individual())
        for _ in range(random_inds_num)
    ]

    # Evaluate the initial population and get fitness of each individual
    for k, ind in enumerate(pop):
        fit, feasible, acceptable = toolbox.evaluate(ind)
        ind.fitness.values = (fit, )
        ind.feasible = feasible
        ind.acceptable = acceptable

    print(f'  Evaluated {len(pop)} individuals')
    bestOfAll = tools.selBest(pop, 1)[0]
    print(
        f"Best individual  : {bestOfAll}\n Fitness: {bestOfAll.fitness.wvalues[0]} Feasible: {bestOfAll.feasible}"
    )

    # These will save statistics
    cs_capacity = fleet.network.nodes[
        fleet.network.charging_stations[0]].capacity
    opt_data = GenerationsData([], [], [], [], [], [], fleet, hp, bestOfAll,
                               bestOfAll.feasible, bestOfAll.acceptable,
                               len(fleet), cs_capacity)

    print("################  Start of evolution  ################")
    # Begin the evolution
    for g in range(hp.max_generations):
        # A new generation
        print(f"-- Generation {g}/{hp.max_generations} --")
        opt_data.generations.append(g)

        # Update block probabilities
        if g < 50:
            block_probabilities = (.33, .33, .33)
        elif g < 100:
            block_probabilities = (.2, .6, .2)
        elif g < 150:
            block_probabilities = (.6, .2, .2)
        elif g < 200:
            block_probabilities = (.33, .33, .33)
        elif g < 250:
            block_probabilities = (.2, .6, .2)
        elif g < 300:
            block_probabilities = (.6, .2, .33)
        else:
            block_probabilities = (.33, .33, .33)

        # Select the best individuals, if given
        if hp.elite_individuals:
            best_individuals = list(
                map(toolbox.clone, tools.selBest(pop, hp.elite_individuals)))

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

        # Mutation
        for mutant in offspring:
            if random() < hp.MUTPB:
                toolbox.mutate(mutant, block_probability=block_probabilities)
                del mutant.fitness.values

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

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

        print(f'  Evaluated {len(invalid_ind)} individuals')

        # The population is entirely replaced by a sorted offspring
        pop[:] = offspring
        pop[:] = tools.selBest(pop, len(pop))

        # Insert best individuals from previous generation
        if hp.elite_individuals:
            pop[:] = best_individuals + pop[:-hp.elite_individuals]

        # Update best individual
        bestInd = tools.selBest(pop, 1)[0]
        if bestInd.fitness.wvalues[0] > bestOfAll.fitness.wvalues[0]:
            bestOfAll = bestInd

        # Real-time info
        print(
            f"Best individual  : {bestInd}\n Fitness: {bestInd.fitness.wvalues[0]} Feasible: {bestInd.feasible} Acceptable: {bestInd.acceptable}"
        )

        worstInd = tools.selWorst(pop, 1)[0]
        print(
            f"Worst individual : {worstInd}\n Fitness: {worstInd.fitness.wvalues[0]} Feasible: {worstInd.feasible}  Acceptable: {worstInd.acceptable}"
        )

        print(
            f"Curr. best-of-all: {bestOfAll}\n Fitness: {bestOfAll.fitness.wvalues[0]} Feasible: {bestOfAll.feasible}  Acceptable: {bestOfAll.acceptable}"
        )

        # Statistics
        fits = [sum(ind.fitness.wvalues) for ind in pop]
        mean = np.average(fits)
        std = np.std(fits)

        print(f"Max {max(fits)}")
        print(f"Min {min(fits)}")
        print(f"Avg {mean}")
        print(f"Std {std}")

        opt_data.best_fitness.append(-max(fits))
        opt_data.worst_fitness.append(-min(fits))
        opt_data.average_fitness.append(mean)
        opt_data.std_fitness.append(std)
        opt_data.best_individuals.append(bestInd)

        print()

        if plot_best_generation:
            toolbox.evaluate(bestOfAll)
            fleet.plot_operation_pyplot()
            plt.show()
            input('Press ENTER to evolve...')

    t_end = time.time()
    print("################  End of (successful) evolution  ################")

    algo_time = t_end - t_init
    print('Algorithm time:', algo_time)

    fit, feasible, acceptable = toolbox.evaluate(bestOfAll)
    routes = toolbox.decode(bestOfAll)

    opt_data.bestOfAll = bestOfAll
    opt_data.feasible = feasible
    opt_data.acceptable = acceptable
    opt_data.algo_time = algo_time
    opt_data.fleet = fleet

    if save_to:
        path = save_to + hp.algorithm_name + f'_fleetsize_{len(fleet)}/'
        try:
            os.mkdir(path)
        except FileExistsError:
            pass
        opt_data.save_opt_data(path, savefig=savefig)
    return routes, opt_data, toolbox
Esempio n. 43
0
def main():
    global client_socket, iter_max, size_word, best_f1, location_id, number_iteration, e_value, \
        best_individual_fitness, best_predict, first_opening, individual_size
    first_opening = True
    # CXPB  is the probability with which two individuals
    #       are crossed
    #
    # MUTPB is the probability for mutating an individual
    CXPB, MUTPB = 0.8, 0.1

    if USE_AS_SERVER:
        globals.stopReceived = False
        globals.stopRequested = False
        globals.shouldListen = True
        size_word, individual_size, iter_max, client_socket = client_data_channel(
        )
        thread = ProcessCommands(client_socket)
        thread.start()
    pathDict = "output.csv"
    # Create dictionary for the database and the location
    predictionTools.construct_dict(pathDict, d, location_id)
    setup_toolbox(size_word)
    number_iteration = 0

    print("Start of evolution")
    pop = toolbox.population(n=population_size)
    # Evaluate the entire population
    fitnesses = list(map(toolbox.evaluate, pop))
    for ind, fit in zip(pop, fitnesses):
        ind.fitness.values = fit

    # Begin the evolution
    while number_iteration < iter_max and not globals.stopReceived:
        start = time.time()
        # reset the best individual fitness for the current iteration
        best_individual_fitness = 0
        number_iteration = number_iteration + 1
        print("-- Generation %i --" % number_iteration)

        # 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]):

            # cross two individuals with probability CXPB
            if random.random() < CXPB:
                toolbox.mate(child1, child2)

                # fitness values of the children
                # must be recalculated later
                del child1.fitness.values
                del child2.fitness.values

        for mutant in offspring:
            # mutate an individual with probability MUTPB
            if random.random() < MUTPB:
                toolbox.mutate(mutant)
            del mutant.fitness.values

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

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

        # The population is entirely replaced by the offspring
        pop[:] = offspring

        # Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]
        ind1 = pop[0]
        for ind in pop:
            if ind1.fitness.values[0] < ind.fitness.values[0]:
                ind1 = ind

        print("  Min %s" % min(fits))
        print("  MAX %s" % max(fits))
        print("  predict %s" % (best_predict * 100))
        print("  test %s" % (best_f1 * 100))
        print("evalue %0.10f" % e_value)
        print("time %f" % (time.time() - start))
        if USE_AS_SERVER and not globals.stopReceived:
            send_data(ind1, number_iteration, best_predict * 100,
                      best_f1 * 100)
            if globals.stopRequested:
                break

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

    best_ind = tools.selBest(pop, 1)[0]
    worst_ind = tools.selWorst(pop, 1)[0]
    print("Best individual is %s, %s\n" % (best_ind, best_ind.fitness.values))
    print("worst individual is %s, %s" % (worst_ind, worst_ind.fitness.values))

    if USE_AS_SERVER:
        globals.shouldListen = False
        thread.join()