Guest User

DEAP multiprocessing parallelization

a guest
Jan 19th, 2016
360
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Jan 17 17:43:16 2016
  4.  
  5. @author: xeNon
  6. """
  7.  
  8. import sys
  9. import random
  10. import numpy as np
  11. import matplotlib.pyplot as plt
  12. import multiprocessing
  13.  
  14. from deap import base, creator, tools
  15. #from scoop import futures
  16. from timeit import default_timer as timer
  17.  
  18. # Create Fitness and Individual Classes
  19. creator.create('FitnessMin', base.Fitness, weights=(-1.0,)) # Weights must be a sequence
  20. creator.create('Individual', list, fitness=creator.FitnessMin)
  21.  
  22. # Create Individual Type
  23. IND_SIZE = 2
  24.  
  25. toolbox = base.Toolbox()
  26. def rand():
  27. return random.uniform(-3,3)
  28.  
  29. toolbox.register('attr_float', rand)
  30. toolbox.register('individual', tools.initRepeat, creator.Individual,
  31. toolbox.attr_float, n=IND_SIZE)
  32.  
  33. # Create Population Type
  34. toolbox.register('population', tools.initRepeat, list, toolbox.individual)
  35.  
  36. # Define evaluation Function
  37. def evaluate(individual):
  38. """Fitness evaluation. Must return a tuple even for single objective optimization.
  39. """
  40. x = (individual[0])
  41. y = (individual[1])
  42.  
  43. # Do some heavy stuff
  44. n = 1873895732
  45. import math
  46. for i in range(10000000):
  47. math.sqrt(n)
  48. i = i + 1
  49.  
  50. print 'Done!'
  51. sys.stdout.flush()
  52. return (rosenbrock(x,y),) # Must be an iterable
  53.  
  54. def rosenbrock(x,y):
  55. return (1-x)**2 + 100*(y-x**2)**2
  56.  
  57. # Define Operators
  58. toolbox.register('mate', tools.cxTwoPoint)
  59. toolbox.register('mutate', tools.mutGaussian, mu = 0, sigma = 1, indpb = 0.1)
  60. toolbox.register('select', tools.selTournament, tournsize=3)
  61. toolbox.register('evaluate', evaluate)
  62. #toolbox.register('map', futures.map) # SHELL: python -m scoop file.py
  63.  
  64. # Optimization Algorithm
  65. def main():
  66. """Defines the optimization process. Should be contained in the main function.
  67. """
  68.  
  69. # General Settings
  70. POP_SIZE = 20 # Population size
  71. NGEN = 1 # Number of generations
  72. CXPB = 0.2 # Crossover probability
  73. MUTPB = 0.5 # Mutation probablilty
  74.  
  75. # Initialize population
  76. pop = toolbox.population(n=POP_SIZE)
  77.  
  78. # Evaluate the entire initial population
  79. jobs = toolbox.map(toolbox.evaluate, pop)
  80. fitnesses = jobs.get()
  81. print('Done initial population evaluation.')
  82. # Assign the fitnesses
  83. for ind, fit in zip(pop,fitnesses):
  84. ind.fitness.values = fit
  85.  
  86. # Evolutionary Loop
  87. for g in range(NGEN):
  88. # Select the next generations individuals (len(pop) individuals)
  89. offspring = toolbox.select(pop, len(pop))
  90. # Clone the selected individuals
  91. offspring = map(toolbox.clone, offspring)
  92.  
  93. # Apply crossover on the offspring
  94. for child1, child2 in zip(offspring[::2], offspring[1::2]): # Chooses offspring with neighbouring indices to mutate
  95. if random.random() < CXPB:
  96. # Do crossover inplace
  97. toolbox.mate(child1, child2)
  98. # Request reevaluation of fitnesses
  99. del child1.fitness.values
  100. del child2.fitness.values
  101.  
  102. # Apply mutation on the offspring
  103. for mutant in offspring:
  104. if random.random() < MUTPB:
  105. # Do mutation inplace
  106. toolbox.mutate(mutant)
  107. # Request reevaluation of fitness
  108. del mutant.fitness.values
  109.  
  110. # Evaluate fitnesses of individuales with invalid fitnesses
  111. invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
  112. jobs = toolbox.map(toolbox.evaluate, invalid_ind)
  113. fitnesses = jobs.get()
  114. for ind, fit in zip(invalid_ind, fitnesses):
  115. ind.fitness.values = fit
  116.  
  117. # Replace the population by the offspring
  118. pop[:] = offspring
  119.  
  120. return pop
  121.  
  122. if __name__ == '__main__':
  123.  
  124. pool = multiprocessing.Pool(processes=4)
  125. toolbox.register('map', pool.map_async)
  126.  
  127. tic = timer()
  128. pop = main()
  129. pool.close()
  130. print timer()-tic
RAW Paste Data