Monkey and bananas

A classic planning puzzle: how does the monkey reach the bananas?

monkey-bananas.js · output · proof · check · try it in the playground


The question

A room has three spots: loc1, loc2 and loc3. Bananas hang from the ceiling at loc1, too high to reach. The monkey stands at loc2. A box sits at loc3.

The monkey can walk, push the box while on the ground, climb on and off the box, and grab the bananas when standing on the box beneath them. Finding a sequence of moves that reaches a goal is called planning.

Which sequences of one to five moves end with the monkey holding the bananas?


Historical background

McCarthy and Hayes used the monkey’s sequence of moving a box, climbing it, and reaching the bananas in their 1969 paper on reasoning about actions. The example gives the monkey a handful of possible moves and searches for a sequence that reaches the bananas. McCarthy and Hayes, Some Philosophical Problems from the Standpoint of Artificial Intelligence


What we tell eyemind: situations and moves

A situation, or state, is a list [Bananas, Monkey, Box, OnBox, HasBananas]: where the bananas, the monkey and the box are, then 'y' or 'n' (yes or no) for whether the monkey is on the box and whether it holds the bananas.

fact(initial_state(['loc1', 'loc2', 'loc3', 'n', 'n']))
// Any state in which the monkey holds the bananas is a goal.
fact(goal_state([_, _, _, _, 'y']))

// Climbing needs the monkey and box at the same place, and grabbing needs
// all three there with the monkey on the box. Pushing moves monkey and box
// together; walking (go) moves only the monkey. Both need it on the ground.
fact(legal_move([B, M, M, 'n', H], 'climb_on', [B, M, M, 'y', H]))
fact(legal_move([B, M, M, 'y', H], 'climb_off', [B, M, M, 'n', H]))
fact(legal_move([B, B, B, 'y', 'n'], 'grab', [B, B, B, 'y', 'y']))
implied_by(
  legal_move([B, M, M, 'n', H], push(X), [B, X, X, 'n', H]),
  [location(X), not_unify(X, M)],
)
implied_by(
  legal_move([B, M, L, 'n', H], go(X), [B, X, L, 'n', H]),
  [location(X), not_unify(X, M)],
)

fact(location('loc1'))
fact(location('loc2'))
fact(location('loc3'))

What we tell eyemind: searching for plans

implied_by(reaches_goal(Moves), [initial_state(I), goal_state(G), reachable(I, Moves, G)])

fact(reachable(S, [], S))
implied_by(reachable(S1, [M, ...Rest], S3), [legal_move(S1, M, S2), reachable(S2, Rest, S3)])

// Plans are tried shortest first: a plan of a given length is a list of that
// many moves still to be chosen. in_range(1, 5, N) yields N = 1, 2, ..., 5.
fact(moves(0, []))
implied_by(moves(N, [_, ...Rest]), [gt(N, 0), is(M, sub(N, 1)), moves(M, Rest)])
implied_by(in_range(Low, High, Low), le(Low, High))
implied_by(
  in_range(Low, High, N),
  [lt(Low, High), is(Next, add(Low, 1)), in_range(Next, High, N)],
)

implies([in_range(1, 5, N), moves(N, Moves), reaches_goal(Moves)], plan(Moves))

What eyemind concludes

// Conclusions: the facts the program derives, in the order derived.
plan([go('loc3'), push('loc1'), 'climb_on', 'grab'])
plan([go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab'])
plan([go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off'])
plan([go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab'])

There is no plan of three moves or fewer. There is one four-move plan: walk to the box, push it under the bananas, climb on, grab. The three five-move plans are variations of it: a detour to loc1 first, an extra climb down at the end, or an extra push via loc2.


The proof, line by line

eyemind --proof writes down how it reached each conclusion. The whole proof has 4 steps, 1 listing and 79 call tables; see the full proof. Here are its claims, its steps and listing, and the call tables that show there is no plan of one move and how the four-move plan is found:

// Conclusions: the facts the program derives, in the order derived.
plan([go('loc3'), push('loc1'), 'climb_on', 'grab'])
plan([go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab'])
plan([go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off'])
plan([go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab'])

// Steps: each derived fact and what it rests on: the clause or control that
// justifies it, the bindings and the goals it uses.
step(plan([go('loc3'), push('loc1'), 'climb_on', 'grab']), clause(18), {N: 4, Moves: [go('loc3'), push('loc1'), 'climb_on', 'grab']}, [in_range(1, 5, 4), moves(4, [go('loc3'), push('loc1'), 'climb_on', 'grab']), reaches_goal([go('loc3'), push('loc1'), 'climb_on', 'grab'])])
step(plan([go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab']), clause(18), {N: 5, Moves: [go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab']}, [in_range(1, 5, 5), moves(5, [go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab']), reaches_goal([go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab'])])
step(plan([go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off']), clause(18), {N: 5, Moves: [go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off']}, [in_range(1, 5, 5), moves(5, [go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off']), reaches_goal([go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off'])])
step(plan([go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab']), clause(18), {N: 5, Moves: [go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab']}, [in_range(1, 5, 5), moves(5, [go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab']), reaches_goal([go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab'])])

// Certificate: the derived facts and every answer of each call the questions make.
derived(plan(A), [plan([go('loc3'), push('loc1'), 'climb_on', 'grab']), plan([go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab']), plan([go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off']), plan([go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab'])])
called(plan(A), [plan([go('loc3'), push('loc1'), 'climb_on', 'grab']), plan([go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab']), plan([go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off']), plan([go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab'])])
called(in_range(1, 5, B), [in_range(1, 5, 1), in_range(1, 5, 2), in_range(1, 5, 3), in_range(1, 5, 4), in_range(1, 5, 5)])
// … the tables counting 2 to 5
called(moves(1, G), [moves(1, [_7])])
called(moves(0, I), [moves(0, [])])
called(reaches_goal([_9]), [])
called(reachable(['loc1', 'loc2', 'loc3', 'n', 'n'], [_10], [_11, _12, _13, _14, 'y']), [])
called(legal_move(['loc1', 'loc2', 'loc3', 'n', 'n'], P, Q), [legal_move(['loc1', 'loc2', 'loc3', 'n', 'n'], go('loc1'), ['loc1', 'loc1', 'loc3', 'n', 'n']), legal_move(['loc1', 'loc2', 'loc3', 'n', 'n'], go('loc3'), ['loc1', 'loc3', 'loc3', 'n', 'n'])])
// … 30 tables: the rest of the one-move search, and the empty two- and
// three-move searches
called(moves(4, G), [moves(4, [_86, _87, _88, _89])])
called(reaches_goal([_90, _91, _92, _93]), [reaches_goal([go('loc3'), push('loc1'), 'climb_on', 'grab'])])
called(reachable(['loc1', 'loc2', 'loc3', 'n', 'n'], [_94, _95, _96, _97], [_98, _99, _100, _101, 'y']), [reachable(['loc1', 'loc2', 'loc3', 'n', 'n'], [go('loc3'), push('loc1'), 'climb_on', 'grab'], ['loc1', 'loc1', 'loc1', 'y', 'y'])])
// … the table for the other first move
called(reachable(['loc1', 'loc3', 'loc3', 'n', 'n'], [_102, _103, _104], [_105, _106, _107, _108, 'y']), [reachable(['loc1', 'loc3', 'loc3', 'n', 'n'], [push('loc1'), 'climb_on', 'grab'], ['loc1', 'loc1', 'loc1', 'y', 'y'])])
// … the table for the other second move
called(reachable(['loc1', 'loc1', 'loc1', 'n', 'n'], [_109, _110], [_111, _112, _113, _114, 'y']), [reachable(['loc1', 'loc1', 'loc1', 'n', 'n'], ['climb_on', 'grab'], ['loc1', 'loc1', 'loc1', 'y', 'y'])])
called(reachable(['loc1', 'loc1', 'loc1', 'y', 'n'], [_115], [_116, _117, _118, _119, 'y']), [reachable(['loc1', 'loc1', 'loc1', 'y', 'n'], ['grab'], ['loc1', 'loc1', 'loc1', 'y', 'y'])])
// … the moves allowed on the box
called(reachable(['loc1', 'loc1', 'loc1', 'y', 'y'], [], [_122, _123, _124, _125, 'y']), [reachable(['loc1', 'loc1', 'loc1', 'y', 'y'], [], ['loc1', 'loc1', 'loc1', 'y', 'y'])])
// … 28 more tables: the dead ends of four moves, and the plans of five

It has three parts:

The step for the first plan says: [go('loc3'), push('loc1'), 'climb_on', 'grab'] is a plan — by rule 18, with N = 4, because 4 is in the range 1 to 5, the plan is a list of 4 moves, and it reaches the goal. The rest of the story is in the tables:

Each reachable table rests on the legal_move table of its state, and each legal_move table on rules 9 and 10 and the facts for climbing and grabbing, with the built-in test not_unify deciding that a walk or push goes somewhere new.


Checked: what the report says

A proof is only worth something if someone else can check it. eyemind -c generates the proof and checks it against the program; --check-proof checks a saved proof. The checker never reasons by itself: it reads the clauses from the program and confirms the proof against them in one procedure:

This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
plan([go('loc3'), push('loc1'), 'climb_on', 'grab'])
plan([go('loc1'), go('loc3'), push('loc1'), 'climb_on', 'grab'])
plan([go('loc3'), push('loc1'), 'climb_on', 'grab', 'climb_off'])
plan([go('loc3'), push('loc2'), push('loc1'), 'climb_on', 'grab'])

verdict('checked')

The verdict is checked: each of the four plans really works, and they are every plan of up to five moves this program allows.


Try it

eyemind examples/monkey-bananas.js            # the answers
eyemind --proof examples/monkey-bananas.js    # answers with their proof

Or open it in the playground. Change in_range(1, 5, N) to in_range(1, 4, N) to allow at most four moves: only the shortest plan remains. Or start the monkey next to the box, by changing the initial state to ['loc1', 'loc3', 'loc3', 'n', 'n']: the shortest plan is now three moves, [push('loc1'), 'climb_on', 'grab'], and there are 18 plans in all.


Takeaway

A plan is a chain of small, checkable moves. eyemind finds the plans and records why each move was allowed, so you can follow the monkey step by step.