1- import numpy as np
2- import gymnasium as gym
3-
41from libcpp.string cimport string
52from libcpp.vector cimport vector
63from libcpp.map cimport map
@@ -14,17 +11,13 @@ from mettagrid.objects.converter cimport Converter
1411from mettagrid.objects.wall cimport Wall
1512
1613cdef class ObservationEncoder:
17- cpdef obs_np_type(self ):
18- return np.uint8
19-
2014 cdef init(self , unsigned int obs_width, unsigned int obs_height):
2115 self ._obs_width = obs_width
2216 self ._obs_height = obs_height
2317
2418 def __init__ (self ) -> None:
2519 self._offsets.resize(ObjectType.Count )
2620 self._type_feature_names.resize(ObjectType.Count )
27- features = []
2821
2922 self._type_feature_names[ObjectType.AgentT] = Agent.feature_names()
3023 self._type_feature_names[ObjectType.WallT] = Wall.feature_names()
@@ -34,38 +27,7 @@ cdef class ObservationEncoder:
3427 # remove these types from code.
3528 for type_id in [ObjectType.AltarT , ObjectType.ArmoryT , ObjectType.FactoryT , ObjectType.GeneratorT , ObjectType.LabT , ObjectType.LaseryT , ObjectType.MineT , ObjectType.TempleT]:
3629 self._type_feature_names[type_id] = Converter.feature_names(type_id )
37-
38- for type_id in range(ObjectType.Count ):
39- for i in range (len (self ._type_feature_names[type_id])):
40- self ._offsets[type_id].push_back(len (features))
41- features.append(self ._type_feature_names[type_id][i])
42- self ._feature_names = features
43-
44- cdef encode(self , GridObject * obj, ObsType[:] obs):
45- self ._encode(obj, obs, self ._offsets[obj._type_id])
46-
47- cdef _encode(self , GridObject * obj, ObsType[:] obs, vector[unsigned int ] offsets):
48- obj.obs(& obs[0 ], offsets)
49-
50- cdef vector[string] feature_names(self ):
51- return self ._feature_names
52-
53- cpdef observation_space(self ):
54- type_info = np.iinfo(self .obs_np_type())
55-
56- return gym.spaces.Box(
57- low = type_info.min, high = type_info.max,
58- shape = (
59- len (self .feature_names()),
60- self ._obs_height, self ._obs_width),
61- dtype = self .obs_np_type()
62- )
63-
64- cdef class SemiCompactObservationEncoder(ObservationEncoder):
65- def __init__ (self ) -> None:
66- super().__init__()
67- self._offsets.resize(ObjectType.Count )
68- self._type_feature_names.resize(ObjectType.Count )
30+
6931 # Generate an offset for each unique feature name.
7032 cdef map[string , int] features
7133 cdef vector[string] feature_names
@@ -77,6 +39,15 @@ cdef class SemiCompactObservationEncoder(ObservationEncoder):
7739 # Set the offset for each feature, using the global offsets.
7840 for type_id in range (ObjectType.Count):
7941 for i in range (len (self ._type_feature_names[type_id])):
80- self ._offsets[type_id][i] = features[self ._type_feature_names[type_id][i]]
42+ self ._offsets[type_id].push_back( features[self ._type_feature_names[type_id][i]])
8143
8244 self ._feature_names = feature_names
45+
46+ cdef encode(self , GridObject * obj, ObsType[:] obs):
47+ self ._encode(obj, obs, self ._offsets[obj._type_id])
48+
49+ cdef _encode(self , GridObject * obj, ObsType[:] obs, vector[unsigned int ] offsets):
50+ obj.obs(& obs[0 ], offsets)
51+
52+ cdef vector[string] feature_names(self ):
53+ return self ._feature_names
0 commit comments