Question: Modify the rewriting system search Jupyter NB program. so that a . it implements ( limited depth ) DFS . b . It must take
Modify the rewriting system search Jupyter NB program.
so that
a it implements limited depth DFS
b It must take a text file in the following format as input. The contains as separate lines:
i Initial state
ii Goal states possibly more than one separated by space
iii. Rewriting rules of the system. Each rule is in the format premise consequent
Two rules are separated by space.
iv Maximum depth
Example of a file contents is given below:
E
abbba babba
Ea aab aba bbba bbbba
c The following modifications should be made:
The data structure for the goal should now be either a set or a list,
The termination condition in the search routine should be changed to a membership
in goal set list
Input file must be parsed so that the rules dictionary for the call to search function
can be created from the file.
For help with string processing and with file input see in the Intro to Python Jupyter NBs folder
wait i will give you the code that is mentioned above so that you can modify as per the task.
import collections
class Rewritingsystem:
def initself:
self.productions
def lhsself:
return self.productions.keys
def rhsself id:
return self.productionsid
class BFSFrontier:
def initself:
self.elements collections.deque
def emptyself:
return lenselfelements
def putself x:
self.elements.appendx
def getself:
return self.elements.popleft
def occurencessub string:
positions
count
for i in rangelenstringlensub:
if sub in stringi:ilensub:
# printfound in position',i
positionspositionsi
return positions
def breadthfirstsearchsystem start, goal:
if startgoal:
return True
else:
count
frontier BFSFrontier
frontier.putstart
visited
visitedstart True
while not frontier.empty:
countcount
printcount
current frontier.get
printVisiting r current
for symbol in system.lhs:
# printsymbol
# printoccurslenoccurencessymbolcurrent times'
if lenoccurencessymbolcurrent:
replacepositionsoccurencessymbolcurrent
printreplacepositions
printsystemrhssymbol
for s in system.rhssymbol:
#printreplacement s
for i in replacepositions:
nextcurrent:iscurrentilensymbol:
#printnext generated state isnext
if nextgoal:
return True
else:
if next not in visited:
printnew state in frontier is next
frontier.putnext
visitednext True
examplesystemRewritingsystem
examplesystem.productions
E:a
a: abba
b:bbb'bba'
emptywordE
goal'babbba'
breadthfirstsearchexamplesystem,emptyword,goal
Note: dont do it with depth limited search and dont use max depth also just do it with simple BFS breadth first search
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
