Vecrl
This namespace provides various vectorized reinforcement learning utilities.
BaseVectorEnv
¶
Bases: VectorEnv
A base class for vectorized gymnasium environments.
In gymnasium 0.29.x, the __init__(...)
method of the base class
gymnasium.vector.VectorEnv
expects the arguments num_envs
,
observation_space
, and action_space
, and then prepares the instance
attributes num_envs
, single_observation_space
, single_action_space
,
observation_space
, and action_space
according to the initialization
arguments it receives.
It appears that with gymnasium 1.x, this API is changing, and
gymnasium.vector.VectorEnv
strictly expects no positional arguments.
This BaseVectorEnv
class is meant as a base class which preserves
the behavior of gymnasium 0.29.x, meaning that it will expects the
arguments, and prepare the attributes mentioned above.
Please note, however, that this BaseVectorEnv
implementation
can only work with environments whose single observation and single
action spaces are either Box
or Discrete
.
Source code in evotorch/neuroevolution/net/vecrl.py
__init__(num_envs, observation_space, action_space)
¶
__init__(...)
: Initialize the vectorized environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_envs
|
int
|
Number of sub-environments handled by this |
required |
observation_space
|
Space
|
Observation space of a single sub-environment.
This can only be given as an instance of type
|
required |
action_space
|
Space
|
Action space of a single sub-environment.
This can only be given as an instance of type
|
required |
Source code in evotorch/neuroevolution/net/vecrl.py
Policy
¶
A Policy for deciding the actions for a reinforcement learning environment.
This can be seen as a stateful wrapper around a PyTorch module.
Let us assume that we have the following PyTorch module:
which has 48 parameters (when all the parameters are flattened).
Let us randomly generate a parameter vector for our module net
:
We can now prepare a policy:
If we generate a random observation:
We can receive our action as follows:
If the PyTorch module that we wish to wrap is a recurrent network (i.e. a network which expects an optional second argument for the hidden state, and returns a second value which represents the updated hidden state), then, the hidden state is automatically managed by the Policy instance.
Let us assume that we have a recurrent network named recnet
.
In this case, because the hidden state of the network is internally managed, the usage is still the same with our previous non-recurrent example:
When using a recurrent module on multiple episodes, it is important to reset the hidden state of the network. This is achieved by the reset method:
policy.reset()
action1 = policy(observation1)
# action2 will be computed with the hidden state generated by the
# previous forward-pass.
action2 = policy(observation2)
policy.reset()
# action3 will be computed according to the renewed hidden state.
action3 = policy(observation3)
Both for non-recurrent and recurrent networks, it is possible to perform vectorized operations. For now, let us return to our first non-recurrent example:
Instead of generating only one parameter vector, we now generate a batch of parameter vectors. Let us say that our batch size is 10:
Like we did in the non-batched examples, we can do:
Because we are now in the batched mode, policy
now expects a batch
of observations and will return a batch of actions:
When doing vectorized reinforcement learning with a recurrent module,
it can be the case that only some of the environments are finished,
and therefore it is necessary to reset the hidden states associated
with those environments only. The reset(...)
method of Policy
has a second argument to specify which of the recurrent network
instances are to be reset. For example, if the episodes of the
environments with indices 2 and 5 are about to restart (and therefore
we wish to reset the states of the networks with indices 2 and 5),
then, we can do:
Source code in evotorch/neuroevolution/net/vecrl.py
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 |
|
h
property
¶
The hidden state of the contained recurrent network, if any.
If the contained recurrent network did not generate a hidden state yet, or if the contained network is not recurrent, then the result will be None.
parameter_length
property
¶
Length of the parameter tensor.
parameters
property
¶
The currently used parameters.
wrapped_module
property
¶
The wrapped torch.nn.Module
instance.
__call__(x)
¶
Pass the given observations through the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The observations, as a PyTorch tensor.
If the parameters were given (via the method
|
required |
Source code in evotorch/neuroevolution/net/vecrl.py
__init__(net, **kwargs)
¶
__init__(...)
: Initialize the Policy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
net
|
Union[str, Callable, Module]
|
The network to be wrapped by the Policy object.
This can be a string, a Callable (e.g. a |
required |
kwargs
|
Expected in the form of additional keyword arguments,
these keyword arguments will be passed to the provided
Callable object (if the argument |
{}
|
Source code in evotorch/neuroevolution/net/vecrl.py
reset(indices=None, *, copy=True)
¶
Reset the hidden states, if the contained module is a recurrent network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
indices
|
Optional[MaskOrIndices]
|
Optionally a sequence of integers or a sequence of booleans, specifying which networks' states will be reset. If left as None, then the states of all the networks will be reset. |
None
|
copy
|
bool
|
When |
True
|
Source code in evotorch/neuroevolution/net/vecrl.py
set_parameters(parameters, indices=None, *, reset=True)
¶
Set the parameters of the policy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameters
|
Tensor
|
A 1-dimensional or a 2-dimensional tensor containing
the flattened parameters to be used with the neural network.
If the given parameters are two-dimensional, then, given that
the leftmost size of the parameter tensor is |
required |
indices
|
Optional[MaskOrIndices]
|
For when the parameters were previously given via a
2-dimensional tensor, provide this argument if you would like
to change only some rows of the previously given parameters.
For example, if |
None
|
reset
|
bool
|
If given as True, the hidden states of the networks whose
parameters just changed will be reset. If |
True
|
Source code in evotorch/neuroevolution/net/vecrl.py
to_torch_module(parameter_vector)
¶
Get a copy of the contained network, parameterized as specified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameter_vector
|
Tensor
|
The parameters to be used by the new network. |
required |
Source code in evotorch/neuroevolution/net/vecrl.py
SyncVectorEnv
¶
Bases: BaseVectorEnv
A vectorized gymnasium environment for handling multiple sub-environments.
This is an alternative implementation to the class gymnasium.vector.SyncVectorEnv
.
This alternative SyncVectorEnv implementation has eager auto-reset.
After taking a step(), any sub-environment whose terminated or truncated signal is True will be immediately subject to resetting, and the returned observation and info will immediately reflect the first state of the new episode. This is compatible with the auto-reset behavior of gymnasium 0.29.x, and is different from the auto-reset behavior introduced in gymnasium 1.x.
Source code in evotorch/neuroevolution/net/vecrl.py
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 |
|
__init__(env_makers, *, empty_info=False, num_episodes=None, device=None)
¶
__init__(...)
: Initialize the SyncVectorEnv
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env_makers
|
Iterable[Env]
|
An iterable object which stores functions that make
the sub-environments to be managed by this |
required |
empty_info
|
bool
|
Whether or not to ignore the actual |
False
|
num_episodes
|
Optional[int]
|
Optionally an integer which represents the number
of episodes one wishes to run for each sub-environment.
If this |
None
|
device
|
Optional[Union[str, device]]
|
Optionally the device on which the observations, rewards,
terminated and truncated booleans and info arrays will be
reported. Please note that the sub-environments are always
expected with a numpy interface. This argument is used only for
optionally converting the sub-environments' state arrays to
PyTorch tensors on the target device. If this is left as None,
the reported arrays will be numpy arrays. If this is given as a
string or as a |
None
|
Source code in evotorch/neuroevolution/net/vecrl.py
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 |
|
close()
¶
render(*args, **kwargs)
¶
reset(**kwargs)
¶
Reset each sub-environment.
Any keyword argument other than seed
will be sent directly to the
reset(...)
methods of the underlying sub-environments.
If, among the keyword arguments, there is seed
, the value for this
seed
keyword argument will be expected either as None, or as an integer.
The setting seed=None
can be used if the user wishes to ensure that
there will be no explicit seeding when resetting the sub-environments
(even when the seed(...)
method of SyncVectorEnv
was called
previously with an explicit seed integer).
The setting seed=S
, where S
is an integer, causes the following
steps to be executed:
(i) prepare a temporary random number generator with seed S
;
(ii) from the temporary random number generator, generate N
sub-seed
integers where N
is the number of sub-environments;
(iii) reset each sub-environment with a sub-seed;
(iv) destroy the temporary random number generator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs
|
Keyword arguments to be passed to the |
{}
|
Source code in evotorch/neuroevolution/net/vecrl.py
seed(seed_integer=None)
¶
Prepare an internal random number generator to be used by the next reset()
.
In more details, if an integer is given via the argument seed_integer
,
an internal random number generator (of type numpy.random.RandomState
)
will be instantiated with seed_integer
as its seed. Then, the next time
reset()
is called, each sub-environment will be given a sub-seed, each
sub-seed being a new integer generated from this internal random number
generator. Once this operation is complete, the internal random generator
is destroyed, so that the remaining reset operations will continue to
be randomized according to the sub-environment-specific generators.
On the other hand, if the argument seed_integer
is given as None
,
the internal random number generator will be destroyed, meaning that the
next call to reset()
will reset each sub-environment without specifying
any sub-seed at all.
As an alternative, one can also provide a seed as a positional argument
to reset()
. The following two usages are equivalent:
vec_env = SyncVectorEnv(
[function_to_make_a_single_env() for _ in range(number_of_sub_envs)]
)
# Usage 1 (calling seed and reset separately):
vec_env.seed(an_integer)
vec_env.reset()
# Usage 2 (calling reset with a seed argument):
vec_env.reset(seed=an_integer)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed_integer
|
Optional[int]
|
An integer if you wish each sub-environment to be randomized via a pseudo-random generator seeded by this given integer. Otherwise, this can be left as None. |
None
|
Source code in evotorch/neuroevolution/net/vecrl.py
step(action)
¶
Take a step within each sub-environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action
|
Union[Tensor, ndarray]
|
A numpy array or a PyTorch tensor that contains the action. The size of the leftmost dimension of this array or tensor is expected to be equal to the number of sub-environments. |
required |
Source code in evotorch/neuroevolution/net/vecrl.py
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 |
|
TorchWrapper
¶
A wrapper for vectorized or non-vectorized gymnasium environments.
This wrapper ensures that the actions, observations, rewards, and the 'done' values are expressed as PyTorch tensors.
Please note that TorchWrapper
does not inherit neither from
gymnasium.Wrapper
, nor from gymnasium.vector.VectorEnvWrapper
.
Once an environment is wrapped via TorchWrapper
, it is NOT
recommended to further wrap it via other types of wrappers.
Source code in evotorch/neuroevolution/net/vecrl.py
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 |
|
array_type
property
¶
Get the array type of the wrapped environment. This can be "jax", "torch", or "numpy".
__init__(env, *, force_classic_api=False, discrete_to_continuous_act=False, clip_actions=False)
¶
__init__(...)
: Initialize the TorchWrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env
|
Union[Env, VectorEnv, TorchWrapper]
|
The gymnasium environment to be wrapped. |
required |
force_classic_api
|
bool
|
Set this as True if you would like to enable
the classic API. In the classic API, the |
False
|
discrete_to_continuous_act
|
bool
|
When this is set as True and the
wrapped environment has a Discrete action space, this wrapper
will transform the action space to Box. A Discrete-action
environment with |
False
|
clip_actions
|
bool
|
Set this as True if you would like to clip the given actions so that they conform to the declared boundaries of the action space. |
False
|
Source code in evotorch/neuroevolution/net/vecrl.py
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 |
|
reset(*args, **kwargs)
¶
Reset the environment
Source code in evotorch/neuroevolution/net/vecrl.py
step(action, *args, **kwargs)
¶
Take a step in the environment
Source code in evotorch/neuroevolution/net/vecrl.py
array_type(x, fallback=None)
¶
Get the type of an array as a string ("jax", "torch", or "numpy"). If the type of the array cannot be determined and a fallback is provided, then the fallback value will be returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
The array whose type will be determined. |
required |
fallback
|
Optional[str]
|
Fallback value, as a string, which will be returned if the array type cannot be determined. |
None
|
Source code in evotorch/neuroevolution/net/vecrl.py
convert_from_torch(x, array_type)
¶
Convert the given PyTorch tensor to an array of the specified type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The PyTorch array that will be converted. |
required |
array_type
|
str
|
Type to which the PyTorch tensor will be converted. Expected as one of these strings: "jax", "torch", "numpy". |
required |
Source code in evotorch/neuroevolution/net/vecrl.py
convert_to_torch(x)
¶
Convert the given array to PyTorch tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
Array to be converted. Can be a JAX array, a numpy array, a PyTorch tensor (in which case the input tensor will be returned as it is) or any Iterable object. |
required |
Source code in evotorch/neuroevolution/net/vecrl.py
convert_to_torch_bool(x)
¶
Convert the given array to a PyTorch tensor of bools.
If the given object is an array of floating point numbers, then, values that are near to 0.0 (with a tolerance of 1e-4) will be converted to False, and the others will be converted to True. If the given object is an array of integers, then zero values will be converted to False, and non-zero values will be converted to True. If the given object is an array of booleans, then no change will be made to those boolean values.
The given object can be a JAX array, a numpy array, or a PyTorch tensor. The result will always be a PyTorch tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
Array to be converted. |
required |
Source code in evotorch/neuroevolution/net/vecrl.py
make_brax_env(env_name, *, force_classic_api=False, num_envs=None, discrete_to_continuous_act=False, clip_actions=False, **kwargs)
¶
Make a brax environment and wrap it via TorchWrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env_name
|
str
|
Name of the brax environment, as string (e.g. "humanoid").
If the string starts with "old::" (e.g. "old::humanoid", etc.),
then the environment will be made using the namespace |
required |
force_classic_api
|
bool
|
Whether or not the classic gym API is to be used. |
False
|
num_envs
|
Optional[int]
|
Batch size for the vectorized environment. |
None
|
discrete_to_continuous_act
|
bool
|
Whether or not the the discrete action space of the environment is to be converted to a continuous one. This does nothing if the environment's action space is not discrete. |
False
|
clip_actions
|
bool
|
Whether or not the actions should be explicitly clipped so that they stay within the declared action boundaries. |
False
|
kwargs
|
Expected in the form of additional keyword arguments, these are passed to the environment. |
{}
|
Source code in evotorch/neuroevolution/net/vecrl.py
make_gym_env(env_name, *, force_classic_api=False, num_envs=None, discrete_to_continuous_act=False, clip_actions=False, empty_info=False, num_episodes=None, device=None, **kwargs)
¶
Make gymnasium environment(s) and wrap them via a TorchWrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env_name
|
str
|
Name of the gymnasium environment, as string (e.g. "Humanoid-v4"). |
required |
force_classic_api
|
bool
|
Whether or not the classic gym API is to be used. |
False
|
num_envs
|
Optional[int]
|
Optionally a batch size for the vectorized environment.
If given as an integer, the environment will be instantiated multiple
times, and then wrapped via |
None
|
discrete_to_continuous_act
|
bool
|
Whether or not the the discrete action space of the environment is to be converted to a continuous one. This does nothing if the environment's action space is not discrete. |
False
|
clip_actions
|
bool
|
Whether or not the actions should be explicitly clipped so that they stay within the declared action boundaries. |
False
|
empty_info
|
bool
|
Whether or not to ignore the info dictionaries of the
sub-environments and always return an empty dictionary for the
extra info. This feature is only available when |
False
|
num_episodes
|
Optional[int]
|
Optionally an integer which specifies the number of
episodes each sub-environment will run for. Until its number of
episodes run out, each sub-environment will be subject to
auto-reset. Alternatively, |
None
|
device
|
Optional[Union[str, device]]
|
Optionally the device on which the state(s) of the environment(s)
will be reported. If None, the reported arrays of the underlying
environment(s) will be unchanged. If given as a |
None
|
kwargs
|
Expected in the form of additional keyword arguments, these are passed to the environment. |
{}
|
Source code in evotorch/neuroevolution/net/vecrl.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
|
make_vector_env(env_name, *, force_classic_api=False, num_envs=None, discrete_to_continuous_act=False, clip_actions=False, gym_kwargs=None, brax_kwargs=None, **kwargs)
¶
Make a new vectorized environment and wrap it via TorchWrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
env_name
|
str
|
Name of the environment, as string.
If the string starts with "gym::" (e.g. "gym::Humanoid-v4", etc.),
then it is assumed that the target environment is a traditional
non-vectorized gymnasium environment. This non-vectorized
will first be duplicated and wrapped via a |
required |
force_classic_api
|
bool
|
Whether or not the classic gym API is to be used. |
False
|
num_envs
|
Optional[int]
|
Batch size for the vectorized environment. |
None
|
discrete_to_continuous_act
|
bool
|
Whether or not the the discrete action space of the environment is to be converted to a continuous one. This does nothing if the environment's action space is not discrete. |
False
|
clip_actions
|
bool
|
Whether or not the actions should be explicitly clipped so that they stay within the declared action boundaries. |
False
|
gym_kwargs
|
Optional[dict]
|
Keyword arguments to pass only if the environment is a classical gymnasium environment. |
None
|
brax_kwargs
|
Optional[dict]
|
Keyword arguments to pass only if the environment is a brax environment. |
None
|
kwargs
|
Expected in the form of additional keyword arguments, these are passed to the environment. |
{}
|
Source code in evotorch/neuroevolution/net/vecrl.py
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 |
|
reset_tensors(x, indices)
¶
Reset the specified regions of the given tensor(s) as 0.
Note that the resetting is performed in-place, which means, the provided tensors are modified.
The regions are determined by the argument indices
, which can be a sequence of booleans (in which case it is
interpreted as a mask), or a sequence of integers (in which case it is interpreted as the list of indices).
For example, let us imagine that we have the following tensor:
import torch
x = torch.tensor(
[
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15],
],
dtype=torch.float32,
)
If we wish to reset the rows with indices 0 and 2, we could use:
The new value of x
would then be:
torch.tensor(
[
[0, 0, 0, 0],
[4, 5, 6, 7],
[0, 0, 0, 0],
[12, 13, 14, 15],
],
dtype=torch.float32,
)
The first argument does not have to be a single tensor. Instead, it can be a container (i.e. a dictionary-like object or an iterable) that stores tensors. In this case, each tensor stored by the container will be subject to resetting. In more details, each tensor within the iterable(s) and each tensor within the value part of the dictionary-like object(s) will be reset.
As an example, let us assume that we have the following collection:
a = torch.tensor(
[
[0, 1],
[2, 3],
[4, 5],
],
dtype=torch.float32,
)
b = torch.tensor(
[
[0, 10, 20],
[30, 40, 50],
[60, 70, 80],
],
dtype=torch.float32,
)
c = torch.tensor(
[
[100],
[200],
[300],
],
dtype=torch.float32,
)
d = torch.tensor([-1, -2, -3], dtype=torch.float32)
my_tensors = [a, {"1": b, "2": (c, d)}]
To clear the regions with indices, e.g, (1, 2), we could do:
and the result would be:
>>> print(a)
torch.tensor(
[
[0, 1],
[0, 0],
[0, 0],
],
dtype=torch.float32,
)
>>> print(b)
torch.tensor(
[
[0, 10, 20],
[0, 0, 0],
[0, 0, 0],
],
dtype=torch.float32,
)
>>> print(c)
c = torch.tensor(
[
[100],
[0],
[0],
],
dtype=torch.float32,
)
>>> print(d)
torch.tensor([-1, 0, 0], dtype=torch.float32)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Any
|
A tensor or a collection of tensors, whose values are subject to resetting. |
required |
indices
|
MaskOrIndices
|
A sequence of integers or booleans, specifying which regions of the tensor(s) will be reset. |
required |
Source code in evotorch/neuroevolution/net/vecrl.py
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 |
|