<reponame>Osrip/Novelty_criticality_PyTorch-NEAT
# Copyright (c) 2018 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.

import os
import ray
import click
import gym
# import neat
import neat_local as neat
import numpy as np

from pytorch_neat.multi_env_eval import MultiEnvEvaluator

from pytorch_neat.neat_reporter import LogReporter
from pytorch_neat.ising_net import IsingNet as AgentNet
# from pytorch_neat.recurrent_net import RecurrentNet as AgentNet

from gym.wrappers.monitoring.video_recorder import VideoRecorder
# import neat.config # works for some reason for local neat


# from os import listdir
# from os.path import isfile, join
# from os.path import dirname, basename, isfile, join
# import glob
# modules = glob.glob(join(dirname(__file__), "*.py"))
# modules = glob.glob('neat/*.py')
# modules = [f for f in listdir('neat/') if isfile(join('neat/', f)) and f.endswith('.py')]
# __all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]

max_env_steps = 2000
batch_size = 7


# def make_env():
#     return gym.make("CartPole-v0")

# register(
#     id='Acrobot-v1',
#     entry_point='gym.envs.classic_control:AcrobotEnv',
#     reward_threshold=-100.0,
#     max_episode_steps=500,
# )
def make_env():

    return gym.make("Acrobot-v1")



def make_net(genome, config, bs):
    return AgentNet.create(genome, config, bs)


def activate_net(net, states):
    outputs = net.activate(states).numpy()
    return outputs[:, 0] > 0.5

def activate_net_acrobot(net, states):
    outputs = net.activate(states).numpy()
    return [int(en) for en in np.sum(outputs, axis=1)/2]


def activate_net_acrobot_RNN(net, states):
    outputs = net.activate(states).numpy()
    outputs = np.round(outputs)
    return [int(en) for en in np.sum(outputs, axis=1)/2]

@click.command()
@click.option("--n_generations", type=int, default=100)
def run(n_generations):
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    config_path = os.path.join(os.path.dirname(__file__), "neat_acrobot.cfg")
    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        config_path,
    )

    evaluator = MultiEnvEvaluator(
        make_net, activate_net_acrobot_RNN, make_env=make_env, max_env_steps=max_env_steps, batch_size=batch_size
    )

    def eval_genomes(genomes, config, display = False):
        for i, (_, genome) in enumerate(genomes):
            # This line displays the first individual of each generation live
            display_env = i == 0 and display
            genome.fitness = evaluator.eval_genome(genome, config, display_env=display_env)

    def eval_genomes_parallel(genomes, config, display=False):
        ray.init()
        ray_funcs = [evaluate_parallel_helper.remote(
            genome, config, display_env=i == 0 and display) for i, (_, genome) in enumerate(genomes)]
        fitnesses = ray.get(ray_funcs)
        for fitness, (_, genome) in zip(fitnesses, genomes):
            genome.fitness = fitness
        ray.shutdown()

    @ray.remote
    def evaluate_parallel_helper(genome, config, display_env=False):
        fitness = evaluator.eval_genome(genome, config, display_env=display_env)
        return fitness

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    reporter = neat.StdOutReporter(True)
    pop.add_reporter(reporter)
    #  This evaluates the best genome of the generation!!!
    logger = LogReporter("neat.log", evaluator.eval_genome)
    pop.add_reporter(logger)

    # pop.run(eval_genomes, n_generations)
    pop.run(eval_genomes_parallel, n_generations)
    # pop.run(eval_genomes, None)



if __name__ == "__main__":


    run()  # pylint: disable=no-value-for-parameter

# Copyright (c) 2018 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.

import os
import ray
import click
import gym
# import neat
import neat_local as neat
import numpy as np

from pytorch_neat.multi_env_eval import MultiEnvEvaluator

from pytorch_neat.neat_reporter import LogReporter
from pytorch_neat.ising_net import IsingNet as AgentNet
# from pytorch_neat.recurrent_net import RecurrentNet as AgentNet

from gym.wrappers.monitoring.video_recorder import VideoRecorder
# import neat.config # works for some reason for local neat


# from os import listdir
# from os.path import isfile, join
# from os.path import dirname, basename, isfile, join
# import glob
# modules = glob.glob(join(dirname(__file__), "*.py"))
# modules = glob.glob('neat/*.py')
# modules = [f for f in listdir('neat/') if isfile(join('neat/', f)) and f.endswith('.py')]
# __all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]

max_env_steps = 2000
batch_size = 7


# def make_env():
#     return gym.make("CartPole-v0")

# register(
#     id='Acrobot-v1',
#     entry_point='gym.envs.classic_control:AcrobotEnv',
#     reward_threshold=-100.0,
#     max_episode_steps=500,
# )
def make_env():

    return gym.make("Acrobot-v1")



def make_net(genome, config, bs):
    return AgentNet.create(genome, config, bs)


def activate_net(net, states):
    outputs = net.activate(states).numpy()
    return outputs[:, 0] > 0.5

def activate_net_acrobot(net, states):
    outputs = net.activate(states).numpy()
    return [int(en) for en in np.sum(outputs, axis=1)/2]


def activate_net_acrobot_RNN(net, states):
    outputs = net.activate(states).numpy()
    outputs = np.round(outputs)
    return [int(en) for en in np.sum(outputs, axis=1)/2]

@click.command()
@click.option("--n_generations", type=int, default=100)
def run(n_generations):
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    config_path = os.path.join(os.path.dirname(__file__), "neat_acrobot.cfg")
    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        config_path,
    )

    evaluator = MultiEnvEvaluator(
        make_net, activate_net_acrobot_RNN, make_env=make_env, max_env_steps=max_env_steps, batch_size=batch_size
    )

    def eval_genomes(genomes, config, display = False):
        for i, (_, genome) in enumerate(genomes):
            # This line displays the first individual of each generation live
            display_env = i == 0 and display
            genome.fitness = evaluator.eval_genome(genome, config, display_env=display_env)

    def eval_genomes_parallel(genomes, config, display=False):
        ray.init()
        ray_funcs = [evaluate_parallel_helper.remote(
            genome, config, display_env=i == 0 and display) for i, (_, genome) in enumerate(genomes)]
        fitnesses = ray.get(ray_funcs)
        for fitness, (_, genome) in zip(fitnesses, genomes):
            genome.fitness = fitness
        ray.shutdown()

    @ray.remote
    def evaluate_parallel_helper(genome, config, display_env=False):
        fitness = evaluator.eval_genome(genome, config, display_env=display_env)
        return fitness

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    reporter = neat.StdOutReporter(True)
    pop.add_reporter(reporter)
    #  This evaluates the best genome of the generation!!!
    logger = LogReporter("neat.log", evaluator.eval_genome)
    pop.add_reporter(logger)

    # pop.run(eval_genomes, n_generations)
    pop.run(eval_genomes_parallel, n_generations)
    # pop.run(eval_genomes, None)



if __name__ == "__main__":


    run()  # pylint: disable=no-value-for-parameter
# Copyright (c) 2018 Uber Technologies, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #     http://www.apache.org/licenses/LICENSE-2.0 # #     Unless required by applicable law or agreed to in writing, software #     distributed under the License is distributed on an "AS IS" BASIS, #     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #     See the License for the specific language governing permissions and #     limitations under the License. # import neat # from pytorch_neat.recurrent_net import RecurrentNet as AgentNet # import neat.config # works for some reason for local neat # from os import listdir # from os.path import isfile, join # from os.path import dirname, basename, isfile, join # import glob # modules = glob.glob(join(dirname(__file__), "*.py")) # modules = glob.glob('neat/*.py') # modules = [f for f in listdir('neat/') if isfile(join('neat/', f)) and f.endswith('.py')] # __all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')] # def make_env(): #     return gym.make("CartPole-v0") # register( #     id='Acrobot-v1', #     entry_point='gym.envs.classic_control:AcrobotEnv', #     reward_threshold=-100.0, #     max_episode_steps=500, # ) # Load the config file, which is assumed to live in # the same directory as this script. # This line displays the first individual of each generation live #  This evaluates the best genome of the generation!!! # pop.run(eval_genomes, n_generations) # pop.run(eval_genomes, None) # pylint: disable=no-value-for-parameter