Example #1
0
def brachistochrone():
	# create initial population
	population = create_population(size_pop)
	
	# evaluate population
	population = [[indiv[:], fitness(start+indiv[:]+finish)] for indiv in population]	
	population.sort(key=itemgetter(1))
	for generation in xrange(size_gen):
		parents = []
		# select parents
		if seleccao==2: # tournament
			for i in xrange(size_pop):
				parents.extend([tournament(population[:],tsize)])
			parents = [tournament(population[:], tsize) for i in xrange(size_pop)]
		else: # roulette
			sumfitness = 0.0
			for i in population:
				sumfitness += 1/i[1]
			
			probability = [0 for i in xrange(size_pop)]
			sumprob = 0.0
			for i in xrange(size_pop):
				probability[i] = sumprob + float((1.0/population[i][1]) / sumfitness)
				sumprob += probability[i]
			
			for i in xrange(size_pop):
				parents.extend([roulette(population[:], probability)])
		
		# produce offspring
		offspring = []
		
		# crossover
		for i in xrange(0, size_pop,2):
			if random.random() < prec:
				offspring.extend(recnpoints(parents[i][:],parents[i+1][:]))
			else:
				offspring.extend([parents[i][:],parents[i+1][:]])
				
		# mutation
		for i in xrange(size_pop):
			offspring[i] = mutation(offspring[i][:])
		
		# evaluate offspring
		offspring2 = [[indiv[0][:], fitness(start+indiv[0][:]+finish)] for indiv in offspring]
		offspring2.sort(key=itemgetter(1))
		
		# select survivors
		for i in xrange(size_pop - elite):
			population[size_pop-i-1] = offspring2[i][:]
		
		population2 = [[indiv[0][:], fitness(start+indiv[0][:]+finish)] for indiv in population]
		population = sorted(population2[:], key=itemgetter(1))
		
		excelWriter.writerow(("Generation: ",str(generation+1), "Best: ",str(population[0][1]), "Worst: ", str(population[-1][1]), "Average: ", str(average(population)), "Desvio: ", str(stdev(population)) ))
		
		if not generation%10:
			print str(generation)+"\n"
	FILE.write("Best Curve in Repetition "+str(repetition+1)+":\n"+str(population[0])+"\n\n")
	return True
Example #2
0
def brachistochrone():
	# create initial population
	population = create_population(size_pop)
	
	# evaluate population
	population = [[indiv[:], fitness(start+indiv[:]+finish)] for indiv in population]	
	population.sort(key=itemgetter(1))
	for generation in xrange(size_gen):
		parents = []
		# select parents
		if seleccao==2: # tournament
			for i in xrange(size_pop):
				parents.extend([tournament(population[:],tsize)])
			parents = [tournament(population[:], tsize) for i in xrange(size_pop)]
		else: # roulette
			sumfitness = 0.0
			for i in population:
				sumfitness += 1/i[1]
			
			probability = [0 for i in xrange(size_pop)]
			sumprob = 0.0
			for i in xrange(size_pop):
				probability[i] = sumprob + float((1.0/population[i][1]) / sumfitness)
				sumprob += probability[i]
			parents = [roulette(population[:], probability) for i in xrange(size_pop)]
		
		# produce offspring
		offspring = []
		
		# crossover
		for i in xrange(0, size_pop,2):
			if random.random() < prec:
				offspring.extend(recnpoints(parents[i][:],parents[i+1][:]))
			else:
				offspring.extend([parents[i][:],parents[i+1][:]])
				
		# mutation
		for i in xrange(size_pop):
			offspring[i] = mutation(offspring[i][:])
		
		# evaluate offspring
		offspring2 = [[indiv[0][:], fitness(start+indiv[0][:]+finish)] for indiv in offspring]
		offspring2.sort(key=itemgetter(1))
		
		# select survivors
		for i in xrange(size_pop - elite):
			population[size_pop-i-1] = offspring2[i][:]
		
		population2 = [[indiv[0][:], fitness(start+indiv[0][:]+finish)] for indiv in population]
		population = sorted(population2[:], key=itemgetter(1))
		
		excelWriter.writerow(("Generation: ",str(generation+1), "Best: ",str(population[0][1]), "Worst: ", str(population[-1][1]), "Average: ", str(average(population)), "Desvio: ", str(stdev(population)) ))
		
		if not generation%10:
			print str(generation)+"\n"
	FILE.write("Best Curve in Repetition "+str(repetition+1)+":\n"+str(population[0])+"\n\n")
	return True
def drawCurve(rTurtle, points, xScale, yScale):
    rTurtle.ht()
    rTurtle.clear()
    rTurtle.pu()
    rTurtle.setpos(
        start[0] * xScale - (canvasWidth / 2) - start[0] * xScale,
        start[1] * yScale - (canvasHeight / 2) - finish[1] * yScale + 100,
    )
    rTurtle.pd()
    rTurtle.color("black")
    for i in xrange(0, len(points)):
        rTurtle.goto(points[i])
        rTurtle.dot()
    rTurtle.pu()

    rTurtle.setpos(
        start[0] * xScale - (canvasWidth / 2) - start[0] * xScale,
        start[1] * yScale - (canvasHeight / 2) - finish[1] * yScale + 100,
    )

    points, bestpoints = brachistochroneReal(start[0], start[1], finish[0], finish[1], n_points + 2)
    rTurtle.pd()
    rTurtle.color("blue")
    for i in xrange(0, len(points)):
        rTurtle.goto(
            points[i][0] * xScale - (canvasWidth / 2) - start[0] * xScale,
            points[i][1] * yScale - (canvasHeight / 2) - finish[1] * yScale + 100,
        )
        rTurtle.dot()
    rTurtle.pu()
    best_fit = fitness(bestpoints)
    print "Real best: %f seconds " % best_fit
Example #4
0
def drawCurve( rTurtle, points, xScale, yScale):
	rTurtle.ht()
	rTurtle.clear()
	rTurtle.pu()
	rTurtle.setpos(start[0]*xScale-(canvasWidth/2)-start[0]*xScale, start[1] *yScale-(canvasHeight/2)-finish[1]*yScale+100)
	rTurtle.pd()
	rTurtle.color("black")
	for i in xrange(0,len(points)):
		rTurtle.goto(points[i])
		rTurtle.dot()
	rTurtle.pu()
	
	rTurtle.setpos(start[0]*xScale-(canvasWidth/2)-start[0]*xScale, start[1] *yScale-(canvasHeight/2)-finish[1]*yScale+100)
	
	points, bestpoints = brachistochroneReal(start[0], start[1], finish[0], finish[1], n_points+2)
	rTurtle.pd()
	rTurtle.color("blue")
	for i in xrange(0, len(points)):
		rTurtle.goto(points[i][0]*xScale-(canvasWidth/2)-start[0]*xScale, points[i][1]*yScale-(canvasHeight/2)-finish[1]*yScale+100)
		rTurtle.dot()
	rTurtle.pu()
	best_fit = fitness(bestpoints)
	print "Real best: %f seconds " %best_fit
Example #5
0
		# select survivors
		for i in xrange(size_pop - elite):
			population[size_pop-i-1] = offspring2[i][:]
		
		population2 = [[indiv[0][:], fitness(start+indiv[0][:]+finish)] for indiv in population]
		population = sorted(population2[:], key=itemgetter(1))
		
		excelWriter.writerow(("Generation: ",str(generation+1), "Best: ",str(population[0][1]), "Worst: ", str(population[-1][1]), "Average: ", str(average(population)), "Desvio: ", str(stdev(population)) ))
		
		if not generation%10:
			print str(generation)+"\n"
	FILE.write("Best Curve in Repetition "+str(repetition+1)+":\n"+str(population[0])+"\n\n")
	return True

points, bestpoints = brachistochroneReal(start[0], start[1], finish[0], finish[1], n_points+2)
bestRealFitness = fitness(bestpoints)

if representacao:
	representacaoStr = "X fixos"
else:
	representacaoStr = "S/ X fixos"
if seleccao == 2:
	seleccaoStr = "Torneio"
else:
	seleccaoStr = "Roleta"

sizes_pop = [50,100,150,200,300,400,500]

for size_pop in sizes_pop:
	
	FILE = open("results-size_pop_"+str(size_pop)+"-best.txt",'w')
Example #6
0
        excelWriter.writerow(
            ("Generation: ", str(generation + 1), "Best: ",
             str(population[0][1]), "Worst: ", str(population[-1][1]),
             "Average: ", str(average(population)), "Desvio: ",
             str(stdev(population))))

        if not generation % 10:
            print str(generation) + "\n"
    FILE.write("Best Curve in Repetition " + str(repetition + 1) + ":\n" +
               str(population[0]) + "\n\n")
    return True


points, bestpoints = brachistochroneReal(start[0], start[1], finish[0],
                                         finish[1], n_points + 2)
bestRealFitness = fitness(bestpoints)

if representacao:
    representacaoStr = "X fixos"
else:
    representacaoStr = "S/ X fixos"
if seleccao == 2:
    seleccaoStr = "Torneio"
else:
    seleccaoStr = "Roleta"

recombinations = [0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5]
rec_points = 5

for prec in recombinations:
def brachistochrone(rTurtle):
    # create initial population
    population = create_population(size_pop)

    # evaluate population
    population = [[indiv[:], fitness(start + indiv[:] + finish)] for indiv in population]
    population.sort(key=itemgetter(1))
    for generation in xrange(size_gen):
        parents = []
        # select parents
        if seleccao == 2:  # tournament
            for i in xrange(size_pop):
                parents.extend([tournament(population[:], tsize)])
            parents = [tournament(population[:], tsize) for i in xrange(size_pop)]
        else:  # roulette
            sumfitness = 0.0
            for i in population:
                sumfitness += 1.0 / i[1]

            probability = [0 for i in xrange(size_pop)]
            sumprob = 0.0
            for i in xrange(size_pop):
                probability[i] = sumprob + float((1.0 / population[i][1]) / sumfitness)
                sumprob += probability[i]

            for i in xrange(size_pop):
                parents.extend([roulette(population[:], probability)])

                # produce offspring
        offspring = []

        # crossover
        for i in xrange(0, size_pop, 2):
            if random.random() < prec:
                offspring.extend(recnpoints(parents[i][:], parents[i + 1][:]))
            else:
                offspring.extend([parents[i][:], parents[i + 1][:]])

                # mutation
        for i in xrange(size_pop):
            offspring[i] = mutation(offspring[i][:])

            # evaluate offspring
        offspring2 = [[indiv[0][:], fitness(start + indiv[0][:] + finish)] for indiv in offspring]
        offspring2.sort(key=itemgetter(1))

        # select survivors
        for i in xrange(size_pop - elite):
            population[size_pop - i - 1] = offspring2[i][:]

        population2 = [[indiv[0][:], fitness(start + indiv[0][:] + finish)] for indiv in population]
        population = sorted(population2[:], key=itemgetter(1))

        FILE.write("Generation: " + str(generation + 1) + "\n\n")
        FILE.write("Best: " + str(population[0][1]) + " seconds\n")
        FILE.write("Worst: " + str(population[-1][1]) + " seconds\n")
        FILE.write("Average: " + str(average(population)) + " seconds\n")
        FILE.write("Standard Deviation: " + str(stdev(population)) + " seconds\n")
        FILE.write("-" * 30 + "\n")

        print str(generation) + "\n"

    points = []

    xScale = canvasWidth / (finish[0] - start[0])
    yScale = canvasHeight / (start[1] - finish[1])

    for i in xrange(0, len(population[0][0]), 2):
        points.append(
            [
                population[0][0][i] * xScale - (canvasWidth / 2) - start[0] * xScale,
                population[0][0][i + 1] * yScale - (canvasHeight / 2) - finish[1] * yScale + 100,
            ]
        )
    points.append(
        [
            finish[0] * xScale - (canvasWidth / 2) - start[0] * xScale,
            finish[1] * yScale - (canvasHeight / 2) - finish[1] * yScale + 100,
        ]
    )

    drawCurve(rTurtle, points, xScale, yScale)

    print "Best took %f seconds " % population[0][1]

    best.append(population[0][1])
    return True
Example #8
0
def brachistochrone( rTurtle):
	# create initial population
	population = create_population(size_pop)
	
	# evaluate population
	population = [[indiv[:], fitness(start+indiv[:]+finish)] for indiv in population]	
	population.sort(key=itemgetter(1))
	for generation in xrange(size_gen):
		parents = []
		# select parents
		if seleccao==2: # tournament
			for i in xrange(size_pop):
				parents.extend([tournament(population[:],tsize)])
			parents = [tournament(population[:], tsize) for i in xrange(size_pop)]
		else: # roulette
			sumfitness = 0.0
			for i in population:
				sumfitness += 1.0/i[1]
			
			probability = [0 for i in xrange(size_pop)]
			sumprob = 0.0
			for i in xrange(size_pop):
				probability[i] = sumprob + float((1.0/population[i][1]) / sumfitness)
				sumprob += probability[i]
			
			for i in xrange(size_pop):
				parents.extend([roulette(population[:], probability)])
		
		# produce offspring
		offspring = []
		
		# crossover
		for i in xrange(0, size_pop,2):
			if random.random() < prec:
				offspring.extend(recnpoints(parents[i][:],parents[i+1][:]))
			else:
				offspring.extend([parents[i][:],parents[i+1][:]])
				
		# mutation
		for i in xrange(size_pop):
			offspring[i] = mutation(offspring[i][:])
		
		# evaluate offspring
		offspring2 = [[indiv[0][:], fitness(start+indiv[0][:]+finish)] for indiv in offspring]
		offspring2.sort(key=itemgetter(1))
		
		# select survivors
		for i in xrange(size_pop - elite):
			population[size_pop-i-1] = offspring2[i][:]
		
		population2 = [[indiv[0][:], fitness(start+indiv[0][:]+finish)] for indiv in population]
		population = sorted(population2[:], key=itemgetter(1))
		
		FILE.write("Generation: "+str(generation+1)+"\n\n")
		FILE.write("Best: "+str(population[0][1])+" seconds\n")
		FILE.write("Worst: "+str(population[-1][1])+" seconds\n")
		FILE.write("Average: "+str(average(population))+" seconds\n")
		FILE.write("Standard Deviation: "+str(stdev(population))+" seconds\n")
		FILE.write("-"*30+"\n")
		
		
		print str(generation)+"\n"
		
		
	points = []
	
	xScale = canvasWidth/(finish[0] - start[0])
	yScale = canvasHeight/(start[1] - finish[1])
	
	for i in xrange(0,len(population[0][0]),2):
		points.append([population[0][0][i]*xScale-(canvasWidth/2)-start[0]*xScale , population[0][0][i+1]*yScale-(canvasHeight/2)-finish[1]*yScale +100])
	points.append([finish[0]*xScale-(canvasWidth/2)-start[0]*xScale, finish[1]*yScale-(canvasHeight/2)-finish[1]*yScale + 100])
	
	drawCurve(rTurtle, points,xScale, yScale)
	
	print "Best took %f seconds " %population[0][1]
	
	
	best.append(population[0][1])
	return True