|
| 1 | +import mmap |
| 2 | +import struct |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from .brain import BrainInfo, BrainParameters |
| 6 | + |
| 7 | + |
| 8 | +class ECSEnvironment(object): |
| 9 | + VEC_SIZE = 8 |
| 10 | + ACT_SIZE = 3 |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + self.comm = UnityCommunication() |
| 14 | + self.step_count = 0 |
| 15 | + print(" READY TO COMMUNICATE") |
| 16 | + |
| 17 | + def reset(self, train_mode=True, config=None, lesson=None): |
| 18 | + u_ready = False |
| 19 | + while not u_ready: |
| 20 | + u_ready = self.comm.unity_ready() |
| 21 | + s = self.comm.read_sensor() |
| 22 | + return self.make_brain_info(s) |
| 23 | + |
| 24 | + def step(self, vector_action=None, memory=None, text_action=None, value=None): |
| 25 | + # print("step") |
| 26 | + self.comm.write_actuator(vector_action["ECSBrain"]) |
| 27 | + self.comm.set_ready() |
| 28 | + |
| 29 | + u_ready = False |
| 30 | + while not u_ready: |
| 31 | + u_ready = self.comm.unity_ready() |
| 32 | + s = self.comm.read_sensor() |
| 33 | + return self.make_brain_info(s) |
| 34 | + |
| 35 | + def close(self): |
| 36 | + self.comm.close() |
| 37 | + |
| 38 | + def make_brain_info(self, sensor): |
| 39 | + # This assumes the order is consistent |
| 40 | + self.step_count+=1 |
| 41 | + done = False |
| 42 | + if self.step_count % 50 == 0: |
| 43 | + 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 | + |
| 52 | + |
| 53 | + @property |
| 54 | + def curriculum(self): |
| 55 | + return None |
| 56 | + |
| 57 | + @property |
| 58 | + def logfile_path(self): |
| 59 | + return None |
| 60 | + |
| 61 | + @property |
| 62 | + def brains(self): |
| 63 | + return {"ECSBrain": BrainParameters("ECSBrain", self.VEC_SIZE, 1, |
| 64 | + [], [self.ACT_SIZE], |
| 65 | + [" "]*self.ACT_SIZE, 1)} # 1 for continuopus |
| 66 | + |
| 67 | + @property |
| 68 | + def global_done(self): |
| 69 | + return False |
| 70 | + |
| 71 | + @property |
| 72 | + def academy_name(self): |
| 73 | + return "ECSAcademy" |
| 74 | + |
| 75 | + @property |
| 76 | + def number_brains(self): |
| 77 | + return 1 |
| 78 | + |
| 79 | + @property |
| 80 | + def number_external_brains(self): |
| 81 | + return 1 |
| 82 | + |
| 83 | + @property |
| 84 | + def brain_names(self): |
| 85 | + return ["ECSBrain"] |
| 86 | + |
| 87 | + @property |
| 88 | + def external_brain_names(self): |
| 89 | + return ["ECSBrain"] |
| 90 | + |
| 91 | + |
| 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 | +# if __name__ == "__main__": |
| 164 | +# comm = UnityCommunication() |
| 165 | +# |
| 166 | +# steps = 0 |
| 167 | +# while True: |
| 168 | +# |
| 169 | +# u_ready = False |
| 170 | +# while not u_ready: |
| 171 | +# u_ready = comm.unity_ready() |
| 172 | +# steps += 1 |
| 173 | +# s = comm.read_sensor() |
| 174 | +# nag, nse, nac = comm.get_parameters() |
| 175 | +# # print(s.shape) |
| 176 | +# # time.sleep(0.1) |
| 177 | +# comm.write_actuator( |
| 178 | +# np.random.normal(size=(nag, nac)) |
| 179 | +# ) |
| 180 | +# comm.set_ready() |
| 181 | +# |
| 182 | + |
| 183 | + |
0 commit comments