-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_stochastic.py
More file actions
143 lines (87 loc) · 3.13 KB
/
decoder_stochastic.py
File metadata and controls
143 lines (87 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#################### COPY OF DECODER.PY WITH MINOR CHANGES FOR PROBABILTY ##########################################
import sys
import math
from random import randint
possible_directions = ['N','S','E','W']
"""
Helper function to read a file
It takes a file name a input and populates a list with the all_actions values.
It returns a list of number of states and all actions
"""
def fun_file_read(file_name):
curr_line = 0
all_actions = []
with open(file_name) as f:
for line in f:
#iterating for all lines
curr_line = curr_line + 1
#splitting a line to extract values
all_words = line.split()
if all_words[0]=='iterations':
number_of_states = curr_line-1
return [number_of_states,all_actions]
continue
else:
all_actions.append(int(all_words[1]))
"""
Helper function to read a grid from a file.
It takes the file name as the only argument.
It returns a grid as output.
"""
def extract_grid(file_name):
with open(file_name) as f:
line_no = 0
for line in f:
all_words = line.split()
if line_no == 0:
#define a matrix of size all_words * all_words
grid = [[0 for i in range(len(all_words))] for j in range(len(all_words))]
for col in range(len(all_words)):
#populating the values
grid[line_no][col] = int(all_words[col])
line_no = line_no + 1
return grid
"""
Function to print the path of the agent
It takes start position, end position, number of states and all actions as input
and print the path.
====> added probability
It does not explicitly return anything
"""
def extract_map(start_position, end_position, number_of_states, all_actions, probab):
curr_state = start_position
end_state = end_position
W = int(math.sqrt(number_of_states))
while curr_state != end_state:
curr_action = all_actions[curr_state]
print(possible_directions[curr_action], end = " ")
i = int(curr_state / W)
j = int(curr_state % W)
probab_val_true = [True] * int(100*probab)
probab_val_false = [False] * (100 - int(100*probab))
all_probab = probab_val_true + probab_val_false
#checking for the action and assigning the nearby state with the value
if curr_action == 0 and all_probab[randint(0, 99)] == True:
nearby_state = (i-1, j)
elif curr_action == 1 and all_probab[randint(0, 99)] == True:
nearby_state = (i+1,j)
elif curr_action == 2 and all_probab[randint(0, 99)] == True:
nearby_state = (i,j+1)
elif curr_action == 3 and all_probab[randint(0, 99)] == True:
nearby_state = (i,j-1)
curr_state = W * nearby_state[0] + nearby_state[1]
print(" ", end = "\n")
if __name__ == "__main__":
input_grid = sys.argv[1]
file_name = sys.argv[2]
probab = sys.argv[3]
grid = extract_grid(input_grid)
W = len(grid[0])
start_position = [(i,j) for i in range(W) for j in range(W) if grid[i][j]==2]
end = [(i,j) for i in range(W) for j in range(W) if grid[i][j]==3]
start_position = start_position[0]
end = end[0]
start_position = W * start_position[0] + start_position[1]
end = W*end[0] + end[1]
[number_of_states,all_actions] = fun_file_read(file_name)
extract_map(start_position,end,number_of_states,all_actions, probab)