Exemple #1
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'
	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]