Ejemplo n.º 1
0
def run_algorithm(sched, n=NUM_GENERATIONS, m=POP_SIZE):
    """
    Runs the genetic algorithm over n generations with a population of size m
    using the provided schedule constraint and returns the found solution.
    """

    # generate an initial population of individuals
    population = []
    for i in range(m):
        soln = gen_rand_solution(sched.assignments)
        population.append(Individual(soln, fitness(soln, sched)))

    # evolve over n generations!
    for i in range(n):
        new_gen = []

        for i in range(m):
            # select 2 parents via tournament selection and breed them to
            # create a child for the new generation
            parents = tournament(population)
            child_soln = crossover(parents[0].todos, parents[1].todos)
            child = Individual(child_soln, fitness(child_soln, sched))

            new_gen.append(child)

        population = new_gen

    # sort the final population by fitness and return the best one
    population.sort(key=lambda indiv: indiv.fitness, reverse=True)
    return population[0]
Ejemplo n.º 2
0
def chemotaxixAndSwim(population, CellSize, PopSize, Ns, StepSize, x_train,
                      y_train, x_validate, y_validate):
    dattack = 0.1
    wattack = 0.2
    hrepellant = 0.1
    wrepellant = 10
    cellHealth_all = []
    for m in range(PopSize):
        cellft = ft.fitness(population[m], x_train, y_train, x_validate,
                            y_validate)
        cellft = cellft[0]
        cellft = cellft + interaction(population[m], population, dattack,
                                      wattack, wrepellant, hrepellant, 20,
                                      1200)
        cellHealth = cellft
        cell2 = population[m].copy()
        for i in range(Ns):
            #DUVIDA CRIAR UMA CELULA VAZIA OU COPIAR DA CELULA MAE? SetepSize array ou escalar?
            random_step = np.random.choice([1, -1], CellSize)
            random_step = np.array(random_step) * StepSize
            cell2 = np.add(np.array(cell2), random_step).tolist()
            cell2ft = ft.fitness(cell2, x_train, y_train, x_validate,
                                 y_validate)
            cell2ft = cell2ft[0]
            cell2ft += interaction(cell2, population, dattack, wattack,
                                   wrepellant, hrepellant, 20, 1200)

            if cell2ft < cellft:
                i = Ns
                break
            else:
                population[m] = cell2
                cellHealth = cellHealth + cell2ft
        cellHealth_all.append(cellHealth)
    return population, cellHealth_all
Ejemplo n.º 3
0
def MOPSO(nIter, nPop, nAr, nChr, func, c1, c2, lb, rb, Vmax, Vmin, M):
    """多目标粒子群算法
    Params:
        nIter: 迭代次数 
        nPOp: 粒子群规模 
        nAr: archive集合的最大规模 
        nChr: 粒子大小 
        func: 优化的函数
        c1、c2: 速度更新参数 
        lb: 解下界
        rb:解上界 
        Vmax: 速度最大值 
        Vmin:速度最小值 
        M: 划分的栅格的个数为M*M个
    Return:
        paretoPops: 帕累托解集
        paretoPops:对应的适应度 
    """
    # 种群初始化
    pops, VPops = initPops(nPop, nChr, lb, rb, Vmax, Vmin)
    # 获取个体极值和种群极值
    fits = fitness(pops, func)
    pBest = pops
    pFits = fits
    gBest = pops
    # 初始化archive集, 选取pops的帕累托面即可
    archive, arFits = getNonDominationPops(pops, fits)
    wStart = 0.9
    wEnd = 0.4

    # 开始主循环
    iter = 1
    while iter <= nIter:
        print("【进度】【{0:20s}】【正在进行{1}代...】【共{2}代】".\
            format('▋'*int(iter/nIter*20), iter, nIter), end='\r')

        # 速度更新
        w = wStart - (wStart - wEnd) * (iter / nIter)**2
        VPops = w*VPops + c1*np.random.rand()*(pBest-pops) + \
            c2*np.random.rand()*(gBest-pops)
        VPops[VPops > Vmax] = Vmax
        VPops[VPops < Vmin] = Vmin
        # 坐标更新
        pops += VPops
        pops[pops < lb] = lb
        pops[pops > rb] = rb  # 防止过界
        fits = fitness(pops, func)

        # 更新个体极值
        pBest, pFits = updatePBest(pBest, pFits, pops, fits)
        # 更新archive集
        archive, arFits = updateArchive(pops, fits, archive, arFits)
        # 检查是否超出规模,如果是,那么剔除掉一些个体
        archive, arFits = checkArchive(archive, arFits, nAr, M)
        gBest = getGBest(pops, fits, archive, arFits, M)  # 重新获取全局最优解
        iter += 1
    print('\n')
    paretoPops, paretoFits = getNonDominationPops(archive, arFits)
    return paretoPops, paretoFits
Ejemplo n.º 4
0
def mutate_vectors_type5(population, F, mode):
    """
    This function will implement the type 5 mutation vector (refer the comments above)

    Method and formulation:
        first find the best vector from the population (choose according to best fitness value), then
        for each vector in target vector T, We have to select four unique and random vectors from the population in the
        target vector space. Then compute the mutated vector for T using the formula:
        V_(i,G) = X_(best,G) + F*(X_(r1,G) - X_(r2,G) + X_(r3,G) - X_(r4,G))

    :param population: (list) of population containing (list) of candidate solution vectors
    :param F: (float) Scaling factor, a real and constant factor between [0, 2] which controls the amplification of the
              differential variation
    :param mode: (string) to set whether working on minimization(pass: "******") or maximization(pass: "******") problem
    :return: (list) of mutated population containing (list) of mutated solution vectors
    """
    mutated_vectors = []

    # find the best(fittest) vector
    X_best = population[0]
    X_best_fitness = fitness(X_best)
    if mode == "max":
        for i in population:
            current_fitness = fitness(i)
            if current_fitness > X_best_fitness:
                X_best = i
                X_best_fitness = current_fitness
    elif mode == "min":
        for i in population:
            current_fitness = fitness(i)
            if current_fitness < X_best_fitness:
                X_best = i
                X_best_fitness = current_fitness

    for i in range(len(population)):
        # Select 3 unique random vectors from population, different from current vector
        r = unique_rn_generator(0, len(population), 4, i)
        X_r1 = population[r[0]]
        X_r2 = population[r[1]]
        X_r3 = population[r[2]]
        X_r4 = population[r[3]]
        V_i = add_lists(X_best, add_lists(scalar_mul_list(F, sub_lists(X_r1, X_r2)),\
                                                 scalar_mul_list(F, sub_lists(X_r3, X_r4))))

        # NOTE: If you need to check if V_i satisfies the domain upper and lowerbounds, do it here.

        mutated_vectors.append(V_i)

    return mutated_vectors
Ejemplo n.º 5
0
def es(population, x_train, y_train, x_validate, y_validate, x_test, y_test):
    mut = 0.3
    cross = 0.7
    #fitness_all = ft.calculate_pop_ft(population, x_train, y_train, x_test, y_test)

    best_fit = ft.find_best(population, x_train, y_train, x_validate,
                            y_validate)
    num_generations = 50
    decendants = []

    for i in range(num_generations):
        number_decendants = 0
        decendants = []
        while number_decendants < (len(population) + 5):
            #print(population)
            father = np.random.random_integers(0, len(population) - 1)
            mother = np.random.random_integers(0, len(population) - 1)
            mother2 = np.random.random_integers(0, len(population) - 1)

            father = population[father]
            mother = population[mother]
            mother2 = population[mother2]

            monft = ft.fitness(mother, x_train, y_train, x_validate,
                               y_validate)
            monft2 = ft.fitness(mother2, x_train, y_train, x_validate,
                                y_validate)

            if monft[0] < monft2[0]:
                mother = mother2

            crossed = crossover(father, mother, cross)
            mutated = mutation(crossed, mut)
            #print(crossed)
            decendants.append(mutated)

            number_decendants += 1
        ft_decendants, model_decendants = ft.calculate_pop_ft(
            decendants, x_train, y_train, x_validate, y_validate)

        new_population, ft_new_pop, model_new = ft.fetch_n_better(
            decendants, ft_decendants, model_decendants, len(population))
        ft_best = (new_population[0], (ft_new_pop[0], model_new[0]))
        population = new_population
        if ft_best[1][0] > best_fit[1][0]:
            best_fit = ft_best
    ee_accu = ft.fitness_best(best_fit[0], best_fit[1][1], x_test, y_test)
    return ee_accu
Ejemplo n.º 6
0
def randomSolver(coordinates):
    bestDistance = 999999.9

    readFile = open("randomBest.txt", "r")
    bestStr = readFile.readline()
    if bestStr:
        bestDistance = float(bestStr)

    bestAnswer = None
    fitnessEvalutations = 3000000
    while fitnessEvalutations:
        print('fitnessEvaluationsCnt:', fitnessEvalutations)
        fitnessEvalutations -= 1
        curList = random.sample(coordinates, len(coordinates))
        curDistance = fitness(curList)
        print(curDistance)
        if curDistance < bestDistance:
            bestDistance = curDistance
            bestAnswer = curList
    print("RANDOM SOLVER")
    print('bestDistance', bestDistance)
    writeFile = open("randomBest.txt", "w")
    writeFile.write(str(bestDistance))
    readFile.close()
    writeFile.close()
Ejemplo n.º 7
0
def generate_population(popsize):
    Population = pd.DataFrame(columns=[
        'Var_Ind_P_1', 'Bits_VI1', 'Propelente', 'Tipo_tobera', 'Cromosoma',
        'Aptitud'
    ])
    while len(Population) < popsize:
        P_1_Camara = np.random.randint(
            10, 150) + np.random.random()  # Ajuste presion inicial
        Propelente = [1, 2, 3, 4, 5]  #LOXH o LOXK , LOXM, etc.
        Tobera_tipo = [0, 1]  # conica o de campana

        spec = {
            'Var_Ind_P_1': P_1_Camara,
            'Bits_VI1': 1,
            'Propelente': 1,
            'Tipo_tobera': 1,
            'Cromosoma': 1,
            'Aptitud': 1
        }

        spec['Bits_VI1'] = hr.float_to_bin(spec['Var_Ind_P_1'])
        spec['Propelente'] = np.binary_repr(np.random.choice(Propelente),
                                            width=4)
        spec['Tipo_tobera'] = np.binary_repr(np.random.choice(Tobera_tipo),
                                             width=1)
        spec['Cromosoma'] = spec['Bits_VI1'] + spec['Propelente'] + spec[
            'Tipo_tobera']
        spec['Aptitud'] = ft.fitness(spec['Cromosoma'])
        spec = pd.Series(spec)
        Population = Population.append(spec, ignore_index=True)
    else:
        Population['Sel_prob'] = hr.prob_list(Population, 'Aptitud')
        return Population
Ejemplo n.º 8
0
 def __init__(self,policynames, policyranges):
     self.policynames  = policynames
     self.policyranges = policyranges
     self.iterCount    = 0
     self.f = fitness.fitness(policynames, policyranges) 
     self.stat_avg_z         = [[]]
     self.stat_avg_z_total   = []
Ejemplo n.º 9
0
def fitness_wrapper(oTrainingData, oTrainingLabels, oTestingData,
                    testingLabels, individual):
    # transposing the data to have the samples become columns and the features become rows
    trainingData = numpy.matrix.transpose(oTrainingData)
    trainingLabels = oTrainingLabels
    testingData = numpy.matrix.transpose(oTestingData)

    #design decision - make use range of length for future flexibility
    fData = None
    tData = None

    # this cycles through the given individual(binary encoding)
    # and only includes the features that are designated by the individual
    for gene_position in range(numpy.size(individual, 1)):
        if individual[0, gene_position] == 1:
            if fData is None:
                fData = trainingData[gene_position]
                tData = testingData[gene_position]
            else:
                fData = numpy.vstack((fData, trainingData[gene_position]))
                tData = numpy.vstack((tData, testingData[gene_position]))

    #call regression on new data to get the optimal classification values
    w, b = regression.regression(fData, trainingLabels)

    #call fitness function to evaluate optimal classification values
    fitness_result = fitness.fitness(tData, w, b, testingLabels)
    return fitness_result
Ejemplo n.º 10
0
def rank_selection(population, mode):
    """
    This function is an implementation of rank selection algorithm

    Rank selection first ranks the population and then every chromosome receives fitness from this ranking. Here,
    selection is based on this ranking rather than absolute differences in fitness.

    :param population: (list of float) containing all chromosomes
    :param mode: (string) to set whether working on minimization(pass: "******") or maximization(pass: "******") problem
    :return: (list of flaot) containing original chromosomes, but ranked in order according to their fitness
    """

    # calculate fitness of all population members and store it in a new list fitnesses
    fitnesses = [fitness(i) for i in population]

    # now we have fitness of each population member, rank them according to their fitness, i.e. sort the population list
    # according to the fitness values of population members, depending on whether it is minimization or maximization,
    # sort ascending or descending.

    # check whether to minimize or maximize
    if mode == "max":
        return [i for _, i in sorted(zip(fitnesses, population), reverse=True)]
    elif mode == "min":
        return [
            i for _, i in sorted(zip(fitnesses, population), reverse=False)
        ]
    else:
        raise ValueError(
            "Incorrect mode selected, please pass 'min' or 'max' as mode")
Ejemplo n.º 11
0
 def evaluate(self):
     """
     evaluate each individual from the generation pool
     append every individual fitness and their areas cleared
     """
     if MULTIPROCESSING:
         pool = Pool(PROCESSES)
         self.fitnesses.append(np.zeros(self.population))
         pool.map(self.parallel_evaluation, range(self.population))
         pool.close()
     else:
         ind_fitness = []
         ind_areas = []
         for ind in range(self.population):
             total_area, collision_number, sensor_values = self.step(
                 self.genome_list[ind], self.map[ind], self.nn[ind])
             ind_areas.append(total_area)
             ind_fitness.append(
                 fitness.fitness(total_area, collision_number,
                                 sensor_values))
             print("individual:", ind + 1, "/", POPULATION, ", generation:",
                   self.current_generation, "/", LIFESPAN - 1, ", fitness:",
                   np.round(ind_fitness[ind], 2),
                   ", n.collisions: ", collision_number, ", area:",
                   np.round(total_area, 3), ", sensors:",
                   np.round(sensor_values, 3))
         self.fitnesses.append(ind_fitness)
         self.area_cleaned.append(ind_areas)
Ejemplo n.º 12
0
def ls(coordinates):
    global fitnessEvaluations
    curSolution = randomSolution(coordinates)
    fit = fitness(curSolution)
    fitnessEvaluations -= 1
    curSolution = (fit, curSolution)
    timer = 1
    temperature = cooling(timer)

    while fitnessEvaluations >= 0:
        neighbors = []
        timer += 10000

        for i in range(200):
            newSolution = getNeighborSolution(curSolution)
            # flag is necessary for the first ascent approach
            flag = False
            if (fitnessEvaluations <= 0):
                return curSolution
            if shouldAccept(newSolution, curSolution,
                            temperature) > random.random():
                curSolution = newSolution
                flag = True
            temperature = cooling(timer)
            if flag:
                break
        print("Current solution:", curSolution[0])

    return curSolution
Ejemplo n.º 13
0
def es(population,x_train, y_train, x_test, y_test):




    #fitness_all = ft.calculate_pop_ft(population, x_train, y_train, x_test, y_test)
    num_generations = 3
    decendants = []
    for i in range(num_generations):
        number_decendants = 0

        while number_decendants < (len(population)+5):
            #print(population)
            father = np.random.random_integers(0,len(population)-1)
            mother = np.random.random_integers(0,len(population)-1)

            father = population[father]
            mother = population[mother]


            crossed = crossover(father,mother,1)
            mutated = mutation(crossed)
            #print(crossed)
            decendants.append(mutated)

            number_decendants += 1

        ft_decendants = ft.calculate_pop_ft(decendants, x_train, y_train, x_test, y_test)
        new_population = fetch_n_better(decendants,ft_decendants,len(population))
        population = new_population
    ee_accu = ft.fitness(population[0],x_train, y_train, x_test, y_test)
    return ee_accu
Ejemplo n.º 14
0
def getNeighborSolution(solution):
    global fitnessEvaluations
    pos1 = random.randint(1, len(solution[1]) - 1)
    pos2 = random.randint(1, len(solution[1]) - 1)
    newSolution = copy.deepcopy(solution[1])
    newSolution[pos1], newSolution[pos2] = newSolution[pos2], newSolution[pos1]
    fitnessEvaluations -= 1
    return (fitness(newSolution), newSolution)
Ejemplo n.º 15
0
def evalOneMax(individual, items):
    costs = []
    print("Inside")
    #print(len(individual))
    for i in range(len(individual)):
        costs.append( tuple([fitness.fitness(individual[i],items[0],items[1],items[2],\
        items[3],items[4],items[5],0,0,0,0)]) )
    return (costs)
Ejemplo n.º 16
0
 def mutate(self):
     i = randint(0, 7)
     before = self.state[i]
     r = randint(1, 8)
     while r == before:
         r = randint(1, 8)
     self.state[i] = r
     self.fitness = fitness(self.state)
Ejemplo n.º 17
0
    def geneticVRP(self):
        cus_num = len(self.dis_cus_cus)
        zonglines = []  #用于存储历代所有种群
        lines = [[0 for i in range(cus_num)] for j in range(self.popSize)]
        R = lines[0]  # 一个随机个体
        R2 = R
        R_len = [0 for i in range(self.popSize)]  # 用来存储每条路径长度

        t = 1
        while t <= self.NGen:
            print "第 %d 次迭代:" % (t)

            #生成随机初始路径
            for i in range(self.popSize):
                line = random.sample(range(1, cus_num + 1), cus_num)
                lines[i] = line

            #交叉
            farm = lines
            farm2 = farm
            for i in range(0, self.popSize, 2):
                if random.random() < self.cxPb and i < self.popSize:
                    route1 = farm[i]
                    route2 = farm[i + 1]
                    route1, route2 = intercross.intercross(route1, route2)
                    farm[i] = route1
                    farm[i + 1] = route2

            #变异
            for i in range(0, self.popSize):
                if random.random() <= self.mutPb:
                    farm[i] = mutate.mutate(farm[i])

            #群体的选择和更新
            FARM = [[0 for i in range(cus_num)] for j in range(self.popSize)]
            fitness_value, Cost, R = fitness.fitness(
                self.dis_cus_cus, self.dis_sta_cus, self.min_dis_depot,
                self.min_dis_station, self.time_win, self.tolerate_time_win,
                self.demand, farm)
            rank = [
                index
                for index, value in sorted(list(enumerate(fitness_value)),
                                           key=lambda x: x[1])
            ]
            for i in range(0, self.popSize):
                FARM[i] = farm[rank[i]]
            fitness_value_s = sorted(fitness_value)
            zonglines.append(fitness_value_s[0])
            R = FARM[0]  #更新最短路径

            t += 1

            print "最佳路径为:"
            for i in range(0, len(FARM[0])):
                print "%d," % (FARM[0][i]),

        return zonglines, R
Ejemplo n.º 18
0
Archivo: cuda.py Proyecto: MtrsN/CUDABC
def ABC_initialization(hive, fitness, global_fitness):

    i = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x

    if (i < bees):
        fitness[i] = fit.fitness(hive[i])

    cuda.syncthreads()

    cuda.atomic.min(global_fitness, 0, fitness[i])
Ejemplo n.º 19
0
def MODE(nIter, nChr, nPop, F, Cr, func, lb, rb):
    """多目标差分进化算法主程序 
    Params:
        nIter: 迭代次数
        nPop: 种群规模 
        F: 缩放因子 
        Cr: 交叉概率 
        func:优化函数 
        lb: 自变量下界 
        rb:自变量上界 
    Return:
        paretoPops: 帕累托解集 
        paretoFits: 对应的适应度 
    """
    # 生成初始种群
    parPops = initPop(nChr, nPop, lb, rb)
    parFits = fitness(parPops, func)

    # 开始迭代
    iter = 1
    while iter <= nIter:
        # 进度条
        print("【进度】【{0:20s}】【正在进行{1}代...】【共{2}代】".\
            format('▋'*int(iter/nIter*20), iter, nIter), end='\r')

        mutantPops = mutate(parPops, F, lb, rb)  # 产生变异向量
        trialPops = crossover(parPops, mutantPops, Cr)  # 产生实验向量
        trialFits = fitness(trialPops, func)  # 重新计算适应度

        pops = np.concatenate((parPops, trialPops), axis=0)  # 合并成新的种群
        fits = np.concatenate((parFits, trialFits), axis=0)
        ranks = nonDominationSort(pops, fits)  # 非支配排序
        distances = crowdingDistanceSort(pops, fits, ranks)  # 计算拥挤度

        parPops, parFits = select1(nPop, pops, fits, ranks, distances)

        iter += 1
    print("\n")
    # 获取等级为0,即实际求解得到的帕累托前沿
    paretoPops = pops[ranks == 0]
    paretoFits = fits[ranks == 0]

    return paretoPops, paretoFits
Ejemplo n.º 20
0
def evaluate(cand_in, gen, maxgen):
    fits = np.random.randint(0, 5)
    return fitness(fits,
                   cand_in,
                   objective_function,
                   gen,
                   reverse=False,
                   c_sigma_truncation=1,
                   PLS_alpha=2,
                   maxgen=maxgen)
Ejemplo n.º 21
0
 def calcula_fitness(self, Populacao_ga, vetor_ref):
     for i in Populacao_ga:
         individuo = Populacao_ga[i]
         if not 'fitness' in individuo.keys():
             modelo_valor = modelo_teste.modelo(self.espaco_simulacao,
                                                individuo)
             fitness_valor = fitness.fitness(modelo_valor, vetor_ref,
                                             self.vetor_peso)
             Populacao_ga[i]['fitness'] = fitness_valor
     return Populacao_ga
Ejemplo n.º 22
0
    def __init__(self, initial=[]):
        if initial:
            if isinstance(initial, list):
                self.initial = initial
            elif isinstance(initial, str):
                self.initial = [int(i) for i in initial.split('-')]
        else:
            self.initial = self.random_initial_state()

        self.state = deepcopy(self.initial)
        self.fitness = fitness(self.state)
Ejemplo n.º 23
0
def start_evolution():
    conta = 0
    generation = 0
    sig = 0
    draw = Draw()
    error = Fitness.fitness(draw,target_matrix)[0]
    while(1):
        conta = conta + 1
        new_draw = copy.deepcopy(draw).mutate()
        if (new_draw.is_dirty()):
            generation = generation + 1
            print conta,generation,sig
            new_error,new_img = Fitness.fitness(new_draw,target_matrix)
            print new_error,"=erro novo /",error,"=erro antigo"
            if (new_error <= error):
                sig = sig  + 1
                draw = new_draw
                draw.dirty = False
                Tools.save(new_img,new_error,generation)
                error = new_error
Ejemplo n.º 24
0
 def evaluate(self, cells_covered, num_collisions, sensor_values):
     """
     function to get the fitness value for the individual with this genome
     :param cells_covered: maximise total area covered
     :param num_collisions: minimise number of collisions
     :param sensor_values: Average value of the robot's sensors
     :return: fitness value
     """
     self.fitness = fitness.fitness(cells_covered, num_collisions,
                                    sensor_values)
     return self.fitness
Ejemplo n.º 25
0
def ga(populationSize, generations, coordinates):
    population = []
    global fitnessEvaluations

    # initialize population
    for i in range(populationSize):
        randSol = randomSolution(coordinates)
        fit = fitness(randSol)
        fitnessEvaluations -= 1
        population.append((fit, randSol))

    best = (99999999999, [])
    mutationRate = 0.5

    for g in range(generations):
        children = []
        while len(children) < populationSize:
            tournamentPoolSize = 5
            parent1 = selection(population, tournamentPoolSize)
            parent2 = selection(population, tournamentPoolSize)

            offspring1 = crossover(parent1, parent2)
            offspring1 = mutate(offspring1, mutationRate)
            offspring1 = (fitness(offspring1[1]), offspring1[1])
            children.append(offspring1)
            fitnessEvaluations -= 1

            offspring2 = crossover(parent2, parent1)
            offspring2 = mutate(offspring2, mutationRate)
            offspring2 = (fitness(offspring2[1]), offspring2[1])
            children.append(offspring2)
            fitnessEvaluations -= 1

        population = replacement(population, children, 3)
        for solution in population:
            if solution[0] < best[0]:
                best = solution
        if fitnessEvaluations <= 0:
            return best
        print("Best in generation", g, ':', best[0])
    return best
Ejemplo n.º 26
0
    def new_egg(self, best, change=True):  # get cuckoo
        new_allel = []
        step_size = np.multiply(best - self.__allel, levy_flight())
        new_allel = (self.__allel + step_size).astype(int)
        new_allel = np.remainder(new_allel, cf.get_maxallel())

        new_fitness = fitness(new_allel)
        if change:
            self.__allel = new_allel
            self.__fitness = new_fitness
        else:
            return new_allel, new_fitness
Ejemplo n.º 27
0
def initialize(total):
    n = n * 1
    queens = [0] * n
    queenpop.clear()
    while total > 0:
        for i in range(0, n):
            queens[i] = random.randint(0, n - 1)
            queenpop.add(tuple(queens))
        total = total - 1
    for config in queenpop:
        # print(config)
        queenspace.append((config, fitness(config, start_time)))
 def parallel_evaluation(self, ind):
     total_area, collision_number, sensor_values = self.step(
         self.genome_list[ind], self.map[ind], self.nn[ind])
     self.area_cleaned[current_generation][ind] = total_area
     self.fitnesses[current_generation][ind] = (fitness.fitness(
         total_area, collision_number, sensor_values))
     print("individual:", ind + 1, "/", POPULATION, ", generation:",
           self.printed_generation, "/", LIFESPAN - 1, ", fitness:",
           np.round(self.fitnesses[-1][ind],
                    2), ", n.collisions: ", collision_number, ", area:",
           np.round(total_area,
                    3), ", sensors:", np.round(sensor_values, 3))
def LocalSearch(x, candidate, args):
    x = list(x)
    lenx= len(x)
    gmat = args['gmat']    
    best_fit_local = fitness(x, gmat)
    
    #print ("np.shape(x)",np.shape(x), "x[0]", x[0],"x[1]",x[1], "x[2]",x[2])
    
    for i in range(lenx):        
        local_neighbors = get_neighbors(x[i], gmat)
        for itm in local_neighbors:
            xx =  x[:i]+ [itm] + x[i+1:]
            if len(set(xx)) < lenx:
                continue
            
            new_fit = fitness(xx, gmat)
            if new_fit > best_fit_local:
                best_fit_local = new_fit
                x = xx
                
    return x, best_fit_local    
Ejemplo n.º 30
0
def fitness_wrapper(genome: Genome, cache: Dict[str, int]) -> int:
    genome_key = get_genome_cache_key(genome)
    print("Paramaters Generated:", args)
    branchLen = genome[:6][0] * 32 + genome[:6][1] * 16 + genome[:6][
        2] * 8 + genome[:6][3] * 4 + genome[:6][4] * 2 + genome[:6][5] * 1
    armLen = genome[6:12][0] * 32 + genome[6:12][1] * 16 + genome[6:12][
        2] * 8 + genome[6:12][3] * 4 + genome[6:12][4] * 2 + genome[6:12][5] * 1
    angle = genome[12:][0] * 32 + genome[12:][1] * 16 + genome[12:][
        2] * 8 + genome[12:][3] * 4 + genome[12:][4] * 2 + genome[12:][5] * 1
    args = [branchLen, armLen, angle]
    print("Paramaters Generated:", args)
    value = fitness(args)
    cache[genome_key] = value
    return value
Ejemplo n.º 31
0
def __wrf__hydro__(i):
	path = os.path.join('RUN', 'P_%03d' %i)
	
	hrlds = Popen(['srun', '-u', '--mpi=pmi2', '-n', '7', '-p', 'mh1', '--qos', 'mh1', './wrf_hydro.exe'], 
	cwd = path, 
	close_fds=True,
	stdout=PIPE,
	preexec_fn=os.setsid,
	shell=False,
	stdin=DEVNULL)
	
	out, err = hrlds.communicate()
	kge = fitness(path)
	np.savetxt(path + '/kge.csv',[kge])
Ejemplo n.º 32
0
    def write_file_and_test_fitness(self):
        #this writes a file, and calls JKK's fitness code and points it to the file
        #and retrieves the lnL (aka fitness) and then cleans up the file
        #also uses a cache ("memo") to check if the fitness has already been calculated for a particular order
        #this saves on the expensive compute
        fnm = sha.sha(str(self.chrom_list)).hexdigest()
        self.tag = fnm
#        print 'running', self.chrom_list, self.tag
        if fnm in memo: self.Fitness = memo[fnm]
        else:  #havent tested this one yet
            b = open(fnm, 'w')
            for i in self.chrom_list:
                scaff = self.scaff_lookup[abs(i)]
                output = self.chrom_dict[scaff]
                if i < 0: output = list(reversed(output))
                for j in output: b.write(scaff+'\t'+j+'\n')
            b.close()
            myfitness = fitness(fnm)[-1]
            self.Fitness = myfitness
            os.remove(fnm)
            memo[fnm] = myfitness
Ejemplo n.º 33
0
def arrange(links, successors, col_count, row_count, verbose, has_expired,
            population_size, max_generations, plateau, crossover_rate, mutation_rate, sample_size,
            **kwargs):
    
    def make_individual():
        """ Construct a chromosome. Select a random node for the first gene. The next ones are chosen
            sequentially. When a gene has another gene to the west, the corresponding node is preferably
            selected among the successors of the latter. NB: Applying the same technic for the north and
            nortwest directions produces better individual, but worse final results. """
        pool = range(box_count)
        chromosome = [pool.pop(randrange(box_count))]
        (x, y) = (0, 0)
        for i in range(1, box_count):
            x += 1
            if x == col_count:
                x = 0
                y += 1
                candidates = set()
            else:
                candidates = successors[chromosome[i-1]].intersection(pool)
            chromosome.append(pool.pop(pool.index(choice(tuple(candidates))) if candidates else randrange(len(pool))))
        return Individual(evaluate(chromosome), chromosome)

    def crossover(chromosome_1, chromosome_2):
        """ Produce two children for a given pair of individuals. A random rectangular zone is first selected.
            The corresponding genes in the first individual are copied in the first child. The remaining places
            are filled with the genes of the second individual taken one by one. Symmetrical operations for the
            second child. """
        (x1, y1) = (randrange(col_count), randrange(row_count))
        (x2, y2) = (randrange(x1+1, col_count+1), randrange(y1+1, row_count+1))
        def mate(chromosome_1, chromosome_2):
            used = set(chromosome_1[x+y*col_count] for x in range(x1, x2) for y in range(y1, y2))
            not_used_next = (allele for allele in chromosome_2 if allele not in used).next
            return [chromosome_1[x+y*col_count] if x1 <= x < x2 and y1 <= y < y2 else not_used_next() for y in range(row_count) for x in range(col_count)]
        return (mate(chromosome_1, chromosome_2), mate(chromosome_2, chromosome_1))

    def next_population():
        """ Evolve the population. The best individual is kept. The others are selected by tournament.
            Some selected pairs produce two children. Each selected individual may mutate at certain
            places. Mutation of a gene simply consists in swapping it with another one. """
        result = [best]
        while len(result) < population_size:
            chromosomes = crossover(tournament(), tournament()) if random() < crossover_rate else [tournament()]
            for chromosome in chromosomes:
                for i in range(box_count):
                    if random() < mutation_rate:
                        j = randrange(box_count)
                        (chromosome[i], chromosome[j]) = (chromosome[j], chromosome[i])
                result.append(Individual(evaluate(chromosome), chromosome))
        return result[:population_size]

    def tournament():
        """ Return a COPY of the best chromosome selected among a random sample. """
        return min(sample(population, sample_size)).chromosome[:]
    
    evaluate = fitness(links, col_count, row_count)
    Individual = namedtuple("Individual", ["score", "chromosome"])
    box_count = col_count * row_count
    patience = plateau
    previous_best_score = None
    population = sorted([make_individual() for _ in range(population_size)])
    best = population[0]
    for generation in range(max_generations):
        if best.score == previous_best_score:
            patience -= 1
        else:
            if verbose:
                print "% 3d: %s" % (generation, best.score)
            previous_best_score = best.score
            patience = plateau
        if best.score == (0, 0) or patience == 0 or has_expired():
            break
        population = sorted(next_population())
        best = population[0]
        generation += 1
    return {
        "distances": best.score[1],
        "crossings": best.score[0],
        "layout": best.chromosome
    }
Ejemplo n.º 34
0
pokedex = load_pokemons()
species = str(raw_input("What pokemon would you like to Optimize? \n"))
pokemon = pokedex[species]
#number = int(raw_input("How many moves to optimize for? (minimum 2) \n"))
number = 4
spectrum = "p"


# Generate the initial population of individuals randomly - first Generation
population = [Individual(species, getRandomMoveset(pokemon, number, spectrum), 0)
              for x in range(POPULATION_SIZE)]

# Evaluate the fitness of each individual in that population
for specimen in population:
    specimen.fitness = fitness(pokemon, specimen.moveset)

population.sort(reverse=True)
print "Initial population's first specimen:"
print population[0]
print "\nEvolution beings... (this will take a while)\n"

# Evolution loop
for generation in range(MAX_GENERATIONS):
    # Select the best-fit individuals for reproduction - parents
    best_fit = population[:POPULATION_SIZE/2]

    # Breed new individuals through crossover and mutation
    new_population = [Individual(species, getRandomMoveset(pokemon, number, spectrum), 0)
                      for x in range(POPULATION_SIZE/2)]
Ejemplo n.º 35
0
indLength = 22
# size of initial population
indNumbers = 50
# max generations
gnmax = 200
probOfCrossover = 0.75
probOfMutaion = 0.05

population = np.round(np.random.rand(indNumbers,indLength))
scnew = np.zeros((indNumbers,indLength))
scnnew = np.zeros((indNumbers,indLength))
ymax = []
ymean = []
xmax = []

(f,p) = fitness.fitness(population)

gn = 1
while gn < gnmax+1:
	tempIndex = [index for index in range(indNumbers)]
	for i in tempIndex[0:indNumbers:2]:
		 # make selection,parents can be selected repeatedly
		 selectN = sel.selection(p)

         # crossover
		 scro = GC.crossover(population,selectN,probOfCrossover)
		 scnew[i,:] = scro[0,:]
		 scnew[i+1,:] = scro[1,:]

		 # mutation
		 scnnew[i,:] = GC.mutation(scnew[i,:],probOfMutaion)
Ejemplo n.º 36
0
from simplesubstitution import SimpleSubstitution as SimpleSub
import random,re
from fitness import fitness

fitness = fitness('bigrams.txt')

CTEXT='''

LMIZ XPQT, 
 
PWE KWCTL Q XIAA CX BPM WXXWZBCVQBG BW EWZS WV BPQA? Q PIL I KZIKS IB IVWBPMZ WN BPM VWBMA GWC NWCVL QV UWVBUIZBZM. QB TWWSA TQSM BPM WTLMAB QBMU QV BPM XIKSMB IVL Q BPWCOPB QB UQOPB JM I OWWL XTIKM BW ABIZB. QB UISMA AWUM AMVAM WN BPM JCKPMVEITL ZMNMZMVKM QV BPM TIBMZ VWBM GWC AMVB. 
 
QB LQLV'B MFXTIQV BPM XIZQA TQVS AW Q AMVB I BMIU QVBW BPIB VMQOPJWCZPWWL BW OIBPMZ QVBMT JCB BPMG LQLV'B KWUM CX EQBP DMZG UCKP. EM LQL OMB WVM ZMXWZB BPIB BPM PWCAM PIL JMMV WEVML JG I OMZUIV NIUQTG JMNWZM BPM EIZ, JCB BPIB QB PIL JMMV BISMV WDMZ JG IV AA WNNQKMZ QV VQVMBMMV NWZBG WVM. ACZMBM ZMKWZLA ACOOMAB BPIB BPM NIUQTG KIUM NZWU EMQUIZ QV ICOCAB VQVMBMMV BPQZBG AMDMV, EPQKP QA ACOOMABQDM OQDMV BPM BQUQVO IVL BPM OMWOZIXPG, AW Q PIDM AMVB BPM BMIU BW JCKPMVEITL BW AMM EPIB BPMG KIV NQVL. 
 
Q IU RCUXQVO BW KWVKTCAQWVA PMZM, JCB BPM XWZBZIQB AIZIP UMVBQWVA PIA BW JM BPM UWVI TQAI. Q PIDM JMMV BZGQVO BW OMB IKKMAA BW QB, JCB BPM NZMVKP ICBPWZQBQMA IZM AXWWSML. BPM BPMNB WN BPM XIQVBQVO JG XMZCOOQI JIKS QV VQVMBMMV MTMDMV PIA UILM BPMU DMZG AMVAQBQDM. BPM VIUM KPICLZWV EIA UMVBQWVML UWZM BPIV WVKM, IVL Q VMML AWUM BQUM BW TWWS QVBW BPM PQABWZG. 
 
QN GWC PIDM IVG QVNTCMVKM IB ITT IB BPM UCAMCU Q BPQVS EM VMML I XZWXMZ MFIUQVIBQWV WN BPM XIQVBQVO, IVL Q VMML BW SVWE EPIB PIXXMVML BW QB LCZQVO BPM EIZ. 
 
ITT BPM JMAB, 
 
PIZZG

'''
CTEXT = re.sub('[^A-Z]','',CTEXT.upper())

maxkey = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
maxscore = -99e9
parentscore,parentkey = maxscore,maxkey[:]

i = 0
Ejemplo n.º 37
0
def run(conf_file=''):
  ''' 
    run Particle Swarm Optimizer 
    @conf_file if a configuration-file name is specified, it is imported and used
     give the name of the file WITHOUT the .py - extension! see conf.py for more details
  '''
  if 'conf' in dir(): del conf  # eliminate configuration from earlier times
  # NOTE: Python seems not to care about this del - method. It's hard to reset values in a module
  #       just by reimporting it. If the conf-modules change, it's no problem though
  if not conf_file=='': exec("import %s as conf"%conf_file)
  else: import conf as conf
  if 'logging' in dir(): del logging # see comment above 
  import logging
  if 'fitness' in dir(): del fitness # see comment above
  import fitness

  moreTrials = True # there is always one - the default
  trials = conf.trialgen()
  while(moreTrials): # runs while there are more trial conditions
    global actRun
    logging.log[conf.actTrialName] = dict()
    logging.log[conf.actTrialName]['pBestLog'] = dict()
    logging.log[conf.actTrialName]['gBestLog'] = dict()
    logging.log[conf.actTrialName]['meanFitnessLog'] = dict()
    logging.log[conf.actTrialName]['georankLog'] = dict()
    logging.log[conf.actTrialName]['closenessLog'] = dict()
    logging.iterLog = {}
    logging.resetLogs(conf)

    if conf.logConsole:
      print '-----------------------------------------------------------------------------------------------'
      print 'Starting trial: ' + conf.actTrialName
      if not conf_file == '': print '  I used this config file: ' + str(conf_file)
      print '  dimensions: ' + str(conf.dimensions)
      print '  swarm size: ' + str(conf.numberOfParticles)
      print '  topology: ' + conf.topology
      print '  function: ' + conf.function
      print '  generations: ' + str(conf.maxIterations)
      if conf.averageOver > 1 : print '  averaged over ' + str(conf.averageOver) + ' runs'

    for r in range(conf.averageOver):
      iterations = 0 
      setup(conf,logging)
      if conf.logConsole: 
        print '----------------- run nr: ' + str(actRun).rjust(2) + ' in trial: ' + conf.actTrialName.ljust(20) + '------------------------------------'
        titlestr =  '                   '
        if conf.logMeanFitness: titlestr += 'mean fitness |'.rjust(15)
        if conf.logPBest: titlestr += 'mean pbest |'.rjust(15)
        if conf.logGBest: titlestr += 'gbest so far |'.rjust(15)
        if conf.logGeoRank: titlestr += 'avg. georank |'.rjust(15)
        if conf.logCloseness: titlestr += 'avg. closeness |'.rjust(10)
        print titlestr
        print '-----------------------------------------------------------------------------------------------'
      
      while iterations <= conf.maxIterations:
 
        sumFitness = 0
        sumPBests = 0
        # Make the "next locations" current and then 
        # test their fitness. 
        for p in particles:
          for d in range(conf.dimensions):
            p.current[d] = p.next[d]
          act_fitness = fitness.fitness(p,conf)
        
          # adjust personal best
          if conf.isEqualOrBetterThan(act_fitness,p.bestSoFar) or not p.bestSoFar:
            p.bestSoFar = act_fitness
            for d in range(conf.dimensions):
              p.best[d] = p.current[d]
        
          global gbest
          global bestParticle
          if bestParticle == None: bestParticle = particles[0]
          # reached new global best?
          if conf.isEqualOrBetterThan(act_fitness,gbest) or not gbest: 
            gbest = act_fitness
            bestParticle = p
        
          sumFitness += act_fitness
          sumPBests += p.bestSoFar
  
        # end of: for p in particles
  
          
        # recalculate next for each particle
        pid = 0
        for p in particles:
          n = getNeighborWithBestFitness(pid,conf)
          for d in range(conf.dimensions):
            # individual and soacial influence
            iFactor = conf.iWeight * randMM(conf.iMin,conf.iMax)
            sFactor = conf.sWeight * randMM(conf.sMin,conf.sMax)
            # this helps to compare against a form of random search
            if conf.random_search:
              pDelta[d] = randMM(conf.initMin,conf.initMax) - p.current[d]
              nDelta[d] = randMM(conf.initMin,conf.initMax) - p.current[d]
            else:
              pDelta[d] = p.best[d] - p.current[d] # the 'cognitive' orientation
              nDelta[d] = n.best[d] - p.current[d] # the 'social' orientation
            delta = (iFactor * pDelta[d]) + (sFactor * nDelta[d])
            
            # acceleration: gradually going from acc_max to acc_min ("cooling down")
            acc = conf.acc_max - ((conf.acc_max - conf.acc_min)/conf.maxIterations) * iterations 

            # calculating the next velocity from old velocity + individual and social influence
            new_vel = (p.velocity[d] * acc) + delta
            p.velocity[d] = constrict(new_vel,conf)
            # updating the next position 
            p.next[d] = p.current[d] + p.velocity[d]
          pid += 1
        # end of: for p in particles
       
        # log (if wanted) 
        if iterations in logging.iterLog:
          logging.logPoint(iterations, sumPBests / conf.numberOfParticles, gbest, sumFitness / conf.numberOfParticles,conf)
        
        if iterations in conf.logPopulationAt: logging.logPopulation(particles,iterations,actRun,conf)
        
        iterations += 1
      # end of: while iterations <= maxIterations 
      actRun += 1
      random.seed() # we should reseed the randomizer for each new run!  
    # end of one run, start new trial?

    try:
      conf.actTrialName = trials.next()
    except:
      moreTrials = False
  # end of trial - while

  logging.writeLog(conf)
Ejemplo n.º 38
0
Archivo: ttp.py Proyecto: noegodinho/EC
def run(probMutation, probCrossover, popSize, tour_selection, crossoverOperator, mutationOperators, selection_survivors_elite, fitnessFunction, generations, numCities, numItems, knapsackWeight, vMax, vMin, coefficient, dropRate, distanceMatrix, weightValueItems, availabilityItems):
	# item list in which position is item_i and the value is the city where it was picked
	itemsPop = initializationItems(numItems, numCities, popSize, availabilityItems)
	citiesPop = initializationCities(numCities, popSize)

	fitnessBest = []
	fitnessAverage = []

	for i in range(len(citiesPop)):
		pos = citiesPop[i][0].index(1)
		temp = citiesPop[i][0][pos]
		citiesPop[i][0][pos] = citiesPop[i][0][0]
		citiesPop[i][0][0] = temp

	# evaluate pop here
	fitnessList = [fitness(numCities, numItems, i[0], j[0], distanceMatrix, weightValueItems, availabilityItems, vMax, vMin, knapsackWeight, coefficient, dropRate) for i, j in zip(citiesPop, itemsPop)]
	itemsPop = [(itemsPop[i][0], fitnessList[i]) for i in range(len(fitnessList))]
	citiesPop = [(citiesPop[i][0], fitnessList[i]) for i in range(len(fitnessList))]

	for i in range(generations):
		matePoolItems = tour_selection(itemsPop)
		matePoolCities = tour_selection(citiesPop)

		progenitoresItems = []
		progenitoresCities = []
		for j in range(0, popSize - 1, 2):
			indivItem1 = matePoolItems[j]
			indivItem2 = matePoolItems[j + 1]
			crossover = choice(crossoverOperator)
			sonsItems = crossover(indivItem1, indivItem2, probCrossover)
			progenitoresItems.extend(sonsItems)

			indivCities1 = matePoolCities[j]
			indivCities2 = matePoolCities[j + 1]
			sonsCities = cycle_cross(indivCities1, indivCities2, probCrossover)
			progenitoresCities.extend(sonsCities)

		#print(len(progenitoresItems), len(progenitoresCities))
		descendentesItems = []
		descendentesCities = []
		for i in range(len(progenitoresItems)):
			newCromoItem = mutationOperators[1](progenitoresItems[i][0], availabilityItems)
			newCromoCity = mutationOperators[0](progenitoresCities[i][0], numCities)
			#print(newCromoItem, newCromoCity)
			value = fitness(numCities, numItems, newCromoCity, newCromoItem, distanceMatrix, weightValueItems, availabilityItems, vMax, vMin, knapsackWeight, coefficient, dropRate)

			descendentesItems.append((newCromoItem, value)) # ALTERAR 0 POR FITNESS FUNCTION			
			descendentesCities.append((newCromoCity, value)) # ALTERAR 0 POR FITNESS FUNCTION

		itemsPop = selection_survivors_elite(itemsPop, descendentesItems)
		citiesPop = selection_survivors_elite(citiesPop, descendentesCities)
		#print(len(citiesPop[0][0]))
		#popSize = len(itemsPop)

		# evaluate the new population
		fitnessList = [fitness(numCities, numItems, i[0], j[0], distanceMatrix, weightValueItems, availabilityItems, vMax, vMin, knapsackWeight, coefficient, dropRate) for i, j in zip(citiesPop, itemsPop)]		
		itemsPop = [(itemsPop[i][0], fitnessList[i]) for i in range(len(fitnessList))]
		citiesPop = [(citiesPop[i][0], fitnessList[i]) for i in range(len(fitnessList))]
		
		fitnessAverage.append(np.mean(fitnessList))
		fitnessBest.append(np.max(fitnessList))

	best_pops = [best_pop(itemsPop), best_pop(citiesPop)]
	
	return best_pops, fitnessBest, fitnessAverage
Ejemplo n.º 39
0
        # Convert the agent's path from something like 'agents/fitness/Reference.py' to an importable submodule
        # path like 'agents.fitness.Reference'
        agent_module_name = agent_repo[0].path.replace('/', '.')[:-3]

        # Do an import by string reference. The 'fromlist' argument is necessary because we are importing a submodule
        # from a package (otherwise only the empty 'fitness' module will be imported
        agent = __import__(agent_module_name, fromlist=['*'])

        try:
            explore_ranges = agent.explore_ranges
        except AttributeError:
            explore_ranges = ranges.ParameterRange

        params = selectParameters(agent_repo[0].name, explore_ranges)

        sample = fitness.fitness(agent_module_name, params, 1000, 20)

        print(agent.__name__)
        print(params)
        print("FITNESS: %d" % sample.avg_payoff)

        # Lastly, pack the record for this simulation run in a way that can neatly be stored in the database

        record = {'agent_name': agent_repo[0].name,
                  'agent_hash': agent_repo[0].hexsha,
                  'repo_head_hash': repo.hc.hexsha,
                  'simulate_hash': (l.hexsha for l in repo.hct.blobs if l.name == 'simulate.py').next(),
                  'timestamp': datetime.datetime.now(),
                  'fitness': sample.avg_payoff,
                  'avg_T_move': sample.avg_T_move,
                  'N_errors': sample.N_errors,
Ejemplo n.º 40
0
    collection = db.solution

    while True:

        # Do an import by string reference. The 'fromlist' argument is necessary because we are importing a submodule
        # from a package (otherwise only the empty 'fitness' module will be imported
        agent = __import__('agents.fitness.BlueGenes', fromlist=['*'])

        try:
            explore_ranges = agent.explore_ranges
        except AttributeError:
            explore_ranges = ranges.ParameterRange

        params = selectParameters('BlueGenes', explore_ranges)

        sample = fitness.fitness('agents.fitness.BlueGenes', params, 1000, 20)

        print(agent.__name__)
        print(params)
        print("FITNESS: %d" % sample.avg_payoff)
        print("ERRORS: %d/%d" % (sample.N_errors, sample.N_runs))

        # Lastly, pack the record for this simulation run in a way that can neatly be stored in the database

        record = {'agent_name': 'BlueGenes',
                  'timestamp': datetime.datetime.now(),
                  'fitness': sample.avg_payoff,
                  'avg_T_move': sample.avg_T_move,
                  'N_errors': sample.N_errors,
                  'N_runs': sample.N_runs
                 }
Ejemplo n.º 41
0
def create_individual(Problem):
    N, M, data = Problem.show()
    sequence = range(0, N)
    random.shuffle(sequence)
    makespan = fitness(sequence, Problem)
    return (sequence, makespan)