初学者Prolog问题

时间:2014-12-19 15:03:04

标签: prolog

我们有一个包含红色&的盒子。黄球。 如果一个人在比赛结束时无法获得2个球,那么他每天都会从禁区内获得2个球。 盒子旁边有一堆红球。 如果男子从盒子里抽出的2个球是相似的,他将红球放入盒子里, 如果他们不同,他会把黄球放在盒子里。 我们假设该框表示如下

initialCan([y, r, y, r, y, r, y, r, y, r]).

y代表黄色球,r代表红色球。 该名男子从名单开头撤回2个球,  然后他又把1球放到了名单的开头。 那么Prolog中的程序是什么,它给出了盒子里最后一个球的颜色  是开头的盒子吗?

2 个答案:

答案 0 :(得分:1)

您可以将问题抽象为可能状态中的搜索。

search(FinalState, FinalState):-
    is_final(FinalState).

search(CurrentState, FinalState):-
    transition(CurrentState, NextState),
    search(NextState, FinalState).

solution(FinalState):-
    initial_state(State0),
    search(State0, FinalState).

所以你从一个州跳到另一个州,直到你到达最后一个成为你的解决方案。你需要做一些事情:

  1. 设计状态的表示(例如,状态可能是[r,y,r,...]之类的列表)

  2. 编写谓词initial_state(S0),如果S0是游戏的初始状态,则会满意

  3. 编写谓词transition(S1, S2),如果您可以从S1S2

  4. ,则为真
  5. 编写谓词is_final(S),如果S是最终状态,则为真

答案 1 :(得分:0)

将状态设计为box(Yellow_count, Red_count)更加容易,而且不需要任何特定的列表(毕竟,球都是相同的,就像电子一样)。这是我的尝试。我可能会在这里写一些人的作业,但这实际上很有趣。

另请考虑通过Edsger W. Dijkstra查看"Why correctness must be a mathematical concern",其中描述了此问题。

% last_ball(box(Yellow_initial_count, Red_initial_count), Last_ball_color, Time_at_end)


% ---------- TRIVIAL CASES ---------

% if there is only 1 yellow ball, the color is 'yellow' and we needed zero steps to reach this state

last_ball(box(1,0), yellow, 0).

% if there is only 1 red ball, the color is 'red' and we needed zero steps to reach this state

last_ball(box(0,1), red,    0).

% ---------- CASES DEFINING INDUCTION OVER Yellow+Red BALLS -----------

% take two yellow: check that this is possible for the given box, 
% then find out what the last color is from the reduced counts, then define the number of steps to be higher by 1

last_ball(box(YI, RI), LBC, TAE) :- YI>=2, YIp is (YI-2), RIp is (RI+1), last_ball(box(YIp,RIp),LBC,TAEp), TAE is (TAEp+1).

% take two red: check that this is possible for the given box, 
% then find out what the last color is from the reduced counts, then define the number of steps to be higher by 1

last_ball(box(YI, RI), LBC, TAE) :- RI>=2, YIp is YI, RIp is (RI-2+1), last_ball(box(YIp,RIp),LBC,TAEp), TAE is (TAEp+1).

% take a red and a yellow: check that this is possible for the given box, 
% then find out what the last color is from the reduced counts, then define the number of steps to be higher by 1

last_ball(box(YI, RI), LBC, TAE) :- RI>=1, YI>=1, YIp is (YI-1+1), RIp is (RI-1), last_ball(box(YIp,RIp),LBC,TAEp), TAE is (TAEp+1).

% Now ask for example:
% ?- last_ball(box(2,1), C, T).

% ===================================
% This problem is of course Edsger W. Dijkstra's "balls in the urn" problem, and 
% there is a very easy way to deduce the color without exhautsive check of the move tree, as Prolog does in the above.
% See: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD07xx/EWD720.html

last_ball_ewd(box(YI, _), red)    :- 0 is (YI mod 2).
last_ball_ewd(box(YI, _), yellow) :- 1 is (YI mod 2).

% We can test this by trying to find a counterexample of the result of last_ball_ewsd for the other color via '\+'

othercolor(red,yellow).
othercolor(yellow,red).

verify(box(YI, RI)) :- last_ball_ewd(box(YI, RI), LBC), othercolor(LBC,LBCO), \+last_ball(box(YI, RI), LBCO, _).

% Now ask for example:
% ?- verify(box(2, 1))