Exemplo n.º 1
0
def fernplot_pil_test(dim=1):
	if dim is 1:
		r = fp.region1()
		r[0] = fp.interval(1.0, 3.0)
		f = fp.fern1(r, 2)
		n = f.begin()
		n.right()
		n.set_leaf_bin(1)
		
		pil_plot(f)
		
	if dim is 2:
		r = fp.region2()
		r.set_uniform(fp.interval(-300.0, 300.0))
		f = fp.fern2(r, 3)
		f.randomize(50)
		print f
		pil_plot(f)
Exemplo n.º 2
0
def run():
	#prepare ferns
	r = fernpy.region1()
	r.set_uniform( fernpy.interval(-3.0, 3.0) )
	population = [fernpy.fern1(r, 2) for i in range(5)]
	for index, individual in enumerate(population):
		population[index].randomize(15)
	
	#prepare data
	n = 10000
	numbers = numpy.random.normal(0.0, 1.0, n)
	classes = [0]*n
	for index, number in enumerate(numbers):
		if number > 1.0:
			classes[index] = 0
		else:
			classes[index] = 1

	job_server = pp.Server(ppservers=())
	jobs = []
	subfuncs = ()
	packages = ("fernpy", "numpy")
	
	for i in range(5):
		pop_fitness = numpy.array([0.0]*5)
		if len(jobs) < 5:
			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()
	
		for index, individual in enumerate(population):
			population[index].randomize(10)
		
		print "Best in gen ", i, " is ", max(pop_fitness)
Exemplo n.º 3
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
Exemplo n.º 4
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