-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RLlib] Issue 22625:
MultiAgentBatch.timeslices()
does not behave a…
…s expected. (#22657)
- Loading branch information
1 parent
4576f53
commit c0ade5f
Showing
5 changed files
with
335 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
import unittest | ||
|
||
from ray.rllib.policy.sample_batch import SampleBatch, MultiAgentBatch | ||
from ray.rllib.utils.test_utils import check_same_batch | ||
|
||
|
||
class TestMultiAgentBatch(unittest.TestCase): | ||
def test_timeslices_non_overlapping_experiences(self): | ||
"""Tests if timeslices works as expected on a MultiAgentBatch | ||
consisting of two non-overlapping SampleBatches. | ||
""" | ||
|
||
def _generate_data(agent_idx): | ||
batch = SampleBatch( | ||
{ | ||
SampleBatch.T: [0, 1], | ||
SampleBatch.EPS_ID: 2 * [agent_idx], | ||
SampleBatch.AGENT_INDEX: 2 * [agent_idx], | ||
SampleBatch.SEQ_LENS: [2], | ||
} | ||
) | ||
return batch | ||
|
||
policy_batches = {str(idx): _generate_data(idx) for idx in (range(2))} | ||
ma_batch = MultiAgentBatch(policy_batches, 4) | ||
sliced_ma_batches = ma_batch.timeslices(1) | ||
|
||
[ | ||
check_same_batch(i, j) | ||
for i, j in zip( | ||
sliced_ma_batches, | ||
[ | ||
MultiAgentBatch( | ||
{ | ||
"0": SampleBatch( | ||
{ | ||
SampleBatch.T: [0], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [0], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
) | ||
}, | ||
1, | ||
), | ||
MultiAgentBatch( | ||
{ | ||
"0": SampleBatch( | ||
{ | ||
SampleBatch.T: [1], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [0], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
) | ||
}, | ||
1, | ||
), | ||
MultiAgentBatch( | ||
{ | ||
"1": SampleBatch( | ||
{ | ||
SampleBatch.T: [0], | ||
SampleBatch.EPS_ID: [1], | ||
SampleBatch.AGENT_INDEX: [1], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
) | ||
}, | ||
1, | ||
), | ||
MultiAgentBatch( | ||
{ | ||
"1": SampleBatch( | ||
{ | ||
SampleBatch.T: [1], | ||
SampleBatch.EPS_ID: [1], | ||
SampleBatch.AGENT_INDEX: [1], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
) | ||
}, | ||
1, | ||
), | ||
], | ||
) | ||
] | ||
|
||
def test_timeslices_partially_overlapping_experiences(self): | ||
"""Tests if timeslices works as expected on a MultiAgentBatch | ||
consisting of two partially overlapping SampleBatches. | ||
""" | ||
|
||
def _generate_data(agent_idx, t_start): | ||
batch = SampleBatch( | ||
{ | ||
SampleBatch.T: [t_start, t_start + 1], | ||
SampleBatch.EPS_ID: [0, 0], | ||
SampleBatch.AGENT_INDEX: 2 * [agent_idx], | ||
SampleBatch.SEQ_LENS: [2], | ||
} | ||
) | ||
return batch | ||
|
||
policy_batches = {str(idx): _generate_data(idx, idx) for idx in (range(2))} | ||
ma_batch = MultiAgentBatch(policy_batches, 4) | ||
sliced_ma_batches = ma_batch.timeslices(1) | ||
|
||
[ | ||
check_same_batch(i, j) | ||
for i, j in zip( | ||
sliced_ma_batches, | ||
[ | ||
MultiAgentBatch( | ||
{ | ||
"0": SampleBatch( | ||
{ | ||
SampleBatch.T: [0], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [0], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
) | ||
}, | ||
1, | ||
), | ||
MultiAgentBatch( | ||
{ | ||
"0": SampleBatch( | ||
{ | ||
SampleBatch.T: [1], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [0], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
), | ||
"1": SampleBatch( | ||
{ | ||
SampleBatch.T: [1], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [1], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
), | ||
}, | ||
1, | ||
), | ||
MultiAgentBatch( | ||
{ | ||
"1": SampleBatch( | ||
{ | ||
SampleBatch.T: [2], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [1], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
) | ||
}, | ||
1, | ||
), | ||
], | ||
) | ||
] | ||
|
||
def test_timeslices_fully_overlapping_experiences(self): | ||
"""Tests if timeslices works as expected on a MultiAgentBatch | ||
consisting of two fully overlapping SampleBatches. | ||
""" | ||
|
||
def _generate_data(agent_idx): | ||
batch = SampleBatch( | ||
{ | ||
SampleBatch.T: [0, 1], | ||
SampleBatch.EPS_ID: [0, 0], | ||
SampleBatch.AGENT_INDEX: 2 * [agent_idx], | ||
SampleBatch.SEQ_LENS: [2], | ||
} | ||
) | ||
return batch | ||
|
||
policy_batches = {str(idx): _generate_data(idx) for idx in (range(2))} | ||
ma_batch = MultiAgentBatch(policy_batches, 4) | ||
sliced_ma_batches = ma_batch.timeslices(1) | ||
|
||
[ | ||
check_same_batch(i, j) | ||
for i, j in zip( | ||
sliced_ma_batches, | ||
[ | ||
MultiAgentBatch( | ||
{ | ||
"0": SampleBatch( | ||
{ | ||
SampleBatch.T: [0], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [0], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
), | ||
"1": SampleBatch( | ||
{ | ||
SampleBatch.T: [0], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [1], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
), | ||
}, | ||
1, | ||
), | ||
MultiAgentBatch( | ||
{ | ||
"0": SampleBatch( | ||
{ | ||
SampleBatch.T: [1], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [0], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
), | ||
"1": SampleBatch( | ||
{ | ||
SampleBatch.T: [1], | ||
SampleBatch.EPS_ID: [0], | ||
SampleBatch.AGENT_INDEX: [1], | ||
SampleBatch.SEQ_LENS: [1], | ||
} | ||
), | ||
}, | ||
1, | ||
), | ||
], | ||
) | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
import pytest | ||
import sys | ||
|
||
sys.exit(pytest.main(["-v", __file__])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters