Ejemplo n.º 1
0
def evolve(gen=1000, population=None, pop=50):
	"""runs fern genetic algorithm and returns final population"""
	if population is None:
		r = fernpy.region2()
		r[0], r[1] = fernpy.interval(-pi, pi), fernpy.interval(-50.0, 50.0)
		r.set_uniform( fernpy.interval(-pi, pi) )
		population = [fernpy.fern2(r, 3) for i in range(pop)]
		randomize=True
	else:
		pop = len(population)
		randomize=False
	
	for index, individual in enumerate(population):
		population[index].set_node_type_chance(node_type_chance)
		population[index].set_mutation_type_chance(mutation_type_chance_fork, 
							   mutation_type_chance_leaf)
	
	if randomize:
		for index, individual in enumerate(population):
			population[index].randomize(100)
	#else:
	#	for index, individual in enumerate(population):
	#		population[index].randomize(50)
	
	job_server = pp.Server(ppservers=())
	jobs = []
	subfuncs = ()
	packages = ("numpy", "fernpy")
	template = pp.Template(job_server, fitness, subfuncs, packages)
	
	max_fitness = [0]*gen
	median_fitness = [0]*gen
	min_fitness = [0]*gen
	
	for generation in range(gen):
		#generating new data for each generation helps encourage
		#a healthy genetic variation
		numbers, classes = generate_data(10000)
	
		#evaluate ferns
		pop_fitness = np.array([0.0]*pop)
		for index, individual in enumerate(population):
			if len(jobs) < pop: #only happens in first generation
				jobs.append( template.submit(individual, numbers, classes) )
			else:
			 	jobs[index] = template.submit(individual, numbers, classes)
		
		for index, result in enumerate(jobs):
			pop_fitness[index] = result()
		
		#record max and median fitness
		max_fitness_index = sp.argmax(pop_fitness)
		max_fitness[generation] = pop_fitness[max_fitness_index]
		median_fitness[generation] = median(pop_fitness)
		min_fitness[generation] = min(pop_fitness)
		
		#control ratio of max to median fitness and normalize
		#fitness_mean = np.mean(pop_fitness);
		#if max_fitness[generation] != fitness_mean: #median_fitness[generation]:
		#	a = log(fitness_ratio) / (log(max_fitness[generation]) - log(fitness_mean))
		#	pop_fitness = (pop_fitness - fitness_mean)**a + fitness_mean
		
		sys.stdout.write("\rgen %i" % generation)
		sys.stdout.flush()
		
		if generation == gen-1: 
			sys.stdout.write("\n")
			break #skip breeding on last step
		
		pop_fitness /= sum(pop_fitness)
		
		#select parents and breed new population
		new_population = []
		new_population.append( population[max_fitness_index] ) #elitism
		for i in range(1, pop):
			new_population.append(fernpy.fern2( population[select(pop_fitness)] ))
			if rand.random() < crossover_rate:
				new_population[-1].crossover( population[select(pop_fitness)] ) 
			if rand.random() < mutation_rate:
				new_population[-1].mutate()
		
		population = new_population
	
	h = 1.0*np.array(range(-40, 40))
	theta = path(h)
	plot.figure()
	plot.plot(theta, h)
	a = plot.gca()
	a.set_xlim([-pi, pi])
	plot.show()
	
	fplt.plot(population[max_fitness_index], "static_satellite.png")
	
	datafile = open("static_satellite.dat", "wb")
	pickler = pickle.Pickler(datafile)
	pickler.dump(population)
	pickler.dump(pop_fitness)
	datafile.close()
	
	plot.figure()
	plot.plot(range(gen), max_fitness, '+', color='green') #plot fitness progression
	plot.plot(range(gen), median_fitness, 'x', color='blue')
	plot.plot(range(gen), min_fitness, '.', color = 'red')
	plot.show()
	
	return population, pop_fitness
Ejemplo n.º 2
0
def evolve(gen=1000, population=None, pop=50):
	"""runs fern genetic algorithm and returns final population"""
	if population is None:
		r = fernpy.region1()
		r[0] = fernpy.interval(-20.0, 20.0)
		population = [fernpy.fern1(r, 3) for i in range(pop)]
		randomize = True
	else:
		pop = len(population)
		randomize = False
	
	for index, individual in enumerate(population):
		population[index].set_node_type_chance(node_type_chance)
		population[index].set_mutation_type_chance(mutation_type_chance_fork, 
							   mutation_type_chance_leaf)
		if randomize:
			population[index].randomize(15)
	
	job_server = pp.Server(ppservers=())
	jobs = []
	subfuncs = (simulate, f)
	packages = ("time", "scipy", "scipy.integrate", "numpy", "math", "fernpy")
	template = pp.Template(job_server, fitness, subfuncs, packages)
	
	max_fitness = [0]*gen
	median_fitness = [0]*gen
	min_fitness = [0]*gen
	#state0 = [random_state() for i in range(5)] #5 simulations per evaluation
	
	for generation in range(gen):
		state0 = [-15, -7, 7, 15] #4 simulations per evaluation
		optimal_fitness = fitness(OptimalController(), state0)
	
		#evaluate ferns
		pop_fitness = numpy.array([0.0]*pop)
		#state0 = [random_state() for i in range(10)] #simulations per evaluation
		for index, individual in enumerate(population):
			if len(jobs) < pop: #only happens in first generation
				jobs.append( template.submit(individual, state0) )
			else:
			 	jobs[index] = template.submit(individual, state0)
		
		for index, result in enumerate(jobs):
			pop_fitness[index] = result() / optimal_fitness 
		
		#record and then normalize fitness
		max_fitness_index = scipy.argmax(pop_fitness)
		max_fitness[generation] = pop_fitness[max_fitness_index]
		median_fitness[generation] = median(pop_fitness)
		min_fitness[generation] = min(pop_fitness)
		
		#control ratio of max to median fitness and normalize
		#fitness_mean = np.mean(pop_fitness);
		#if max_fitness[generation] != median_fitness[generation]: #fitness_mean: 
		#	a = log(fitness_ratio) / (log(max_fitness[generation]) - log(median_fitness[generation]))
		#	pop_fitness = (pop_fitness - median_fitness[generation])**a + median_fitness[generation]
		
		sys.stdout.write("\rgen %i" % generation)
		sys.stdout.flush()
		
		if generation == gen-1: 
			sys.stdout.write("\n")
			break #skip breeding on last step
			
		pop_fitness /= sum(pop_fitness)
		
		#select parents and breed new population
		new_population = []
		new_population.append( population[max_fitness_index] ) #elitism
		for i in range(1, pop):
			new_population.append(fernpy.fern1( population[select(pop_fitness)] ))
			if random.random() < crossover_rate:
				new_population[-1].crossover( population[select(pop_fitness)] ) 
			if random.random() < mutation_rate:
				new_population[-1].mutate()
		
		population = new_population
			
	##### plots ########
	fplt.plot(population[max_fitness_index], "velocity_control.png")
	#print population[max_fitness_index]
	
	datafile = open("velocity_control.dat", "wb")
	pickler = pickle.Pickler(datafile)
	pickler.dump(population)
	pickler.dump(pop_fitness)
	datafile.close()
	
	plot_sim( population[ max_fitness_index ] ) #plot best solution
	plot.figure()
	plot.plot(range(gen), max_fitness, '+', color='green') #plot fitness progression
	plot.plot(range(gen), median_fitness, 'x', color='blue')
	plot.plot(range(gen), min_fitness, '.', color = 'red')
	plot.show()
	
	return population, pop_fitness
Ejemplo n.º 3
0
def evolve(gen=500, population=None, pop=50):
	"""runs fern genetic algorithm and returns final population"""
	if population is None:
		r = fernpy.region1()
		r.set_uniform( fernpy.interval(-3.0, 3.0) )
		population = [fernpy.fern1(r, 2) for i in range(pop)]
		randomize = True
	else:
		pop = len(population)
		randomize = False
	
	for index, individual in enumerate(population):
		population[index].set_node_type_chance(node_type_chance)
		population[index].set_mutation_type_chance(mutation_type_chance_fork, 
							   mutation_type_chance_leaf)
		population[index].randomize(15)
	
	job_server = pp.Server(ppservers=())
	jobs = []
	subfuncs = ()
	packages = ("numpy", "fernpy",)
	
	max_fitness = [0]*gen
	median_fitness = [0]*gen
	min_fitness = [0]*gen
	
	for generation in range(gen):
		#generating new data for each generation helps encourage
		#a healthy genetic variation
		numbers, classes = generate_data()
	
		#evaluate ferns
		pop_fitness = numpy.array([0.0]*pop)
		if len(jobs) < pop:
			for index, individual in enumerate(population):
				jobs.append(job_server.submit(fitness, 
							      (individual, numbers, classes,), 
							      subfuncs, packages))
		else:
			for index, individual in enumerate(population):
			 	jobs[index] = job_server.submit(fitness, 
			 					(individual, numbers, classes,), 
			 					subfuncs, packages)
		
		for index, result in enumerate(jobs):
			pop_fitness[index] = result()
		
		#record and then normalize fitness
		max_fitness_index = sp.argmax(pop_fitness)
		max_fitness[generation] = pop_fitness[max_fitness_index]
		median_fitness[generation] = median(pop_fitness)
		min_fitness[generation] = min(pop_fitness)
		
		#control ratio of max to median fitness and normalize
		#fitness_mean = numpy.mean(pop_fitness);
		#if max_fitness[generation] != fitness_mean: 
		#	a = log(fitness_ratio) / (log(max_fitness[generation]) - log(fitness_mean))
		#	pop_fitness = (pop_fitness - fitness_mean)**a + fitness_mean
		
		sys.stdout.write("\rgen %i" % generation)
		sys.stdout.flush()
		
		if generation == gen-1: 
			sys.stdout.write("\n")
			break #skip breeding on last step
		
		pop_fitness /= sum(pop_fitness)
		
		#select parents and breed new population
		new_population = []
		new_population.append( population[max_fitness_index] ) #elitism
		for i in range(1, pop):
			new_population.append(fernpy.fern1( population[select(pop_fitness)] ))
			if random.random() < crossover_rate:
				new_population[-1].crossover( population[select(pop_fitness)] ) 
			if random.random() < mutation_rate:
				new_population[-1].mutate()
		
		population = new_population
	
	fplt.plot(population[max_fitness_index], "classify_fern2.png", 7)
	
	datafile = open("classify_fern2.dat", "wb")
	pickler = pickle.Pickler(datafile)
	pickler.dump(population)
	pickler.dump(pop_fitness)
	datafile.close()
	
	plot.figure()
	plot.plot(range(gen), max_fitness, '+', color='green') #plot fitness progression
	plot.plot(range(gen), median_fitness, 'x', color='blue')
	plot.plot(range(gen), min_fitness, '.', color = 'red')
	plot.xlabel("Generation")
	plot.ylabel("Fitness (# correct)")
	plot.show()
	
	return population, pop_fitness