Skip to content

Commit 1f8459a

Browse files
Merge pull request #5 from Unity-Technologies/develop-black-cleanups
Reformatted python via black
2 parents 3c59b91 + 8a7b736 commit 1f8459a

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

Assets/ECSEnvironment.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from python_communication import UnityCommunication
66
from .brain import BrainInfo, BrainParameters
77

8+
89
class ECSEnvironment(object):
910
VEC_SIZE = 8
1011
ACT_SIZE = 3
@@ -37,18 +38,23 @@ def close(self):
3738

3839
def make_brain_info(self, sensor):
3940
# This assumes the order is consistent
40-
self.step_count+=1
41+
self.step_count += 1
4142
done = False
4243
if self.step_count % 50 == 0:
4344
done = True
44-
return {"ECSBrain" : BrainInfo([], sensor, [" "] * sensor.shape[0],
45-
reward=sensor[:,0],
46-
agents=list(range(sensor.shape[0])),
47-
local_done=[done] * sensor.shape[0],
48-
max_reached=[done] * sensor.shape[0],
49-
vector_action=sensor,
50-
text_action = [" "] * sensor.shape[0])}
51-
45+
return {
46+
"ECSBrain": BrainInfo(
47+
[],
48+
sensor,
49+
[" "] * sensor.shape[0],
50+
reward=sensor[:, 0],
51+
agents=list(range(sensor.shape[0])),
52+
local_done=[done] * sensor.shape[0],
53+
max_reached=[done] * sensor.shape[0],
54+
vector_action=sensor,
55+
text_action=[" "] * sensor.shape[0],
56+
)
57+
}
5258

5359
@property
5460
def curriculum(self):
@@ -60,9 +66,17 @@ def logfile_path(self):
6066

6167
@property
6268
def brains(self):
63-
return {"ECSBrain": BrainParameters("ECSBrain", self.VEC_SIZE, 1,
64-
[], [self.ACT_SIZE],
65-
[" "]*self.ACT_SIZE, 1)} # 1 for continuopus
69+
return {
70+
"ECSBrain": BrainParameters(
71+
"ECSBrain",
72+
self.VEC_SIZE,
73+
1,
74+
[],
75+
[self.ACT_SIZE],
76+
[" "] * self.ACT_SIZE,
77+
1,
78+
)
79+
} # 1 for continuopus
6680

6781
@property
6882
def global_done(self):
@@ -89,7 +103,6 @@ def external_brain_names(self):
89103
return ["ECSBrain"]
90104

91105

92-
93106
# if __name__ == "__main__":
94107
# comm = UnityCommunication()
95108
#
@@ -110,4 +123,3 @@ def external_brain_names(self):
110123
# comm.set_ready()
111124
#
112125

113-

Assets/python_communication.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import struct
33
import numpy as np
44

5+
56
class UnityCommunication:
67
FILE_CAPACITY = 200000
78
NUMBER_AGENTS_POSITION = 0
@@ -20,26 +21,30 @@ def __init__(self):
2021
# memory-map the file, size 0 means whole file
2122
self.accessor = mmap.mmap(f.fileno(), 0)
2223

23-
def get_int(self, position : int) -> int:
24-
return struct.unpack("i", self.accessor[position:position + 4])[0]
24+
def get_int(self, position: int) -> int:
25+
return struct.unpack("i", self.accessor[position : position + 4])[0]
2526

2627
def read_sensor(self) -> np.ndarray:
2728
sensor_size = self.get_int(self.SENSOR_SIZE_POSITION)
2829
number_agents = self.get_int(self.NUMBER_AGENTS_POSITION)
2930

3031
sensor = np.frombuffer(
31-
buffer=self.accessor[self.SENSOR_DATA_POSITION: self.SENSOR_DATA_POSITION
32-
+ 4*sensor_size*number_agents],
32+
buffer=self.accessor[
33+
self.SENSOR_DATA_POSITION : self.SENSOR_DATA_POSITION
34+
+ 4 * sensor_size * number_agents
35+
],
3336
dtype=np.float32,
3437
count=sensor_size * number_agents,
35-
offset=0
38+
offset=0,
3639
)
3740
return np.reshape(sensor, (number_agents, sensor_size))
3841

3942
def get_parameters(self) -> (int, int, int):
40-
return self.get_int(self.NUMBER_AGENTS_POSITION), \
41-
self.get_int(self.SENSOR_SIZE_POSITION), \
42-
self.get_int(self.ACTUATOR_SIZE_POSITION)
43+
return (
44+
self.get_int(self.NUMBER_AGENTS_POSITION),
45+
self.get_int(self.SENSOR_SIZE_POSITION),
46+
self.get_int(self.ACTUATOR_SIZE_POSITION),
47+
)
4348

4449
def write_actuator(self, actuator: np.ndarray):
4550
actuator_size = self.get_int(self.ACTUATOR_SIZE_POSITION)
@@ -50,18 +55,24 @@ def write_actuator(self, actuator: np.ndarray):
5055
actuator = actuator.astype(np.float32)
5156

5257
try:
53-
assert(actuator.shape == (number_agents, actuator_size))
58+
assert actuator.shape == (number_agents, actuator_size)
5459
except:
5560
print("_________")
5661
print(actuator.shape)
5762
print((number_agents, actuator_size))
5863

59-
self.accessor[self.ACTUATOR_DATA_POSITION: self.ACTUATOR_DATA_POSITION + 4*actuator_size*number_agents] = \
60-
actuator.tobytes()
64+
self.accessor[
65+
self.ACTUATOR_DATA_POSITION : self.ACTUATOR_DATA_POSITION
66+
+ 4 * actuator_size * number_agents
67+
] = actuator.tobytes()
6168

6269
def set_ready(self):
63-
self.accessor[self.UNITY_READY_POSITION: self.UNITY_READY_POSITION+1] = bytearray(struct.pack("b", False))
64-
self.accessor[self.PYTHON_READY_POSITION: self.PYTHON_READY_POSITION+1] = bytearray(struct.pack("b", True))
70+
self.accessor[
71+
self.UNITY_READY_POSITION : self.UNITY_READY_POSITION + 1
72+
] = bytearray(struct.pack("b", False))
73+
self.accessor[
74+
self.PYTHON_READY_POSITION : self.PYTHON_READY_POSITION + 1
75+
] = bytearray(struct.pack("b", True))
6576

6677
def unity_ready(self) -> bool:
6778
return self.accessor[self.UNITY_READY_POSITION]
@@ -71,30 +82,19 @@ def close(self):
7182

7283

7384
if __name__ == "__main__":
74-
comm = UnityCommunication()
75-
85+
COMMS = UnityCommunication()
7686
steps = 0
7787
while True:
7888

7989
u_ready = False
8090
while not u_ready:
81-
u_ready = comm.unity_ready()
91+
u_ready = COMMS.unity_ready()
8292
steps += 1
83-
s = comm.read_sensor()
84-
nag, nse, nac = comm.get_parameters()
85-
print('Number of agents is {}'.format(nag))
93+
s = COMMS.read_sensor()
94+
nag, nse, nac = COMMS.get_parameters()
95+
print("Number of agents is {}".format(nag))
8696
# print(s.shape)
8797
# time.sleep(0.1)
88-
comm.write_actuator(
89-
np.random.normal(size=(nag, nac))
90-
)
91-
comm.set_ready()
92-
93-
94-
95-
96-
97-
98-
99-
98+
COMMS.write_actuator(np.random.normal(size=(nag, nac)))
99+
COMMS.set_ready()
100100

0 commit comments

Comments
 (0)