-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
284 lines (242 loc) · 10.7 KB
/
parser.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from typing import List
import sys
import os
import subprocess
CELL_ON = "L"
CELL_AT = "D"
BUTTON_AT = "d"
BUTTON_ON = "l"
POSITION = "position"
SOLVER = ""
PROBLEM_PATH = ""
DOMAIN_PATH = ""
def generate_cell_on_button_at(len_lines, len_columns):
predicates_cell_on = []
for i in range(1, len_lines+1):
for j in range(1, len_columns+1):
if(lines[i-1][j-1] == CELL_ON):
predicates_cell_on.append(f"(cell-on x{i} y{j})")
if(lines[i-1][j-1] == BUTTON_AT):
predicates_cell_on.append(f"(button-at x{i} y{j})")
if(lines[i-1][j-1] == BUTTON_ON):
predicates_cell_on.append(f"(button-at x{i} y{j})")
predicates_cell_on.append(f"(cell-on x{i} y{j})")
return " ".join(predicates_cell_on)
def generate_edge(len_lines, len_columns):
predicates_edge = []
for i in range(1, len_lines+1):
for j in range(1, len_columns+1):
if ((j == 1 and i!=1 and i != len_lines) or (j == len_columns and i!=1 and i != len_lines) or (i == 1 and j!=1 and j != len_columns) or (i == len_lines and j!=1 and j != len_columns)):
predicates_edge.append(f"(is-edge x{i} y{j})")
return " ".join(predicates_edge)
def generate_vertice(len_lines, len_columns):
predicates_vertice = []
predicates_vertice.append(f"(is-vertice x{1} y{1})")
predicates_vertice.append(f"(is-vertice x{1} y{len_columns})")
predicates_vertice.append(f"(is-vertice x{len_lines} y{1})")
predicates_vertice.append(f"(is-vertice x{len_lines} y{len_columns})")
return " ".join(predicates_vertice)
def generate_incs(len_lines, len_columns):
predicates_x = []
predicates_y = []
for i in range(1, len_lines):
predicates_x.append(f"(inc x{i} x{i+1})")
for i in range(1, len_columns):
predicates_y.append(f"(inc y{i} y{i+1})")
return " ".join(predicates_x), " ".join(predicates_y)
def generate_decs(len_lines, len_columns):
predicates_x = []
predicates_y = []
for i in range(len_lines, 1, -1):
predicates_x.append(f"(dec x{i} x{i-1})")
for i in range(len_columns, 1, -1):
predicates_y.append(f"(dec y{i} y{i-1})")
return " ".join(predicates_x), " ".join(predicates_y)
def generate_problem(map_path):
problem_path = map_path + ".pddl"
y_positions = []
x_positions = []
for i in range(num_lines):
x_positions.append(f"x{i+1}")
for j in range(num_columns):
y_positions.append(f"y{j+1}")
inc_x, inc_y = generate_incs(num_lines, num_columns)
dec_x, dec_y = generate_decs(num_lines, num_columns)
cell_on = generate_cell_on_button_at(num_lines, num_columns)
is_vertice = generate_vertice(num_lines, num_columns)
is_edge = generate_edge(num_lines, num_columns)
with open(problem_path, 'w') as problem_file:
problem_file.write(f"""
(define (problem {map_path})
(:domain lightsout)
(:objects {" ".join(x_positions)} - x_position {" ".join(y_positions)} - y_position)
(:init
{inc_x}
{inc_y}
{dec_x}
{dec_y}
{cell_on}
{is_vertice}
{is_edge}
)
(:goal
(and
(forall (?x - x_position ?y - y_position)
(not (cell-on ?x ?y))
)
)
)
)
""")
def generate_domain():
domain_path = "domainOptimizedTeste.pddl"
with open(domain_path, 'w') as domain_file:
domain_file.write(f"""
(define (domain lightsout)
(:requirements :strips)
(:types
x_position y_position - position
)
(:predicates
(button-at ?a - x_position ?b - y_position)
(inc ?a ?b - position)
(dec ?a ?b - position)
(cell-on ?a - x_position ?b - y_position)
(is-vertice ?a - x_position ?b - y_position)
(is-edge ?a - x_position ?b - y_position)
)
(:action INVERT_CELL
:parameters (?x ?x2 ?x0 - x_position ?y ?y2 ?y0 - y_position)
:precondition (and (inc ?y ?y2)
(inc ?x ?x2)
(dec ?y ?y0)
(dec ?x ?x0)
)
:effect (and
(when (and (cell-on ?x ?y) (not(button-at ?x ?y))) (not (cell-on ?x ?y)))
(when (and (not (cell-on ?x ?y)) (not(button-at ?x ?y))) (cell-on ?x ?y))
(when (cell-on ?x ?y2) (not (cell-on ?x ?y2)))
(when (not (cell-on ?x ?y2)) (cell-on ?x ?y2))
(when (cell-on ?x ?y0) (not (cell-on ?x ?y0)))
(when (not (cell-on ?x ?y0)) (cell-on ?x ?y0))
(when (cell-on ?x0 ?y) (not (cell-on ?x0 ?y)))
(when (not (cell-on ?x0 ?y)) (cell-on ?x0 ?y))
(when (cell-on ?x2 ?y) (not (cell-on ?x2 ?y)))
(when (not (cell-on ?x2 ?y)) (cell-on ?x2 ?y))
)
)
(:action INVERT_VERTICE
:parameters(?x ?x2 - x_position ?y ?y2 - y_position)
:precondition (and
(is-vertice ?x ?y)
(or
(and (inc ?x ?x2) (dec ?y ?y2))
(and (inc ?y ?y2) (inc ?x ?x2))
(and (dec ?x ?x2) (dec ?y ?y2))
(and (dec ?x ?x2) (inc ?y ?y2))
)
)
:effect (and
(when (and (cell-on ?x ?y) (not(button-at ?x ?y))) (not (cell-on ?x ?y)))
(when (and (not (cell-on ?x ?y))(not(button-at ?x ?y))) (cell-on ?x ?y))
(when (cell-on ?x ?y2) (not (cell-on ?x ?y2)))
(when (not (cell-on ?x ?y2)) (cell-on ?x ?y2))
(when (cell-on ?x2 ?y) (not (cell-on ?x2 ?y)))
(when (not (cell-on ?x2 ?y)) (cell-on ?x2 ?y))
)
)
(:action INVERT_EDGE
:parameters (?x ?a - x_position ?y ?c - y_position ?b - position)
:precondition (and
(is-edge ?x ?y)
(or
(and (inc ?x ?a) (dec ?x ?b) (dec ?y ?c))
(and (inc ?x ?a) (dec ?x ?b) (inc ?y ?c))
(and (inc ?x ?a) (inc ?y ?b) (dec ?y ?c))
(and (dec ?x ?a) (inc ?y ?b) (dec ?y ?c))
)
)
:effect (and
(when (and (cell-on ?x ?y) (not(button-at ?x ?y))) (not (cell-on ?x ?y)))
(when (and (not (cell-on ?x ?y))(not(button-at ?x ?y))) (cell-on ?x ?y))
(when (cell-on ?a ?y) (not (cell-on ?a ?y)))
(when (not (cell-on ?a ?y)) (cell-on ?a ?y))
(when (cell-on ?x ?c) (not (cell-on ?x ?c)))
(when (not (cell-on ?x ?c)) (cell-on ?x ?c))
(when (and (cell-on ?b ?y)(dec ?x ?b)) (not (cell-on ?b ?y)))
(when (and (not (cell-on ?b ?y))(dec ?x ?b)) (cell-on ?b ?y))
(when (and (cell-on ?x ?b) (inc ?y ?b)) (not (cell-on ?x ?b)))
(when (and (not (cell-on ?x ?b)) (inc ?y ?b)) (cell-on ?x ?b))
)
)
)
""")
def main():
global lines
lines = []
global num_lines
num_lines = 0
global num_columns
num_columns = 0
while True:
try:
user_input = input()
lines.append(user_input)
num_lines += 1
except EOFError:
break
num_columns = len(lines[0])
generate_domain()
generate_problem("problemaOptimizedTeste")
command = " /tmp/dir/software/planners/madagascar/M -S 1 domainOptimizedTeste.pddl problemaOptimizedTeste.pddl"
result = subprocess.check_output(command, shell=True, text=True)
clicks = result.split("\n")
# Deixando apenas as linhas com invert nela
only_invert = []
for line in clicks:
if "STEP" in line:
index_begin = line.find("i")
line = line[index_begin:]
only_invert.append(line)
# Para cada linha da lista, um comando invert
one_invert_per_line = []
for line in only_invert:
# Checando numero de ocorrencias do invert
count = line.count("invert")
if count > 1:
# Separar a linha em varias linhas, um invert em cada
new_lines = line.split()
# A lista se torna ela + as linhas separadas
one_invert_per_line = one_invert_per_line + new_lines
else:
# Se so tiver um invert, some a linha a nova lista
one_invert_per_line.append(line)
# Lista final com as coordenadas conforme enunciado do exercicio
final_list = []
for line in one_invert_per_line:
index_x = line.find("x")
first_comma = line.find(",")
index_y = line.find("y")
second_comma = line.find(",", first_comma+1)
third_comma = line.find(",", second_comma+1)
if "cell" in line:
final_comma = line.find(",", third_comma+1)
else:
final_comma = third_comma
x = int(line[index_x+1 : first_comma]) - 1
y = int(line[index_y+1:final_comma]) - 1
text = "(" + str(x) + ", " + str(y) + ")"
final_list.append(text)
for i in range(len(final_list)-1):
print(final_list[i]+";", end="")
print(final_list[-1])
#Ler saída do madascagar
#Tratar a saída
#Printar a saída
if __name__ == "__main__":
""" if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <map_filepath>")
print("Error: map path must be specified")
exit(1) """
main()
####################