def evaluate_individ_obj_function(genome, generation):
    """
    The function to evaluate individual objective function
    Arguments:
        genome:     The objective function genome
        generation: The current generation of evolution
    Returns:
        The NoveltyItem created using evaluation results.
    """
    # create NoveltyItem for genome and store it into map
    genome_id = genome.GetID()
    n_item = archive.NoveltyItem(generation=generation, genomeId=genome_id)
    # run the simulation
    multi_net = NEAT.NeuralNetwork()
    genome.BuildPhenotype(multi_net)
    depth = 2
    try:
        genome.CalculateDepth()
        depth = genome.GetDepth()
    except:
        pass
    obj_net = ANN(multi_net, depth=depth)

    # set inputs and get ouputs ([a, b])
    output = obj_net.activate([0.5])

    # store coefficients
    n_item.data.append(output[0])
    n_item.data.append(output[1])

    return n_item
def eval_individual(genome_id, genome, genomes, n_items_map, config):
    """
    Evaluates the individual represented by genome.
    Arguments:
        genome_id:      The ID of genome.
        genome:         The genome to evaluate.
        genomes:        The genomes population for current generation.
        n_items_map:    The map to hold novelty items for current generation.
        config:         The NEAT configuration holder.
    Return:
        The True if successful solver found.
    """
    # create NoveltyItem for genome and store it into map
    n_item = archive.NoveltyItem(generation=trial_sim.population.generation,
                                 genomeId=genome_id)
    n_items_map[genome_id] = n_item
    # run the simulation
    maze_env = copy.deepcopy(trial_sim.orig_maze_environment)
    control_net = neat.nn.FeedForwardNetwork.create(genome, config)
    goal_fitness = maze.maze_simulation_evaluate(env=maze_env,
                                                 net=control_net,
                                                 time_steps=SOLVER_TIME_STEPS,
                                                 n_item=n_item,
                                                 mcns=MCNS)

    if goal_fitness == -1:
        # The individual doesn't meet the minimal fitness criterion
        print("Individ with ID: %d marked for extiction, MCNS: %f" %
              (genome_id, MCNS))
        return False

    # Store simulation results into the agent record
    record = agent.AgentRecord(generation=trial_sim.population.generation,
                               agent_id=genome_id)
    record.fitness = goal_fitness
    record.x = maze_env.agent.location.x
    record.y = maze_env.agent.location.y
    record.hit_exit = maze_env.exit_found
    record.species_id = trial_sim.population.species \
        .get_species_id(genome_id)
    record.species_age = record.generation - \
        trial_sim.population.species.get_species(genome_id).created
    # add record to the store
    trial_sim.record_store.add_record(record)

    # Evaluate the novelty of a genome and add the novelty item to the archive of Novelty items if appropriate
    if not maze_env.exit_found:
        # evaluate genome novelty and add it to the archive if appropriate
        record.novelty = trial_sim.archive \
            .evaluate_individual_novelty(genome=genome, genomes=genomes,
                                        n_items_map=n_items_map)

    # update fittest organisms list
    trial_sim.archive.update_fittest_with_genome(genome=genome,
                                                 n_items_map=n_items_map)

    return maze_env.exit_found
def eval_individual(genome_id, genome, genomes, n_items_map, generation):
    """
    Evaluates the individual represented by genome.
    Arguments:
        genome_id:      The ID of genome.
        genome:         The genome to evaluate.
        genomes:        The genomes population for current generation.
        n_items_map:    The map to hold novelty items for current generation.
        generation:     The current generation.
    Return:
        The True if successful solver found.
    """
    # create NoveltyItem for genome and store it into map
    n_item = archive.NoveltyItem(generation=generation, genomeId=genome_id)
    n_items_map[genome_id] = n_item
    # run the simulation
    maze_env = copy.deepcopy(trial_sim.orig_maze_environment)
    multi_net = NEAT.NeuralNetwork()
    genome.BuildPhenotype(multi_net)
    control_net = ANN(multi_net)
    goal_fitness = maze.maze_simulation_evaluate(
                                        env=maze_env, 
                                        net=control_net, 
                                        time_steps=SOLVER_TIME_STEPS,
                                        n_item=n_item)

    # Store simulation results into the agent record
    record = agent.AgenRecord(generation=generation, agent_id=genome_id)
    record.fitness = goal_fitness
    record.x = maze_env.agent.location.x
    record.y = maze_env.agent.location.y
    record.hit_exit = maze_env.exit_found
    #record.species_id = trial_sim.population.species.get_species_id(genome_id)
    #record.species_age = record.generation - trial_sim.population.species.get_species(genome_id).created
    # add record to the store
    trial_sim.record_store.add_record(record)

    # Evaluate the novelty of a genome and add the novelty item to the archive of Novelty items if appropriate
    if not maze_env.exit_found:
        # evaluate genome novelty and add it to the archive if appropriate
        record.novelty = trial_sim.archive.evaluate_individual_novelty(genome=Genome(genome), 
                                                                        genomes=genomes, n_items_map=n_items_map)

    # update fittest organisms list
    trial_sim.archive.update_fittest_with_genome(genome=Genome(genome), n_items_map=n_items_map)

    return (maze_env.exit_found, goal_fitness)
def evaluate_individual_solution(genome, generation, robot):
    """
    The function to evaluate individual solution against maze environment.
    Arguments:
        genome:         The genome to evaluate.
        generation:     The current generation.
        robot:          The object encapsulating the robots population
    Return:
        The tuple specifying if solution was found and the distance from maze exit of final robot position.
    """
    # create NoveltyItem for genome and store it into map
    genome_id = genome.GetID()
    n_item = archive.NoveltyItem(generation=generation, genomeId=genome_id)
    # run the simulation
    maze_env = copy.deepcopy(robot.orig_maze_environment)
    multi_net = NEAT.NeuralNetwork()
    genome.BuildPhenotype(multi_net)
    depth = 8
    try:
        genome.CalculateDepth()
        depth = genome.GetDepth()
    except:
        pass
    control_net = ANN(multi_net, depth=depth)
    distance = maze.maze_simulation_evaluate(env=maze_env,
                                             net=control_net,
                                             time_steps=SOLVER_TIME_STEPS,
                                             n_item=n_item)

    # Store simulation results into the agent record
    record = agent.AgenRecord(generation=generation, agent_id=genome_id)
    record.distance = distance
    record.x = maze_env.agent.location.x
    record.y = maze_env.agent.location.y
    record.hit_exit = maze_env.exit_found
    record.species_id = robot.get_species_id(genome)
    robot.record_store.add_record(record)

    return (maze_env.exit_found, distance, n_item)