Вы находитесь на странице: 1из 11

Developing Knowledge-Base using a First-Order Predicate Logic

based language called OPS5


Consider the task of developing a knowledge-base that tells a
robot to reach an electrical switch and turn the light on.
Assume that the robot is in a 10 x 10 (feet) room. The switch can
be on any of the walls. Each wall is divided into 10 x 10 (feet)
grid. The switch can be at any of the grid points (0, 0, 0) to (10,
10, 10).
Assume that the robot is at location 1, 1 on the ground; the switch
is at 0, 8, 8 (the 3rd dimension indicates the height).
Q. How will the robot turn on the switch?
Solution:
1.
2.
3.
4.

Goal is to turn on the switch


The robot walks up to the position of the switch;
The robot turns the switch on;
The robot walks back to its original position;

Can we develop a generic knowledge-based solution to the


problem?

Using OPS5 Language:


Declare the objects of interest:
(Object Robot ^initial-position ^current-position ^goal ^status)
(Object switch ^position ^status)

Declarations:
(Object Robot ^initial-position {(0,0,0) to (10,10,0)} ^currentposition {(0,0,0) to (10,10,0)} ^goal {Turn-On-Switch} ^status
{NYC, Done})
(Object Switch ^position {((0, 0, 0) to (0, 10, 10)), ((0, 10, 0) to
(10, 10, 10)), ((10, 0, 0) to (10, 10, 10)), ((0, 0, 0) to (10, 0, 10))}
^status {on, off})
The Knowledge-Base :
Cases:
1. Switch is already on:
a. Nothing to do; just inform;
2. Switch is off; Robot NOT at the Switch position:
a. Walk up to the Switch position; Turn Switch on;
Walk back to the original position;
3. Switch is off; Robot is originally at the Switch position:
a. Turn Switch on; Remain in the original position;
R1 /* Case # 1 */
If (
(Object Robot ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^status = on)
Then
(Command Execute (Robot, shout, Sir, the switch is already
on))
(modify Robot ^status = Done)
)

R2 /* Case # 2 */
If (
(Object Robot ^initial-position = (x, y, 0) ^current-position = (x,
y, 0) ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^position = (p, q, r) ((p <> x) OR (q <>
y))^status = off)
Then
(Command Execute (Robot, Move, (x, y, 0), (p, q, 0)))
(Command Execute (Robot, Shout, Sir, I am moving to position
of the switch)
(modify Robot ^current-position = (p, q, 0))
(Command Execute (Robot, shout, Sir, the switch is changed
from off to on))
(modify Switch ^status = on)
(Command Execute (Robot, Move, (p, q, 0), (x, y, 0)))
(Command Execute (Robot, Shout, Sir, I am moving back to my
initial position)
(modify Robot ^current-position = (x, y, 0) ^status = Done)
)
R3 /* Case # 3 */
If (
(Object Robot ^initial-position = (x, y, 0) ^current-position = (x,
y, 0) ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^position = (x, y, z) ^status = off)
Then
(Command Execute (Robot, shout, Sir, the switch is changed
from off to on))
(modify Switch ^status = on)
(modify Robot ^status = Done)
)

Assume that,
If the Switch is above the ground by 7 feet or more, then the
robot needs a ladder to climb and turn the Switch on;
The ladder will always be on the ground but anywhere in
the room;
Write FOPL Rules using OPS5 like language.
/* R1, R2 and R3 rules will be the same as before, if the Switch is
at lower than 7 feet height */
R1 /* Case # 1 */
If (
(Object Robot ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^status = on)
Then
(Command Execute (Robot, shout, Sir, the switch is already
on))
(modify Robot ^status = Done)
)
R2 /* Case # 2 */
If (
(Object Robot ^initial-position = (x, y, 0) ^current-position = (x,
y, 0) ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^position = (p, q, r) (r < 7) ((p <> x) OR (q <>
y))^status = off)
Then
(Command Execute (Robot, Move, (x, y, 0), (p, q, 0)))
(Command Execute (Robot, Shout, Sir, I am moving to position
of the switch)

(modify Robot ^current-position = (p, q, 0))


(Command Execute (Robot, shout, Sir, the switch is changed
from off to on))
(modify Switch ^status = on)
(Command Execute (Robot, Move, (p, q, 0), (x, y, 0)))
(Command Execute (Robot, Shout, Sir, I am moving back to my
initial position)
(modify Robot ^current-position = (x, y, 0) ^status = Done)
)
R3 /* Case # 3 */
If (
(Object Robot ^initial-position = (x, y, 0) ^current-position = (x,
y, 0) ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^position = (x, y, z) (z < 7) ^status = off)
Then
(Command Execute (Robot, shout, Sir, the switch is changed
from off to on))
(modify Switch ^status = on)
(modify Robot ^status = Done)
)

/* Rules to reason if the Switch is above the ground by 7 feet or


above */
Cases:
1. Ladder NOT at the Switch; Robot NOT at the Ladder:
a. Walk up to the Ladder; Push the Ladder to the Switch
position; Climb up; turn on; Climb down; put ladder
back to its normal position; walk back to Robot initial
position;
2. Ladder NOT at the Switch; But Robot and the Ladder are
in the same position:
a. Push the Ladder to the Switch position; Climb up; turn
on; Climb down; put ladder back to its normal
position; remain at that position;
3. Ladder at the Switch; Robot not at the Ladder:
a. Walk up to the Ladder; Climb up; turn the Switch
on; Climb down; walk back to Robot initial position;
4. Robot at the Switch position; Ladder NOT at the Switch
position;
a. Walk up to the Ladder; Push Ladder to the Switch
position; Climb up; Turn Switch on; Climb down;
put Ladder back to its original position; Walk back to
Robots original position;
5. Ladder at the Switch; Robot at the Switch:
a. Climb up; turn the Switch on; Climb down;

R4 /* Case # 1 */
If (
(Object Robot ^initial-position = (x, y, 0) ^current-position = (x,
y, 0) ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^position = (p, q, r) (r >= 7) ((p <> x) OR (q <>
y)) ^status = off)
(Object Ladder ^ initial-position = (x1, y1, 0) ^current-position
= (x1, y1, 0) ((x1 <> x) AND (x1 <> p)) OR (y1 <> y) AND (y1 <>
q)))
Then
(Command Execute (Robot, Shout, Sir, I am moving to the
position of the Ladder)
(Command Execute (Robot, Move, (x, y, 0), (x1, y1, 0)))
(modify Robot ^current-position = (x1, y1, 0))
(Command Execute (Robot, Shout, Sir, I am pushing the Ladder
to the position of the Switch)
(Command Execute (Robot, Push Ladder, (x1, y1, 0), (p, q, 0)))
(modify Ladder ^current-position = (p, q, 0))
(modify Robot ^current-position = (p, q, 0))
(Command Execute (Robot, Shout, Sir, I am Climbing on the
Ladder)
(Command Execute (Robot, Climb up the Ladder)
(Command Execute (Robot, shout, Sir, the switch is changed
from off to on))
(modify Switch ^status = on)
(Command Execute (Robot, Shout, Sir, I am Climbing down the
Ladder)
(Command Execute (Robot, Climb down the Ladder)
(Command Execute (Robot, Shout, Sir, I am putting the Ladder
back to its initial position)
(Command Execute (Robot, Push Ladder, (p, q, 0), (x1, y1, 0)))

(modify Robot ^current-position = (x1, y1, 0))


(modify Ladder ^current-position = (x1, y1, 0))
(Command Execute (Robot, Shout, Sir, I am moving back to my
initial position)
(Command Execute (Robot, Move, (x1, y1, 0), (x, y, 0)))
(modify Robot ^current-position = (x, y, 0) ^status = Done)
)

R5 /* Case # 2 */
If (
(Object Robot ^initial-position = (x, y, 0) ^current-position = (x,
y, 0) ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^position = (p, q, r) (r >= 7) ((p <> x) OR (q <>
y)) ^status = off)
(Object Ladder ^ initial-position = (x, y, 0) ^current-position =
(x, y, 0))
Then
(Command Execute (Robot, Shout, Sir, I am pushing the Ladder
to the position of the Switch)
(Command Execute (Robot, Push Ladder, (x, y, 0), (p, q, 0)))
(modify Ladder ^current-position = (p, q, 0))
(modify Robot ^current-position = (p, q, 0))
(Command Execute (Robot, Shout, Sir, I am Climbing on the
Ladder)
(Command Execute (Robot, Climb up the Ladder)
(Command Execute (Robot, shout, Sir, the switch is changed
from off to on))
(modify Switch ^status = on)

(Command Execute (Robot, Shout, Sir, I am Climbing down the


Ladder)
(Command Execute (Robot, Climb down the Ladder)
(Command Execute (Robot, Shout, Sir, I am putting the Ladder
back to its initial position)
(Command Execute (Robot, Push Ladder, (p, q, 0), (x, y, 0)))
(modify Ladder ^current-position = (x, y, 0))
(modify Robot ^current-position = (x, y, 0) ^status = Done)
)

R6 /* Case # 3 */
If (
(Object Robot ^initial-position = (x, y, 0) ^current-position = (x,
y, 0) ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^position = (p, q, r) (r >= 7) ((p <> x) OR (q <>
y)) ^status = off)
(Object Ladder ^ initial-position = (p, q, 0) ^current-position =
(p, q, 0))
Then
(Command Execute (Robot, Shout, Sir, I am moving up to the
Ladder)
(Command Execute (Robot, Move, (x, y, 0), (p, q, 0)))
(modify Robot ^current-position = (p, q, 0))
(Command Execute (Robot, Shout, Sir, I am Climbing on the
Ladder)
(Command Execute (Robot, Climb up the Ladder)
(Command Execute (Robot, shout, Sir, the switch is changed
from off to on))
(modify Switch ^status = on)

(Command Execute (Robot, Shout, Sir, I am Climbing down the


Ladder)
(Command Execute (Robot, Climb down the Ladder)
(Command Execute (Robot, Shout, Sir, I am moving back to my
initial position)
(Command Execute (Robot, Move, (p, q, 0), (x, y, 0)))
(modify Robot ^current-position = (x, y, 0) ^status = Done)
)

R7 /* Case # 4 */
If (
(Object Robot ^initial-position = (x, y, 0) ^current-position = (x,
y, 0) ^goal = Turn-On-Switch ^status = NYC)
(Object Switch ^position = (x, y, r) (r >= 7) ^status = off)
(Object Ladder ^ initial-position = (p, q, 0) ^current-position =
(p, q, 0) ((p <> x) OR (q <> y)))
Then
(Command Execute (Robot, Shout, Sir, I am moving to the
position of the Ladder)
(Command Execute (Robot, Move, (x, y, 0), (p, q, 0)))
(modify Robot ^current-position = (p, q, 0))
(Command Execute (Robot, Shout, Sir, I am pushing the Ladder
to the position of the Switch)
(Command Execute (Robot, push Ladder, (x1, y1, 0), (p, q, 0)))
(modify Ladder ^current-position = (x, y, 0))
(modify Robot ^current-position = (x, y, 0))
(Command Execute (Robot, Shout, Sir, I am Climbing on the
Ladder)
(Command Execute (Robot, Climb up the Ladder)

(Command Execute (Robot, shout, Sir, the switch is changed


from off to on))
(modify Switch ^status = on)
(Command Execute (Robot, Shout, Sir, I am Climbing down the
Ladder)
(Command Execute (Robot, Climb down the Ladder)
(Command Execute (Robot, Shout, Sir, I am putting the Ladder
back to its initial position)
(Command Execute (Robot, push Ladder, (x, y, 0), (p, q, 0)))
(modify Robot ^current-position = (p, q, 0))
(modify Ladder ^current-position = (p, q, 0))
(Command Execute (Robot, Shout, Sir, I am moving back to my
initial position)
(Command Execute (Robot, Move, (p, q, 0), (x, y, 0)))
(modify Robot ^current-position = (x, y, 0) ^status = Done)
)

R8 /* Case # 5 */
/* Home Assignment: please write this rule */

Вам также может понравиться