當前位置: 首頁>>代碼示例>>Python>>正文


Python Pool.putQuerry方法代碼示例

本文整理匯總了Python中pool.Pool.putQuerry方法的典型用法代碼示例。如果您正苦於以下問題:Python Pool.putQuerry方法的具體用法?Python Pool.putQuerry怎麽用?Python Pool.putQuerry使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pool.Pool的用法示例。


在下文中一共展示了Pool.putQuerry方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: myAsyncEA

# 需要導入模塊: from pool import Pool [as 別名]
# 或者: from pool.Pool import putQuerry [as 別名]
def myAsyncEA(population, start_gen, toolbox, cxpb, mutpb, ngen,
              stats, halloffame, logbook, verbose, id=None):

    """ Asynchronous EA """ 

    def eval_individual(ind):
        ind.fitness.values = toolbox.evaluate(ind)
        return ind

    def gen_new_offspring():
        valid_offspring = True
        offspring = None
        while valid_offspring:
            # select two individuals and clone them 
            offspring = map(toolbox.clone, toolbox.select(population, 2))
            # apply crossover and mutation, take the first offspring
            offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb)[0]
            valid_offspring = offspring.fitness.valid
            # if the offspring is valid it is already in the population
        return offspring 

    def log_stats():
        record = stats.compile(population)
        logbook.record(gen=start_gen + (num_evals // popsize),  **record)
        if verbose:
            print(logbook.stream)

    def save_checkpoint():
        cp = dict(population=population, generation=start_gen + (num_evals // popsize), halloffame=halloffame,
                  logbook=logbook, rndstate=random.getstate())
        if id is None:
            cp_name = "checkpoint_ea.pkl"
        else:
            cp_name = "checkpoint_ea_{}.pkl".format(id)
        pickle.dump(cp, open(cp_name, "wb"))
            
    
    popsize = len(population)
    MIN_POP_SIZE = popsize // 2 
    total_time = datetime.timedelta(seconds=0)

    pool = Pool(processors=PROCESSORS, evalfunc=eval_individual)    

    db = Database()
    
    if all([ind.fitness.valid for ind in population]):
        print("All individuals have valid fitness")
        # all inds are valid (loaded from checkpoint), generate some new to fill the pool 
        for _ in range(PROCESSORS):
            offspring = gen_new_offspring() 
            pool.putQuerry(offspring)
    else:
        # put all individuals to the pool 
        for ind in population:
            pool.putQuerry(ind)
        population = [] # we get them back later 

    num_evals = 0

    start_time = datetime.datetime.now()
    while num_evals < (ngen-start_gen)*popsize:

        # get finished individual and add him to population 
        ind = pool.getAnswer()
        assert ind.fitness.valid
        # add fitness and individual to the database
        db.insert(ind, ind.fitness.values[0])
        population.append(ind)
        num_evals += 1
        
        # update halloffame
        halloffame.update([ind])

        if len(population) < MIN_POP_SIZE:
            continue

        if num_evals % popsize == 0:
            # record statistics and save checkpoint
            log_stats()            
            save_checkpoint()
            
        # check time    
        eval_time = datetime.datetime.now() - start_time
        total_time = total_time + eval_time
        #print("Time ", total_time)
        if total_time > datetime.timedelta(hours=8*24):
            print("Time limit exceeded.")
            break
        start_time = datetime.datetime.now() 

        
        if len(population) > popsize:
            # delete the worst one
            population = list(sorted(population, key=attrgetter('fitness')))[1:]

        # generate new offspring and put him to the pool 
        offspring = gen_new_offspring() 
        pool.putQuerry(offspring)


#.........這裏部分代碼省略.........
開發者ID:JiriVanek,項目名稱:GAKeras,代碼行數:103,代碼來源:paralg.py


注:本文中的pool.Pool.putQuerry方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。