Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions Assets/ECSEnvironment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from python_communication import UnityCommunication
from .brain import BrainInfo, BrainParameters


class ECSEnvironment(object):
VEC_SIZE = 8
ACT_SIZE = 3
Expand Down Expand Up @@ -37,18 +38,23 @@ def close(self):

def make_brain_info(self, sensor):
# This assumes the order is consistent
self.step_count+=1
self.step_count += 1
done = False
if self.step_count % 50 == 0:
done = True
return {"ECSBrain" : BrainInfo([], sensor, [" "] * sensor.shape[0],
reward=sensor[:,0],
agents=list(range(sensor.shape[0])),
local_done=[done] * sensor.shape[0],
max_reached=[done] * sensor.shape[0],
vector_action=sensor,
text_action = [" "] * sensor.shape[0])}

return {
"ECSBrain": BrainInfo(
[],
sensor,
[" "] * sensor.shape[0],
reward=sensor[:, 0],
agents=list(range(sensor.shape[0])),
local_done=[done] * sensor.shape[0],
max_reached=[done] * sensor.shape[0],
vector_action=sensor,
text_action=[" "] * sensor.shape[0],
)
}

@property
def curriculum(self):
Expand All @@ -60,9 +66,17 @@ def logfile_path(self):

@property
def brains(self):
return {"ECSBrain": BrainParameters("ECSBrain", self.VEC_SIZE, 1,
[], [self.ACT_SIZE],
[" "]*self.ACT_SIZE, 1)} # 1 for continuopus
return {
"ECSBrain": BrainParameters(
"ECSBrain",
self.VEC_SIZE,
1,
[],
[self.ACT_SIZE],
[" "] * self.ACT_SIZE,
1,
)
} # 1 for continuopus

@property
def global_done(self):
Expand All @@ -89,7 +103,6 @@ def external_brain_names(self):
return ["ECSBrain"]



# if __name__ == "__main__":
# comm = UnityCommunication()
#
Expand All @@ -110,4 +123,3 @@ def external_brain_names(self):
# comm.set_ready()
#


62 changes: 31 additions & 31 deletions Assets/python_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import struct
import numpy as np


class UnityCommunication:
FILE_CAPACITY = 200000
NUMBER_AGENTS_POSITION = 0
Expand All @@ -20,26 +21,30 @@ def __init__(self):
# memory-map the file, size 0 means whole file
self.accessor = mmap.mmap(f.fileno(), 0)

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

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

sensor = np.frombuffer(
buffer=self.accessor[self.SENSOR_DATA_POSITION: self.SENSOR_DATA_POSITION
+ 4*sensor_size*number_agents],
buffer=self.accessor[
self.SENSOR_DATA_POSITION : self.SENSOR_DATA_POSITION
+ 4 * sensor_size * number_agents
],
dtype=np.float32,
count=sensor_size * number_agents,
offset=0
offset=0,
)
return np.reshape(sensor, (number_agents, sensor_size))

def get_parameters(self) -> (int, int, int):
return self.get_int(self.NUMBER_AGENTS_POSITION), \
self.get_int(self.SENSOR_SIZE_POSITION), \
self.get_int(self.ACTUATOR_SIZE_POSITION)
return (
self.get_int(self.NUMBER_AGENTS_POSITION),
self.get_int(self.SENSOR_SIZE_POSITION),
self.get_int(self.ACTUATOR_SIZE_POSITION),
)

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

try:
assert(actuator.shape == (number_agents, actuator_size))
assert actuator.shape == (number_agents, actuator_size)
except:
print("_________")
print(actuator.shape)
print((number_agents, actuator_size))

self.accessor[self.ACTUATOR_DATA_POSITION: self.ACTUATOR_DATA_POSITION + 4*actuator_size*number_agents] = \
actuator.tobytes()
self.accessor[
self.ACTUATOR_DATA_POSITION : self.ACTUATOR_DATA_POSITION
+ 4 * actuator_size * number_agents
] = actuator.tobytes()

def set_ready(self):
self.accessor[self.UNITY_READY_POSITION: self.UNITY_READY_POSITION+1] = bytearray(struct.pack("b", False))
self.accessor[self.PYTHON_READY_POSITION: self.PYTHON_READY_POSITION+1] = bytearray(struct.pack("b", True))
self.accessor[
self.UNITY_READY_POSITION : self.UNITY_READY_POSITION + 1
] = bytearray(struct.pack("b", False))
self.accessor[
self.PYTHON_READY_POSITION : self.PYTHON_READY_POSITION + 1
] = bytearray(struct.pack("b", True))

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


if __name__ == "__main__":
comm = UnityCommunication()

COMMS = UnityCommunication()
steps = 0
while True:

u_ready = False
while not u_ready:
u_ready = comm.unity_ready()
u_ready = COMMS.unity_ready()
steps += 1
s = comm.read_sensor()
nag, nse, nac = comm.get_parameters()
print('Number of agents is {}'.format(nag))
s = COMMS.read_sensor()
nag, nse, nac = COMMS.get_parameters()
print("Number of agents is {}".format(nag))
# print(s.shape)
# time.sleep(0.1)
comm.write_actuator(
np.random.normal(size=(nag, nac))
)
comm.set_ready()








COMMS.write_actuator(np.random.normal(size=(nag, nac)))
COMMS.set_ready()