Форум программистов, компьютерный форум, киберфорум
Наши страницы
Prolog
Войти
Регистрация
Восстановить пароль
 
Рейтинг 4.80/5: Рейтинг темы: голосов - 5, средняя оценка - 4.80
persej
3 / 3 / 3
Регистрация: 26.11.2014
Сообщений: 83
1

Задача перестановка кубиков

08.02.2018, 05:10. Просмотров 925. Ответов 7
Метки нет (Все метки)

Стоят вертикально кубики b,a,c надо чтобы выводился путь перехода в состояние a,b,c. кубики можно переставлять только по одному за раз.

Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
s([[a,b,c],[],[]],_).
s([[],[a,b,c],[]],_).
s([[],[],[a,b,c]],_).
 
s( Stacks, [ Stack1 , [Top1 |Stack2] ,OtherStacks] ) :-
    del([Top1|Stack1], Stacks, Stacks1 ) ,
    del( Stack2, Stacks1 , OtherStacks).
 
del( X, [X | L], L) .
del( X, [Y | L], [Y | L1]) :-
    del( X, L, L1 ) .
 
goal( Situation):-
    member( [a,b,c], Situation).

Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
?- s([[a,b,c],[],[]],A).
true .
 
?- s([[b,a,c],[],[]],A).
A = [[a, c], [b], [[]]] .
 
?- s([[b,a,c],[],[]],A).
A = [[a, c], [b], [[]]] ;
A = [[a, c], [b], [[]]] ;
false.
?- solve( [[b,a,c],[],[]], Solution).
ERROR: Undefined procedure: solve/2 (DWIM could not correct goal)
?-
Добавлено через 9 часов 9 минут
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
?- [s229].
true.
 
?- s([[c,b,a],[],[]],Z).
Z = [[b, a], [c], [[]]] .
 
?- s([[c,b,a],[],[]],Z).
Z = [[b, a], [c], [[]]] ;
Z = [[b, a], [c], [[]]] ;
false.
 
?-
Должен выводится путь к одному из состояний:
Prolog
1
2
3
s([[a,b,c],[],[]],_).
s([[],[a,b,c],[]],_).
s([[],[],[a,b,c]],_).
Что то упустил.
0
Лучшие ответы (1)
Similar
Эксперт
41792 / 34177 / 6122
Регистрация: 12.04.2006
Сообщений: 57,940
08.02.2018, 05:10
Ответы с готовыми решениями:

перестановка кубиков
привет всем) очень прошу вас помочь с задачкой по турбо прологу: на столе...

Swi-prolog отображение состояния в мире кубиков
Напишите процедуру show( Situation) для отображения проблемного состояния...

Составление кубиков, поиск в пространстве состояний, монотонный поиск в ширину [Turbo Prolog]
Помогите решить задачу с кубиками в турбо прологе с несложной визуализацией. ...

Перестановка фишек
Дано задание. Пешки. На горизонтальной доске состоящей из 7 ячеек расположены...

Циклическая перестановка элементов
Всем привет! Являюсь новичком, "пытаюсь" понять азы языка, использую...

7
persej
3 / 3 / 3
Регистрация: 26.11.2014
Сообщений: 83
09.02.2018, 06:12  [ТС] 2
Точнее
Prolog
1
2
3
s(_,[[a,b,c],[],[]]).
s(_,[[],[a,b,c],[]]).
s(_,[[],[],[a,b,c]]).
Программа вроде правильна, но как запустить, чтобы выводила путь к конечному состоянию

Чуть именил факты:

Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
s(_,[[a,b,c],[],[]]).
s(_,[[],[a,b,c],[]]).
s(_,[[],[],[a,b,c]]).
 
s( Stacks, [ Stack1 , [Top1 |Stack2] ,OtherStacks] ) :-
    del([Top1|Stack1], Stacks, Stacks1 ) ,
    del( Stack2, Stacks1 , OtherStacks).
 
del( X, [X | L], L) .
del( X, [Y | L], [Y | L1]) :-
    del( X, L, L1 ) .
 
goal( Situation):-
    member( [a,b,c], Situation).
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
 ?-s([[c,b,a],[],[]],Z).
Z = [[a, b, c], [], []] .
 
?- s([[c,b,a],[],[]],Z).
Z = [[a, b, c], [], []] ;
Z = [[], [a, b, c], []] ;
Z = [[], [], [a, b, c]] ;
Z = [[b, a], [c], [[]]] ;
Z = [[b, a], [c], [[]]] ;
false.
 
?- goal([[b,c,a],[],[]]).
false.
Что-то вокруг да около..
0
persej
3 / 3 / 3
Регистрация: 26.11.2014
Сообщений: 83
11.02.2018, 20:03  [ТС] 3
Задача перестановка кубиков

Непонятно как запускается перебор и поиск.
0
Black Fregat
2612 / 1352 / 368
Регистрация: 31.05.2009
Сообщений: 5,139
12.02.2018, 01:11 4
Почему бы не порчитать, наконец, Поиск в пространстве состояний (поиск по графам тоже сюда!) ?
0
persej
3 / 3 / 3
Регистрация: 26.11.2014
Сообщений: 83
18.02.2018, 14:39  [ТС] 5
s230.pl
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
%s(_,[[a,b,c],[],[]]).
%s(_,[[],[a,b,c],[]]).
%s(_,[[],[],[a,b,c]]).
 
s( Stacks, [ Stack1 , [Top1 |Stack2] ,OtherStacks] ) :-
    del([Top1|Stack1], Stacks, Stacks1 ) ,
    del( Stack2, Stacks1 , OtherStacks).    
 
 
del( X, [X | L], L).
del( X, [Y | L], [Y | L1]) :-
    del( X, L, L1 ).
 
goal( Situation ):-
    member( [a,b,c], Situation).
 
%solve( Start, Solution).
 
solve( N, [N]) :-
    goal( N).
 
solve( N, [ N | Sol1 ] ) :-
    s(N, N1),
    solve( N1, Sol1 ) .
Но после запуска solve( [[b,a,c],[],[]], Solution).
,подвисает.
Должно выводится решение в виде списка Solution=[??????].

Bash
1
2
3
4
5
6
7
[s230].
true.
 
?- solve( [[b,a,c],[],[]], Solution).
^CAction (h for help) ? abort
% Execution Aborted
?-
безконечный цикл должно остановить member( [a,b,c], Situation). в программе.
поэтому факты s(_,[[a,b,c],[],[]]).
s(_,[[],[a,b,c],[]]).
s(_,[[],[],[a,b,c]]).
излишние.

решение уже где то рядом...

Добавлено через 13 минут
Запуск в режиме трасировки

Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
?- trace.
true.
 
[trace]  ?- solve( [[b,a,c],[],[]], Solution).
   Call: (8) solve([[b, a, c], [], []], _752) ? creep
   Call: (9) goal([[b, a, c], [], []]) ? creep
   Call: (10) lists:member([a, b, c], [[b, a, c], [], []]) ? creep
   Fail: (10) lists:member([a, b, c], [[b, a, c], [], []]) ? creep
   Fail: (9) goal([[b, a, c], [], []]) ? creep
   Redo: (8) solve([[b, a, c], [], []], _752) ? creep
   Call: (9) s([[b, a, c], [], []], _1028) ? creep
   Call: (10) del([_1024|_1012], [[b, a, c], [], []], _1060) ? creep
   Exit: (10) del([b, a, c], [[b, a, c], [], []], [[], []]) ? creep
   Call: (10) del(_1026, [[], []], _1030) ? creep
   Exit: (10) del([], [[], []], [[]]) ? creep
   Exit: (9) s([[b, a, c], [], []], [[a, c], [b], [[]]]) ? creep
   Call: (9) solve([[a, c], [b], [[]]], _1008) ? creep
   Call: (10) goal([[a, c], [b], [[]]]) ? creep
   Call: (11) lists:member([a, b, c], [[a, c], [b], [[]]]) ? creep
   Fail: (11) lists:member([a, b, c], [[a, c], [b], [[]]]) ? creep
   Fail: (10) goal([[a, c], [b], [[]]]) ? creep
   Redo: (9) solve([[a, c], [b], [[]]], _1008) ? creep
   Call: (10) s([[a, c], [b], [[]]], _1064) ? creep
   Call: (11) del([_1060|_1048], [[a, c], [b], [[]]], _1096) ? creep
   Exit: (11) del([a, c], [[a, c], [b], [[]]], [[b], [[]]]) ? creep
   Call: (11) del(_1062, [[b], [[]]], _1066) ? creep
   Exit: (11) del([b], [[b], [[]]], [[[]]]) ? creep
   Exit: (10) s([[a, c], [b], [[]]], [[c], [a, b], [[[]]]]) ? creep
   Call: (10) solve([[c], [a, b], [[[]]]], _1044) ? creep
   Call: (11) goal([[c], [a, b], [[[]]]]) ? creep
   Call: (12) lists:member([a, b, c], [[c], [a, b], [[[]]]]) ? creep
   Fail: (12) lists:member([a, b, c], [[c], [a, b], [[[]]]]) ? creep
   Fail: (11) goal([[c], [a, b], [[[]]]]) ? creep
   Redo: (10) solve([[c], [a, b], [[[]]]], _1044) ? creep
   Call: (11) s([[c], [a, b], [[[]]]], _1100) ? creep
   Call: (12) del([_1096|_1084], [[c], [a, b], [[[]]]], _1132) ? creep
   Exit: (12) del([c], [[c], [a, b], [[[]]]], [[a, b], [[[]]]]) ? creep
   Call: (12) del(_1098, [[a, b], [[[]]]], _1102) ? creep
   Exit: (12) del([a, b], [[a, b], [[[]]]], [[[[]]]]) ? creep
   Exit: (11) s([[c], [a, b], [[[]]]], [[], [c, a, b], [[[[]]]]]) ? creep
   Call: (11) solve([[], [c, a, b], [[[[]]]]], _1080) ? creep
   Call: (12) goal([[], [c, a, b], [[[[]]]]]) ? creep
   Call: (13) lists:member([a, b, c], [[], [c, a, b], [[[[]]]]]) ? creep
   Fail: (13) lists:member([a, b, c], [[], [c, a, b], [[[[]]]]]) ? creep
   Fail: (12) goal([[], [c, a, b], [[[[]]]]]) ? creep
   Redo: (11) solve([[], [c, a, b], [[[[]]]]], _1080) ? creep
   Call: (12) s([[], [c, a, b], [[[[]]]]], _1136) ? creep
   Call: (13) del([_1132|_1120], [[], [c, a, b], [[[[]]]]], _1168) ? creep
   Call: (14) del([_1132|_1120], [[c, a, b], [[[[]]]]], _1152) ? creep
   Exit: (14) del([c, a, b], [[c, a, b], [[[[]]]]], [[[[[]]]]]) ? creep
   Exit: (13) del([c, a, b], [[], [c, a, b], [[[[]]]]], [[], [[[[]]]]]) ? creep
   Call: (13) del(_1134, [[], [[[[]]]]], _1138) ? creep
   Exit: (13) del([], [[], [[[[]]]]], [[[[[]]]]]) ? creep
   Exit: (12) s([[], [c, a, b], [[[[]]]]], [[a, b], [c], [[[[[]]]]]]) ? creep
   Call: (12) solve([[a, b], [c], [[[[[]]]]]], _1116) ? creep
   Call: (13) goal([[a, b], [c], [[[[[]]]]]]) ? creep
   Call: (14) lists:member([a, b, c], [[a, b], [c], [[[[[]]]]]]) ? creep
   Fail: (14) lists:member([a, b, c], [[a, b], [c], [[[[[]]]]]]) ? creep
   Fail: (13) goal([[a, b], [c], [[[[[]]]]]]) ? creep
   Redo: (12) solve([[a, b], [c], [[[[[]]]]]], _1116) ? creep
   Call: (13) s([[a, b], [c], [[[[[]]]]]], _1178) ? creep
   Call: (14) del([_1174|_1162], [[a, b], [c], [[[[[]]]]]], _1210) ? creep
   Exit: (14) del([a, b], [[a, b], [c], [[[[[]]]]]], [[c], [[[[[]]]]]]) ? creep
   Call: (14) del(_1176, [[c], [[[[[]]]]]], _1180) ? creep
   Exit: (14) del([c], [[c], [[[[[]]]]]], [[[[[[]]]]]]) ?
Добавлено через 40 минут
выдаются сообщия с ошибкой (нет членства [a,b,c] в предлагаемых списках)
Bash
1
Fail: (21) lists:member([a, b, c], [[b], [a, c], [[[[[...]]]]]]) ? creep
Ни разу небыло сообщения типа....
Bash
1
member([a, b, c], [[], [a, b, c], [[]]]) ? creep
Чтобы остановить перебор.

Поэтому и возникает безконечный перебор. Непонятно почему отношение s не находит состояния типа:
[[], [a, b, c], [[]]].
0
persej
3 / 3 / 3
Регистрация: 26.11.2014
Сообщений: 83
22.02.2018, 06:12  [ТС] 6
Алгоритм поиска в глубину не более заданной глубины.
s233.pl
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
%s(_,[[a,b,c],[],[]]).
%s(_,[[],[a,b,c],[]]).
%s(_,[[],[],[a,b,c]]).
 
s( Stacks, [ Stack1 , [Top1 |Stack2] ,OtherStacks] ) :-
    del([Top1|Stack1], Stacks, Stacks1 ) ,
    del( Stack2, Stacks1 , OtherStacks).    
 
 
del( X, [X | L], L).
del( X, [Y | L], [Y | L1]) :-
    del( X, L, L1 ).
 
% solve[ Hade, S o l u t i o n ) :
%
%Решение Solution представляет собой ациклический путь (узлы в котором
%iуказаны в обратном порядке) между узлом Node к целью
 
solve( Node, Solution,Maxdepth):-
    depthfirst2( Node , Solution,Maxdepth) .
 
 
 
% d e p t h f i r s t 2 [ Node, Solution, Maxdepth):
%
%Решение Solution представляет собой путь от узла Node до целевого узла,
% длима которого не превышает Maxdepth
depthfirst2( Node, [Node], _):-
    goal( Node) .
 
depthfirst2( Node, [Node|Sol], Maxdepth):-
    Maxdepth > 0,
    s( Node, Model),
    Max1 is Maxdepth - 1,
    depthfirst2( Model, Sol, Max1).
 
goal( Situation):-
    member( [a,b,c], Situation).
Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
?- [s233].
true.
 
?- solve( [[a,b,c],[],[]], Solution,10).
Solution = [[[a, b, c], [], []]] .
 
?- solve( [[a,b,c],[],[]], Solution,10).
Solution = [[[a, b, c], [], []]] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...]] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...], [[...|...]|...], [...|...]] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], [[]]], [[c], [b, a], [[[]]]], [[], [c, b, a], [[[...]]]], [[b, a], [c], [[...]]], [[a], [b|...], [...]], [[], [...|...]|...], [[...|...]|...], [...|...]] .
 
?- solve( [[b,c,a],[],[]], Solution,10).
false.
 
?- trace.
true.
 
[trace]  ?- solve( [[b,c,a],[],[]], Solution,10).
   Call: (8) solve([[b, c, a], [], []], _18482828, 10) ? creep
   Call: (9) depthfirst2([[b, c, a], [], []], _18482828, 10) ? creep
   Call: (10) goal([[b, c, a], [], []]) ? creep
   Call: (11) lists:member([a, b, c], [[b, c, a], [], []]) ? creep
   Fail: (11) lists:member([a, b, c], [[b, c, a], [], []]) ? creep
   Fail: (10) goal([[b, c, a], [], []]) ? creep
   Redo: (9) depthfirst2([[b, c, a], [], []], _18482828, 10) ? creep
   Call: (10) 10>0 ? creep
   Exit: (10) 10>0 ? creep
   Call: (10) s([[b, c, a], [], []], _18483110) ? creep
   Call: (11) del([_18483106|_18483094], [[b, c, a], [], []], _18483142) ? creep
   Exit: (11) del([b, c, a], [[b, c, a], [], []], [[], []]) ? creep
   Call: (11) del(_18483108, [[], []], _18483112) ? creep
   Exit: (11) del([], [[], []], [[]]) ? creep
   Exit: (10) s([[b, c, a], [], []], [[c, a], [b], [[]]]) ? creep
   Call: (10) _18483144 is 10+ -1 ? creep
   Exit: (10) 9 is 10+ -1 ? creep
   Call: (10) depthfirst2([[c, a], [b], [[]]], _18483090, 9) ? creep
   Call: (11) goal([[c, a], [b], [[]]]) ? creep
   Call: (12) lists:member([a, b, c], [[c, a], [b], [[]]]) ? creep
   Fail: (12) lists:member([a, b, c], [[c, a], [b], [[]]]) ? creep
   Fail: (11) goal([[c, a], [b], [[]]]) ? creep
   Redo: (10) depthfirst2([[c, a], [b], [[]]], _18483090, 9) ? creep
   Call: (11) 9>0 ? creep
   Exit: (11) 9>0 ? creep
   Call: (11) s([[c, a], [b], [[]]], _18483152) ? creep
   Call: (12) del([_18483148|_18483136], [[c, a], [b], [[]]], _18483184) ? creep
   Exit: (12) del([c, a], [[c, a], [b], [[]]], [[b], [[]]]) ? creep
   Call: (12) del(_18483150, [[b], [[]]], _18483154) ? creep
   Exit: (12) del([b], [[b], [[]]], [[[]]]) ? creep
   Exit: (11) s([[c, a], [b], [[]]], [[a], [c, b], [[[]]]]) ? creep
   Call: (11) _18483186 is 9+ -1 ? creep
   Exit: (11) 8 is 9+ -1 ? creep
   Call: (11) depthfirst2([[a], [c, b], [[[]]]], _18483132, 8) ? creep
   Call: (12) goal([[a], [c, b], [[[]]]]) ? creep
   Call: (13) lists:member([a, b, c], [[a], [c, b], [[[]]]]) ? creep
   Fail: (13) lists:member([a, b, c], [[a], [c, b], [[[]]]]) ? creep
   Fail: (12) goal([[a], [c, b], [[[]]]]) ? creep
   Redo: (11) depthfirst2([[a], [c, b], [[[]]]], _18483132, 8) ? creep
   Call: (12) 8>0 ? creep
   Exit: (12) 8>0 ? creep
   Call: (12) s([[a], [c, b], [[[]]]], _18483194) ? creep
   Call: (13) del([_18483190|_18483178], [[a], [c, b], [[[]]]], _18483226) ? creep
   Exit: (13) del([a], [[a], [c, b], [[[]]]], [[c, b], [[[]]]]) ? creep
   Call: (13) del(_18483192, [[c, b], [[[]]]], _18483196) ? creep
   Exit: (13) del([c, b], [[c, b], [[[]]]], [[[[]]]]) ? creep
   Exit: (12) s([[a], [c, b], [[[]]]], [[], [a, c, b], [[[[]]]]]) ? creep
   Call: (12) _18483228 is 8+ -1 ? creep
   Exit: (12) 7 is 8+ -1 ? creep
   Call: (12) depthfirst2([[], [a, c, b], [[[[]]]]], _18483174, 7) ? creep
   Call: (13) goal([[], [a, c, b], [[[[]]]]]) ? creep
   Call: (14) lists:member([a, b, c], [[], [a, c, b], [[[[]]]]]) ? creep
   Fail: (14) lists:member([a, b, c], [[], [a, c, b], [[[[]]]]]) ? creep
   Fail: (13) goal([[], [a, c, b], [[[[]]]]]) ? creep
   Redo: (12) depthfirst2([[], [a, c, b], [[[[]]]]], _18483174, 7) ? creep
   Call: (13) 7>0 ? creep
   Exit: (13) 7>0 ? creep
   Call: (13) s([[], [a, c, b], [[[[]]]]], _18483236) ? creep
   Call: (14) del([_18483232|_18483220], [[], [a, c, b], [[[[]]]]], _18483268) ? creep
   Call: (15) del([_18483232|_18483220], [[a, c, b], [[[[]]]]], _18483252) ? creep
   Exit: (15) del([a, c, b], [[a, c, b], [[[[]]]]], [[[[[]]]]]) ? creep
   Exit: (14) del([a, c, b], [[], [a, c, b], [[[[]]]]], [[], [[[[]]]]]) ? creep
   Call: (14) del(_18483234, [[], [[[[]]]]], _18483238) ? creep
   Exit: (14) del([], [[], [[[[]]]]], [[[[[]]]]]) ? creep
   Exit: (13) s([[], [a, c, b], [[[[]]]]], [[c, b], [a], [[[[[]]]]]]) ? creep
   Call: (13) _18483276 is 7+ -1 ? creep
   Exit: (13) 6 is 7+ -1 ? creep
   Call: (13) depthfirst2([[c, b], [a], [[[[[]]]]]], _18483216, 6) ? creep
   Call: (14) goal([[c, b], [a], [[[[[]]]]]]) ? creep
   Call: (15) lists:member([a, b, c], [[c, b], [a], [[[[[]]]]]]) ? creep
   Fail: (15) lists:member([a, b, c], [[c, b], [a], [[[[[]]]]]]) ? creep
   Fail: (14) goal([[c, b], [a], [[[[[]]]]]]) ? creep
   Redo: (13) depthfirst2([[c, b], [a], [[[[[]]]]]], _18483216, 6) ? creep
   Call: (14) 6>0 ? creep
   Exit: (14) 6>0 ? creep
   Call: (14) s([[c, b], [a], [[[[[]]]]]], _18483284) ? creep
   Call: (15) del([_18483280|_18483268], [[c, b], [a], [[[[[]]]]]], _18483316) ? creep
   Exit: (15) del([c, b], [[c, b], [a], [[[[[]]]]]], [[a], [[[[[]]]]]]) ? creep
   Call: (15) del(_18483282, [[a], [[[[[]]]]]], _18483286) ? creep
   Exit: (15) del([a], [[a], [[[[[]]]]]], [[[[[[]]]]]]) ? creep
   Exit: (14) s([[c, b], [a], [[[[[]]]]]], [[b], [c, a], [[[[[[]]]]]]]) ? creep
   Call: (14) _18483318 is 6+ -1 ? creep
   Exit: (14) 5 is 6+ -1 ? creep
   Call: (14) depthfirst2([[b], [c, a], [[[[[[]]]]]]], _18483264, 5) ? creep
   Call: (15) goal([[b], [c, a], [[[[[[]]]]]]]) ? creep
   Call: (16) lists:member([a, b, c], [[b], [c, a], [[[[[...]]]]]]) ? creep
   Fail: (16) lists:member([a, b, c], [[b], [c, a], [[[[[...]]]]]]) ? creep
   Fail: (15) goal([[b], [c, a], [[[[[[]]]]]]]) ? creep
   Redo: (14) depthfirst2([[b], [c, a], [[[[[[]]]]]]], _18483264, 5) ? creep
   Call: (15) 5>0 ? creep
   Exit: (15) 5>0 ? creep
   Call: (15) s([[b], [c, a], [[[[[[]]]]]]], _18483326) ? creep
   Call: (16) del([_18483322|_18483310], [[b], [c, a], [[[[[[]]]]]]], _18483358) ? creep
   Exit: (16) del([b], [[b], [c, a], [[[[[[]]]]]]], [[c, a], [[[[[[]]]]]]]) ? creep
   Call: (16) del(_18483324, [[c, a], [[[[[[]]]]]]], _18483328) ? creep
   Exit: (16) del([c, a], [[c, a], [[[[[[]]]]]]], [[[[[[[]]]]]]]) ? creep
   Exit: (15) s([[b], [c, a], [[[[[[]]]]]]], [[], [b, c, a], [[[[[[...]]]]]]]) ? 
...............................
..............................
Не понятно почему в учебнике отношение s(A,B) не может найти состояние типа: [[a,b,c],[],[]] чтобы остановить перебор
0
persej
3 / 3 / 3
Регистрация: 26.11.2014
Сообщений: 83
15.04.2018, 21:00  [ТС] 7
Интерактивный поиск в глубину.
s234_2.pl
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
s(a,b).
s(b,d).
s(d,h).
s(h,d).% это обратная петля для проверки обхода безконечных циклов
s(b,e).
s(e,i).
s(e,j).
 
s(a,c).
s(c,f).
s(f,k).
s(c,g).
s(h,z).
s(g,f).
 
goal(f).
goal(j).
 
/*% к упражнению 11.4 
s( Stacks, [ Stack1 , [Top1 |Stack2] ,OtherStacks] ) :-% переход из одного состояния в другое
    del([Top1|Stack1], Stacks, Stacks1 ) ,
    del( Stack2, Stacks1 , OtherStacks).   
 
del( X, [X | L], L).
del( X, [Y | L], [Y | L1]) :-
    del( X, L, L1 ).
 
goal( Situation ):-
   member( [a,b,c], Situation).
*/
% упражнение 11.4
path(Node, Node, [Node]).
 
path(FirstNode,LastNode,[LastNode|Path]):-
    path(FirstNode, OneButLast, Path),
    s(OneButLast, LastNode),
    not(member(LastNode, Path)).
 
depth_first_iterative_deepening( Node, Solution):-
    path(Node, GoalNode, Solution),
    goal( GoalNode).
Ввожу в терминале и выход терминала:
Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
?- [s234_2].
true.
 
?- depth_first_iterative_deepening( a, Solution).
Solution = [f, c, a] ;
Solution = [j, e, b, a] ;
Solution = [f, g, c, a] ;
^CAction (h for help) ? abort
% Execution Aborted
?- path(a,Last,Path).
Last = a,
Path = [a] ;
Last = b,
Path = [b, a] ;
Last = c,
Path = [c, a] ;
Last = d,
Path = [d, b, a] ;
Last = e,
Path = [e, b, a] ;
Last = f,
Path = [f, c, a] ;
Last = g,
Path = [g, c, a] ;
Last = h,
Path = [h, d, b, a] ;
Last = i,
Path = [i, e, b, a] ;
Last = j,
Path = [j, e, b, a] ;
Last = k,
Path = [k, f, c, a] ;
Last = f,
Path = [f, g, c, a] ;
Last = z,
Path = [z, h, d, b, a] ;
Last = k,
Path = [k, f, g, c, a] ;
;^CAction (h for help) ? abort
% Execution Aborted
?-
Если узлы заранее известны, то работает без проблем.
Основная работа производится в path(A,B,Path).
Он производит интерактивный поиск в глубину.
Если задать только начальный узел, а конечный узел и путь в виде переменной, то он будет перебирать узлы и находить путь от начального к конечному узлу в обратном порядке. Работает как надо.
А теперь изменим s234_2.pl и выведем то, что было в коментариях. Но в первую очередь посмотрим вывод path(A,B,Path).

s234_2.pl
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* 
s(a,b).
s(b,d).
s(d,h).
s(h,d).% это обратная петля для проверки обхода безконечных циклов
s(b,e).
s(e,i).
s(e,j).
 
s(a,c).
s(c,f).
s(f,k).
s(c,g).
s(h,z).
s(g,f).
 
goal(f).
goal(j).
*/
 
% к упражнению 11.4 
s( Stacks, [ Stack1 , [Top1 |Stack2] ,OtherStacks] ) :-% переход из одного состояния в другое
    del([Top1|Stack1], Stacks, Stacks1 ) ,
    del( Stack2, Stacks1 , OtherStacks).   
 
del( X, [X | L], L).
del( X, [Y | L], [Y | L1]) :-
    del( X, L, L1 ).
 
goal( Situation ):-
   member( [a,b,c], Situation).
 
% упражнение 11.4
path(Node, Node, [Node]).
 
path(FirstNode,LastNode,[LastNode|Path]):-
    path(FirstNode, OneButLast, Path),
    s(OneButLast, LastNode),
    not(member(LastNode, Path)).
 
depth_first_iterative_deepening( Node, Solution):-
    path(Node, GoalNode, Solution),
    goal( GoalNode).
Ввожу в терминале и выход терминала:
Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
?- [s234_2].
true.
 
?- path([[b,c,a],[],[]],Last,Path).
Last = [[b, c, a], [], []],
Path = [[[b, c, a], [], []]] ;
Last = [[c, a], [b], [[]]],
Path = [[[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[c, a], [b], [[]]],
Path = [[[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[a], [c, b], [[[]]]],
Path = [[[a], [c, b], [[[]]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[a], [c, []], [[b]]],
Path = [[[a], [c, []], [[b]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[], [b, c, a], [[[]]]],
Path = [[[], [b, c, a], [[[]]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[], [b, []], [[c, a]]],
Path = [[[], [b, []], [[c, a]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[], [[], c, a], [[b]]],
Path = [[[], [[], c, a], [[b]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[], [[], b], [[c, a]]],
Path = [[[], [[], b], [[c, a]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[a], [c, b], [[[]]]],
Path = [[[a], [c, b], [[[]]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[a], [c, []], [[b]]],
Path = [[[a], [c, []], [[b]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[], [b, c, a], [[[]]]],
Path = [[[], [b, c, a], [[[]]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[], [b, []], [[c, a]]],
Path = [[[], [b, []], [[c, a]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[], [[], c, a], [[b]]],
Path = [[[], [[], c, a], [[b]]], [[c, a], [b], [[]]], [[b, c, a], [], []]] ;
Last = [[], [[], b], [[c, a]]],
Path = [[[], [[], b], [[c, a]]], [[c, a], [b], [[]]], [[b, c, a], [], []]]
...
...
узел состояния выводится типа Last = [[], [[], b], [[c, a]]], а должно что-то вроде Last = [[], [b], [c, a]]. Много разных пустый скобок появляется. Как видно такое вывод дает учебный пример s( Stacks, [ Stack1 , [Top1 |Stack2] ,OtherStacks] ):-

Как изменить, s()/3, чтобы выводилось что-то вроде Last = [[], [b], [c, a]] . Чтобы не добавлялись скобки?
0
persej
3 / 3 / 3
Регистрация: 26.11.2014
Сообщений: 83
26.04.2018, 14:38  [ТС] 8
Лучший ответ Сообщение было отмечено Black Fregat как решение

Решение

Нашел решение.
Если кому интересно.

Основная идея- преобразовать сложную в понимании правило s/2 в набор нескольких правил s/2, но с ясной логикой.
Выигрываем в ясности понимания, но пригрываем в том, что правил несколько.

s234_3.pl
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
% к упражнению 11.4 
s( Stacks, [ Stack1 , [Top1|Stack2] ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[[Top1|Stack1],Stack2,Stack3].
  
s( Stacks, [ Stack1 , Stack2 ,[Top1|Stack3]] ) :-% переход из одного состояния в другое
    Stacks=[[Top1|Stack1],Stack2,Stack3].
 
s( Stacks, [ [Top1|Stack1] , Stack2 ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,[Top1|Stack2],Stack3].
 
s( Stacks, [ Stack1 , Stack2 ,[Top1|Stack3]] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,[Top1|Stack2],Stack3].
 
s( Stacks, [ [Top1|Stack1] , Stack2 ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,Stack2,[Top1|Stack3]].
 
s( Stacks, [ Stack1 , [Top1|Stack2] ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,Stack2,[Top1|Stack3]].
 
 
goal( Situation ):-
   member( [a,b,c], Situation).
 
 
% упражнение 11.4. 
path(Node, Node, [Node]).
 
path(FirstNode,LastNode,[LastNode|Path]):-
    path(FirstNode, OneButLast, Path),
    s(OneButLast, LastNode),
    not(member(LastNode, Path)).
 
depth_first_iterative_deepening( Node, Solution):-
    path(Node, GoalNode, Solution),
    goal( GoalNode).
Сначала проверим как path/3 осуществляет итерактивное углубление. И найдет ли нужное состояние из кубиков [a,b,c] из произвольного начального состояния?
Результат в терминале:
PureBasic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
?- [s234_3].
true.
 
?- path([[b,c,a],[],[]], LastNode, Path).
LastNode = [[b, c, a], [], []],
Path = [[[b, c, a], [], []]] ;
LastNode = [[c, a], [b], []],
Path = [[[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[c, a], [], [b]],
Path = [[[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[a], [c, b], []],
Path = [[[a], [c, b], []], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[a], [b], [c]],
Path = [[[a], [b], [c]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[c, a], [], [b]],
Path = [[[c, a], [], [b]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[a], [c], [b]],
Path = [[[a], [c], [b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[a], [], [c, b]],
Path = [[[a], [], [c, b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[c, a], [b], []],
Path = [[[c, a], [b], []], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[], [a, c, b], []],
Path = [[[], [a, c, b], []], [[a], [c, b], []], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[], [c, b], [a]],
Path = [[[], [c, b], [a]], [[a], [c, b], []], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[a], [b], [c]],
Path = [[[a], [b], [c]], [[a], [c, b], []], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[], [a, b], [c]],
Path = [[[], [a, b], [c]], [[a], [b], [c]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[], [b], [a, c]],
Path = [[[], [b], [a, c]], [[a], [b], [c]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[b, a], [], [c]],
Path = [[[b, a], [], [c]], [[a], [b], [c]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[a], [], [b, c]],
Path = [[[a], [], [b, c]], [[a], [b], [c]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[a], [c, b], []],
Path = [[[a], [c, b], []], [[a], [b], [c]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[a], [c], [b]],
Path = [[[a], [c], [b]], [[c, a], [], [b]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[a], [], [c, b]],
Path = [[[a], [], [c, b]], [[c, a], [], [b]], [[c, a], [b], []], [[b, c, a], [], []]] ;
LastNode = [[], [a, c], [b]],
Path = [[[], [a, c], [b]], [[a], [c], [b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[], [c], [a, b]],
Path = [[[], [c], [a, b]], [[a], [c], [b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[a], [], [c, b]],
Path = [[[a], [], [c, b]], [[a], [c], [b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[b, a], [c], []],
Path = [[[b, a], [c], []], [[a], [c], [b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[a], [b, c], []],
Path = [[[a], [b, c], []], [[a], [c], [b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[], [a], [c, b]],
Path = [[[], [a], [c, b]], [[a], [], [c, b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[], [], [B][a, c, b][/B]],
Path = [[[], [],[B] [a, c, b][/B]], [[a], [], [c, b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
LastNode = [[a], [c], [b]],
Path = [[[a], [c], [b]], [[a], [], [c, b]], [[c, a], [], [b]], [[b, c, a], [], []]] 
......
Итак все работает как надо и главное не появляюся лишние скобки

Посмотрим чем закончится результат вызова правила: depth_first_iterative_deepening/2?
Должен дать результат пути от данного состояния до цели (состояние типа [[],[a,b,c],[]] ).

Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 depth_first_iterative_deepening( [[b,c,a],[],[]], Solution).
Solution = [[[], [], [a, b, c]], [[a], [], [b, c]], [[a], [b], [c]], [[c, a], [b], []], [[b, c, a], [], []]] ;
Solution = [[[], [a, b, c], []], [[a], [b, c], []], [[a], [c], [b]], [[c, a], [], [b]], [[b, c, a], [], []]] ;
Solution = [[[], [], [a, b, c]], [[a], [], [b, c]], [[a], [b], [c]], [[a], [c, b], []], [[c, a], [b], []], [[b, c|...], [], []]] ;
Solution = [[[], [], [a, b, c]], [[a], [], [b, c]], [[b, a], [], [c]], [[a], [b], [c]], [[c, a], [b], []], [[b, c|...], [], []]] ;
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[a], [], [b, c]], [[a], [b], [c]], [[c, a], [b], []], [[b, c|...], [], []]] ;
Solution = [[[], [a, b, c], []], [[a], [b, c], []], [[a], [c], [b]], [[c, a], [], [b]], [[c, a], [b], []], [[b, c|...], [], []]] ;
Solution = [[[], [a, b, c], []], [[a], [b, c], []], [[b, a], [c], []], [[a], [c], [b]], [[c, a], [], [b]], [[b, c|...], [], []]] ;
Solution = [[[], [a, b, c], []], [[], [b, c], [a]], [[a], [b, c], []], [[a], [c], [b]], [[c, a], [], [b]], [[b, c|...], [], []]] ;
Solution = [[[], [a, b, c], []], [[a], [b, c], []], [[a], [c], [b]], [[a], [], [c, b]], [[c, a], [], [b]], [[b, c|...], [], []]] ;
Solution = [[[], [], [a, b, c]], [[a], [], [b, c]], [[a], [b], [c]], [[c, a], [b], []], [[c, a], [], [b]], [[b, c|...], [], []]] 
........
?- depth_first_iterative_deepening( [[a,c,b],[],[]], Solution).
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[b], [a], [c]], [[c, b], [a], []], [[a, c, b], [], []]] ;
Solution = [[[], [a, b, c], []], [[], [b, c], [a]], [[b], [c], [a]], [[c, b], [], [a]], [[a, c, b], [], []]] ;
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[b], [a], [c]], [[b], [c, a], []], [[c, b], [a], []], [[a, c|...], [], []]] ;
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[], [b, a], [c]], [[b], [a], [c]], [[c, b], [a], []], [[a, c|...], [], []]] ;
Solution = [[[], [], [a, b, c]], [[a], [], [b, c]], [[], [a], [b, c]], [[b], [a], [c]], [[c, b], [a], []], [[a, c|...], [], []]] ;
Solution = [[[], [a, b, c], []], [[], [b, c], [a]], [[b], [c], [a]], [[c, b], [], [a]], [[c, b], [a], []], [[a, c|...], [], []]] ;
Solution = [[[], [a, b, c], []], [[a], [b, c], []], [[], [b, c], [a]], [[b], [c], [a]], [[c, b], [], [a]], [[a, c|...], [], []]] ;
Solution = [[[], [a, b, c], []], [[], [b, c], [a]], [[], [c], [b, a]], [[b], [c], [a]], [[c, b], [], [a]], [[a, c|...], [], []]] ;
Solution = [[[], [a, b, c], []], [[], [b, c], [a]], [[b], [c], [a]], [[b], [], [c, a]], [[c, b], [], [a]], [[a, c|...], [], []]] ;
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[b], [a], [c]], [[c, b], [a], []], [[c, b], [], [a]], [[a, c|...], [], []]] ;
Solution = [[[a, b, c], [], []], [[b, c], [a], []], [[c], [a], [b]], [[], [c, a], [b]], [[b], [c, a], []], [[c, b], [a], []], [[a|...], []|...]] ;
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[], [b, a], [c]], [[b], [a], [c]], [[b], [c, a], []], [[c, b], [a], []], [[a|...], []|...]] .
Работает как надо.
Как видно из правила s/3 предоставляется только 3 столбика для прекладки кубиков. Интересно, а сможет ли перенести больше кубиков (например 5) и позиции крайней левого столбика в крайний правый столбик (головоломка Ханойская башня) сохраняя туже очередность кубиков?

Добавлено через 38 минут
s234_3.pl
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
% к упражнению 11.4 
s( Stacks, [ Stack1 , [Top1|Stack2] ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[[Top1|Stack1],Stack2,Stack3].
  
s( Stacks, [ Stack1 , Stack2 ,[Top1|Stack3]] ) :-% переход из одного состояния в другое
    Stacks=[[Top1|Stack1],Stack2,Stack3].
 
s( Stacks, [ [Top1|Stack1] , Stack2 ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,[Top1|Stack2],Stack3].
 
s( Stacks, [ Stack1 , Stack2 ,[Top1|Stack3]] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,[Top1|Stack2],Stack3].
 
s( Stacks, [ [Top1|Stack1] , Stack2 ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,Stack2,[Top1|Stack3]].
 
s( Stacks, [ Stack1 , [Top1|Stack2] ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,Stack2,[Top1|Stack3]].
 
 
goal( GoalNode ):-
    GoalNode=[[],[],[a,b,c]].
  
 
% упражнение 11.4. 
path(Node, Node, [Node]).
 
path(FirstNode,LastNode,[LastNode|Path]):-
    path(FirstNode, OneButLast, Path),
    s(OneButLast, LastNode),
    not(member(LastNode, Path)).
 
depth_first_iterative_deepening( Node, Solution):-
    path(Node, GoalNode, Solution),
    goal( GoalNode).
Результат в терминале:
Bash
1
2
3
4
5
?- depth_first_iterative_deepening( [[a,b,c],[],[]], Solution).
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[], [b, a], [c]], [[c], [b, a], []], [[b, c], [a], []], [[a, b|...], [], []]] ;
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[], [b, a], [c]], [[], [c, b, a], []], [[c], [b, a], []], [[b, c], [a], []], [[a|...], []|...]] ;
Solution = [[[], [], [a, b, c]], [[], [a], [b, c]], [[b], [a], [c]], [[], [b, a], [c]], [[c], [b, a], []], [[b, c], [a], []], [[a|...], []|...]] ;
Solution = [[[], [], [a, b, c]], [[a], [], [b, c]], [[], [a], [b, c]], [[], [b, a], [c]], [[c], [b, a], []], [[b, c], [a], []], [[a|...], []|...]] .
Тоесть при 3-х кубиках нужный путь найден.

А при 4-х кубиках?
s234_3.pl
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
% к упражнению 11.4 
s( Stacks, [ Stack1 , [Top1|Stack2] ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[[Top1|Stack1],Stack2,Stack3].
  
s( Stacks, [ Stack1 , Stack2 ,[Top1|Stack3]] ) :-% переход из одного состояния в другое
    Stacks=[[Top1|Stack1],Stack2,Stack3].
 
s( Stacks, [ [Top1|Stack1] , Stack2 ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,[Top1|Stack2],Stack3].
 
s( Stacks, [ Stack1 , Stack2 ,[Top1|Stack3]] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,[Top1|Stack2],Stack3].
 
s( Stacks, [ [Top1|Stack1] , Stack2 ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,Stack2,[Top1|Stack3]].
 
s( Stacks, [ Stack1 , [Top1|Stack2] ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,Stack2,[Top1|Stack3]].
 
 
goal( GoalNode ):-
    GoalNode=[[],[],[a,b,c,d]].
  
 
% упражнение 11.4. 
path(Node, Node, [Node]).
 
path(FirstNode,LastNode,[LastNode|Path]):-
    path(FirstNode, OneButLast, Path),
    s(OneButLast, LastNode),
    not(member(LastNode, Path)).
 
depth_first_iterative_deepening( Node, Solution):-
    path(Node, GoalNode, Solution),
    goal( GoalNode).
Результат в терминале:
Bash
1
2
3
4
5
?- depth_first_iterative_deepening( [[a,b,c,d],[],[]], Solution).
Solution = [[[], [], [a, b, c, d]], [[], [a], [b, c, d]], [[], [b, a], [c, d]], [[], [c, b, a], [d]], [[d], [c, b|...], []], [[c, d], [b|...], []], [[b|...], [...]|...], [[...|...]|...]] ;
Solution = [[[], [], [a, b, c, d]], [[], [a], [b, c, d]], [[], [b, a], [c, d]], [[], [c, b, a], [d]], [[], [d, c|...], []], [[d], [c|...], []], [[c|...], [...|...]|...], [[...|...]|...], [...|...]] ;
Solution = [[[], [], [a, b, c, d]], [[], [a], [b, c, d]], [[], [b, a], [c, d]], [[c], [b, a], [d]], [[], [c, b|...], [d]], [[d], [c|...], []], [[c|...], [...|...]|...], [[...|...]|...], [...|...]] ;
Solution = [[[], [], [a, b, c, d]], [[], [a], [b, c, d]], [[b], [a], [c, d]], [[], [b, a], [c, d]], [[], [c, b|...], [d]], [[d], [c|...], []], [[c|...], [...|...]|...], [[...|...]|...], [...|...]] .
А при 5-ти кубиках?
s234_3.pl
Prolog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
% к упражнению 11.4 
s( Stacks, [ Stack1 , [Top1|Stack2] ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[[Top1|Stack1],Stack2,Stack3].
  
s( Stacks, [ Stack1 , Stack2 ,[Top1|Stack3]] ) :-% переход из одного состояния в другое
    Stacks=[[Top1|Stack1],Stack2,Stack3].
 
s( Stacks, [ [Top1|Stack1] , Stack2 ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,[Top1|Stack2],Stack3].
 
s( Stacks, [ Stack1 , Stack2 ,[Top1|Stack3]] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,[Top1|Stack2],Stack3].
 
s( Stacks, [ [Top1|Stack1] , Stack2 ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,Stack2,[Top1|Stack3]].
 
s( Stacks, [ Stack1 , [Top1|Stack2] ,Stack3] ) :-% переход из одного состояния в другое
    Stacks=[Stack1,Stack2,[Top1|Stack3]].
 
 
goal( GoalNode ):-
    GoalNode=[[],[],[a,b,c,d,e]].
  
 
% упражнение 11.4. 
path(Node, Node, [Node]).
 
path(FirstNode,LastNode,[LastNode|Path]):-
    path(FirstNode, OneButLast, Path),
    s(OneButLast, LastNode),
    not(member(LastNode, Path)).
 
depth_first_iterative_deepening( Node, Solution):-
    path(Node, GoalNode, Solution),
    goal( GoalNode).
Результат в терминале:
Bash
1
2
3
4
5
6
7
?- depth_first_iterative_deepening( [[a,b,c,d,e],[],[]], Solution).
Solution = [[[], [], [a, b, c, d, e]], [[], [a], [b, c, d, e]], [[], [b, a], [c, d, e]], [[], [c, b, a], [d, e]], [[], [d, c|...], [e]], [[e], [d|...], []], [[d|...], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[], [], [a, b, c, d, e]], [[], [a], [b, c, d, e]], [[], [b, a], [c, d, e]], [[], [c, b, a], [d, e]], [[], [d, c|...], [e]], [[], [e|...], []], [[e], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[], [], [a, b, c, d, e]], [[], [a], [b, c, d, e]], [[], [b, a], [c, d, e]], [[], [c, b, a], [d, e]], [[d], [c, b|...], [e]], [[], [d|...], [...]], [[e], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[], [], [a, b, c, d, e]], [[], [a], [b, c, d, e]], [[], [b, a], [c, d, e]], [[c], [b, a], [d, e]], [[], [c, b|...], [d|...]], [[], [d|...], [...]], [[e], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[], [], [a, b, c, d, e]], [[], [a], [b, c, d, e]], [[b], [a], [c, d, e]], [[], [b, a], [c, d|...]], [[], [c, b|...], [d|...]], [[], [d|...], [...]], [[e], [...|...]|...], [[...|...]|...], [...|...]|...] ;
Solution = [[[], [], [a, b, c, d, e]], [[a], [], [b, c, d, e]], [[], [a], [b, c, d|...]], [[], [b, a], [c, d|...]], [[], [c, b|...], [d|...]], [[], [d|...], [...]], [[e], [...|...]|...], [[...|...]|...], [...|...]|...] .
Все работает!!!

Поторопился.
В конце не разглядеть начало состояния из исходной позиции [[a,b,c,d,e],[],[]].
1
26.04.2018, 14:38
MoreAnswers
Эксперт
37091 / 29110 / 5898
Регистрация: 17.06.2006
Сообщений: 43,301
26.04.2018, 14:38

Перестановка элементов списка
Подскажите, пожалуйста, (или дайте ссылки на) решения следующих задач со...

Перестановка элементов и нахождение минимума
Надо еще сделать две такие задачки: 1. Создайте предикат, осуществляющий...

Перестановка элементов на диагоналях матрицы.
Помогите пожалуйста! Дано квадратную матрицу неизвестного размера, нужно...


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
8
Ответ Создать тему
Опции темы

КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Рейтинг@Mail.ru