Example #1
0
def create_initial_population(params):
    # Create network size based off coral and parameters.
    n_inputs, n_outputs = Coral.calculate_inouts(params)

    genome_prototype = NEAT.Genome(
        0,  # ID
        n_inputs,
        0,  # NUM_HIDDEN
        n_outputs,
        False,  # FS_NEAT
        NEAT.ActivationFunction.
        UNSIGNED_SIGMOID,  # Output activation function.
        NEAT.ActivationFunction.
        UNSIGNED_SIGMOID,  # Hidden activation function.
        0,  # Seed type, must be 1 to have hidden nodes.
        params.neat,
        0)
    pop = NEAT.Population(
        genome_prototype,  # Seed genome.
        params.neat,
        True,  # Randomize weights.
        1.0,  # Random Range.
        int(time.time())  # Random number generator seed.
    )
    return pop
Example #2
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
Example #3
0
    def test_multi_neat(self):
        params = NEAT.Parameters()
        params.PopulationSize = 100
        genome = NEAT.Genome(
            0,  # ID
            3,  # number of inputs. Note: always add one extra input, for bias
            0,  # number of hidden nodes
            2,  # number of outputs
            False,  # FS_NEAT; auto-determine an appropriate set of inputs for the evolved networks
            NEAT.ActivationFunction.UNSIGNED_SIGMOID,  # OutputActType
            NEAT.ActivationFunction.UNSIGNED_SIGMOID,  # HiddenActType
            0,  # SeedType
            params  # Parameters
        )
        seed = 42
        pop = NEAT.Population(
            genome,
            params,
            True,  # whether the population should be randomized
            1.0,  # how much the population should be randomized,
            seed)

        for generation in range(3):
            # retrieve a list of all genomes in the population
            genome_list = NEAT.GetGenomeList(pop)

            # apply the evaluation function to all genomes
            for genome in genome_list:
                fitness = self.evaluate(genome)
                genome.SetFitness(fitness)

            # advance to the next generation
            pop.Epoch()
Example #4
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(i)

    generations = 0
    for generation in range(1000):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = NEAT.EvaluateGenomeList_Serial(genome_list,
                                                      evaluate,
                                                      display=False)
        NEAT.ZipFitness(genome_list, fitness_list)

        best = max([x.GetLeader().GetFitness() for x in pop.Species])

        pop.Epoch()

        generations = generation
        if best > 15.0:
            break

    return generations
Example #5
0
def getbest(i):
    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, i)
    pop.RNG.Seed(i)

    for generation in range(2000):
        genome_list = NEAT.GetGenomeList(pop)
        fitnesses = NEAT.EvaluateGenomeList_Serial(genome_list,
                                                   evaluate,
                                                   display=False)
        [
            genome.SetFitness(fitness)
            for genome, fitness in zip(genome_list, fitnesses)
        ]

        best = max([x.GetLeader().GetFitness() for x in pop.Species])

        pop.Epoch()
        generations = generation
        if best > 15.0:
            break

    return generations
Example #6
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()
Example #7
0
    def validator(self, NEAT_file):
        """ Validate a single run. 

        Args:
            NEAT_File: file for the NEAT genome
        """
        global man, quadruped

        # Initialize the manager to be unique to the process.
        man = ODEManager(near_callback,
                         stepsize=self.dt / self.n,
                         log_data=self.log_frames,
                         run_num=self.run_num)

        # Initialize the quadruped
        quadruped = Quadruped(man=man)

        # If logging the output, tell manager to write the body type, dimensions, and position to the logging file.
        if self.log_frames:
            man.log_world_setup(self.eval_time, ind_num=self.run_num)

        # Load in the best performing NEAT genome
        genome = NEAT.Genome(NEAT_file)
        self.current_network = NEAT.NeuralNetwork()
        if not self.hyperNEAT:
            genome.BuildPhenotype(self.current_network)
        else:
            genome.BuildHyperNEATPhenotype(self.current_network,
                                           self.substrate)

        fit = self.physics_only_simulation_validator()

        print(fit)
def evolve():
    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, 1)
    pop.RNG.Seed(int(time.clock()*100))

    generations = 0
    for generation in range(1000):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = []
        for genome in genome_list:
            fitness_list.append(evaluate(genome))
        NEAT.ZipFitness(genome_list, fitness_list)
        pop.Epoch()
        generations = generation
        best = max(fitness_list)
        bestG = pop.GetBestGenome()
        plot_nn(bestG)
        plt.pause(0.001)
        plt.ion()
        plt.show(block=False)
        print("Mejor fitness [",generation,"]: ",best)
        if best > 15.9:
            break

    return generations
Example #9
0
def getbest():

    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)

    generations = 0
    for generation in range(1000):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = NEAT.EvaluateGenomeList_Serial(genome_list,
                                                      evaluate,
                                                      display=False)
        NEAT.ZipFitness(genome_list, fitness_list)

        best = max([x.GetLeader().GetFitness() for x in pop.Species])
        #        print 'Best fitness:', best, 'Species:', len(pop.Species)

        # test
        net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().BuildPhenotype(net)
        img = np.zeros((250, 250, 3), dtype=np.uint8)
        img += 10
        NEAT.DrawPhenotype(img, (0, 0, 250, 250), net)

        cv2.imshow("nn_win", img)
        cv2.waitKey(1)

        pop.Epoch()
        #        print "Generation:", generation
        generations = generation
        if best > 15.5:
            break

    return generations
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
Example #11
0
def evolve():

    global generations
    global global_best
    global rrse_list
    global mae_list
    global rows
    global cols

    # print("LOO Validation:")
    g = NEAT.Genome(0, (cols - 1), (3), 1, False,
                    NEAT.ActivationFunction.LINEAR,
                    NEAT.ActivationFunction.LINEAR, 1, params, 1)
    for test_idx in range(rows):
        pop = NEAT.Population(g, params, True, 1.0, 0)
        pop.RNG.Seed(int(time.clock() * 100))
        generations = 0
        global_best = -99999999
        no_improvement = 0
        # Run for a maximum of N generations
        while no_improvement < 7 and generations < 100:  #TODO: make max gens into variable and set via command line
            # Reset the population if this path does not seem promising
            if (generations > 7 and global_best < -150):
                pop = NEAT.Population(g, params, True, 1.0, 0)
                pop.RNG.Seed(int(time.clock() * 100))
                generations = 0
                global_best = -99999999
                no_improvement = 0

            genome_list = NEAT.GetGenomeList(pop)
            fitness_list = []
            for genome in genome_list:
                fitness_list.append(evaluate(genome, test_idx))
            NEAT.ZipFitness(genome_list, fitness_list)
            pop.Epoch()
            generations += 1
            best = max(fitness_list)
            #print("[ROW:", test_idx, "] ", -global_best, " (", no_improvement, " g. of no improvement)")
            if best > global_best:
                no_improvement = 0
                global_best = best
            else:
                no_improvement += 1

        #print("LOO test error (RRSE):")
        #print(rrse_list[test_idx])
        #print("LOO test error (MAE):")
        #print(mae_list[test_idx])

    print(rrse_list)
    print(mae_list)
    avg_rrse = np.mean(rrse_list)
    avg_mae = np.mean(mae_list)
    return [avg_rrse, avg_mae]
Example #12
0
def evolve(env_name, seed, params, max_evaluations, num_batches):
    env = gym.make(env_name)
    discrete_output = isinstance(env.action_space,
                                 gym.spaces.discrete.Discrete)

    train_network, test_network = configure_train_test(env_name, seed)

    if discrete_output:
        g = NEAT.Genome(0, len(np.reshape(env.observation_space.sample(),
                                          -1)), 0, env.action_space.n, False,
                        NEAT.ActivationFunction.LINEAR,
                        NEAT.ActivationFunction.RELU, 0, params)
    else:
        g = NEAT.Genome(0, len(np.reshape(env.observation_space.sample(), -1)),
                        0, 2 * len(np.reshape(env.action_space.sample(), -1)),
                        False, NEAT.ActivationFunction.LINEAR,
                        NEAT.ActivationFunction.RELU, 0, params)

    env.close()
    population = NEAT.Population(g, params, True, 1.0, seed)
    population.RNG.Seed(seed)

    run_neat = configure_neat(population, train_network)
    iterator = run_neat()

    generations_per_batch = max(
        (max_evaluations / num_batches) / params.PopulationSize, 1)

    current_best = None
    i = 0

    while i * params.PopulationSize < max_evaluations:
        for _ in xrange(generations_per_batch):
            generation, current_best = iterator.next()
            i += 1
            assert i == generation

        best = pickle.loads(current_best)
        results = test_network(best)
        neurons, connections = network_size(best)
        yield i * params.PopulationSize, results, neurons, connections
Example #13
0
def init_pop_new(pop_size, current_project):
    """ Create a new randomized population using the default parameters"""
    MN.Parameters.Load(params, para_file)
    params.PopulationSize = pop_size
    genome = MN.Genome(0, current_project.nn_in, current_project.nn_hidden,
                       current_project.nn_outs,
                       current_project.nn_start_minimal,
                       MN.ActivationFunction.UNSIGNED_SIGMOID,
                       MN.ActivationFunction.UNSIGNED_SIGMOID, 1, params, 0)
    pop = MN.Population(genome, params, True, 1.0,
                        random_seed)  # randomized for first gen
    return pop, pop_size
Example #14
0
    def init_neat(self):
        params = NEAT.Parameters()
        params.PopulationSize = self.args.population_size
        params.AllowClones = self.args.allow_clones
        params.MaxWeight = self.args.max_weight
        params.WeightMutationMaxPower = self.args.weight_mutation_max_power
        params.MutateWeightsSevereProb = self.args.mutate_weights_severe_prob
        params.WeightMutationRate = self.args.weight_mutation_rate
        params.InterspeciesCrossoverRate = self.args.interspecies_crossover_rate
        params.CrossoverRate = self.args.crossover_rate
        params.OverallMutationRate = self.args.mutation_rate
        params.RecurrentProb = 0.0
        params.RecurrentLoopProb = 0.0
        params.Elitism = self.args.elitism
        params.SurvivalRate = self.args.survival_rate
        num_inputs = len(self.neural_input_vectors[0])
        num_hidden_nodes = 0
        num_outputs = self.effect.num_parameters
        if self.args.neural_mode == 'targets':
            num_outputs *= self.num_frames
            params.MutateAddNeuronProb = 0.0
            params.MutateAddLinkProb = 0.0
            params.MutateRemLinkProb = 0.0
            params.MutateRemSimpleNeuronProb = 0.0
        else:
            params.MutateAddNeuronProb = self.args.add_neuron_probability
            params.MutateAddLinkProb = self.args.add_link_probability
            params.MutateRemLinkProb = self.args.remove_link_probability
            params.MutateRemSimpleNeuronProb = self.args.remove_simple_neuron_probability

        output_activation_function = NEAT.ActivationFunction.UNSIGNED_SIGMOID
        if self.args.output_activation_function == 'linear':
            output_activation_function = NEAT.ActivationFunction.LINEAR
        elif self.args.output_activation_function == 'sine':
            output_activation_function = NEAT.ActivationFunction.UNSIGNED_SINE

        genome = NEAT.Genome(
            0,  # ID
            num_inputs,
            num_hidden_nodes,
            num_outputs,
            self.args.fs_neat,
            output_activation_function,  # OutputActType
            NEAT.ActivationFunction.TANH,  # HiddenActType
            0,  # SeedType
            params  # Parameters
        )
        self.population = NEAT.Population(
            genome,
            params,
            True,  # whether the population should be randomized
            2.0,  # how much the population should be randomized,
            self.seed)
Example #15
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
Example #16
0
def optimize_neat(config, n_inputs, n_hidden, n_outputs, out_dir):
    print('Starting Optimization')
    params = NEAT.Parameters()
    params.PopulationSize = 60
    params.OldAgeTreshold = 10
    params.SpeciesMaxStagnation = 20
    params.AllowLoops = False

    for i in range(config['n_morphogens']):
        addTrait(params, 'K%i' % i, (.03, .08))
        addTrait(params, 'F%i' % i, (.01, .06))
        addTrait(params, 'diffU%i' % i, (.005, .02))
        addTrait(params, 'diffV%i' % i, (.0025, .01))

    ######################## Create NEAT objects ###############################
    fs_neat = False
    seed_type = 0
    out_type = NEAT.ActivationFunction.UNSIGNED_SIGMOID
    hidden_type = NEAT.ActivationFunction.UNSIGNED_SIGMOID
    genome_prototye = NEAT.Genome(0, n_inputs, n_hidden, n_outputs, fs_neat, \
                                  out_type, hidden_type, seed_type, params, 0)
    rand_seed = int(time.time())
    pop = NEAT.Population(genome_prototye, params, True, 1.0, rand_seed)

    ######################## Main evolution loop ###############################
    top_fitness = 0  # Fitness function is defined in [0, 1]
    top_grid = None

    for generation in range(config['generations']):
        print('Starting generation', generation)
        genomes = NEAT.GetGenomeList(pop)
        fitness_list = [simulate_genome(g, config)[0] for g in genomes]
        NEAT.ZipFitness(genomes, fitness_list)
        max_fitness = max(fitness_list)

        print('Generation complete')
        print('Max fitness', max_fitness)
        print('Mean fitness', np.mean(fitness_list))

        if max_fitness > top_fitness:
            print('New best fitness')
            best_genome = genomes[fitness_list.index(max_fitness)]
            _, best_grid = simulate_genome(best_genome, config)

            top_fitness = max_fitness
            top_grid = best_grid

            np.save(out_dir + '/grid_%i' % generation, best_grid)
            best_genome.Save(out_dir + '/genome_%i' % generation)

        pop.Epoch()
        print()
Example #17
0
def create_dummy_stuff(mult=1.0):
        
        params = NEAT.Parameters()
        params.PopulationSize = 500
        params.DynamicCompatibility = True
        params.WeightDiffCoeff = 4.0
        params.CompatTreshold = 2.0
        params.YoungAgeTreshold = 15
        params.SpeciesMaxStagnation = 15
        params.OldAgeTreshold = 35
        params.MinSpecies = 5
        params.MaxSpecies = 25
        params.RouletteWheelSelection = False
        params.RecurrentProb = 0.0
        params.OverallMutationRate = 0.5 * mult

        params.MutateWeightsProb = 0.8

        params.WeightMutationMaxPower = 1.5 *mult
        params.WeightReplacementMaxPower = 2.0
        params.MutateWeightsSevereProb = 0.2 *mult
        params.WeightMutationRate = 0.2 *mult

        params.MaxWeight = 8

        params.MutateAddNeuronProb = 0.03
        params.MutateAddLinkProb = 0.05
        params.MutateRemLinkProb = 0.0

        params.MinActivationA  = 4.9
        params.MaxActivationA  = 4.9

        params.ActivationFunction_SignedSigmoid_Prob = 0.0
        params.ActivationFunction_UnsignedSigmoid_Prob = 1.0
        params.ActivationFunction_Tanh_Prob = 0.0
        params.ActivationFunction_SignedStep_Prob = 0.0

        params.CrossoverRate = 0.75  # mutate only 0.25
        params.MultipointCrossoverRate = 0.4
        params.SurvivalRate = 0.2
        
        g= NEAT.Genome(0, 3, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID, NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params)
        seed = 1032
        pop = NEAT.Population(g, params, True, 1.0, seed)

        species = pop.Species[0]
        RNG=pop.RNG
        return pop,species,RNG,params
Example #18
0
def getbest(run):
    g = NEAT.Genome(0, 7, 1, True, NEAT.ActivationFunction.SIGNED_SIGMOID,
                    NEAT.ActivationFunction.SIGNED_SIGMOID, params)

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

        fitnesses = NEAT.EvaluateGenomeList_Serial(genome_list,
                                                   evaluate_xor,
                                                   display=False)
        [
            genome.SetFitness(fitness)
            for genome, fitness in zip(genome_list, 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().Build_ES_Phenotype(net, substrate, params)
        img = np.zeros((500, 500, 3), dtype=np.uint8)
        img += 10

        Utilities.DrawPhenotype(img, (0, 0, 500, 500), net, substrate=True )
        cv2.imshow("NN", img)
        cv2.waitKey(1)
        '''
        if max([x.GetLeader().GetFitness() for x in pop.Species]) > 15.0:
            break

        # Epoch
        generations = generation
        pop.Epoch()

    return generations
Example #19
0
def getbest():
    g = NEAT.Genome(0, substrate.GetMinCPPNInputs(), 0,
                    substrate.GetMinCPPNOutputs(), False,
                    NEAT.ActivationFunction.SIGNED_GAUSS,
                    NEAT.ActivationFunction.SIGNED_GAUSS, 0, params)

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

    for generation in range(1000):
        genome_list = NEAT.GetGenomeList(pop)
        #    fitnesses = NEAT.EvaluateGenomeList_Parallel(genome_list, evaluate)
        fitnesses = NEAT.EvaluateGenomeList_Serial(genome_list,
                                                   evaluate,
                                                   display=False)
        [
            genome.SetFitness(fitness)
            for genome, fitness in zip(genome_list, fitnesses)
        ]

        best = max([x.GetLeader().GetFitness() for x in pop.Species])
        #        print 'Best fitness:', best

        # test
        net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().BuildPhenotype(net)
        img = np.zeros((250, 250, 3), dtype=np.uint8)
        img += 10
        NEAT.DrawPhenotype(img, (0, 0, 250, 250), net)
        cv2.imshow("CPPN", img)

        net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().BuildHyperNEATPhenotype(net, substrate)
        img = np.zeros((250, 250, 3), dtype=np.uint8)
        img += 10
        NEAT.DrawPhenotype(img, (0, 0, 250, 250), net, substrate=True)
        cv2.imshow("NN", img)

        cv2.waitKey(1)

        pop.Epoch()
        #        print "Generation:", generation
        generations = generation
        if best > 15.5:
            break

    return generations
Example #20
0
def validator(NEAT_file, Morph_file, log_frames, run_num, eval_time, dt, n,
              num_joints, output_path, gennumber, eucquad):

    genome = [0, {}]

    # Load in the best performing NEAT genome
    genome[0] = NEAT.Genome(NEAT_file)

    # Load the morphology
    with open(Morph_file, "r") as f:
        line = f.readline()
        line = line.strip().split(',')
        genome[1][0] = float(line[0])  # read frequency

    GlobalVarWorkaround.man = ODEManager(near_callback,
                                         stepsize=dt / n,
                                         log_data=log_frames,
                                         fluid_dynamics=1,
                                         gravity=0,
                                         run_num=run_num)
    GlobalVarWorkaround.worm = Worm(man=GlobalVarWorkaround.man,
                                    morphology_genome=genome[1],
                                    num_joints=num_joints,
                                    logging=log_frames,
                                    log_path=output_path,
                                    logfileprefix='rep' + str(run_num) +
                                    '_gen' + str(gennumber))
    GlobalVarWorkaround.foodenv = FoodEnvironment(man=GlobalVarWorkaround.man,
                                                  square_size=40,
                                                  eucquadrant=eucquad)

    # If logging the output, tell manager to write the body type, dimensions, and position to the logging file.
    # Must be placed after creating the quadruped.
    if log_frames:
        GlobalVarWorkaround.man.log_world_setup(eval_time, ind_num=run_num)

    simulation = Simulation(eval_time,
                            dt=dt,
                            n=n,
                            man=GlobalVarWorkaround.man,
                            worm=GlobalVarWorkaround.worm)
    #simulation.current_network = NEAT.NeuralNetwork()
    #genome[0].BuildPhenotype(simulation.current_network)

    fit = simulation.evaluate_individual(genome)
    print(fit)
Example #21
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():
    g = NEAT.Genome(0, 7, 1, False, NEAT.ActivationFunction.SIGNED_SIGMOID,
                    NEAT.ActivationFunction.SIGNED_SIGMOID, params)

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

    for generation in range(2000):

        genome_list = NEAT.GetGenomeList(pop)
        #    fitnesses = NEAT.EvaluateGenomeList_Parallel(genome_list, evaluate)
        fitnesses = NEAT.EvaluateGenomeList_Serial(genome_list,
                                                   evaluate_xor,
                                                   display=True)
        [
            genome.SetFitness(fitness)
            for genome, fitness in zip(genome_list, fitnesses)
        ]

        best = max([x.GetLeader().GetFitness() for x in pop.Species])

        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)

        net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().Build_ES_Phenotype(net, substrate, params)
        img = np.zeros((500, 500, 3), dtype=np.uint8)
        img += 10

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

        generations = generation

        if best > 15.0:
            break

        pop.Epoch()

    return generations
Example #23
0
def getbest():

    g = NEAT.Genome(0, 3, 2, 1, False,
                    NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                    NEAT.ActivationFunction.UNSIGNED_SIGMOID, 1, params)
    pop = NEAT.Population(g, params, True, 1.0)

    pool = mpc.Pool(processes=4)

    generations = 0
    for generation in range(1000):
        genome_list = []
        for s in pop.Species:
            for i in s.Individuals:
                genome_list.append(i)

    #    for g in genome_list:
    #        f = evaluate(g)
    #        g.SetFitness(f)

    # Parallel processing
        fits = pool.map(evaluate, genome_list)
        for f, g in zip(fits, genome_list):
            g.SetFitness(f)

        best = max([x.GetLeader().GetFitness() for x in pop.Species])
        #        print 'Best fitness:', best, 'Species:', len(pop.Species)

        # test
        net = NEAT.NeuralNetwork()
        pop.Species[0].GetLeader().BuildPhenotype(net)
        img = np.zeros((250, 250, 3), dtype=np.uint8)
        img += 10
        NEAT.DrawPhenotype(img, (0, 0, 250, 250), net)
        cv2.imshow("nn_win", img)
        cv2.waitKey(1)

        pop.Epoch()
        #        print "Generation:", generation
        generations = generation
        if best > 15.5:
            break

    return generations
def create_robot(maze_env, seed):
    """
    The function to create population of robots.
    """
    params = create_robot_params()
    # Genome has 11 inputs and two outputs
    genome = NEAT.Genome(0, 11, 0, 2, False,
                         NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                         NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params,
                         0)
    pop = NEAT.Population(genome, params, True, 1.0, seed)
    pop.RNG.Seed(seed)

    robot_archive = archive.NoveltyArchive(metric=maze.maze_novelty_metric)
    robot = Robot(maze_env=maze_env,
                  archive=robot_archive,
                  genome=genome,
                  population=pop)
    return robot
    def validator(self,NEAT_file,Morph_file):
        """ Validate a single run. 

        Args:
            NEAT_File: file for the NEAT genome
            Morph_file: file for the morphology components
        """
        global man, quadruped

        self.validating = True

        # Initialize the manager to be unique to the process.
        man = ODEManager(near_callback, stepsize=self.dt/self.n, log_data=self.log_frames, run_num=self.run_num,erp=0.5,cfm=1E-2)

        genome = [0,{}]
        if NEAT_file:
            # Load in the best performing NEAT genome
            genome[0] = NEAT.Genome(NEAT_file)
            self.current_network = NEAT.NeuralNetwork()
            if not self.hyperNEAT:
                genome[0].BuildPhenotype(self.current_network)
            else:
                genome[0].BuildHyperNEATPhenotype(self.current_network,self.substrate)

        if Morph_file:
            with open(Morph_file,"r") as f:
                line = f.readline()
                line = line.strip().split(',')
                genome[1]['erp'] = float(line[0])
                genome[1]['cfm'] = float(line[1])

        # Initialize the quadruped
        quadruped = Quadruped(man=man,morphology_genome={'erp':genome[1]['erp'],'cfm':genome[1]['cfm']})

        # If logging the output, tell manager to write the body type, dimensions, and position to the logging file.
        # Must be placed after creating the quadruped.
        if self.log_frames:
            man.log_world_setup(self.eval_time,ind_num=self.run_num)

        fit = self.physics_only_simulation_validator()

        return self.ann_activations,self.joint_feedback
Example #26
0
File: fool.py Project: yazici/fobj
def objective_driven(seed):
    i = seed
    g = NEAT.Genome(0, 6, 0, 4, False, NEAT.ActivationFunction.SIGNED_SIGMOID,
                    NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params)
    pop = NEAT.Population(g, params, True, 1.0, i)
    #pop.RNG.Seed(i)

    generations = 0
    for generation in range(250):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = NEAT.EvaluateGenomeList_Serial(genome_list,
                                                      evaluate,
                                                      display=False)
        fitness_list = [k[0] for k in fitness_list]
        NEAT.ZipFitness(genome_list, fitness_list)

        best_fits = [x.GetLeader().GetFitness() for x in pop.Species]
        best = max(best_fits)
        idx = best_fits.index(best)
        print best, pop.Species[idx].GetLeader().GetFitness()
        imgs, res = evaluate(pop.Species[idx].GetLeader(),
                             debug=True,
                             save="gen%d.ply" % generation)

        plt.ion()
        plt.clf()
        subfig = 1
        t_imgs = len(imgs)
        for img in imgs:
            plt.subplot(t_imgs, 1, subfig)
            plt.title("Confidence: %0.2f%%" %
                      (res[subfig - 1, target_class] * 100.0))
            plt.imshow(img)
            subfig += 1
        plt.draw()
        plt.pause(0.1)
        plt.savefig("out%d.png" % generation)
        pop.Epoch()

        generations = generation

    return generations
Example #27
0
    def __init__(self,
                 population_size=100,
                 input_size=9,
                 output_size=1,
                 generations=50):
        """Initialize the trainer.

        Keyword arguments:
        population_size -- number of genomes per generation (default 100)
        input_size -- size of the input state + 1 (default 9)
        output_size -- size of the result of the genome neural networks (default 1)
        generations -- number of generations (default 50)
        """
        self.generations = generations
        self.params = NEAT.Parameters()
        self.params.PopulationSize = population_size
        genome = NEAT.Genome(0, input_size, 0, output_size, False,
                             NEAT.ActivationFunction.UNSIGNED_SIGMOID,
                             NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0,
                             self.params, 0)
        self.population = NEAT.Population(genome, self.params, True, 1.0, 0)
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
Example #29
0
def evolve():
    g = NEAT.Genome(0, 2, 0, 1, False, NEAT.ActivationFunction.LINEAR,
                    NEAT.ActivationFunction.LINEAR, 0, params, 0)
    pop = NEAT.Population(g, params, True, 1.0, 1)
    pop.RNG.Seed(int(time.clock()*100))

    generations = 0
    for generation in range(50):
        genome_list = NEAT.GetGenomeList(pop)
        fitness_list = []
        for genome in genome_list:
            fitness_list.append(evaluate(genome))
        NEAT.ZipFitness(genome_list, fitness_list)
        pop.Epoch()
        generations = generation
        best = -max(fitness_list)
        bestG = pop.GetBestGenome()
        
        plot_nn(bestG)
        plt.pause(0.01)
        plt.ion()
        plt.show(block=False)
        
        print("Mejor fitness [",generation,"]: ",best)
        if best < 0.01:
            break

    testNet = NEAT.NeuralNetwork()
    bestG.BuildPhenotype(testNet)
    for i in range(10):
        testNet.Flush()
        testNet.Input(np.array([float(100+2*i), 1]))
        for _ in range(2):
            testNet.Activate()
        o = testNet.Output()
        print(100+2*i,"/ 2 = ",o[0]) 
        
    return generations
Example #30
0
def hillclimb(g,params,evals,evaluate,seed=1):
    pop = NEAT.Population(g, params, True, 1.0, seed)
    pop.RNG.Seed(seed)

    species = pop.Species[0]
    champ = g
    c_fitness,beh = evaluate(champ)
    champ.SetFitness(c_fitness)
    champ.SetEvaluated()

    for x in xrange(evals):
     baby = NEAT.Genome(champ) #copy.copy(champ)
     species.MutateGenome(False,pop,baby,params,pop.RNG)
     b_fitness,beh = evaluate(baby)
     #print b_fitness, evaluate(champ)
     if b_fitness > c_fitness:
      #print b_fitness,evaluate(champ)
      c_fitness = b_fitness
      champ.Destroy() 
      champ = baby   
     else:
       baby.Destroy()
    return champ,c_fitness