Esempio n. 1
0
def run():
    # 128x128 thumbnails, 1500x1500 rendered images, 1100x810 viewer, grayscale images.
    pb = PictureBreeder(128, 128, 1500, 1500, 1100, 810, 'gray', 4)
    cfg = config.Config('config')

    # Make sure the network has the expected number of outputs.
    if pb.scheme == 'color':
        cfg.output_nodes = 3
    else:
        cfg.output_nodes = 1

    cfg.pop_size = pb.num_cols * pb.num_rows
    pop = population.Population(cfg)
    while 1:
        pop.run(pb.eval_fitness, 1)
Esempio n. 2
0
def run():
    cfg = config.Config('novelty_config')
    cfg.pop_size = 100
    cfg.max_fitness_threshold = 1e38

    ne = NoveltyEvaluator(4, 'gray')
    if ne.scheme == 'color':
        cfg.output_nodes = 3
    else:
        cfg.output_nodes = 1

    pop = population.Population(cfg)

    while 1:
        pop.run(ne.evaluate, 1)

        winner = pop.statistics.best_genome()
        if ne.scheme == 'gray':
            image = eval_gray_image(winner, full_scale * width,
                                    full_scale * height)
        elif ne.scheme == 'color':
            image = eval_color_image(winner, full_scale * width,
                                     full_scale * height)
        elif ne.scheme == 'mono':
            image = eval_mono_image(winner, full_scale * width,
                                    full_scale * height)
        else:
            raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme))

        im = np.clip(np.array(image), 0, 255).astype(np.uint8)
        im = ne.image_from_array(im)
        im.save('winning-novelty-{0:06d}.png'.format(pop.generation))

        if ne.scheme == 'gray':
            image = eval_gray_image(winner, width, height)
        elif ne.scheme == 'color':
            image = eval_color_image(winner, width, height)
        elif ne.scheme == 'mono':
            image = eval_mono_image(winner, width, height)
        else:
            raise Exception('Unexpected scheme: {0!r}'.format(ne.scheme))

        float_image = np.array(image, dtype=np.float32) / 255.0
        ne.archive.append(float_image)
Esempio n. 3
0
def main():
    global game_sequence, min_weight, max_weight
    game_path = os.path.join(os.path.join(os.getcwd(), "Games"), "FlappyBird")
    # genome_path = os.path.join(game_path, "genomes")
    config_path = os.path.join(game_path, 'flappy_config')
    pop_config = config.Config(config_path)
    pop_config.save_best = True
    min_weight, max_weight = pop_config.min_weight, pop_config.max_weight
    try:
        init_population = []
        pop_size = pop_config.pop_size

        genome_files = sorted(
            [f for f in os.listdir() if f.startswith("best_genome_")],
            key=lambda x: int(x.split("_")[-1]))
        for filename in genome_files[-pop_size:]:  # get best ones
            with open(os.path.join(os.getcwd(), filename), "rb") as f:
                genome = pickle.load(f)
            genome.ID = str(len(init_population))
            init_population.append(genome)
        best_genome = init_population[-1]
        for i in range(
                len(init_population), pop_size
        ):  # Fill rest with best genome if there is not 50 genomes
            new_genome = copy.deepcopy(best_genome)
            new_genome.ID = str(i)
            init_population.append(new_genome)
        gen_number = int(genome_files[-1].split("_")
                         [-1])  # in order not to override previous generations
    except IndexError:
        init_population = None
        gen_number = -1
    pop = population.Population(pop_config, init_population)
    pop.generation = gen_number

    game_sequence = Sequence(speed, FlappyBirdAPI(True), None,
                             "flappybirdneat")

    pop.run(flappy_fitness, 200)
Esempio n. 4
0
from ReverseEncodingTree.utils.operator import Operator

has_environment = False
for environment_space in gym.envs.registry.all():
    if "LunarLander-v2" in environment_space.id:
        has_environment = True
        break

if not has_environment:
    raise Exception("no environment named LunarLander-v2.")

environment = gym.make("LunarLander-v2")

if __name__ == '__main__':
    task_config = config.Config(genome.DefaultGenome, reproduction.DefaultReproduction,
                                species.DefaultSpeciesSet, stagnation.DefaultStagnation,
                                "../configures/example/lunar-lander-v2")

    # load evolution process.
    fitter = FitDevice(FitProcess())

    fitter.set_environment(environment=environment, episode_steps=300, episode_generation=5,
                           input_type=TypeCorrect.List, output_type=TypeCorrect.Value)

    # initialize the NeuroEvolution
    operator = Operator(config=task_config, fitter=fitter,
                        node_names={-1: '1', -2: '2', -3: '3', -4: '4',
                                    -5: '5', -6: '6', -7: '7', -8: '8',
                                    0: 'fire engine'},
                        max_generation=1000, output_path="../output/")
Esempio n. 5
0
        fit = 0
        tot = 0.0
        net = nn.create_recurrent_phenotype(g)
        with open("match_prepared.dat", 'r') as f, open("match_out_prepared.dat", 'r') as f2:
            for line_tr, line_te in itertools.izip(f, f2):
                if tot > 14000:
                    break
                res = int(net.activate([float(num) for num in line_tr.strip().split()])[0])
                if res == int(line_te.strip()):
                    fit += 1
                tot += 1
        g.fitness = fit/tot
        print "Correct guesses {0}, in total of {1} matches.\n".format(str(fit), str(tot))


conf = config.Config('flappy_config')
conf.save_best = True

try:
    init_population = []
    pop_size = conf.pop_size

    genome_files = sorted([f for f in os.listdir('.') if f.startswith("best_genome_")],
                          key=lambda x: int(x.split("_")[-1]))
    for filename in genome_files[-pop_size:]:  # get best ones
        with open(os.path.join(os.getcwd(), filename), "rb") as f:
            genome = pickle.load(f)
        genome.ID = str(len(init_population))
        init_population.append(genome)
    best_genome = init_population[-1]
    for i in range(len(init_population), pop_size):  # Fill rest with best genome if there is not 50 genomes
has_environment = False
for environment_space in gym.envs.registry.all():
    if "CartPole-v0" in environment_space.id:
        has_environment = True
        break

if not has_environment:
    raise Exception("no environment named CartPole-v0.")

environment = gym.make("CartPole-v0").unwrapped

if __name__ == '__main__':
    # load configuration.
    task_config = config.Config(genome.DefaultGenome,
                                reproduction.DefaultReproduction,
                                species.DefaultSpeciesSet,
                                stagnation.DefaultStagnation,
                                "../configures/example/cart-pole-v0")

    # load evolution process.
    fitter = FitDevice(FitProcess())
    fitter.set_environment(environment=environment,
                           episode_steps=300,
                           episode_generation=10,
                           input_type=TypeCorrect.List,
                           output_type=TypeCorrect.Value)

    # initialize the NeuroEvolution
    operator = Operator(config=task_config,
                        fitter=fitter,
                        node_names={
Esempio n. 7
0
    def __init__(self,
                 method_type,
                 logic_type,
                 max_generation,
                 display_results=False,
                 checkpoint=-1,
                 stdout=False):
        """
        initialize the logical task.

        :param method_type: the evolution strategy, FS-NEAT, Bi-NEAT, or GS-NEAT.
        :param logic_type: the task type, IMPLY, NAND, NOR, or XOR.
        :param max_generation: maximum generation of the evolution strategy,
                               if the generation exceeds the maximum, it will be terminated.
        :param display_results: whether result visualization is required.
        :param checkpoint: check the statistics point.
        :param stdout: Whether outputting the genome information in the process is required.
        """

        data_inputs = None
        data_outputs = None

        if logic_type == LogicType.NAND:
            data_inputs = [(0.0, 0.0), (0.0, 1.0), (1.0, 0.0), (1.0, 1.0)]
            data_outputs = [(1.0, ), (1.0, ), (1.0, ), (0.0, )]
            self.filename = "nand."
        elif logic_type == LogicType.NOR:
            data_inputs = [(0.0, 0.0), (0.0, 1.0), (1.0, 0.0), (1.0, 1.0)]
            data_outputs = [(0.0, ), (0.0, ), (0.0, ), (1.0, )]
            self.filename = "nor."
        elif logic_type == LogicType.IMPLY:
            data_inputs = [(0.0, 0.0), (0.0, 1.0), (1.0, 0.0), (1.0, 1.0)]
            data_outputs = [(1.0, ), (1.0, ), (0.0, ), (1.0, )]
            self.filename = "imply."
        elif logic_type == LogicType.XOR:
            data_inputs = [(0.0, 0.0), (0.0, 1.0), (1.0, 0.0), (1.0, 1.0)]
            data_outputs = [(0.0, ), (1.0, ), (1.0, ), (0.0, )]
            self.filename = "xor."

        # load evolution process.
        fitter = FitDevice(
            FitProcess(init_fitness=4, eval_type=EvalType.ManhattanDistance))
        fitter.set_dataset({"i": data_inputs, "o": data_outputs})

        # load configuration.
        task_config = None
        if method_type == MethodType.N:
            task_config = config.Config(genome.DefaultGenome,
                                        reproduction.DefaultReproduction,
                                        species.DefaultSpeciesSet,
                                        stagnation.DefaultStagnation,
                                        "../configures/task/logic.n")
            self.filename += "fs"
        elif method_type == MethodType.FS:
            task_config = config.Config(genome.DefaultGenome,
                                        reproduction.DefaultReproduction,
                                        species.DefaultSpeciesSet,
                                        stagnation.DefaultStagnation,
                                        "../configures/task/logic.fs")
            self.filename += "fs"
        elif method_type == MethodType.BI:
            task_config = config.Config(autogenome.GlobalGenome,
                                        bi.Reproduction,
                                        autospecies.StrongSpeciesSet,
                                        stagnation.DefaultStagnation,
                                        "../configures/task/logic.bi")
            self.filename += "bi"
        elif method_type == MethodType.GS:
            task_config = config.Config(autogenome.GlobalGenome,
                                        gs.Reproduction,
                                        autospecies.StrongSpeciesSet,
                                        stagnation.DefaultStagnation,
                                        "../configures/task/logic.gs")
            self.filename += "gs"

        # initialize the NeuroEvolution
        self.operator = Operator(config=task_config,
                                 fitter=fitter,
                                 node_names={
                                     -1: 'A',
                                     -2: 'B',
                                     0: 'A operate B'
                                 },
                                 max_generation=max_generation,
                                 checkpoint_value=checkpoint,
                                 stdout=stdout,
                                 output_path="../output/")

        # set whether display results
        self.display_results = display_results

        self.max_generation = max_generation
Esempio n. 8
0
    def __init__(self,
                 method_type,
                 game_type,
                 episode_steps,
                 episode_generation,
                 max_generation,
                 attacker=None,
                 noise_level=-1,
                 display_results=False,
                 checkpoint=-1,
                 stdout=False):
        """
        initialize the game task.

        :param method_type: the evolution strategy, FS-NEAT, Bi-NEAT, or GS-NEAT.
        :param game_type: the task type, CartPole_v0 or LunarLander_v2.
        :param episode_steps: step parameter of game task
        :param episode_generation:  generation parameter of game task.
        :param max_generation: maximum generation of the evolution strategy,
                               if the generation exceeds the maximum, it will be terminated.
        :param attacker: noise attacker, see evolution/bean/attacker.py.
        :param noise_level: noise level.
        :param display_results: whether result visualization is required.
        :param checkpoint: check the statistics point.
        :param stdout: Whether outputting the genome information in the process is required.
        """

        game_environment = None
        if game_type == GameType.CartPole_v0:
            game_environment = gym.make("CartPole-v0").unwrapped
            self.filename = "cart-pole-v0."
            self.node_name = {
                -1: 'In0',
                -2: 'In1',
                -3: 'In3',
                -4: 'In4',
                0: 'act1',
                1: 'act2'
            }
        elif game_type == GameType.LunarLander_v2:
            game_environment = gym.make("LunarLander-v2")
            self.filename = "lunar-lander-v2."
            self.node_name = {
                -1: '1',
                -2: '2',
                -3: '3',
                -4: '4',
                -5: '5',
                -6: '6',
                -7: '7',
                -8: '8',
                0: 'fire engine'
            }

        fitter = FitDevice(FitProcess())
        fitter.set_environment(environment=game_environment,
                               input_type=TypeCorrect.List,
                               output_type=TypeCorrect.Value,
                               episode_steps=episode_steps,
                               episode_generation=episode_generation,
                               attacker=attacker,
                               noise_level=noise_level)
        # load configuration.
        task_config = None
        if method_type == MethodType.N:
            self.filename += "n"
            task_config = config.Config(genome.DefaultGenome,
                                        reproduction.DefaultReproduction,
                                        species.DefaultSpeciesSet,
                                        stagnation.DefaultStagnation,
                                        "../configures/task/" + self.filename)
        elif method_type == MethodType.FS:
            self.filename += "fs"
            task_config = config.Config(genome.DefaultGenome,
                                        reproduction.DefaultReproduction,
                                        species.DefaultSpeciesSet,
                                        stagnation.DefaultStagnation,
                                        "../configures/task/" + self.filename)
        elif method_type == MethodType.BI:
            self.filename += "bi"
            task_config = config.Config(autogenome.GlobalGenome,
                                        bi.Reproduction,
                                        autospecies.StrongSpeciesSet,
                                        stagnation.DefaultStagnation,
                                        "../configures/task/" + self.filename)
        elif method_type == MethodType.GS:
            self.filename += "gs"
            task_config = config.Config(autogenome.GlobalGenome,
                                        gs.Reproduction,
                                        autospecies.StrongSpeciesSet,
                                        stagnation.DefaultStagnation,
                                        "../configures/task/" + self.filename)

        # initialize the NeuroEvolution
        self.operator = Operator(config=task_config,
                                 fitter=fitter,
                                 node_names=self.node_name,
                                 max_generation=max_generation,
                                 checkpoint_value=checkpoint,
                                 stdout=stdout,
                                 output_path="../output/")

        # set whether display results
        self.display_results = display_results

        self.max_generation = max_generation