Exemplo n.º 1
0
def getbest(i):
    g = NEAT.Genome(0, substrate.GetMinCPPNInputs(), 2,
                    substrate.GetMinCPPNOutputs(), False,
                    NEAT.ActivationFunction.TANH, NEAT.ActivationFunction.TANH,
                    0, params)

    pop = NEAT.Population(g, params, True, 1.0, i)
    pop.RNG.Seed(i)

    for generation in range(2000):
        genome_list = NEAT.GetGenomeList(pop)
        # if sys.platform == 'linux':
        #    fitnesses = EvaluateGenomeList_Parallel(genome_list, evaluate, display=False)
        # else:
        fitnesses = EvaluateGenomeList_Serial(genome_list,
                                              evaluate,
                                              display=False)
        [
            genome.SetFitness(fitness)
            for genome, fitness in zip(genome_list, fitnesses)
        ]

        print('Gen: %d Best: %3.5f' % (generation, max(fitnesses)))

        best = max(fitnesses)

        pop.Epoch()
        generations = generation

        if best > 15.0:
            break

    return generations
Exemplo n.º 2
0
def getbest(i):
    g = NEAT.Genome(0, 3, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                    NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params, 0)
    pop = NEAT.Population(g, params, True, 1.0, i)
    # pop.RNG.Seed(int(time.clock()*100))
    pop.RNG.Seed(1234)

    generations = 0
    for generation in range(max_generations):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = EvaluateGenomeList_Serial(genome_list, evaluate, display=False)
        # fitness_list = EvaluateGenomeList_Parallel(genome_list, evaluate, display=False)
        NEAT.ZipFitness(genome_list, fitness_list)
        pop.Epoch()
        generations = generation
        best = max(fitness_list)
        if best > 15.0:
            break

    net = NEAT.NeuralNetwork()
    pop.GetBestGenome().BuildPhenotype(net)

    # img = NEAT.viz.Draw(net)
    # cv2.imshow("current best", img)
    # cv2.waitKey(1)
    
    return generations, net.NumHiddenNeurons(), net.NumConnections()
def run_experiment(config_file, trial_id, n_generations, out_dir, view_results=False, save_results=True):
    """
    The function to run the experiment against hyper-parameters 
    defined in the provided configuration file.
    The winner genome will be rendered as a graph as well as the
    important statistics of neuroevolution process execution.
    Arguments:
        config_file:    The path to the file with experiment 
                        configuration
        trial_id:       The ID of current trial
        n_generations:  The number of evolutionary generations
        out_dir:        The directory to save intermediate results.
        view_results:   The flag to control if intermediate results should be displayed after each trial
        save_results:   The flag to control whether intermediate results should be saved after each trial.
    Returns:
        The tuple (solution_found, generation, complexity, best_genome_fitness) that has flag indicating whether
        solution was found, the generation when solution was found, the complextity of best genome, and the fitness
        of best genome.
    """
    g = NEAT.Genome(0, 4+1, 0, 1+1, False, NEAT.ActivationFunction.TANH, 
                NEAT.ActivationFunction.TANH, 0, params, 0)
    pop = NEAT.Population(g, params, True, 1.0, trial_id)

     # set random seed
    seed = int(time.time())
    pop.RNG.Seed(seed)

    generations = 0
    solved = False
    best_trial_fitness = 0
    best_trial_complexity = 0
    for generation in range(n_generations):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = EvaluateGenomeList_Serial(genome_list, evaluate, display=view_results)
        NEAT.ZipFitness(genome_list, fitness_list)
        generations = generation
        best = max(genome_list, key=get_fitness)
        best_fitness = best.GetFitness()
        complexity = best.NumNeurons() + best.NumLinks()
        solved = best_fitness >= cart.MAX_FITNESS # Changed to correspond limit used with other tested libraries
        if solved:
            best_trial_fitness = best_fitness
            best_trial_complexity = complexity
            print("Trial: %2d\tgeneration: %d\tfitness: %f\tcomplexity: %d\tseed: %d" % 
                    (trial_id, generations, best_trial_fitness, complexity, seed))
            break
        # check if best fitness in this generation is better than current maximum
        if best_fitness > best_trial_fitness:
            best_trial_complexity = complexity
            best_trial_fitness = best_fitness

        # move to the next epoch
        pop.Epoch()
            
    if not solved:
        print("Trial: %2d\tFAILED\t\tfitness: %f\tcomplexity: %d\tseed: %d" % 
                (trial_id, best_trial_fitness, best_trial_complexity, seed))

    return solved, generations, best_trial_complexity, best_trial_fitness
Exemplo n.º 4
0
def getbest(run):
    g = NEAT.Genome(0, substrate.GetMinCPPNInputs(), 0,
                    substrate.GetMinCPPNOutputs(), False,
                    NEAT.ActivationFunction.TANH, NEAT.ActivationFunction.TANH,
                    0, params)

    pop = NEAT.Population(g, params, True, 1.0, run)
    for generation in range(1000):
        # Evaluate genomes
        genome_list = NEAT.GetGenomeList(pop)

        fitnesses = EvaluateGenomeList_Serial(genome_list,
                                              evaluate_xor,
                                              display=False)
        [
            genome.SetFitness(fitness)
            for genome, fitness in zip(genome_list, fitnesses)
        ]

        print('Gen: %d Best: %3.5f' % (generation, max(fitnesses)))

        # Print best fitness
        # print("---------------------------")
        # print("Generation: ", generation)
        # print("max ", max([x.GetLeader().GetFitness() for x in pop.Species]))

        # Visualize best network's Genome

        net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().BuildPhenotype(net)
        img = np.zeros((500, 500, 3), dtype=np.uint8)
        img += 10
        NEAT.DrawPhenotype(img, (0, 0, 500, 500), net)
        cv2.imshow("CPPN", img)
        # Visualize best network's Pheotype
        net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().BuildESHyperNEATPhenotype(
            net, substrate, params)
        img = np.zeros((500, 500, 3), dtype=np.uint8)
        img += 10

        NEAT.DrawPhenotype(img, (0, 0, 500, 500), net, substrate=True)
        cv2.imshow("NN", img)
        cv2.waitKey(1)

        if max(fitnesses) > 15.0:
            break

        # Epoch
        generations = generation
        pop.Epoch()

    return generations
Exemplo n.º 5
0
def getbest(i):
    g = NEAT.Genome(0, 3, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                    NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params)
    pop = NEAT.Population(g, params, True, 1.0, i)
    pop.RNG.Seed(int(time.clock()*100))

    generations = 0
    for generation in range(1000):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = EvaluateGenomeList_Serial(genome_list, evaluate, display=False)
        NEAT.ZipFitness(genome_list, fitness_list)
        pop.Epoch()
        generations = generation
        best = max(fitness_list)
        if best > 15.0:
            break

    return generations
def getbest(i):
    g = NEAT.Genome(0, substrate.GetMinCPPNInputs(), 0,
                    substrate.GetMinCPPNOutputs(), False,
                    NEAT.ActivationFunction.TANH, NEAT.ActivationFunction.TANH,
                    0, params, 0)

    pop = NEAT.Population(g, params, True, 1.0, i)
    pop.RNG.Seed(i)

    for generation in range(max_generations):
        genome_list = NEAT.GetGenomeList(pop)
        # if sys.platform == 'linux':
        #    fitnesses = EvaluateGenomeList_Parallel(genome_list, evaluate, display=False)
        # else:
        fitnesses = EvaluateGenomeList_Serial(genome_list,
                                              evaluate,
                                              display=False)
        [
            genome.SetFitness(fitness)
            for genome, fitness in zip(genome_list, fitnesses)
        ]

        net = NEAT.NeuralNetwork()
        pop.GetBestGenome().BuildPhenotype(net)

        complexity = "complexity ({}, {})".format(net.NumHiddenNeurons(),
                                                  net.NumConnections())
        print('Gen: %d/%d Best: %3.5f. %s' %
              (generation, max_generations - 1, max(fitnesses), complexity))

        best = max(fitnesses)

        pop.Epoch()
        generations = generation

        if best > 15.0:
            break

    return generations
def run_experiment(params, trial_id, n_generations, out_dir=None, view_results=False, save_results=True):
    g = NEAT.Genome(0, 3, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                    NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params, 0)
    pop = NEAT.Population(g, params, True, 1.0, trial_id)

    # set random seed
    seed = int(time.time())
    pop.RNG.Seed(seed)

    generations = 0
    solved = False
    max_fitness = 0
    complexity = 0
    for generation in range(n_generations):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = EvaluateGenomeList_Serial(genome_list, evaluate, display=view_results)
        NEAT.ZipFitness(genome_list, fitness_list)
        generations = generation
        best = max(genome_list, key=get_fitness)
        best_fitness = best.GetFitness()
        complexity = best.NumNeurons() + best.NumLinks()
        solved = best_fitness > 15.5 # Changed to correspond limit used with other tested libraries
        if solved:
            max_fitness = best_fitness
            print("Trial: %2d\tgeneration: %d\tfitness: %f\tcomplexity: %d\tseed: %d" % (trial_id, generations, max_fitness, complexity, seed))
            break
        # check if best fitness in this generation is better than current maximum
        max_fitness = max(best_fitness, max_fitness)

        # move to the next epoch
        pop.Epoch()
            
    if not solved:
        print("Trial: %2d\tFAILED\t\tfitness: %f\tcomplexity: %d\tseed: %d" % (trial_id, max_fitness, complexity, seed))

    return solved, generations, complexity, max_fitness
Exemplo n.º 8
0
def getbest(run):
    g = NEAT.Genome(0,
                     substrate.GetMinCPPNInputs(),
                    3,
                    substrate.GetMinCPPNOutputs(),
                    False,
                    NEAT.ActivationFunction.TANH,
                    NEAT.ActivationFunction.TANH,
                    0,
                    params)

    pop = NEAT.Population(g, params, True, 1.0, run)
    maxf_ever = 0
    hof = []
    for generation in range(300):
        # Evaluate genomes
        genome_list = NEAT.GetGenomeList(pop)

        fitnesses = EvaluateGenomeList_Serial(genome_list, evaluate_xor, display=True)
        [genome.SetFitness(fitness) for genome, fitness in zip(genome_list, fitnesses)]

        print('Gen: %d Best: %3.5f' % (generation, max(fitnesses)))

        # Print best fitness
        # print("---------------------------")
        # print("Generation: ", generation)
        # print("max ", max([x.GetLeader().GetFitness() for x in pop.Species]))
        # maxf = max([x.GetFitness() for x in NEAT.GetGenomeList(pop)])
        # print('Generation: {}, max fitness: {}'.format(generation, maxf))

        # if maxf > maxf_ever:
        # 	with open("lastGenWinner",'w') as f:

        #     	f.write(pickle.dumps(net)
        #     maxf_ever = maxf


        # Visualize best network's Genome

        net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().BuildPhenotype(net)
        # img = np.zeros((400, 400, 3), dtype=np.uint8)
        # img += 10
        # NEAT.DrawPhenotype(img, (0, 0, 400, 400), net)
        # cv2.imshow("CPPN", img)
        # # Visualize best network's Pheotype
        # net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().BuildESHyperNEATPhenotype(net, substrate, params)
        # img = np.zeros((400, 400, 3), dtype=np.uint8)
        # img += 10

        # NEAT.DrawPhenotype(img, (0, 0, 400, 400), net, substrate=True)
        # cv2.imshow("NN", img)
        # cv2.waitKey(33)
        # time.sleep(2)



        with open('winnerGenome', 'wb') as f:
            pickle.dump(str(pop.GetBestGenome()),f)
        with open('winnerNet', 'wb') as f:
            pickle.dump(str(net), f)
        with open('winnerSub', 'wb') as f:
            pickle.dump(str(substrate), f)
        with open('winnerParams', 'wb') as f:
            pickle.dump(str(params), f)

        if max(fitnesses) > 20000.0:
            break

        # Epoch
        generations = generation
        pop.Epoch()

    return generations
Exemplo n.º 9
0
def run_population_evolution():

    genoma = NEAT.Genome(0, 2, num_neuron_hidden, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID, NEAT.ActivationFunction.UNSIGNED_SIGMOID, seed_gen, params, num_hidden_layers)
    pop = NEAT.Population(genoma, params, True, 1.0, 0)

    best_index_ever = 0
    best_generation_ever = 0

    for generation in xrange(0, full_generations):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = EvaluateGenomeList_Serial(genome_list, train_genome, display=False)
        NEAT.ZipFitness(genome_list, fitness_list)

        best = max(fitness_list)
        best_index = fitness_list.index(max(fitness_list))
        datah.evhist.append(best)
        if best > datah.best_fit_ever:
            sys.stdout.flush()
            datah.best_gs.append(pop.GetBestGenome())
            datah.best_fit_ever = best
            best_index_ever = best_index
            best_generation_ever = generation
        else:
            pass


        # Store the species contained in the population
        storeSpecies = []
        numSpecies = [0 for n in xrange(0, len(pop.Species))]
        for n in xrange(0, len(numSpecies)):
            numSpecies[n] = specie()
            numSpecies[n].ID = pop.Species[n].ID()
            numSpecies[n].tamano = pop.Species[n].NumIndividuals()
            storeSpecies.append(numSpecies[n])
        datah.species.append(storeSpecies)

        # Store the statistical data for representation of the evolution
        datah.bestIndividualsInGeneration[datah.generation] = pop.GetBestGenome()
        datah.valueFitnessMax[datah.generation] = best
        datah.valueFitnessMediana[datah.generation] = np.median(fitness_list)
        datah.perc75[datah.generation] = np.percentile(fitness_list, 75)
        datah.perc25[datah.generation] = np.percentile(fitness_list, 25)
        datah.valueFitnessMedia[datah.generation] = np.average(fitness_list)
        datah.fitnessValidation[datah.generation] = validation_genome(pop.GetBestGenome())
        datah.generation = cp.deepcopy(datah.generation + 1)

        if datah.generation > datah.max_generations - 1:
            sf.saveStatistics(datah, directory)
        else:
            pass

        if datah.generation > datah.max_generations - 1:
            sf.representationTrain_Validation(datah, directory, topLimit)
        else:
            pass

        pop.Epoch()
        print 'Best_fit_ever:', datah.best_fit_ever
        print('Generacion:', generation, 'Mejor:', round(best, 3), 'Mediana:', round(np.median(fitness_list), 3), 'Media:',round(np.average(fitness_list), 3), 'Std:', round(np.std(fitness_list), 3))
        print(' ')

    return (best_generation_ever, best_index_ever, datah.best_gs[len(datah.best_gs) -1], datah.best_fit_ever)