Skip to content

Commit

Permalink
3x3 neighborhood function done
Browse files Browse the repository at this point in the history
Appears to be finding solution a faster - still need to edit wall padding
  • Loading branch information
adamasb committed Nov 29, 2022
1 parent c96c8b5 commit 877a83f
Show file tree
Hide file tree
Showing 21 changed files with 1,534 additions and 48 deletions.
2 changes: 1 addition & 1 deletion makeHPC.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ voltash

source virt1/bin/activate

export /zhome/8b/7/122640/Mavi/src/raya3c PYTHONPATH='$PYTHONPATH:/zhome/8b/7/122640/Mavi/src'
export PYTHONPATH='$PYTHONPATH:/zhome/8b/7/122640/Mavi/src'

module load cuda

Binary file modified src/mazeenv/__pycache__/maze_register.cpython-39.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion src/mazeenv/maze_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
id="MazeDeterministic_empty4-v0",
entry_point='mazeenv.maze_environment:MazeEnvironment',
max_episode_steps=200,
kwargs=dict(size=5, blockpct=0), #size=4 is default, change this to change size of map# removed seed = 0
kwargs=dict(size=4, blockpct=0), #size=4 is default, change this to change size of map# removed seed = 0
)

from ray.tune.registry import register_env
Expand Down
87 changes: 54 additions & 33 deletions src/raya3c/example_vin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, obs_space, action_space, num_outputs, model_config, name):


#not being used right now
self.nn = SlimFC(4*4*4, self.num_outputs)# generalize input dimensions
self.nn = SlimFC(3*3*4, self.num_outputs)# generalize input dimensions


#is this really all it is?
Expand All @@ -69,7 +69,8 @@ def __init__(self, obs_space, action_space, num_outputs, model_config, name):


layers = []
prev_layer_size = int(np.product(obs_space.shape)*4/3) #hacky way to get the right input size
#can hardcode the value because it will always be 3x3*4
prev_layer_size = 36# int(np.product(obs_space.shape)*4/3) #hacky way to get the right input size
self._logits = None

# Create layers 0 to second-last.
Expand Down Expand Up @@ -120,9 +121,6 @@ def __init__(self, obs_space, action_space, num_outputs, model_config, name):
self._last_flat_in = None


""" Everythin above (until comment) is stolen straight from fcnet"""


"""What the heck is this?"""
# def value_function_for_env_state(self, state):
# """WHAR IS THIS SUPPOSED TO BE?"""
Expand Down Expand Up @@ -227,6 +225,41 @@ def VP_nn(self,s,phi,K=10):
return vp



def get_neighborhood(self, obs,dim4,a_index):

neighborhood = []
v_matrix, w_matrix, a_matrix,g_matrix = [], [], [], []

for ii in range(obs.shape[0]):
v_matrix.append(torch.nn.functional.pad(dim4[ii].squeeze(),(1,1,1,1))) #dont wanna override tensors
w_matrix.append(torch.nn.functional.pad(obs[ii][:,:,0],(1,1,1,1))) #change padding to 1's (or invert 1s and 0s all over)
a_matrix.append(torch.nn.functional.pad(obs[ii][:,:,1],(1,1,1,1)))
g_matrix.append(torch.nn.functional.pad(obs[ii][:,:,2],(1,1,1,1)))

rowNumber = a_index[ii][0] +1 #numpy array so okay to override
colNumber = a_index[ii][1] +1 #plus 1 to account for padding
v_result, w_result, a_result, g_result = [], [], [], []

for rowAdd in range(-1, 2):
newRow = rowNumber + rowAdd
if newRow >= 0 and newRow <= len(v_matrix[ii])-1:
for colAdd in range(-1, 2):
newCol = colNumber + colAdd
if newCol >= 0 and newCol <= len(v_matrix[ii])-1:
if newCol == colNumber and newRow == rowNumber:
pass# this is the agent location itself
#continue
v_result.append(v_matrix[ii][newRow][newCol])
w_result.append(w_matrix[ii][newRow][newCol])
a_result.append(a_matrix[ii][newRow][newCol])
g_result.append(g_matrix[ii][newRow][newCol])

neighborhood.append(torch.tensor([w_result, a_result, g_result, v_result]).flatten())


return torch.stack(neighborhood)


def forward(self, input_dict, state, seq_lens): #dont think this is currently being used
obs = input_dict["obs_flat"]
Expand Down Expand Up @@ -264,7 +297,9 @@ def forward(self, input_dict, state, seq_lens): #dont think this is currently be
#generalize dimensions
dim4.append(self.VP_nn(obs[ii].reshape((width,width,3)),phi_vals))

s = obs[ii].reshape((width,width,3))

""" removed these lines because using the get_neighborhood function instead"""
s = input_dict["obs"][ii]
s[:,:,0]= 1 - s[:, :, 0] #walls
vp.append(torch.flatten(torch.cat((s,dim4[-1]),dim=2))) #concatenating the 3d tensor with the 2d tensor, and flattening it
# np.append(vp,self.VP_nn(obs[ii].reshape((4,4,3)),phi_vals))
Expand All @@ -276,7 +311,7 @@ def forward(self, input_dict, state, seq_lens): #dont think this is currently be
# if len(a_index) > 1:
# print('more than one batch')
else:
a_index.append([0,0])
a_index.append([1,1]) #this doesnt really matter

# V_np = []
# assert( (V_np - V_torch.numpy())< 1e-8 )
Expand All @@ -302,43 +337,29 @@ def forward(self, input_dict, state, seq_lens): #dont think this is currently be

""" consider passing just the values of the 3x3 neighbourhood around the agent into the network"""

#should be a list of tensors of size (5*3) (maybe * batch size)
# neighbourhood = []
# matrix = []
# neib= []
# result = [ [] for _ in obs.shape[0] ]
# for ii in range(obs.shape[0]):
# matrix.append(torch.nn.functional.pad(vp[ii],(1,1,1,1))) #dont wanna override tensors
# # rowNumber = a_index[ii][0] #numpy array so okay to override
# # colNumber = a_index[ii][1]
# # result = []
# # for rowAdd in range(-1, 2):
# # newRow = rowNumber + rowAdd
# # if newRow >= 0 and newRow <= len(matrix[ii])-1:
# # for colAdd in range(-1, 2):
# # newCol = colNumber + colAdd
# # if newCol >= 0 and newCol <= len(matrix[ii])-1:
# # if newCol == colNumber and newRow == rowNumber:
# # continue
# # result.append(matrix[ii][newCol][newRow])

# should be a list of tensors of size (5*3) (maybe * batch size)

# neighbourhood.append(result)
# # return result
# print(input_dict["obs"].shape)
# print(dim4.shape)
#print(type(a_index))
#neighborhood = self.get_neighborhood(input_dict["obs"],dim4,a_index)

# print(self.get_neighborhood(input_dict["obs"],dim4,a_index))
# print("a")
#neighborhood[0][:9].reshape(3,3)


# if obs.shape[0] > 1:
# if obs.any() !=0:
# #print(neighbourhood[-1])
# pass

"""has shape (batch size, 3*3*4)"""
# vp = self.get_neighborhood(input_dict["obs"],dim4,a_index)





self._last_flat_in = torch.stack(vp)
self._last_flat_in = self.get_neighborhood(input_dict["obs"],dim4,a_index)
# self._last_flat_in = torch.stack(vp)

#.reshape(vp.shape[0], -1)
#mat1 and mat2 shapes cannot be multiplied (32x64 and 48x24)
Expand Down
Loading

0 comments on commit 877a83f

Please sign in to comment.