File size: 22,194 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import attr
import cattr
import pickle
import pytest
import yaml

from typing import Dict, List, Optional

from mlagents.trainers.settings import (
    RunOptions,
    TrainerSettings,
    NetworkSettings,
    RewardSignalType,
    RewardSignalSettings,
    CuriositySettings,
    EnvironmentSettings,
    EnvironmentParameterSettings,
    ConstantSettings,
    UniformSettings,
    GaussianSettings,
    MultiRangeUniformSettings,
    deep_update_dict,
    strict_to_cls,
    ScheduleType,
)
from mlagents.trainers.ppo.trainer import PPOSettings, TRAINER_NAME as PPO_TRAINER_NAME
from mlagents.trainers.sac.trainer import SACSettings, TRAINER_NAME as SAC_TRAINER_NAME

from mlagents.trainers.exception import TrainerConfigError

TRAINER_SETTING_TYPES = {"ppo": PPOSettings, "sac": SACSettings}


def check_if_different(testobj1: object, testobj2: object) -> None:
    assert testobj1 is not testobj2
    if attr.has(testobj1.__class__) and attr.has(testobj2.__class__):
        for key, val in attr.asdict(testobj1, recurse=False).items():
            if (
                isinstance(val, dict) or isinstance(val, list) or attr.has(val)
            ) and val != {}:
                # Note: this check doesn't check the contents of mutables.
                check_if_different(val, attr.asdict(testobj2, recurse=False)[key])


def check_dict_is_at_least(
    testdict1: Dict, testdict2: Dict, exceptions: Optional[List[str]] = None
) -> None:
    """
    Check if everything present in the 1st dict is the same in the second dict.
    Excludes things that the second dict has but is not present in the heirarchy of the
    1st dict. Used to compare an underspecified config dict structure (e.g. as
    would be provided by a user) with a complete one (e.g. as exported by RunOptions).
    """
    for key, val in testdict1.items():
        if exceptions is not None and key in exceptions:
            continue
        assert key in testdict2
        if isinstance(val, dict):
            check_dict_is_at_least(val, testdict2[key])
        elif isinstance(val, list):
            assert isinstance(testdict2[key], list)
            for _el0, _el1 in zip(val, testdict2[key]):
                if isinstance(_el0, dict):
                    check_dict_is_at_least(_el0, _el1)
                else:
                    assert val == testdict2[key]
        else:  # If not a dict, don't recurse into it
            assert val == testdict2[key]


def test_is_new_instance():
    """
    Verify that every instance of RunOptions() and its subclasses
    is a new instance (i.e. all factory methods are used properly.)
    """
    check_if_different(RunOptions(), RunOptions())
    check_if_different(TrainerSettings(), TrainerSettings())


def test_no_configuration():
    """
    Verify that a new config will have a PPO trainer with extrinsic rewards.
    """
    blank_runoptions = RunOptions()
    blank_runoptions.behaviors.set_config_specified(False)
    assert isinstance(blank_runoptions.behaviors["test"], TrainerSettings)
    assert isinstance(blank_runoptions.behaviors["test"].hyperparameters, PPOSettings)
    assert (
        RewardSignalType.EXTRINSIC in blank_runoptions.behaviors["test"].reward_signals
    )


def test_strict_to_cls():
    """
    Test strict structuring method.
    """

    @attr.s(auto_attribs=True)
    class TestAttrsClass:
        field1: int = 0
        field2: str = "test"

    correct_dict = {"field1": 1, "field2": "test2"}
    assert strict_to_cls(correct_dict, TestAttrsClass) == TestAttrsClass(**correct_dict)

    incorrect_dict = {"field3": 1, "field2": "test2"}

    with pytest.raises(TrainerConfigError):
        strict_to_cls(incorrect_dict, TestAttrsClass)

    with pytest.raises(TrainerConfigError):
        strict_to_cls("non_dict_input", TestAttrsClass)


def test_deep_update_dict():
    dict1 = {"a": 1, "b": 2, "c": {"d": 3}}
    dict2 = {"a": 2, "c": {"d": 4, "e": 5}}

    deep_update_dict(dict1, dict2)
    assert dict1 == {"a": 2, "b": 2, "c": {"d": 4, "e": 5}}


def test_trainersettings_structure():
    """
    Test structuring method for TrainerSettings
    """
    trainersettings_dict = {
        "trainer_type": SAC_TRAINER_NAME,
        "hyperparameters": {"batch_size": 1024},
        "max_steps": 1.0,
        "reward_signals": {"curiosity": {"encoding_size": 64}},
    }
    trainer_settings = TrainerSettings.structure(trainersettings_dict, TrainerSettings)
    # check_trainer_setting_types([trainer_settings], TRAINER_SETTING_TYPES)
    assert isinstance(trainer_settings.hyperparameters, SACSettings)
    assert trainer_settings.trainer_type == SAC_TRAINER_NAME
    assert isinstance(trainer_settings.max_steps, int)
    assert RewardSignalType.CURIOSITY in trainer_settings.reward_signals

    # Check invalid trainer type
    with pytest.raises(TrainerConfigError):
        trainersettings_dict = {
            "trainer_type": "puppo",
            "hyperparameters": {"batch_size": 1024},
            "max_steps": 1.0,
        }
        TrainerSettings.structure(trainersettings_dict, TrainerSettings)

    # Check invalid hyperparameter
    with pytest.raises(TrainerConfigError):
        trainersettings_dict = {
            "trainer_type": PPO_TRAINER_NAME,
            "hyperparameters": {"notahyperparam": 1024},
            "max_steps": 1.0,
        }
        TrainerSettings.structure(trainersettings_dict, TrainerSettings)

    # Check non-dict
    with pytest.raises(TrainerConfigError):
        TrainerSettings.structure("notadict", TrainerSettings)

    # Check hyperparameters specified but trainer type left as default.
    # This shouldn't work as you could specify non-PPO hyperparameters.
    with pytest.raises(TrainerConfigError):
        trainersettings_dict = {"hyperparameters": {"batch_size": 1024}}
        TrainerSettings.structure(trainersettings_dict, TrainerSettings)


def test_trainersettingsschedules_structure():
    """
    Test structuring method for Trainer Settings Schedule
    """
    trainersettings_dict = {
        "trainer_type": PPO_TRAINER_NAME,
        "hyperparameters": {
            "learning_rate_schedule": "linear",
            "beta_schedule": "constant",
        },
    }
    trainer_settings = TrainerSettings.structure(trainersettings_dict, TrainerSettings)
    assert isinstance(trainer_settings.hyperparameters, PPOSettings)
    assert (
        trainer_settings.hyperparameters.learning_rate_schedule == ScheduleType.LINEAR
    )
    assert trainer_settings.hyperparameters.beta_schedule == ScheduleType.CONSTANT
    assert trainer_settings.hyperparameters.epsilon_schedule == ScheduleType.LINEAR


def test_even_checkpoints_structure():
    """
    Test structuring for even checkpoints
    """
    trainersettings_dict = {
        "trainer_type": PPO_TRAINER_NAME,
        "keep_checkpoints": 2,
        "even_checkpoints": True,
        "max_steps": 100.0,
    }

    trainer_settings = TrainerSettings.structure(trainersettings_dict, TrainerSettings)
    assert isinstance(trainer_settings.hyperparameters, PPOSettings)
    assert trainer_settings.checkpoint_interval == 50


def test_default_checkpoint_interval_structure():
    """
    Test structuring for even checkpoints
    """
    trainersettings_dict = {
        "trainer_type": PPO_TRAINER_NAME,
        "keep_checkpoints": 2,
        "max_steps": 100.0,
    }

    trainer_settings = TrainerSettings.structure(trainersettings_dict, TrainerSettings)
    assert isinstance(trainer_settings.hyperparameters, PPOSettings)
    assert trainer_settings.checkpoint_interval == 500000


def test_reward_signal_structure():
    """
    Tests the RewardSignalSettings structure method. This one is special b/c
    it takes in a Dict[RewardSignalType, RewardSignalSettings].
    """
    reward_signals_dict = {
        "extrinsic": {"strength": 1.0},
        "curiosity": {"strength": 1.0},
    }
    reward_signals = RewardSignalSettings.structure(
        reward_signals_dict, Dict[RewardSignalType, RewardSignalSettings]
    )
    assert isinstance(reward_signals[RewardSignalType.EXTRINSIC], RewardSignalSettings)
    assert isinstance(reward_signals[RewardSignalType.CURIOSITY], CuriositySettings)

    # Check invalid reward signal type
    reward_signals_dict = {"puppo": {"strength": 1.0}}
    with pytest.raises(ValueError):
        RewardSignalSettings.structure(
            reward_signals_dict, Dict[RewardSignalType, RewardSignalSettings]
        )

    # Check missing GAIL demo path
    reward_signals_dict = {"gail": {"strength": 1.0}}
    with pytest.raises(TypeError):
        RewardSignalSettings.structure(
            reward_signals_dict, Dict[RewardSignalType, RewardSignalSettings]
        )

    # Check non-Dict input
    with pytest.raises(TrainerConfigError):
        RewardSignalSettings.structure(
            "notadict", Dict[RewardSignalType, RewardSignalSettings]
        )


def test_memory_settings_validation():
    with pytest.raises(TrainerConfigError):
        NetworkSettings.MemorySettings(sequence_length=128, memory_size=63)

    with pytest.raises(TrainerConfigError):
        NetworkSettings.MemorySettings(sequence_length=128, memory_size=0)


def test_env_parameter_structure():
    """
    Tests the EnvironmentParameterSettings structure method and all validators.
    """
    env_params_dict = {
        "mass": {
            "sampler_type": "uniform",
            "sampler_parameters": {"min_value": 1.0, "max_value": 2.0},
        },
        "scale": {
            "sampler_type": "gaussian",
            "sampler_parameters": {"mean": 1.0, "st_dev": 2.0},
        },
        "length": {
            "sampler_type": "multirangeuniform",
            "sampler_parameters": {"intervals": [[1.0, 2.0], [3.0, 4.0]]},
        },
        "gravity": 1,
        "wall_height": {
            "curriculum": [
                {
                    "name": "Lesson1",
                    "completion_criteria": {
                        "measure": "reward",
                        "behavior": "fake_behavior",
                        "threshold": 10,
                    },
                    "value": 1,
                },
                {"value": 4, "name": "Lesson2"},
            ]
        },
    }
    env_param_settings = EnvironmentParameterSettings.structure(
        env_params_dict, Dict[str, EnvironmentParameterSettings]
    )
    assert isinstance(env_param_settings["mass"].curriculum[0].value, UniformSettings)
    assert isinstance(env_param_settings["scale"].curriculum[0].value, GaussianSettings)
    assert isinstance(
        env_param_settings["length"].curriculum[0].value, MultiRangeUniformSettings
    )

    # Check __str__ is correct
    assert (
        str(env_param_settings["mass"].curriculum[0].value)
        == "Uniform sampler: min=1.0, max=2.0"
    )
    assert (
        str(env_param_settings["scale"].curriculum[0].value)
        == "Gaussian sampler: mean=1.0, stddev=2.0"
    )
    assert (
        str(env_param_settings["length"].curriculum[0].value)
        == "MultiRangeUniform sampler: intervals=[(1.0, 2.0), (3.0, 4.0)]"
    )
    assert str(env_param_settings["gravity"].curriculum[0].value) == "Float: value=1"

    assert isinstance(
        env_param_settings["wall_height"].curriculum[0].value, ConstantSettings
    )
    assert isinstance(
        env_param_settings["wall_height"].curriculum[1].value, ConstantSettings
    )

    # Check invalid distribution type
    invalid_distribution_dict = {
        "mass": {
            "sampler_type": "beta",
            "sampler_parameters": {"alpha": 1.0, "beta": 2.0},
        }
    }
    with pytest.raises(ValueError):
        EnvironmentParameterSettings.structure(
            invalid_distribution_dict, Dict[str, EnvironmentParameterSettings]
        )

    # Check min less than max in uniform
    invalid_distribution_dict = {
        "mass": {
            "sampler_type": "uniform",
            "sampler_parameters": {"min_value": 2.0, "max_value": 1.0},
        }
    }
    with pytest.raises(TrainerConfigError):
        EnvironmentParameterSettings.structure(
            invalid_distribution_dict, Dict[str, EnvironmentParameterSettings]
        )

    # Check min less than max in multirange
    invalid_distribution_dict = {
        "mass": {
            "sampler_type": "multirangeuniform",
            "sampler_parameters": {"intervals": [[2.0, 1.0]]},
        }
    }
    with pytest.raises(TrainerConfigError):
        EnvironmentParameterSettings.structure(
            invalid_distribution_dict, Dict[str, EnvironmentParameterSettings]
        )

    # Check multirange has valid intervals
    invalid_distribution_dict = {
        "mass": {
            "sampler_type": "multirangeuniform",
            "sampler_parameters": {"intervals": [[1.0, 2.0], [3.0]]},
        }
    }
    with pytest.raises(TrainerConfigError):
        EnvironmentParameterSettings.structure(
            invalid_distribution_dict, Dict[str, EnvironmentParameterSettings]
        )

    # Check non-Dict input
    with pytest.raises(TrainerConfigError):
        EnvironmentParameterSettings.structure(
            "notadict", Dict[str, EnvironmentParameterSettings]
        )

    invalid_curriculum_dict = {
        "wall_height": {
            "curriculum": [
                {
                    "name": "Lesson1",
                    "completion_criteria": {
                        "measure": "progress",
                        "behavior": "fake_behavior",
                        "threshold": 10,
                    },  # > 1 is too large
                    "value": 1,
                },
                {"value": 4, "name": "Lesson2"},
            ]
        }
    }
    with pytest.raises(TrainerConfigError):
        EnvironmentParameterSettings.structure(
            invalid_curriculum_dict, Dict[str, EnvironmentParameterSettings]
        )


@pytest.mark.parametrize("use_defaults", [True, False])
def test_exportable_settings(use_defaults):
    """
    Test that structuring and unstructuring a RunOptions object results in the same
    configuration representation.
    """
    # Try to enable as many features as possible in this test YAML to hit all the
    # edge cases. Set as much as possible as non-default values to ensure no flukes.
    test_yaml = """
    behaviors:
        3DBall:
            trainer_type: sac
            hyperparameters:
                learning_rate: 0.0004
                learning_rate_schedule: constant
                batch_size: 64
                buffer_size: 200000
                buffer_init_steps: 100
                tau: 0.006
                steps_per_update: 10.0
                save_replay_buffer: true
                init_entcoef: 0.5
                reward_signal_steps_per_update: 10.0
            network_settings:
                deterministic: true
                normalize: false
                hidden_units: 256
                num_layers: 3
                vis_encode_type: nature_cnn
                memory:
                    memory_size: 1288
                    sequence_length: 12
            reward_signals:
                extrinsic:
                    gamma: 0.999
                    strength: 1.0
                curiosity:
                    gamma: 0.999
                    strength: 1.0
            keep_checkpoints: 5
            max_steps: 500000
            time_horizon: 1000
            summary_freq: 12000
            checkpoint_interval: 1
            threaded: true
    env_settings:
        env_path: test_env_path
        env_args:
            - test_env_args1
            - test_env_args2
        base_port: 12345
        num_envs: 8
        num_areas: 8
        seed: 12345
    engine_settings:
        width: 12345
        height: 12345
        quality_level: 12345
        time_scale: 12345
        target_frame_rate: 12345
        capture_frame_rate: 12345
        no_graphics: true
    checkpoint_settings:
        run_id: test_run_id
        initialize_from: test_directory
        load_model: false
        resume: true
        force: true
        train_model: false
        inference: false
    debug: true
    environment_parameters:
        big_wall_height:
            curriculum:
              - name: Lesson0
                completion_criteria:
                    measure: progress
                    behavior: BigWallJump
                    signal_smoothing: true
                    min_lesson_length: 100
                    threshold: 0.1
                value:
                    sampler_type: uniform
                    sampler_parameters:
                        min_value: 0.0
                        max_value: 4.0
              - name: Lesson1
                completion_criteria:
                    measure: reward
                    behavior: BigWallJump
                    signal_smoothing: true
                    min_lesson_length: 100
                    threshold: 0.2
                value:
                    sampler_type: gaussian
                    sampler_parameters:
                        mean: 4.0
                        st_dev: 7.0
              - name: Lesson2
                completion_criteria:
                    measure: progress
                    behavior: BigWallJump
                    signal_smoothing: true
                    min_lesson_length: 20
                    threshold: 0.3
                value:
                    sampler_type: multirangeuniform
                    sampler_parameters:
                        intervals: [[1.0, 2.0],[4.0, 5.0]]
              - name: Lesson3
                value: 8.0
        small_wall_height: 42.0
        other_wall_height:
            sampler_type: multirangeuniform
            sampler_parameters:
                intervals: [[1.0, 2.0],[4.0, 5.0]]
    """
    if not use_defaults:
        loaded_yaml = yaml.safe_load(test_yaml)
        run_options = RunOptions.from_dict(yaml.safe_load(test_yaml))
    else:
        run_options = RunOptions()
    dict_export = run_options.as_dict()

    if not use_defaults:  # Don't need to check if no yaml
        check_dict_is_at_least(
            loaded_yaml, dict_export, exceptions=["environment_parameters"]
        )
    # Re-import and verify has same elements
    run_options2 = RunOptions.from_dict(dict_export)
    second_export = run_options2.as_dict()

    check_dict_is_at_least(dict_export, second_export)
    # Should be able to use equality instead of back-and-forth once environment_parameters
    # is working
    check_dict_is_at_least(second_export, dict_export)
    # Check that the two exports are the same
    assert dict_export == second_export

    # check if cehckpoint_settings priorotizes resume over initialize from
    run_options2.checkpoint_settings.prioritize_resume_init()
    assert run_options2.checkpoint_settings.initialize_from is None


def test_environment_settings():
    # default args
    EnvironmentSettings()

    # 1 env is OK if no env_path
    EnvironmentSettings(num_envs=1)

    # 2 areas are OK
    EnvironmentSettings(num_areas=2)

    # multiple envs is OK if env_path is set
    EnvironmentSettings(num_envs=42, env_path="/foo/bar.exe")

    # Multiple environments with no env_path is an error
    with pytest.raises(ValueError):
        EnvironmentSettings(num_envs=2)


def test_default_settings():
    # Make default settings, one nested and one not.
    default_settings = {
        "max_steps": 1,
        "network_settings": {"num_layers": 1000, "deterministic": True},
    }
    behaviors = {"test1": {"max_steps": 2, "network_settings": {"hidden_units": 2000}}}
    run_options_dict = {"default_settings": default_settings, "behaviors": behaviors}
    run_options = RunOptions.from_dict(run_options_dict)

    # Check that a new behavior has the default settings
    default_settings_cls = cattr.structure(default_settings, TrainerSettings)
    check_if_different(default_settings_cls, run_options.behaviors["test2"])

    # Check that an existing behavior overrides the defaults in specified fields
    test1_settings = run_options.behaviors["test1"]
    assert test1_settings.max_steps == 2
    assert test1_settings.network_settings.hidden_units == 2000
    assert test1_settings.network_settings.deterministic is True
    assert test1_settings.network_settings.num_layers == 1000

    # Change the overridden fields back, and check if the rest are equal.
    test1_settings.max_steps = 1
    test1_settings.network_settings.hidden_units = (
        default_settings_cls.network_settings.hidden_units
    )
    check_if_different(test1_settings, default_settings_cls)


def test_config_specified():
    # Test require all behavior names to be specified (or not)
    # Remove any pre-set defaults
    TrainerSettings.default_override = None
    behaviors = {"test1": {"max_steps": 2, "network_settings": {"hidden_units": 2000}}}
    run_options_dict = {"behaviors": behaviors}
    ro = RunOptions.from_dict(run_options_dict)
    # Don't require all behavior names
    ro.behaviors.set_config_specified(False)
    # Test that we can grab an entry that is not in the dict.
    assert isinstance(ro.behaviors["test2"], TrainerSettings)

    # Create strict RunOptions with no defualt_settings
    run_options_dict = {"behaviors": behaviors}
    ro = RunOptions.from_dict(run_options_dict)
    # Require all behavior names
    ro.behaviors.set_config_specified(True)
    with pytest.raises(TrainerConfigError):
        # Variable must be accessed otherwise Python won't query the dict
        print(ro.behaviors["test2"])

    # Create strict RunOptions with default settings
    default_settings = {"max_steps": 1, "network_settings": {"num_layers": 1000}}
    run_options_dict = {"default_settings": default_settings, "behaviors": behaviors}
    ro = RunOptions.from_dict(run_options_dict)
    # Require all behavior names
    ro.behaviors.set_config_specified(True)
    # Test that we can grab an entry that is not in the dict.
    assert isinstance(ro.behaviors["test2"], TrainerSettings)


def test_pickle():
    # Make sure RunOptions is pickle-able.
    run_options = RunOptions()
    p = pickle.dumps(run_options)
    pickle.loads(p)