def main(): # collectors queues: fetch waxman network and send best chromosome params #waxqueue = Queue(1) #bestqueue = Queue(maxsize=0) ## Manages parallel communication between node and collector #collector = CollectorCommunication(waxqueue, bestqueue) #collector.start() # np.float64 array (NxN matrix) @waxnet: Waxman Network Graph (blockin call) #collector.request = 'waxnet\n' waxnet = waxnet_generator() # applies several genetic operators on individuals env = Environment() # open parallel communication between node and its neighbours (migration) #env.open_borders() sleep(4) print 'pga: init population' # generates initial population with random but valid chromosomes population = env.init_population(waxnet) print 'pga: entrando no GA loop' # <GeneticAlgorithm> start the GA on this node for generation in range(info.NUMBER_OF_GENERATIONS): print 'pga: calculating fitness' # perform evaluation (fitness calculation) fitnesses = env.evaluate(population, waxnet) print 'pga: selecting' # perform selection mating_pool = env.select(population, fitnesses) print 'pga: crossing' # perform crossover children = env.cross(mating_pool) print 'pga: mutating' # perform mutation mystique = env.mutate(population, waxnet) print 'pga: receiving best from neighbour' # perform (im)migration by polling foreign = env.immigrate() print 'pga: sorting, organizing and rearranging' # TODO BUG sort population and replace worst individuals with better ones population, fitnesses = env.rearrange(population, fitnesses,\ children, mystique, foreign) #print 'pga: sending best to neighbour' ## perform (e)migration of best individual #env.emigrate(population[0]) #print 'pga: sending best to collector' ## send best chromosome and its fitness to collector #collector.request = 'chromo\n' #print population[0], fitnesses[0] #bestqueue.put((population[0], fitnesses[0])) # </GeneticAlgorithm> # perform evaluation (fitness calculation) fitnesses = env.evaluate(population, waxnet) # TODO: Close all communications (sockets) of this node #collector.running = False ## open parallel communication between node and its neighbours (migration) #env.close_borders() ## TODO: Close all parallel processing (threads) of this node #bestqueue.join() #collector.join() print 'best fit: ', fitnesses[0]
def main(): # collectors queues: fetch waxman network and send best chromosome params waxqueue = Queue(1) bestqueue = Queue(maxsize=0) # Manages parallel communication between node and collector collector = CollectorCommunication(waxqueue, bestqueue) collector.start() # np.float64 array (NxN matrix) @waxnet: Waxman Network Graph (blockin call) collector.request = 'waxnet\n' (waxsource, waxtarget, waxnet) = waxqueue.get() waxqueue.join() if waxsource == waxtarget: print 'pga: fatal error: loop to itself tomar no seu cu' sys.exit(0) # applies several genetic operators on individuals env = Environment() # open parallel communication between node and its neighbours (migration) env.open_borders() sleep(3) # generates initial population with random but valid chromosomes print 'pga: init pop generating %d individuals' % info.SIZE_NODE_POPULATION population = [] # [ [[chrom], fit], [[chrom], fit], ..., [[chrom], fit] ] while len(population) < info.SIZE_NODE_POPULATION: allels = range(info.NUMBER_OF_WAXNODES) chromosome = env.make_chromosome(waxnet, waxsource, waxtarget, allels) individual = [chromosome, 0.0] if chromosome and individual not in population: population.append(individual) # <GeneticAlgorithm> start the GA on this node print 'pga: entrando no GA loop' for generation in range(info.NUMBER_OF_GENERATIONS): # perform evaluation (fitness calculation) print 'pga: evaluating population' for ind in xrange(len(population)): fit = env.evaluate(population[ind][0], waxnet) population[ind][1] = fit # perform selection print 'pga: selecting parents' mating_pool = env.select(list(population), info.RATE_CROSSOVER) # perform crossover print 'pga: crossing parents' offspring = env.cross(mating_pool) if offspring: for child in offspring: population.pop() population.insert(0, [child, 0.0]) # perform mutation print 'pga: mutating' for i in xrange(int(math.ceil(info.RATE_MUTATION*len(population)))): normal_ind = random.choice(population) trans_ind = env.mutate(normal_ind[0], waxnet) if trans_ind != normal_ind: population.remove(normal_ind) population.insert(0, [trans_ind, 0.0]) # perform (im)migration by polling print 'pga: immigration receiving best from neighbour' foreign = env.immigrate() if foreign: population.append([foreign, 0.0]) # rearrange sort pop print 'pga: rearranging: descendent sort by fitness' population.sort(key=itemgetter(1), reverse=True) #population.sort(key=itemgetter(1)) # perform (e)migration of best individual print 'pga: emmigration: sending best chromosome (only) to neighbour' env.emigrate(population[0][0]) # send best chromosome and its fitness to collector print 'pga: sending best chrom and fit to collector' collector.request = 'chromo\n' bestqueue.put((population[0][0], population[0][1])) # </GeneticAlgorithm> # perform evaluation (fitness calculation) print 'pga: last evaluating population' for ind in xrange(len(population)): population[ind][1] = env.evaluate(population[ind][0], waxnet) # rearrange sort pop print 'pga: last rearranging' population.sort(key=itemgetter(1), reverse=True) print population[0] # TODO: Close all communications (sockets) of this node #collector.running = False # open parallel communication between node and its neighbours (migration) env.close_borders() # TODO: Close all parallel processing (threads) of this node bestqueue.join() collector.join() print 'best fit: ', population[0][1]
for i in range(steps): # выбираем cross_count пары и скрещиваем # получаем leng2 + 2*cross_count особей #for ind in env.individuals: # print(ind.gets()) #break for cross in range(cross_count): r1 = randint(0, len(env.individuals) - 1) + 1 r2 = randint(0, len(env.individuals) - 1) + 1 env.individuals.append(env.get(r1).clone()) env.individuals.append(env.get(r2).clone()) for crossover in range(cross_n): p1 = len(env.individuals) - 1 p2 = len(env.individuals) env.cross(p1, p2, 1) env.individuals[r1 - 1].fitness = env.check(r1) env.individuals[r2 - 1].fitness = env.check(r2) # проводим mutation_count мутаций for mutation in range(mutation_count): p = randint(0, len(env.individuals) - 1) + 1 if env.get(p).fitness > 10: env.get(p).mutation() env.individuals[p - 1].fitness = env.check(p) else: pass # проверяем жизнеспособность особей check_arr = [] for index, individual in enumerate(env.individuals):