예제 #1
0
def get_fitness(n):
    #print 'Getting fitness of ' + str(n)
    return play(True, n)
예제 #2
0
def performance(n):
    return play(False, n)  # Returning the score of the current neural network
예제 #3
0
def get_new_pop(pop):
    new_pop = []
    pop = sorted(pop, key=get_fitness, reverse=True)
    print str(pop[0])
    new_pop.extend(pop[0:5])
    while len(new_pop) < len(pop):
        first = select(pop)
        second = select(pop)
        first, second = breed(first, second)
        new_pop.extend([first, second])

    return new_pop


## Code to play the game with previous weights assigned i.e; before the genetic algorithm is run
# test AI
#n = Net()
#good = [-.5524816518, -0.251785039, -1.2697808633, -0.72796955875826, -0.81684247849, 3.97877468847]
#	good = [-1.0863879678143158, -0.38785036446364252, -2.4946001121260855, -1.5860913785563859, -1.7712810292659231, 8.1115617366593806]
#n.decode(good)
#play(False, n)

# start genetic algo
pop = init_pop()
for i in xrange(GENERATIONS):
    print '==========================='
    print 'GENERATION ' + str(i)
    pop = get_new_pop(pop)
    play(False, pop[0])
    print str(pop[0])
def get_new_pop(pop):
	new_pop = []
	pop = sorted(pop, key=get_fitness,reverse=True)
	print str(pop[0])
	new_pop.extend(pop[0:5])
	while len(new_pop) < len(pop):
		first = select(pop)
		second = select(pop)
		first, second = breed(first, second)
		new_pop.extend([first, second])

	return new_pop

## Code to play the game with previous weights assigned i.e; before the genetic algorithm is run
# test AI
#n = Net()
#good = [-.5524816518, -0.251785039, -1.2697808633, -0.72796955875826, -0.81684247849, 3.97877468847]
#	good = [-1.0863879678143158, -0.38785036446364252, -2.4946001121260855, -1.5860913785563859, -1.7712810292659231, 8.1115617366593806]
#n.decode(good)
#play(False, n)

# start genetic algo
pop = init_pop()
for i in xrange(GENERATIONS):
    print '==========================='
    print 'GENERATION ' + str(i)
    pop = get_new_pop(pop)
    play(False, pop[0])
    print str(pop[0])

def get_fitness(n):
	#print 'Getting fitness of ' + str(n)
	return play(True, n)
예제 #6
0
def performance(n):
    return play(False, n)
예제 #7
0
    if performance(first) > performance(second):
        return first
    else:
        return second


def newnetlist(genome):
    newnetlist = []
    genome = sorted(genome, key=performance, reverse=True)
    newnetlist.extend(genome[0:5])

    while len(newnetlist) < len(genome):
        first = survivaloffittest(genome)
        second = survivaloffittest(genome)
        first, second = updateweights(first, second)
        newnetlist.append(first)
        newnetlist.append(second)
    return newnetlist


genome = []
for i in range(0, netsize):
    netelement = NN.NeuralNet()
    genome.append(netelement)

for i in range(0, generations):
    print 'GENERATION ' + str(i)
    genome = newnetlist(genome)
    #genome = sorted(genome, key=performance,reverse=True)
    play(False, genome[0])
def performance(n):
    #Parameter 'True' represents playing the game without displaying on screen
    return play(True, n)
def performance(n):
    #Parameter 'True' represents playing the game without displaying on screen
    return play(True,n)
	while len(newnetlist) < len(genome):
		#Till the newgenome doesn't have 'netlist' number of NeuralNets
		#Selecting 2 NeuralNets from the genome which have a good performance value by calling
		#the survival of the fittest function defined above
		first = survivaloffittest(genome)
		second = survivaloffittest(genome)
		#For these 2 NeuralNets, now updating their weights by calling the update weights function defined above
		first, second = updateweights(first, second)
                #Adding the updated weight NeuralNets to the newgenome
		newnetlist.append(first)
                newnetlist.append(second)
	return newnetlist


n = NeuralNet()
good = [-.5492326596523, -0.2495625644, -1.272124584125, -0.7305451455, -0.809896551525, 3.9812152125252]
n.listtonet(good)
play(False, n)

# Running of the main genetic algorithm
#Initialising the genome by calling the oldnetlist function
genome = oldnetlist()
#Iterating over all the generations
for i in range(0,generations):
    print 'GENERATION ' + str(i)
    genome = newnetlist(genome)
    #Playing the game after complete training in every generation
    #Here the play function is called with parameter False which indicates showing it on the screen
    play(False, genome[0])