Skip to content

Commit 8295136

Browse files
committed
Deleting duplicate UnityEnvironment
1 parent 4c2d2bb commit 8295136

File tree

2 files changed

+3
-72
lines changed

2 files changed

+3
-72
lines changed

Assets/ECSEnvironment.py

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import struct
33
import numpy as np
44

5+
from python_communication import UnityCommunication
56
from .brain import BrainInfo, BrainParameters
67

7-
88
class ECSEnvironment(object):
99
VEC_SIZE = 8
1010
ACT_SIZE = 3
@@ -90,76 +90,6 @@ def external_brain_names(self):
9090

9191

9292

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-
16393
# if __name__ == "__main__":
16494
# comm = UnityCommunication()
16595
#

Assets/python_communication.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55

66

7+
78
class UnityCommunication:
89
FILE_CAPACITY = 200000
910
NUMBER_AGENTS_POSITION = 0
@@ -14,7 +15,7 @@ class UnityCommunication:
1415

1516
PYTHON_READY_POSITION = 100000
1617
ACTUATOR_DATA_POSITION = 100001
17-
18+
# FILE_NAME = "../../../ml-agents-ecs/Assets/shared_communication_file.txt"
1819
FILE_NAME = "shared_communication_file.txt"
1920

2021
def __init__(self):

0 commit comments

Comments
 (0)