普罗洛文路交叉

发布于 2025-01-23 17:45:13 字数 1399 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖树树初阳… 2025-01-30 17:45:13
% state(person on side, animals west, animals east)
initial_state(state(west, cats_dogs(3, 3), cats_dogs(0, 0))).
desired_state(state(east, cats_dogs(0, 0), cats_dogs(3, 3))).

solve(Path) :-
    initial_state(Initial),
    desired_state(Desired),
    % Use iterative deepening for convenience, so no need to guard against circular states
    length(Path, _),
    path(Initial, Desired, Path).

path(Desired, Desired, []).
path(Initial, Desired, [State1|Path]) :-
    move(Initial, State1),
    path(State1, Desired, Path).

road_side(west).
road_side(east).

% Map east & west onto this and other sides
cats_dogs_side(west, W, E, W, E).
cats_dogs_side(east, W, E, E, W).

move(Initial, State1) :-
    Initial = state(HumanSide, West, East),
    % Change from west & east to this and other, in terms of sides
    cats_dogs_side(HumanSide, West, East, This, Other),
    cats_dogs_move(This, Other, This1, Other1),
    % Human moves to other side
    dif(HumanSide, HumanSide1),
    road_side(HumanSide1),
    % Change back to west & east sides, for state-representation consistency
    cats_dogs_side(HumanSide, This1, Other1, West1, East1),
    State1 = state(HumanSide1, West1, East1).

cats_dogs_move(This, Other, This1, Other1) :-
    between(1, 2, AnimalsToMove),
    between(0, AnimalsToMove, CatsToMove),
    DogsToMove is AnimalsToMove - CatsToMove,
    move_safe(This, Other, CatsToMove, DogsToMove, This1, Other1).

move_safe(This, Other, CatsToMove, DogsToMove, This1, Other1) :-
    shift_animals(-1, CatsToMove, DogsToMove, This, This1),
    shift_animals(1, CatsToMove, DogsToMove, Other, Other1),
    safe_cats_dogs(This1),
    safe_cats_dogs(Other1).

shift_animals(Sign, CatsToMove, DogsToMove, cats_dogs(Cats, Dogs), cats_dogs(Cats1, Dogs1)) :-
    Cats1 is Cats + (Sign * CatsToMove),
    Cats1 >= 0,
    Dogs1 is Dogs + (Sign * DogsToMove),
    Dogs1 >= 0.

% Check that side is safe
safe_cats_dogs(cats_dogs(Cats, Dogs)) :-
    (   Dogs > Cats,
        Cats > 0
    ->  false
    ;   true ).

导致SWI-Promog(前2个):

?- time(solve(Path)).
% 2,287,324 inferences, 0.245 CPU in 0.242 seconds (101% CPU, 9329359 Lips)
Path = [state(east,cats_dogs(3,1),cats_dogs(0,2)),state(west,cats_dogs(3,2),cats_dogs(0,1)),state(east,cats_dogs(3,0),cats_dogs(0,3)),state(west,cats_dogs(3,1),cats_dogs(0,2)),state(east,cats_dogs(1,1),cats_dogs(2,2)),state(west,cats_dogs(2,2),cats_dogs(1,1)),state(east,cats_dogs(0,2),cats_dogs(3,1)),state(west,cats_dogs(0,3),cats_dogs(3,0)),state(east,cats_dogs(0,1),cats_dogs(3,2)),state(west,cats_dogs(0,2),cats_dogs(3,1)),state(east,cats_dogs(0,0),cats_dogs(3,3))] ;
% 328 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 1197565 Lips)
Path = [state(east,cats_dogs(3,1),cats_dogs(0,2)),state(west,cats_dogs(3,2),cats_dogs(0,1)),state(east,cats_dogs(3,0),cats_dogs(0,3)),state(west,cats_dogs(3,1),cats_dogs(0,2)),state(east,cats_dogs(1,1),cats_dogs(2,2)),state(west,cats_dogs(2,2),cats_dogs(1,1)),state(east,cats_dogs(0,2),cats_dogs(3,1)),state(west,cats_dogs(0,3),cats_dogs(3,0)),state(east,cats_dogs(0,1),cats_dogs(3,2)),state(west,cats_dogs(1,1),cats_dogs(2,2)),state(east,cats_dogs(0,0),cats_dogs(3,3))] ;
% state(person on side, animals west, animals east)
initial_state(state(west, cats_dogs(3, 3), cats_dogs(0, 0))).
desired_state(state(east, cats_dogs(0, 0), cats_dogs(3, 3))).

solve(Path) :-
    initial_state(Initial),
    desired_state(Desired),
    % Use iterative deepening for convenience, so no need to guard against circular states
    length(Path, _),
    path(Initial, Desired, Path).

path(Desired, Desired, []).
path(Initial, Desired, [State1|Path]) :-
    move(Initial, State1),
    path(State1, Desired, Path).

road_side(west).
road_side(east).

% Map east & west onto this and other sides
cats_dogs_side(west, W, E, W, E).
cats_dogs_side(east, W, E, E, W).

move(Initial, State1) :-
    Initial = state(HumanSide, West, East),
    % Change from west & east to this and other, in terms of sides
    cats_dogs_side(HumanSide, West, East, This, Other),
    cats_dogs_move(This, Other, This1, Other1),
    % Human moves to other side
    dif(HumanSide, HumanSide1),
    road_side(HumanSide1),
    % Change back to west & east sides, for state-representation consistency
    cats_dogs_side(HumanSide, This1, Other1, West1, East1),
    State1 = state(HumanSide1, West1, East1).

cats_dogs_move(This, Other, This1, Other1) :-
    between(1, 2, AnimalsToMove),
    between(0, AnimalsToMove, CatsToMove),
    DogsToMove is AnimalsToMove - CatsToMove,
    move_safe(This, Other, CatsToMove, DogsToMove, This1, Other1).

move_safe(This, Other, CatsToMove, DogsToMove, This1, Other1) :-
    shift_animals(-1, CatsToMove, DogsToMove, This, This1),
    shift_animals(1, CatsToMove, DogsToMove, Other, Other1),
    safe_cats_dogs(This1),
    safe_cats_dogs(Other1).

shift_animals(Sign, CatsToMove, DogsToMove, cats_dogs(Cats, Dogs), cats_dogs(Cats1, Dogs1)) :-
    Cats1 is Cats + (Sign * CatsToMove),
    Cats1 >= 0,
    Dogs1 is Dogs + (Sign * DogsToMove),
    Dogs1 >= 0.

% Check that side is safe
safe_cats_dogs(cats_dogs(Cats, Dogs)) :-
    (   Dogs > Cats,
        Cats > 0
    ->  false
    ;   true ).

Result in swi-prolog (first 2):

?- time(solve(Path)).
% 2,287,324 inferences, 0.245 CPU in 0.242 seconds (101% CPU, 9329359 Lips)
Path = [state(east,cats_dogs(3,1),cats_dogs(0,2)),state(west,cats_dogs(3,2),cats_dogs(0,1)),state(east,cats_dogs(3,0),cats_dogs(0,3)),state(west,cats_dogs(3,1),cats_dogs(0,2)),state(east,cats_dogs(1,1),cats_dogs(2,2)),state(west,cats_dogs(2,2),cats_dogs(1,1)),state(east,cats_dogs(0,2),cats_dogs(3,1)),state(west,cats_dogs(0,3),cats_dogs(3,0)),state(east,cats_dogs(0,1),cats_dogs(3,2)),state(west,cats_dogs(0,2),cats_dogs(3,1)),state(east,cats_dogs(0,0),cats_dogs(3,3))] ;
% 328 inferences, 0.000 CPU in 0.000 seconds (100% CPU, 1197565 Lips)
Path = [state(east,cats_dogs(3,1),cats_dogs(0,2)),state(west,cats_dogs(3,2),cats_dogs(0,1)),state(east,cats_dogs(3,0),cats_dogs(0,3)),state(west,cats_dogs(3,1),cats_dogs(0,2)),state(east,cats_dogs(1,1),cats_dogs(2,2)),state(west,cats_dogs(2,2),cats_dogs(1,1)),state(east,cats_dogs(0,2),cats_dogs(3,1)),state(west,cats_dogs(0,3),cats_dogs(3,0)),state(east,cats_dogs(0,1),cats_dogs(3,2)),state(west,cats_dogs(1,1),cats_dogs(2,2)),state(east,cats_dogs(0,0),cats_dogs(3,3))] ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文