Example #1
0
def test_initialize_trainers(mock_communicator, mock_launcher):
    open_name = 'unitytrainers.trainer_controller' + '.open'
    with mock.patch('yaml.load') as mock_load:
        with mock.patch(open_name, create=True) as _:
            mock_communicator.return_value = MockCommunicator(
                discrete_action=True, visual_inputs=1)
            tc = TrainerController(' ', ' ', 1, None, True, True, False, 1, 1,
                                   1, 1, '', "tests/test_unitytrainers.py",
                                   False)

            # Test for PPO trainer
            mock_load.return_value = dummy_config
            config = tc._load_config()
            tf.reset_default_graph()
            with tf.Session() as sess:
                tc._initialize_trainers(config, sess)
                assert (len(tc.trainers) == 1)
                assert (isinstance(tc.trainers['RealFakeBrain'], PPOTrainer))

            # Test for Behavior Cloning Trainer
            mock_load.return_value = dummy_bc_config
            config = tc._load_config()
            tf.reset_default_graph()
            with tf.Session() as sess:
                tc._initialize_trainers(config, sess)
                assert (isinstance(tc.trainers['RealFakeBrain'],
                                   BehavioralCloningTrainer))

            # Test for proper exception when trainer name is incorrect
            mock_load.return_value = dummy_bad_config
            config = tc._load_config()
            tf.reset_default_graph()
            with tf.Session() as sess:
                with pytest.raises(UnityEnvironmentException):
                    tc._initialize_trainers(config, sess)
Example #2
0
def test_initialization():
    with mock.patch('subprocess.Popen'):
        with mock.patch('socket.socket') as mock_socket:
            with mock.patch('glob.glob') as mock_glob:
                mock_glob.return_value = ['FakeLaunchPath']
                mock_socket.return_value.accept.return_value = (mock_socket, 0)
                mock_socket.recv.return_value.decode.return_value = dummy_start
                tc = TrainerController(' ', ' ', 1, None, True, True, False, 1,
                                       1, 1, 1, '', "tests/test_unitytrainers.py")
                assert(tc.env.brain_names[0] == 'RealFakeBrain')
Example #3
0
def test_load_config(mock_communicator, mock_launcher):
    open_name = 'unitytrainers.trainer_controller' + '.open'
    with mock.patch('yaml.load') as mock_load:
        with mock.patch(open_name, create=True) as _:
            mock_load.return_value = dummy_config
            mock_communicator.return_value = MockCommunicator(
                discrete_action=True, visual_inputs=1)
            mock_load.return_value = dummy_config
            tc = TrainerController(' ', ' ', 1, None, True, True, False, 1, 1,
                                   1, 1, '', '', False)
            config = tc._load_config()
            assert (len(config) == 1)
            assert (config['default']['trainer'] == "ppo")
Example #4
0
def test_load_config():
    open_name = 'unitytrainers.trainer_controller' + '.open'
    with mock.patch('yaml.load') as mock_load:
        with mock.patch(open_name, create=True) as _:
            with mock.patch('subprocess.Popen'):
                with mock.patch('socket.socket') as mock_socket:
                    with mock.patch('glob.glob') as mock_glob:
                        mock_load.return_value = dummy_config
                        mock_glob.return_value = ['FakeLaunchPath']
                        mock_socket.return_value.accept.return_value = (mock_socket, 0)
                        mock_socket.recv.return_value.decode.return_value = dummy_start
                        mock_load.return_value = dummy_config
                        tc = TrainerController(' ', ' ', 1, None, True, True, False, 1,
                                                   1, 1, 1, '','')
                        config = tc._load_config()
                        assert(len(config) == 1)
                        assert(config['default']['trainer'] == "ppo")
def run_training(sub_id, use_seed, options):
    # Docker Parameters
    if options['--docker-target-name'] == 'Empty':
        docker_target_name = ''
    else:
        docker_target_name = options['--docker-target-name']

    # General parameters
    run_id = options['--run-id']
    num_runs = int(options['--num-runs'])
    seed = int(options['--seed'])
    load_model = options['--load']
    train_model = options['--train']
    save_freq = int(options['--save-freq'])
    env_path = options['<env>']
    keep_checkpoints = int(options['--keep-checkpoints'])
    worker_id = int(options['--worker-id'])
    curriculum_file = str(options['--curriculum'])
    if curriculum_file == "None":
        curriculum_file = None
    lesson = int(options['--lesson'])
    fast_simulation = not bool(options['--slow'])
    no_graphics = options['--no-graphics']

    # Constants
    # Assumption that this yaml is present in same dir as this file
    base_path = os.path.dirname(__file__)
    TRAINER_CONFIG_PATH = os.path.abspath(
        os.path.join(base_path, "trainer_config.yaml"))

    if env_path is None and num_runs > 1:
        raise TrainerError(
            "It is not possible to launch more than one concurrent training session "
            "when training from the editor")

    tc = TrainerController(env_path, run_id + "-" + str(sub_id), save_freq,
                           curriculum_file, fast_simulation, load_model,
                           train_model, worker_id + sub_id, keep_checkpoints,
                           lesson, use_seed, docker_target_name,
                           TRAINER_CONFIG_PATH, no_graphics)
    tc.start_learning()
Example #6
0
def test_initialize_trainers():
    open_name = 'unitytrainers.trainer_controller' + '.open'
    with mock.patch('yaml.load') as mock_load:
        with mock.patch(open_name, create=True) as _:
            with mock.patch('subprocess.Popen'):
                with mock.patch('socket.socket') as mock_socket:
                    with mock.patch('glob.glob') as mock_glob:
                        mock_glob.return_value = ['FakeLaunchPath']
                        mock_socket.return_value.accept.return_value = (mock_socket, 0)
                        mock_socket.recv.return_value.decode.return_value = dummy_start
                        tc = TrainerController(' ', ' ', 1, None, True, True, False, 1,
                                               1, 1, 1, '', "tests/test_unitytrainers.py")

                        # Test for PPO trainer
                        mock_load.return_value = dummy_config
                        config = tc._load_config()
                        tf.reset_default_graph()
                        with tf.Session() as sess:
                            tc._initialize_trainers(config, sess)
                            assert(len(tc.trainers) == 1)
                            assert(isinstance(tc.trainers['RealFakeBrain'], PPOTrainer))

                        # Test for Behavior Cloning Trainer
                        mock_load.return_value = dummy_bc_config
                        config = tc._load_config()
                        tf.reset_default_graph()
                        with tf.Session() as sess:
                            tc._initialize_trainers(config, sess)
                            assert(isinstance(tc.trainers['RealFakeBrain'], BehavioralCloningTrainer))

                        # Test for proper exception when trainer name is incorrect
                        mock_load.return_value = dummy_bad_config
                        config = tc._load_config()
                        tf.reset_default_graph()
                        with tf.Session() as sess:
                            with pytest.raises(UnityEnvironmentException):
                                tc._initialize_trainers(config, sess)
Example #7
0
    logger.info(options)
    # Docker Parameters
    if options['--docker-target-name'] == 'Empty':
        docker_target_name = ''
    else:
        docker_target_name = options['--docker-target-name']

    # General parameters
    run_id = options['--run-id']
    seed = int(options['--seed'])
    load_model = options['--load']
    train_model = options['--train']
    save_freq = int(options['--save-freq'])
    env_path = options['<env>']
    keep_checkpoints = int(options['--keep-checkpoints'])
    worker_id = int(options['--worker-id'])
    curriculum_file = str(options['--curriculum'])
    if curriculum_file == "None":
        curriculum_file = None
    lesson = int(options['--lesson'])
    fast_simulation = not bool(options['--slow'])

    # Constants
    # Assumption that this yaml is present in same dir as this file
    base_path = os.path.dirname(__file__)
    TRAINER_CONFIG_PATH = os.path.abspath(os.path.join(base_path, "trainer_config.yaml"))

    tc = TrainerController(env_path, run_id, save_freq, curriculum_file, fast_simulation, load_model, train_model,
                           worker_id, keep_checkpoints, lesson, seed, docker_target_name, TRAINER_CONFIG_PATH)
    tc.start_learning()
Example #8
0
def test_initialization(mock_communicator, mock_launcher):
    mock_communicator.return_value = MockCommunicator(discrete_action=True,
                                                      visual_inputs=1)
    tc = TrainerController(' ', ' ', 1, None, True, True, False, 1, 1, 1, 1,
                           '', "tests/test_unitytrainers.py", False)
    assert (tc.env.brain_names[0] == 'RealFakeBrain')
from unitytrainers.trainer_controller import TrainerController
import os
import numpy as np
import time
tc = TrainerController('python/Balance', None, None, None, None, None, True,
                      0, 0, 0, 0, '', os.path.abspath(os.path.join(os.path.dirname(__file__), "trainer_config.yaml")))

class Net():
    def forward(self):
        return np.random.randint(0,180)

pop = 10
bots = []
env = tc.env
[bots.append(Net()) for i in range(pop)]
default_brain = env.external_brain_names[0]
brain = env.brains[default_brain]
rewards = [0] * pop
fittest = {}
max_gens = 1000
max_frames = 10000
state = env.reset(train_mode=True)[default_brain]
print()

for generation in range(max_gens):
    print("         GENERATION "+str(generation))
    print("----------------------------")
    print()
    for count, bot in enumerate(bots):
        frame = 0
        terminal = False
Example #10
0
        docker_target_name = options['--docker-target-name']

    # General parameters
    run_id = options['--run-id']
    seed = int(options['--seed'])
    load_model = options['--load']
    train_model = options['--train']
    save_freq = int(options['--save-freq'])
    env_path = options['<env>']
    keep_checkpoints = int(options['--keep-checkpoints'])
    worker_id = int(options['--worker-id'])
    curriculum_file = str(options['--curriculum'])
    if curriculum_file == "None":
        curriculum_file = None
    lesson = int(options['--lesson'])
    fast_simulation = not bool(options['--slow'])
    no_graphics = options['--no-graphics']
    trainer_config_path = options['--trainer-config-path']

    if not trainer_config_path:
        # Assumption that this yaml is present in same dir as this file
        base_path = os.path.dirname(__file__)
        trainer_config_path = trainer_config_path or os.path.abspath(
            os.path.join(base_path, "trainer_config.yaml"))

    tc = TrainerController(env_path, run_id, save_freq, curriculum_file,
                           fast_simulation, load_model, train_model, worker_id,
                           keep_checkpoints, lesson, seed, docker_target_name,
                           trainer_config_path, no_graphics)
    tc.start_learning()
Example #11
0
    logger.info(options)
    # Docker Parameters
    if options['--docker-target-name'] == 'Empty':
        docker_target_name = ''
    else:
        docker_target_name = options['--docker-target-name']

    # General parameters
    run_id = options['--run-id']
    seed = int(options['--seed'])
    load_model = options['--load']
    train_model = options['--train']
    save_freq = int(options['--save-freq'])
    env_path = options['<env>']
    keep_checkpoints = int(options['--keep-checkpoints'])
    worker_id = int(options['--worker-id'])
    curriculum_file = str(options['--curriculum'])
    if curriculum_file == "None":
        curriculum_file = None
    lesson = int(options['--lesson'])
    fast_simulation = not bool(options['--slow'])

    # Constants
    # Assumption that this yaml is present in same dir as this file
    base_path = os.path.dirname(__file__)
    TRAINER_CONFIG_PATH = os.path.abspath(
        os.path.join(base_path, "trainer_config.yaml"))

    tc = TrainerController(env_path, None, None, None, None, None, train_model,
                           0, keep_checkpoints, 0, 0, '', TRAINER_CONFIG_PATH)