-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsokoPlan.py
executable file
·242 lines (212 loc) · 6.62 KB
/
sokoPlan.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
#!/usr/bin/env python3
import sys
import enum
from enum import IntEnum
pddlStrings = [
"""(define (problem sokoban-1)
(:domain sokoban)
(:requirements :strips :typing)
(:objects """,
""" )
(:init""",
""" )
(:goal (and \t""",
""" ))
)""",
]
xM = 0
xY = 0
level = []
requiredCountWall = 0
opt_wall = []
requiredCountBox = 0
opt_box = []
requiredCountGoal = 0
opt_goal = []
opt_player = []
pddlGrid = []
class Block(IntEnum):
FLOOR = 0
WALL = 1
GOAL = 2
BOX = 3
BOX_ON_GOAL = 4
PLAYER = 5
PLAYER_ON_GOAL = 6
def isAcessible(block):
if block in {
Block.FLOOR,
Block.GOAL,
Block.BOX,
Block.BOX_ON_GOAL,
Block.PLAYER,
Block.PLAYER_ON_GOAL,
}:
return True
return False
def readOptional(f):
opt = [[False for c in row] for row in level]
for i, line in enumerate(f):
if line[0] == ';':
break
for j, c in enumerate(line):
if c == "?":
opt[i][j] = True
required = 0
if len(line.split()) > 2:
required = int(line.split()[2])
return required, opt
def readLevelFromFile(pathToLevel):
f = open(pathToLevel, "r")
global yM
global xM
for line in f:
if line[0] == ';':
continue
if len(line) - 1 > xM:
xM = len(line) - 1
f.seek(0)
global level
for line in f:
if line[0] == ';':
break
# print(line,end='')
row = []
for c in line:
if c == "#":
row.append(Block.WALL)
if c == "+":
row.append(Block.PLAYER_ON_GOAL)
if c == "@":
row.append(Block.PLAYER)
if c == "$":
row.append(Block.BOX)
if c == "*":
row.append(Block.BOX_ON_GOAL)
if c == ".":
row.append(Block.GOAL)
if c == "_" or c == " " or c == "-":
row.append(Block.FLOOR)
for i in range(xM - len(row)):
row.append(Block.WALL)
assert len(row) == xM
level.append(row)
yM = len(level)
global requiredCountWall
global opt_wall
requiredCountWall, opt_wall = readOptional(f)
global requiredCountBox
global opt_box
requiredCountBox, opt_box = readOptional(f)
global requiredCountGoal
global opt_goal
requiredCountGoal, opt_goal = readOptional(f)
global opt_player
_, opt_player = readOptional(f)
def getPddlGrid(level):
global pddlGrid
pddlGrid = [["" for c in row] for row in level]
for y in range(yM):
for x in range(xM):
if not isAcessible(level[y][x]):
continue
# if deadSquares[y][x]:
# squares[y][x] = "p" + str(y) + "_" + str(x)
# else:
# squares[y][x] = "b" + str(y) + "_" + str(x)
pddlGrid[y][x] = "s" + str(y) + "_" + str(x)
def printObjects(pddlGrid):
pddlPlayer = "\t"
pddlBox = "\t"
for row in pddlGrid:
for c in row:
if c != "":
if c[0] == "s":
pddlPlayer += c + " "
# if c[0] == "p":
# pddlPlayer += c + " "
# if c[0] == "b":
# pddlBox += c + " "
pddlPlayer += "- square"
# pddlPlayer += "- player"
# pddlBox += "- box"
print(pddlPlayer)
# print(pddlBox)
print("\t" + " ".join(["count_wall" + str(i) for i in range(requiredCountWall + 1)]) + " - count_wall")
print("\t" + " ".join(["count_box" + str(i) for i in range(requiredCountBox + 1)]) + " - count_box")
print("\t" + " ".join(["count_goal" + str(i) for i in range(requiredCountGoal + 1)]) + " - count_goal")
def printAbove(pddlGrid):
for x in range(xM):
for y in range(yM - 1):
if pddlGrid[y][x] != "" and pddlGrid[y + 1][x] != "":
print("\t(above " + pddlGrid[y][x] + " " + pddlGrid[y + 1][x] + ")")
def printLeft(pddlGrid):
for y in range(yM):
for x in range(xM - 1):
if pddlGrid[y][x] != "" and pddlGrid[y][x + 1] != "":
print("\t(left " + pddlGrid[y][x] + " " + pddlGrid[y][x + 1] + ")")
def printOpt(pre, opt):
for y in range(yM):
for x in range(xM):
if opt[y][x]:
print("\t(" + pre + " " + pddlGrid[y][x] + ")")
def printCount(pre, required):
print("\t(current_" + pre + " " + pre + "0)")
for i in range(required):
print("\t(next_" + pre + " " + pre + str(i) + " " + pre + str(i + 1) + ")")
def printInit(pddlGrid, level):
printAbove(pddlGrid)
printLeft(pddlGrid)
printOpt("opt_wall", opt_wall)
printOpt("opt_box", opt_box)
printOpt("opt_goal", opt_goal)
printOpt("opt_player", opt_player)
printCount("count_wall", requiredCountWall)
printCount("count_box", requiredCountBox)
printCount("count_goal", requiredCountGoal)
print("\t(construction)")
for y in range(yM):
for x in range(xM):
if pddlGrid[y][x] == "":
continue
if level[y][x] == Block.PLAYER or level[y][x] == Block.PLAYER_ON_GOAL:
print("\t(current_count_player)")
print("\t(player " + pddlGrid[y][x] + ")")
if level[y][x] == Block.BOX or level[y][x] == Block.BOX_ON_GOAL:
print("\t(box " + pddlGrid[y][x] + ")")
if (
level[y][x] == Block.GOAL
or level[y][x] == Block.BOX_ON_GOAL
or level[y][x] == Block.PLAYER_ON_GOAL
):
print("\t(goal " + pddlGrid[y][x] + ")")
def printGoal(pddlGrid, level):
for y in range(yM):
for x in range(xM):
if (
level[y][x] == Block.GOAL
or level[y][x] == Block.BOX_ON_GOAL
or level[y][x] == Block.PLAYER_ON_GOAL
):
print("\t(box " + pddlGrid[y][x] + ")")
print("\t(current_count_wall count_wall" + str(requiredCountWall) +")")
print("\t(current_count_box count_box" + str(requiredCountBox) +")")
print("\t(current_count_goal count_goal" + str(requiredCountGoal) +")")
print("\t(current_count_player)")
print("\t(final)")
def main():
if len(sys.argv) < 2:
print("Usage: ./" + sys.argv[0] + " levelConstraints.txt")
exit(0)
readLevelFromFile(sys.argv[1])
getPddlGrid(level)
# print PDDL
print(pddlStrings[0])
printObjects(pddlGrid)
print(pddlStrings[1])
printInit(pddlGrid, level)
print(pddlStrings[2])
printGoal(pddlGrid, level)
print(pddlStrings[3])
if __name__ == "__main__":
main()