File size: 5,659 Bytes
079c32c |
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 |
import pytest
import copy
from collections import deque
import numpy as np
import torch
from ding.rl_utils import get_gae, get_gae_with_default_last_value, get_nstep_return_data, get_train_sample
@pytest.mark.unittest
class TestAdder:
def get_transition(self):
return {
'value': torch.randn(1),
'reward': torch.rand(1),
'action': torch.rand(3),
'other': np.random.randint(0, 10, size=(4, )),
'obs': torch.randn(3),
'done': False
}
def get_transition_multi_agent(self):
return {
'value': torch.randn(1, 8),
'reward': torch.rand(1, 1),
'action': torch.rand(3),
'other': np.random.randint(0, 10, size=(4, )),
'obs': torch.randn(3),
'done': False
}
def test_get_gae(self):
transitions = deque([self.get_transition() for _ in range(10)])
last_value = torch.randn(1)
output = get_gae(transitions, last_value, gamma=0.99, gae_lambda=0.97, cuda=False)
for i in range(len(output)):
o = output[i]
assert 'adv' in o.keys()
for k, v in o.items():
if k == 'adv':
assert isinstance(v, torch.Tensor)
assert v.shape == (1, )
else:
if k == 'done':
assert v == transitions[i][k]
else:
assert (v == transitions[i][k]).all()
output1 = get_gae_with_default_last_value(
copy.deepcopy(transitions), True, gamma=0.99, gae_lambda=0.97, cuda=False
)
for i in range(len(output)):
assert output[i]['adv'].ne(output1[i]['adv'])
data = copy.deepcopy(transitions)
data.append({'value': last_value})
output2 = get_gae_with_default_last_value(data, False, gamma=0.99, gae_lambda=0.97, cuda=False)
for i in range(len(output)):
assert output[i]['adv'].eq(output2[i]['adv'])
def test_get_gae_multi_agent(self):
transitions = deque([self.get_transition_multi_agent() for _ in range(10)])
last_value = torch.randn(1, 8)
output = get_gae(transitions, last_value, gamma=0.99, gae_lambda=0.97, cuda=False)
for i in range(len(output)):
o = output[i]
assert 'adv' in o.keys()
for k, v in o.items():
if k == 'adv':
assert isinstance(v, torch.Tensor)
assert v.shape == (
1,
8,
)
else:
if k == 'done':
assert v == transitions[i][k]
else:
assert (v == transitions[i][k]).all()
output1 = get_gae_with_default_last_value(
copy.deepcopy(transitions), True, gamma=0.99, gae_lambda=0.97, cuda=False
)
for i in range(len(output)):
for j in range(output[i]['adv'].shape[1]):
assert output[i]['adv'][0][j].ne(output1[i]['adv'][0][j])
data = copy.deepcopy(transitions)
data.append({'value': last_value})
output2 = get_gae_with_default_last_value(data, False, gamma=0.99, gae_lambda=0.97, cuda=False)
for i in range(len(output)):
for j in range(output[i]['adv'].shape[1]):
assert output[i]['adv'][0][j].eq(output2[i]['adv'][0][j])
def test_get_nstep_return_data(self):
nstep = 3
data = deque([self.get_transition() for _ in range(10)])
output_data = get_nstep_return_data(data, nstep=nstep)
assert len(output_data) == 10
for i, o in enumerate(output_data):
assert o['reward'].shape == (nstep, )
if i >= 10 - nstep + 1:
assert o['done'] is data[-1]['done']
assert o['reward'][-(i - 10 + nstep):].sum() == 0
data = deque([self.get_transition() for _ in range(12)])
output_data = get_nstep_return_data(data, nstep=nstep)
assert len(output_data) == 12
def test_get_train_sample(self):
data = [self.get_transition() for _ in range(10)]
output = get_train_sample(data, unroll_len=1, last_fn_type='drop')
assert len(output) == 10
output = get_train_sample(data, unroll_len=4, last_fn_type='drop')
assert len(output) == 2
for o in output:
for v in o.values():
assert len(v) == 4
output = get_train_sample(data, unroll_len=4, last_fn_type='null_padding')
assert len(output) == 3
for o in output:
for v in o.values():
assert len(v) == 4
assert output[-1]['done'] == [False, False, True, True]
for i in range(1, 10 % 4 + 1):
assert id(output[-1]['obs'][-i]) != id(output[-1]['obs'][0])
output = get_train_sample(data, unroll_len=4, last_fn_type='last')
assert len(output) == 3
for o in output:
for v in o.values():
assert len(v) == 4
miss_num = 4 - 10 % 4
for i in range(10 % 4):
assert id(output[-1]['obs'][i]) != id(output[-2]['obs'][miss_num + i])
output = get_train_sample(data, unroll_len=11, last_fn_type='last')
assert len(output) == 1
assert len(output[0]['obs']) == 11
assert output[-1]['done'][-1] is True
assert output[-1]['done'][0] is False
assert id(output[-1]['obs'][-1]) != id(output[-1]['obs'][0])
test = TestAdder()
test.test_get_gae_multi_agent()
|