我正在将inspyred库用于遗传算法。我有以下配置:
def distance(route, distances):
totalDistance = 0
for idx, customer in enumerate(route):
if (idx < len(route) - 1):
distance = distances[customer][route[idx + 1]]
totalDistance += distance
return totalDistance
def evaluator(candidates, args):
data = args['data']
distances = data.distances
fitness = []
for candidate in candidates:
tot_distance = 0
for index, route in enumerate(candidate):
tot_distance += distance(route, distances)
fitness.append(tot_distance)
return fitness
def main(debugMode):
start = default_timer()
data = DataProblem(debugMode)
prng = Random()
prng.seed(time())
problem = inspyred.benchmarks.TSP(data.distances)
ea = inspyred.ec.EvolutionaryComputation(prng)
ea.selector = inspyred.ec.selectors.tournament_selection
ea.variator = [custom_partially_matched_crossover, custom_inversion_mutation]
ea.replacer = inspyred.ec.replacers.generational_replacement
ea.terminator = inspyred.ec.terminators.generation_termination
final_pop = ea.evolve(generator=generator,
bounder=problem.bounder,
maximize=False,
evaluator=inspyred.ec.evaluators.parallel_evaluation_mp,
mp_evaluator=evaluator,
pop_size=100,
data=data,
max_generations=100000,
tournament_size=5,
num_selected=100,
num_elites=1)
best = max(ea.population)
print('Best Solution: {0}: {1}'.format(str(best.candidate), best.fitness))
duration = default_timer() - start
print(duration / 60)
return ea
if __name__ == '__main__':
try:
fileLocation = sys.argv[1]
timeOfDay = sys.argv[2]
debugMode = False
except:
debugMode = True
timeOfDay = 'morning'
main(debugMode)
mp_evaluator启用多重处理并使用以下方法:
def parallel_evaluation_mp(candidates, args):
import time
import multiprocessing
nprocs = multiprocessing.cpu_count()
start = time.time()
try:
pool = multiprocessing.Pool(processes=nprocs)
results = [pool.apply_async(evaluator, ([c], pickled_args)) for c in candidates]
pool.close()
pool.join()
return [r.get()[0] for r in results]
except (OSError, RuntimeError) as e:
logger.error('failed parallel_evaluation_mp: {0}'.format(str(e)))
raise
else:
end = time.time()
logger.debug('completed parallel_evaluation_mp in {0} seconds'.format(end - start))
但是,当我查看活动监视器时,只能看到1个处理器的85%约有4个线程使用。在未启用多处理器的情况下,代码实际上在1个处理器上执行得更快。
但是对于四核计算机,为什么要使用4个线程而不是4个处理器?