22import struct
33import numpy as np
44
5+
56class 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
7384if __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