-
Notifications
You must be signed in to change notification settings - Fork 1
/
logicPlan.py
265 lines (217 loc) · 8.53 KB
/
logicPlan.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
# logicPlan.py
# ------------
# Licensing Information: You are free to use or extend these projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# ([email protected]) and Dan Klein ([email protected]).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel ([email protected]).
"""
In logicPlan.py, you will implement logic planning methods which are called by
Pacman agents (in logicAgents.py).
"""
import util
import sys
import logic
import game
pacman_str = 'P'
ghost_pos_str = 'G'
ghost_east_str = 'GE'
pacman_alive_str = 'PA'
class PlanningProblem:
"""
This class outlines the structure of a planning problem, but doesn't implement
any of the methods (in object-oriented terminology: an abstract class).
You do not need to change anything in this class, ever.
"""
def getStartState(self):
"""
Returns the start state for the planning problem.
"""
util.raiseNotDefined()
def getGhostStartStates(self):
"""
Returns a list containing the start state for each ghost.
Only used in problems that use ghosts (FoodGhostPlanningProblem)
"""
util.raiseNotDefined()
def getGoalState(self):
"""
Returns goal state for problem. Note only defined for problems that have
a unique goal state such as PositionPlanningProblem
"""
util.raiseNotDefined()
def tinyMazePlan(problem):
"""
Returns a sequence of moves that solves tinyMaze. For any other maze, the
sequence of moves will be incorrect, so only use this for tinyMaze.
"""
from game import Directions
s = Directions.SOUTH
w = Directions.WEST
return [s, s, w, s, w, w, s, w]
def sentence1():
"""Returns a logic.Expr instance that encodes that the following expressions are all true.
A or B
(not A) if and only if ((not B) or C)
(not A) or (not B) or C
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def sentence2():
"""Returns a logic.Expr instance that encodes that the following expressions are all true.
C if and only if (B or D)
A implies ((not B) and (not D))
(not (B and (not C))) implies A
(not D) implies C
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def sentence3():
"""Using the symbols WumpusAlive[1], WumpusAlive[0], WumpusBorn[0], and WumpusKilled[0],
created using the logic.PropSymbolExpr constructor, return a logic.PropSymbolExpr
instance that encodes the following English sentences (in this order):
The Wumpus is alive at time 1 if and only if the Wumpus was alive at time 0 and it was
not killed at time 0 or it was not alive and time 0 and it was born at time 0.
The Wumpus cannot both be alive at time 0 and be born at time 0.
The Wumpus is born at time 0.
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def findModel(sentence):
"""Given a propositional logic sentence (i.e. a logic.Expr instance), returns a satisfying
model if one exists. Otherwise, returns False.
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def atLeastOne(literals) :
"""
Given a list of logic.Expr literals (i.e. in the form A or ~A), return a single
logic.Expr instance in CNF (conjunctive normal form) that represents the logic
that at least one of the literals in the list is true.
>>> A = logic.PropSymbolExpr('A');
>>> B = logic.PropSymbolExpr('B');
>>> symbols = [A, B]
>>> atleast1 = atLeastOne(symbols)
>>> model1 = {A:False, B:False}
>>> print logic.pl_true(atleast1,model1)
False
>>> model2 = {A:False, B:True}
>>> print logic.pl_true(atleast1,model2)
True
>>> model3 = {A:True, B:True}
>>> print logic.pl_true(atleast1,model2)
True
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def atMostOne(literals) :
"""
Given a list of logic.Expr literals, return a single logic.Expr instance in
CNF (conjunctive normal form) that represents the logic that at most one of
the expressions in the list is true.
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def exactlyOne(literals) :
"""
Given a list of logic.Expr literals, return a single logic.Expr instance in
CNF (conjunctive normal form)that represents the logic that exactly one of
the expressions in the list is true.
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def extractActionSequence(model, actions):
"""
Convert a model in to an ordered list of actions.
model: Propositional logic model stored as a dictionary with keys being
the symbol strings and values being Boolean: True or False
Example:
>>> model = {"North[3]":True, "P[3,4,1]":True, "P[3,3,1]":False, "West[1]":True, "GhostScary":True, "West[3]":False, "South[2]":True, "East[1]":False}
>>> actions = ['North', 'South', 'East', 'West']
>>> plan = extractActionSequence(model, actions)
>>> print plan
['West', 'South', 'North']
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def pacmanSuccessorStateAxioms(x, y, t, walls_grid):
"""
Successor state axiom for state (x,y,t) (from t-1), given the board (as a
grid representing the wall locations).
Current <==> (previous position at time t-1) & (took action to move to x, y)
"""
"*** YOUR CODE HERE ***"
return logic.Expr('A') # Replace this with your expression
def positionLogicPlan(problem):
"""
Given an instance of a PositionPlanningProblem, return a list of actions that lead to the goal.
Available actions are game.Directions.{NORTH,SOUTH,EAST,WEST}
Note that STOP is not an available action.
"""
walls = problem.walls
width, height = problem.getWidth(), problem.getHeight()
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def foodLogicPlan(problem):
"""
Given an instance of a FoodPlanningProblem, return a list of actions that help Pacman
eat all of the food.
Available actions are game.Directions.{NORTH,SOUTH,EAST,WEST}
Note that STOP is not an available action.
"""
walls = problem.walls
width, height = problem.getWidth(), problem.getHeight()
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
def ghostPositionSuccessorStateAxioms(x, y, t, ghost_num, walls_grid):
"""
Successor state axiom for patrolling ghost state (x,y,t) (from t-1).
Current <==> (causes to stay) | (causes of current)
GE is going east, ~GE is going west
"""
pos_str = ghost_pos_str+str(ghost_num)
east_str = ghost_east_str+str(ghost_num)
"*** YOUR CODE HERE ***"
return logic.Expr('A') # Replace this with your expression
def ghostDirectionSuccessorStateAxioms(t, ghost_num, blocked_west_positions, blocked_east_positions):
"""
Successor state axiom for patrolling ghost direction state (t) (from t-1).
west or east walls.
Current <==> (causes to stay) | (causes of current)
"""
pos_str = ghost_pos_str+str(ghost_num)
east_str = ghost_east_str+str(ghost_num)
"*** YOUR CODE HERE ***"
return logic.Expr('A') # Replace this with your expression
def pacmanAliveSuccessorStateAxioms(x, y, t, num_ghosts):
"""
Successor state axiom for patrolling ghost state (x,y,t) (from t-1).
Current <==> (causes to stay) | (causes of current)
"""
ghost_strs = [ghost_pos_str+str(ghost_num) for ghost_num in xrange(num_ghosts)]
"*** YOUR CODE HERE ***"
return logic.Expr('A') # Replace this with your expression
def foodGhostLogicPlan(problem):
"""
Given an instance of a FoodGhostPlanningProblem, return a list of actions that help Pacman
eat all of the food and avoid patrolling ghosts.
Ghosts only move east and west. They always start by moving East, unless they start next to
and eastern wall.
Available actions are game.Directions.{NORTH,SOUTH,EAST,WEST}
Note that STOP is not an available action.
"""
walls = problem.walls
width, height = problem.getWidth(), problem.getHeight()
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
# Abbreviations
plp = positionLogicPlan
flp = foodLogicPlan
fglp = foodGhostLogicPlan
# Some for the logic module uses pretty deep recursion on long expressions
sys.setrecursionlimit(100000)