|
2 | 2 | import struct |
3 | 3 | import numpy as np |
4 | 4 |
|
| 5 | +from python_communication import UnityCommunication |
5 | 6 | from .brain import BrainInfo, BrainParameters |
6 | 7 |
|
7 | | - |
8 | 8 | class ECSEnvironment(object): |
9 | 9 | VEC_SIZE = 8 |
10 | 10 | ACT_SIZE = 3 |
@@ -90,76 +90,6 @@ def external_brain_names(self): |
90 | 90 |
|
91 | 91 |
|
92 | 92 |
|
93 | | - |
94 | | - |
95 | | -class UnityCommunication: |
96 | | - FILE_CAPACITY = 200000 |
97 | | - NUMBER_AGENTS_POSITION = 0 |
98 | | - SENSOR_SIZE_POSITION = 4 |
99 | | - ACTUATOR_SIZE_POSITION = 8 |
100 | | - UNITY_READY_POSITION = 12 |
101 | | - SENSOR_DATA_POSITION = 13 |
102 | | - |
103 | | - PYTHON_READY_POSITION = 100000 |
104 | | - ACTUATOR_DATA_POSITION = 100001 |
105 | | - |
106 | | - # FILE_NAME = "../../../ml-agents-ecs/Assets/shared_communication_file.txt" |
107 | | - FILE_NAME = "shared_communication_file.txt" # This is relative to where the script is called |
108 | | - |
109 | | - def __init__(self): |
110 | | - with open(self.FILE_NAME, "r+b") as f: |
111 | | - # memory-map the file, size 0 means whole file |
112 | | - self.accessor = mmap.mmap(f.fileno(), 0) |
113 | | - |
114 | | - def get_int(self, position : int) -> int: |
115 | | - return struct.unpack("i", self.accessor[position:position + 4])[0] |
116 | | - |
117 | | - def read_sensor(self) -> np.ndarray: |
118 | | - sensor_size = self.get_int(self.SENSOR_SIZE_POSITION) |
119 | | - number_agents = self.get_int(self.NUMBER_AGENTS_POSITION) |
120 | | - |
121 | | - sensor = np.frombuffer( |
122 | | - buffer=self.accessor[self.SENSOR_DATA_POSITION: self.SENSOR_DATA_POSITION + 4*sensor_size*number_agents], |
123 | | - dtype=np.float32, |
124 | | - count=sensor_size * number_agents, |
125 | | - offset=0 |
126 | | - ) |
127 | | - return np.reshape(sensor, (number_agents, sensor_size)) |
128 | | - |
129 | | - def get_parameters(self) -> (int, int, int): |
130 | | - return self.get_int(self.NUMBER_AGENTS_POSITION), \ |
131 | | - self.get_int(self.SENSOR_SIZE_POSITION), \ |
132 | | - self.get_int(self.ACTUATOR_SIZE_POSITION) |
133 | | - |
134 | | - def write_actuator(self, actuator: np.ndarray): |
135 | | - actuator_size = self.get_int(self.ACTUATOR_SIZE_POSITION) |
136 | | - number_agents = self.get_int(self.NUMBER_AGENTS_POSITION) |
137 | | - |
138 | | - # TODO : Support more types ? |
139 | | - if actuator.dtype != np.float32: |
140 | | - actuator = actuator.astype(np.float32) |
141 | | - |
142 | | - try: |
143 | | - assert(actuator.shape == (number_agents, actuator_size)) |
144 | | - except: |
145 | | - print("_________") |
146 | | - print(actuator.shape) |
147 | | - print((number_agents, actuator_size)) |
148 | | - |
149 | | - self.accessor[self.ACTUATOR_DATA_POSITION: self.ACTUATOR_DATA_POSITION + 4*actuator_size*number_agents] = \ |
150 | | - actuator.tobytes() |
151 | | - |
152 | | - def set_ready(self): |
153 | | - self.accessor[self.UNITY_READY_POSITION: self.UNITY_READY_POSITION+1] = bytearray(struct.pack("b", False)) |
154 | | - self.accessor[self.PYTHON_READY_POSITION: self.PYTHON_READY_POSITION+1] = bytearray(struct.pack("b", True)) |
155 | | - |
156 | | - def unity_ready(self) -> bool: |
157 | | - return self.accessor[self.UNITY_READY_POSITION] |
158 | | - |
159 | | - def close(self): |
160 | | - self.accessor.close() |
161 | | - |
162 | | - |
163 | 93 | # if __name__ == "__main__": |
164 | 94 | # comm = UnityCommunication() |
165 | 95 | # |
|
0 commit comments