Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I tried to do a prolog code to answer these questions please fix it for me fully as I know it include some errors specially
I tried to do a prolog code to answer these questions please fix it for me fully as I know it include some errors specially last question. Question
Define connections between rooms in the house
connectedoutside porch
connectedporch kitchen
connectedkitchen livingroom
connectedlivingroom, corridor
connectedcorridor wc
connectedcorridor bedroom
connectedbedroom outside
connectedcorridor masterbedroom
connectedmasterbedroom, porch
connectedporch outside
Define bidirectional connections
If X is connected to Y then Y is also connected to X
adjacentX Y : connectedX Y
adjacentX Y : connectedY X
Define the path predicate using depthfirst search algorithm
B case for depthfirst search: reached the destination
pathA B Path :
depthfirst A B RevPath
reverseRevPath Path
The resulting path is then reversed to give the correct order and is returned as the third argument of the path predicate.
depthfirstPath B BBPath
depthfirstVisited A B Path :
adjacentA C
C B
memberC Visited Avoid loops
depthfirstAVisited C B Path
Provide all possible paths
allpathsA B Paths :
findallPath pathA B Path Paths
The findall predicate collects all solutions of the path predicate into a list Paths
It gathers all possible paths between A and B and stores them in the variable Paths.
Provide error message for incorrect input
pathA B :
connectedA
connected A
connectedB
connected B
formatError: ~w or ~w does not exist in the plan of the house.A B fail.
Define the predicate to find a path from A to B
pathA B Path :
travelA BA Q
reverseQ Path
travelX X PXP
travelX Y V P :
connectedX Z
Z Y
memberZ V
travelZ YZV P
Define the predicate to find a common destination from two origins
commondestinationOrigin Origin Destination, Path :
pathOrigin Destination, Path
pathOrigin Destination, Path
appendPath Path Path
Q
connectedoutside porch
connectedporch kitchen,
connectedkitchen livingroom,
connectedlivingroom, porch
connectedporch outside,
connectedlivingroom corridor,
connectedcorridor bedroom,
connectedcorridor wc
connectedcorridor masterbedroom,
Define the predicate to find a path from Location to Location with cost
pathwithcostLocation Location Path, Cost :
travelwithcostLocation LocationLocation Q Cost
reverseQ Path
travelwithcostX X PXP
travelwithcostX Y V P Cost :
connectedX Z C
Z Y
memberZ V
travelwithcostZ YZV P RemainingCost
Define the predicate to find a common destination from two origins with the same cost
commondestinationsamecostOrigin Origin Destination, Path, Cost :
pathwithcostOrigin Destination, Path Cost
pathwithcostOrigin Destination, Path Cost
appendPath Path Path
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started