def run():
    t0 = time.time()

    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, "xor2_config"))

    num_workers = 6
    pool = Pool(num_workers)
    print("Starting with %d workers" % num_workers)

    def fitness(genomes):
        return eval_fitness(genomes, pool)

    pop = population.Population(config)
    pop.epoch(fitness, 400)

    print("total evolution time %.3f sec" % (time.time() - t0))
    print("time per generation %.3f sec" % ((time.time() - t0) / pop.generation))

    winner = pop.most_fit_genomes[-1]
    print("Number of evaluations: %d" % winner.ID)

    # Verify network output against training data.
    print("\nBest network output:")
    net = nn.create_feed_forward_phenotype(winner)
    for i, inputs in enumerate(INPUTS):
        output = net.serial_activate(inputs)  # serial activation
        print("%1.5f \t %1.5f" % (OUTPUTS[i], output[0]))

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores)
    visualize.plot_species(pop.species_log)
    visualize.draw_net(winner, view=True)
Example #2
0
def evaluate_genome(g):
    net = nn.create_feed_forward_phenotype(g)

    fitnesses = []

    for runs in range(runs_per_net):
        sim = cart_pole.CartPole()

        # Run the given simulation for up to num_steps time steps.
        fitness = 0.0
        for s in range(num_steps):
            inputs = sim.get_scaled_state()
            action = net.serial_activate(inputs)

            # Apply action to the simulated cart-pole
            force = cart_pole.discrete_actuator_force(action)
            sim.step(force)

            # Stop if the network fails to keep the cart within the position or angle limits.
            # The per-run fitness is the number of time steps the network can balance the pole
            # without exceeding these limits.
            if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians:
                break

            fitness += 1.0

        fitnesses.append(fitness)

    # The genome's fitness is its worst performance across all runs.
    return min(fitnesses)
def eval_fitness(genomes):
    for g in genomes:
        net = nn.create_feed_forward_phenotype(g)

        # When the output matches expected for all inputs, fitness will reach
        # its maximum value of 1.0.
        g.fitness = get_fitness(net.serial_activate)
Example #4
0
def testGenome(genome, v=False,summary=False):
    g = genome    
    net = nn.create_feed_forward_phenotype(g)
    wordsFound=[]
    allwords=[]
    score = 0.0
    for i in range(tests):
        letters = [random.randint(0,1) for i in range(26)]
        output = net.serial_activate(letters)
        word = decrypt(output)
        allwords.append(word)
        if word in words:
            wordsFound.append(word)
            
    wordsFound = set(wordsFound)
    if v: print(wordsFound)
    profile = ([len(set(map(lambda x: x[i],wordsFound))) for i in range(outputLength)])
    profile = sum([x/float(len(letters)) for x in profile])/(outputLength)

    score  = 0.5*profile
    score += 0.5*len(wordsFound)/float(tests)
    if summary:
        for i in allwords:
            if i in words:print('%s - GOOD'%i)
            else:print('%s - BAD'%i)
        print (score)   
    return score
def run():
    t0 = time.time()

    # Get the path to the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'xor2_config')

    # Use a pool of four workers to evaluate fitness in parallel.
    pe = parallel.ParallelEvaluator(4, fitness)
    pop = population.Population(config_path)
    pop.run(pe.evaluate, 400)

    print("total evolution time {0:.3f} sec".format((time.time() - t0)))
    print("time per generation {0:.3f} sec".format(
        ((time.time() - t0) / pop.generation)))

    print('Number of evaluations: {0:d}'.format(pop.total_evaluations))

    # Show output of the most fit genome against training data.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nBest network output:')
    net = nn.create_feed_forward_phenotype(winner)
    for i, inputs in enumerate(xor_inputs):
        output = net.serial_activate(inputs)  # serial activation
        print("{0:1.5f} \t {1:1.5f}".format(xor_outputs[i], output[0]))
 def eval_fitness(genomes):
     for g in genomes:
         net = nn.create_feed_forward_phenotype(g)
         sum_square_error = 0.0
         for inputs, expected in zip(inputsTrain, outputsTrain):
             output = net.serial_activate(inputs)
             sum_square_error += np.mean((output - expected) ** 2)
         g.fitness = -sum_square_error
Example #7
0
def initialize():
    module_dir = os.path.dirname(os.path.abspath(__file__))
    textfile_path = os.path.join(module_dir, 'nn_winner_genome')
    with open(textfile_path, 'rb') as f:
        genome = pickle.load(f)

    print genome
    return nn.create_feed_forward_phenotype(genome)
def simpler_eval_fitness(genomes):
    for g in genomes:
        net = nn.create_feed_forward_phenotype(g)

        error = 0.0
        for inputs, expected in zip(simpler_inputs, simpler_outputs):
            output = net.serial_activate(inputs)
            error += (output[0] - expected) ** 2
        g.fitness = 1-error
def update_fitness_G(genomes,D):

    for g in genomes:

        g.fitness = 0.0
        for i in range(len(dataset)):
            noise = [ random.random() for x in range(G_config.input_nodes) ]
            generated = apply_network(nn.create_feed_forward_phenotype(g),noise)
            g.fitness += apply_network(D,generated)[0] / len(dataset)
def parallel_evaluation(genome):
    """ This function will run in parallel """
    net = nn.create_feed_forward_phenotype(genome)

    error = 0.0
    for inputData, outputData in zip(INPUTS, OUTPUTS):
        # serial activation
        output = net.serial_activate(inputData)
        error += (output[0] - outputData) ** 2

    return 1 - math.sqrt(error / len(OUTPUTS))
Example #11
0
def eval_fitness(genomes):
    for g in genomes:
        net = nn.create_feed_forward_phenotype(g)

        sum_square_error = 0.0
        for inputs, expected in zip(xor_inputs, xor_outputs):
            # Serial activation propagates the inputs through the entire network.
            output = net.serial_activate(inputs)
            sum_square_error += (output[0] - expected) ** 2

        # When the output matches expected for all inputs, fitness will reach
        # its maximum value of 1.0.
        g.fitness = 1 - sum_square_error
Example #12
0
def eval_mono_image(genome, width, height):
    net = nn.create_feed_forward_phenotype(genome)
    image = []
    for r in range(height):
        y = -2.0 + 4.0 * r / (height - 1)
        row = []
        for c in range(width):
            x = -2.0 + 4.0 * c / (width - 1)
            output = net.serial_activate([x, y])
            gray = 255 if output[0] > 0.0 else 0
            row.append(gray)
        image.append(row)

    return image
Example #13
0
def eval_gray_image(genome, width, height):
    net = nn.create_feed_forward_phenotype(genome)
    image = []
    for r in range(height):
        y = -1.0 + 2.0 * r / (height - 1)
        row = []
        for c in range(width):
            x = -1.0 + 2.0 * c / (width - 1)
            output = net.serial_activate([x, y])
            gray = int(round((output[0] + 1.0) * 255 / 2.0))
            gray = max(0, min(255, gray))
            row.append(gray)
        image.append(row)

    return image
Example #14
0
    def __init__(self, genome):
        self.genome = genome
        self.color = ['blue', 'red', 'yellow', 'black'][rnd.randint(0,3)]
        self.state = 0 # denotes the state of the wing
        self.generator = cycle([0,1,2,1]) # iterator of the wing states
        self.alive = True
        self.jumps = 0 # counts total number of flaps

        self.x = WIDTH // 5
        self.y = HEIGHT // 2.5

        self.velocity = -8 # vertical velocity
        self.acceleration = 1 # vertical acceleration

        self.brain = nn.create_feed_forward_phenotype(genome)
def update_fitness_D(genomes,G):

    for g in genomes:
        real_inputs, real_outputs = zip(*dataset)

        noise = [ random.random() for x in range(G_config.input_nodes) ]

        fake_inputs = [ apply_network(G,noise) for _ in range( len(dataset) ) ]
        fake_outputs = [ [0] for x in range( len(dataset) ) ]

        inputs = list(real_inputs) + fake_inputs
        outputs = list(real_outputs) + fake_outputs


        g.fitness = 1 - evaluate_network(nn.create_feed_forward_phenotype(g),
                                        inputs,outputs)
Example #16
0
 def eval_fitness(self,genomes):
     for g in genomes:
         net = nn.create_feed_forward_phenotype(g)
         output = []
         for x in self.fit_X:
             l = net.serial_activate(x)
             count = 0
             sum = 0
             for j in l:
                 #print(l)
                 sum += j
                 count += 1
             k = sum/count
             output.append(k)
         error = roc_auc_score(self.fit_Y,output)
         g.fitness = 1 - error
def new_and_simpler_stuff():
    pop.epoch(simpler_eval_fitness, num_epochs)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

# Display the most fit genome.
    print('\nBest genome:')
    winner = pop.most_fit_genomes[-1]
    print(winner)

# Verify network output against training data.
    print('\nOutput:')
    winner_net = nn.create_feed_forward_phenotype(winner)
    for inputs, expected in zip(simpler_inputs, simpler_outputs):
        output = winner_net.serial_activate(inputs)
        print("expected {0:1.5f} got {1:1.5f}".format(expected, output[0]))
    import ipdb; ipdb.set_trace()
    import pydevd; pydevd.settrace()
Example #18
0
    def __init__(self, x, main_game, genome):
        self._id = x
        self.main_game = main_game
        self._x = random.randint(0, self.main_game.screen_width) * 1.0
        self._y = random.randint(0, self.main_game.screen_height) * 1.0
        self._r = random.randint(0, 360) * 1.0
        self._line = 0
        self._sprite = pygame.Surface((20, 20))
        pygame.draw.polygon(self._sprite, Color("Purple"), ((6, 0), (0, 12), (12, 12)))

        self._net = nn.create_feed_forward_phenotype(genome)
        self._genome = genome
        self._rect = self._sprite.get_rect()
        self.projectiles = []
        self.alive = True
        self._genome.fitness = 0
        self.hunger = 0
        self.reload = 0
Example #19
0
def eval_color_image(genome, width, height):
    net = nn.create_feed_forward_phenotype(genome)
    image = []
    for r in range(height):
        y = -1.0 + 2.0 * r / (height - 1)
        row = []
        for c in range(width):
            x = -1.0 + 2.0 * c / (width - 1)
            output = net.serial_activate([x, y])
            red = int(round((output[0] + 1.0) * 255 / 2.0))
            green = int(round((output[1] + 1.0) * 255 / 2.0))
            blue = int(round((output[2] + 1.0) * 255 / 2.0))
            red = max(0, min(255, red))
            green = max(0, min(255, green))
            blue = max(0, min(255, blue))
            row.append((red, green, blue))
        image.append(row)

    return image
def visualizeWinners(checkpoint, config, picdir, rounds):
    colors = ['red', 'yellow', 'green', 'blue']
    while len(checkpoint) < 4:
        checkpoint.extend(checkpoint)
    checkpoint = checkpoint[:4]
    print("Going to let fight: ", checkpoint)
    print("With colors: ", colors)
    nets = {}
    for i, c in enumerate(checkpoint):
        pop = population.Population(config)
        pop.load_checkpoint(c)
        trainfunc = partial(eval_fitness_internalfight, num_runs=10, steplength=200, x=16, y=16)
        pop.run(trainfunc, 1)
        winner = pop.statistics.best_genome()
        nets[c+str(i)] = nn.create_feed_forward_phenotype(winner)

    filelist = glob.glob(os.path.join(picdir, 'step*.png'))
    for f in filelist:
        os.remove(f)

    x = 40; y = 40
    game = ca.CellularAutomaton(initialState=ca.initializeHexagonal(x, y), param=ca.defaultParameters)
    for i,k in enumerate(nets.keys()):
        game.setNewSpecies(util.nicePositions4(i,x, y), k, colors[i])

    util.saveStatePicture(game.getState(), picdir)

    while game.step < rounds:
        state = game.getState()
        for s in game.findSpecies():
            try:
                game.setDecisions(s, netDecision(state, s, nets[s]))
            except:
                pass
        game.evolve()
        util.saveStatePicture(state, picdir)

    app = QApplication(sys.argv)
    pics = util.sort_nicely(glob.glob(os.path.join(picdir, 'step*.png')))
    gui = SimpleGUI(pics)

    gui.show()
    sys.exit(app.exec_())
def fitness(genome):
    """
    This function will be run in parallel by ParallelEvaluator.  It takes one
    argument (a single genome) and should return one float (that genome's fitness).

    Note that this function needs to be in module scope for multiprocessing.Pool
    (which is what ParallelEvaluator uses) to find it.  Because of this, make
    sure you check for __main__ before executing any code (as we do here in the
    last two lines in the file), otherwise you'll have made a fork bomb
    instead of a neuroevolution demo. :)
    """
    net = nn.create_feed_forward_phenotype(genome)

    sum_square_error = 0.0
    for inputData, outputData in zip(xor_inputs, xor_outputs):
        # serial activation
        output = net.serial_activate(inputData)
        sum_square_error += (output[0] - outputData) ** 2

    return 1 - math.sqrt(sum_square_error / len(xor_outputs))
Example #22
0
def run():
    pop = population.Population('xor2_config')
    pop.epoch(eval_fitness, 300)

    winner = pop.most_fit_genomes[-1]
    print('Number of evaluations: %d' % winner.ID)

    # Verify network output against training data.
    print('\nBest network output:')
    net = nn.create_feed_forward_phenotype(winner)
    for inputs, expected in zip(INPUTS, OUTPUTS):
        output = net.serial_activate(inputs)
        print("expected %1.5f got %1.5f" % (expected, output[0]))

    print(nn.create_feed_forward_function(winner))

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores)
    visualize.plot_species(pop.species_log)
    visualize.draw_net(winner, view=True)
def old_more_complex_stuff():
#    pop.epoch(eval_fitness, num_epochs)
    while True:
        try:
            pop.run(eval_fitness, num_epochs)
        except KeyboardInterrupt:
            import pdb; pdb.set_trace()
        except (ZeroDivisionError, ValueError, OverflowError):
            pass

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

# Display the most fit genome.
    print('\nBest genome:')
    winner = pop.most_fit_genomes[-1]
    print(winner)

# Verify network output against training data.
    print('\nOutput:')
    winner_net = nn.create_feed_forward_phenotype(winner)
    for inputs, expected in zip(xor_inputs, xor_outputs):
        output = winner_net.serial_activate(inputs)
        print("expected {0:1.5f} got {1:1.5f}".format(expected, output[0]))
def eval_fitness_internalfight(allgenomes, num_runs=3, steplength=100, x=16, y=16):
    for g in allgenomes:
        g.fitness = 0
    # sadly, the number of genomes from neat-python is not fixed, so we only train some to fit %4
    topad = int(np.ceil(len(allgenomes)/4)*4-len(allgenomes))
    traincount = np.zeros(len(allgenomes))
    trainfitness = np.zeros(len(allgenomes))
    print(len(allgenomes),topad)
    for _ in range(num_runs):
        # geht nur, wenn genomes durch 4 teilbar ist TODO change this
        grouping = np.random.permutation(len(allgenomes))
        if topad > 0:
            grouping = np.concatenate((grouping, np.random.choice(grouping,topad)))

        grouping = np.reshape(grouping, (len(grouping)/4, 4))
        for group in grouping:
            nets = []
            game = ca.CellularAutomaton(initialState=ca.initializeHexagonal(x, y), param=ca.defaultParameters)
            for i, g in enumerate(group):
                nets.append(nn.create_feed_forward_phenotype(allgenomes[g]))
                game.setNewSpecies(util.nicePositions4(i,x,y), 'spec' + str(i))
            while game.step < steplength:
                state = game.getState()
                for j, g in enumerate(group):
                    game.setDecisions('spec' + str(j), netDecision(state, 'spec' + str(j), nets[j]))
                game.evolve()
            for k, g in enumerate(group):
                trainfitness[g] += countSpecies(game.getState(), 'spec' + str(k))
                traincount[g] += 1
    # divide training results by traincount, bc of padding etc this has to be done
    # fitness of all below median is set to zero
    trainfitness = trainfitness/traincount
    trainfitness[trainfitness < np.median(trainfitness)] = 0

    # results of fights define the fitness
    for k, g in enumerate(allgenomes):
        g.fitness = trainfitness[k]
Example #25
0
 def fit(self, X, Y):
     self.fit_X = X # XXX: store X_Train as a tmp class var
     self.fit_Y = Y # XXX: store Y_Train as a tmp class var
     self.pop.run(self.eval_fitness,self.n)
     self.winner = self.pop.statistics.best_genome()
     self.winner_net = nn.create_feed_forward_phenotype(self.winner)
Example #26
0
def eval_fitness(genomes):
    for g in genomes:
        net = nn.create_feed_forward_phenotype(g)
        score,turns = play_game(net)
        #print ("Score, turns", score, turns)
        g.fitness = score + turns*10
Example #27
0
            # Serial activation propagates the inputs through the entire network.
            output = net.serial_activate(inputs)
            sum_square_error += (output[0] - expected) ** 2

        # When the output matches expected for all inputs, fitness will reach
        # its maximum value of 1.0.
        g.fitness = 1 - sum_square_error


local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'xor2_config')
pop = population.Population(config_path)
pop.run(eval_fitness, 300)

# Log statistics.
statistics.save_stats(pop.statistics)
statistics.save_species_count(pop.statistics)
statistics.save_species_fitness(pop.statistics)

print('Number of evaluations: {0}'.format(pop.total_evaluations))

# Show output of the most fit genome against training data.
winner = pop.statistics.best_genome()
print('\nBest genome:\n{!s}'.format(winner))
print('\nOutput:')
winner_net = nn.create_feed_forward_phenotype(winner)
for inputs, expected in zip(xor_inputs, xor_outputs):
    output = winner_net.serial_activate(inputs)
    print("expected {0:1.5f} got {1:1.5f}".format(expected, output[0]))

Example #28
0
                    ranks = nx.pagerank(g.G)
            ge.fitness = fit
        print ge.fitness


pop = population.Population('conf')

pop.run(eval_fitness, 100)
statistics.save_stats(pop.statistics)
statistics.save_species_count(pop.statistics)
statistics.save_species_fitness(pop.statistics)

to_test = []
winner = pop.statistics.best_genome()
wn = nn.create_feed_forward_phenotype(winner)

test_years = [
    '2008-09', '2009-10', '2010-11', '2011-12', '2012-13', '2013-14', '2015-16'
]


def test_winner(w, league='/E0.csv', test_years=test_years):
    fit = 100
    for year in test_years:
        s = season('./seasons/' + year + league)
        s.read_season()
        g = network(s)
        g.make_graph_to()
        ranks = nx.pagerank(g.G)
        while True:
def get_evo_network(pop):

    winner = pop.statistics.best_genome()
    return nn.create_feed_forward_phenotype(winner)
Example #30
0
from __future__ import print_function

import pickle

import pygame

import evolve
from neat import nn, visualize

evolve.W = 1000
evolve.H = 1000

pb = evolve.PictureBreeder(128, 128, 1500, 1500, 1280, 1024, 'color', 4)

with open("genome-20219-701.bin", "rb") as f:
    g = pickle.load(f)
    print(g)
    node_names = {0: 'x', 1: 'y', 2: 'gray'}
    visualize.draw_net(g,
                       view=True,
                       filename="picture-net.gv",
                       show_disabled=False,
                       prune_unused=True,
                       node_names=node_names)

    net = nn.create_feed_forward_phenotype(g)
    pb.make_high_resolution(g)
Example #31
0
                break
            if reward > 0:
                reward = 1

            cum_reward += reward

        fitnesses.append(cum_reward)

    fitness = (np.array(fitnesses).mean())
    print("Species fitness: %s" % str(fitness))
    scores.append(fitness)
    return fitness


with open(winner, 'rb') as pickle_file:
    winner_a = pickle.load(pickle_file)

winner_net = nn.create_feed_forward_phenotype(winner_a)
for i in range(100):
    simulate_species(scores, winner_net, my_env, 1, 50000, True)

## We can decide to save only the sum of the scores, or to use min max normalization
#final_score=(np.mean(scores)-np.min(scores))/(np.max(scores)-np.min(scores))
final_score = np.sum(scores)

mx = np.max(scores)
with open('scores.txt', 'a') as the_file:
    the_file.write(
        'Mean scores for game {0} and trained on {1}: {2} with MAX={3}  '.
        format(game_name, winner, final_score, mx))
def update_fitness(genomes):
    for g in genomes:
        input_data,output_data = zip(*dataset)
        result = evaluate_network(  nn.create_feed_forward_phenotype(g),
                                    input_data, output_data )
        g.fitness = 1 - result
Example #33
0
 def __init__(self, genome):
     self.i = 0
     self.genome = genome
     self.net = nn.create_feed_forward_phenotype(genome)
Example #34
0
import pickle

from cart_pole import CartPole, discrete_actuator_force
from movie import make_movie

from neat import nn

# load the winner
with open('nn_winner_genome', 'rb') as f:
    c = pickle.load(f)

print('Loaded genome:')
print(c)

net = nn.create_feed_forward_phenotype(c)
sim = CartPole()

print()
print("Initial conditions:")
print("        x = {0:.4f}".format(sim.x))
print("    x_dot = {0:.4f}".format(sim.dx))
print("    theta = {0:.4f}".format(sim.theta))
print("theta_dot = {0:.4f}".format(sim.dtheta))
print()

# Run the given simulation for up to 100k time steps.
num_balanced = 0
for s in range(10 ** 5):
    inputs = sim.get_scaled_state()
    action = net.serial_activate(inputs)
Example #35
0
				print fitness
				env.reset()
				break
				
		genome.fitness = fitness #assigning fitness to the genome.


pop = population.Population(os.getcwd() + '/cartPole-v0_configuration') # Attaching the config file required by the NEAT Library
pop.run(eval_fitness, 300)

best = pop.statistics.best_genome()

env.monitor.start('cartpole-experiment/', force=True)

streak = 0
best_phenotype = nn.create_feed_forward_phenotype(best)

observation = env.reset()
env.render()

while streak < 100:
	fitness = 0
	frames = 0
	while 1:
		inputs = observation

		# active neurons
		output = best_phenotype.serial_activate(inputs)
		if (output[0] >= 0):
			observation, reward, done, info = env.step(MOVEMENT_RIGHT)
		else: