-
Notifications
You must be signed in to change notification settings - Fork 0
/
flp17-log.pl
224 lines (185 loc) · 6.38 KB
/
flp17-log.pl
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
%FLP 2nd Project
%Jordan Jarolim, xjarol03, FIT VUTBR
%1. 5. 2017
%flp17-log.pl
% Main taking care about averything
main :-
prompt(_, ''),
read_file(user_input, Matrix),
getDimensions(Matrix, Rows, Cols),
% Replace hole
atom_concat('A', Rows, Hole),
putHole(Matrix, Rows, Replaced, '**', Hole),
list_butlast(_, Replaced),
% Define Model to be reached
defModel(Rows, Cols, Goal),!,
% getMaxPermut can raise in int_overflow -> manually selected 15:/
%getMaxPermut(Rows, Cols, Permut),write(Permut),nl,!,
numlist(1, 15, MaxDepthList), !, % "Breakpoint" is needed here
% Generate depths
nth1(_, MaxDepthList, MaxDepth),
% Start backtracking with defined depth
backtrackSolution(Replaced, 0, MaxDepth, Goal, Moves, []),
% Print results
putHole(Replaced, Rows, Solved, Hole, '**'),
printMatrix(Solved),
trackPath(Replaced, Moves).
% Read file line by line
%http://stackoverflow.com/questions/4805601/read-a-file-line-by-line-in-prolog
%http://stackoverflow.com/questions/23411139/prolog-unexpected-end-of-file
read_file(Stream,[]) :-
at_end_of_stream(Stream).
read_file(Stream,[X|L]) :-
\+ at_end_of_stream(Stream),
read_line_to_codes(Stream,Codes),
atom_chars(Atoms, Codes),
%http://stackoverflow.com/questions/15436464/split-string-in-items
atomic_list_concat(X,' ', Atoms),
read_file(Stream,L), !.
%Determine matrix dimension
getDimensions(Matrix, Rows, Cols) :-
length(Matrix, Rows),
nth1(1, Matrix, Row), !,
length(Row, Cols).
% Find hole
getHolePosition(Matrix, PosRow, PosCol, ToFind) :-
nth1(PosRow, Matrix, Row),
nth1(PosCol, Row, ToFind).
% Define correct matrix
defModel(Rows,Cols,Goal) :-
numlist(1,Rows,NumberedRows),%1,2,3,4
Max is Cols+65,
numlist(65, Max, NumberedCols),%A,B,C,D
buildModel(NumberedRows, NumberedCols, Goal),
list_butlast(_, Goal).
%Build matrix rows
buildRows(_, [], _).
buildRows(A,[C|D], [E|F]):-
atom_chars(MyChar, [C]),
atom_number(MyNum, A),
atom_concat(MyChar, MyNum, E),
buildRows(A, D, F).
%Build final correct matrix
buildModel([],_, _).
buildModel([A|B], Cols, [X|Y]):-
buildRows(A, Cols, ModelRows),
list_butlast(ModelRows, X),
buildModel(B, Cols, Y).
% Put AX or ** to the matrix according to its parameters
putHole([], _, _, _, _).
putHole([A|B], Rows, [X|Y], In, Out):-
replace(In, Out, A, X),
putHole(B, Rows, Y, In, Out).
% Print matrix
printMatrix([]).
printMatrix([Row|T]) :-
printRow(Row),nl,
printMatrix(T).
% Print row of matrix
printRow([]).
printRow([A|B]):-
write(A),
write(' '),
printRow(B).
% Experimental - get number of all possible combinations
getMaxPermut(Rows, Cols, Result):-
factorial(Rows*Cols, Value1),
factorial(Rows, Value2),
Result is Value1/Value2.
% Experimental - get factorial
factorial( 0, 1 ).
factorial( N, Value ) :-
N > 0,
Prev is N - 1,
factorial( Prev, Prevfact ),
Value is Prevfact * N.
%%%%%%%%% Backtracking %%%%%%%%%
% Reached the solution
backtrackSolution(ActualTower, _ ,_ , ActualTower, _, _).
% Cannot solve
backtrackSolution(_, Depth, MaxDepth, _ ,_ ,_ ) :-
Depth >= MaxDepth,
!,
fail.
% Backtrack until the max depth of rotations is reached
backtrackSolution(ActualTower, Depth, MaxDepth, Model,[Move|Moves], Changes) :-
% Increase Depth
Depth1 is Depth + 1,
% Generate Move
(
tryRotation(ActualTower, NewTower, Move);
changeHole(ActualTower, NewTower, Move)
),
% Check if it is a new move
not(member(NewTower, Changes)),
% Check if its correct solution or go deeper
backtrackSolution(NewTower, Depth1, MaxDepth, Model, Moves, [ActualTower|Changes]).
% Rotate row - rotates row and save its index to Move Array -> Important for replay ->
% nth1(?Index, ?List, ?Elem) - Is true when Elem is the Index'th element of List. Counting starts at 1.
% http://www.swi-prolog.org/pldoc/man?predicate=nth1/3
tryRotation(Matrix, RotatedMatrix, Index) :-
% Rows stores all possible rows (if fail it just takes another), Index stores index of a row in matrix
nth1(Index, Matrix, Rows),
% Rotate - see def of Rotate() below and stackoverflow link
rotate(Rows, RotatedRow),
% Place rotated row in matrix
% http://www.swi-prolog.org/pldoc/man?predicate=select/4
% replace() defined below (from stack overflow) doesnt work, I dont know why :/
select(Rows, Matrix, RotatedRow, RotatedMatrix).
% Rotate list
% http://stackoverflow.com/questions/10255703/how-to-rotate-lists-in-prolog
rotate([H|T],R) :- append(T,[H],R).
changeHole(Matrix, Result, Move):-
% Define type of move for replay
Move is -1,
% Get matrix dimensions
getDimensions(Matrix, Rows, _),
% Define hole appereance
atom_concat('A', Rows, ToFind),
% Find hole position
getHolePosition(Matrix, PosRow, PosCol, ToFind),
% Define direction of swap
(Rows > PosRow ->
ToBeSwapped is PosRow + 1
;
ToBeSwapped is 1
),
% Get hole value
nth1(PosRow, Matrix, RowHole),
nth1(PosCol, RowHole, HoleValue),
% Get value to be swapped
nth1(ToBeSwapped, Matrix, RowSwap),
nth1(PosCol, RowSwap, NewValue),
% Swap in rows - replace defined below doesnt work :/
select(HoleValue, RowHole, NewValue, NewHoleRow),
select(NewValue, RowSwap, HoleValue, NewSwapRow),
% Put rows back
select(RowHole, Matrix, NewHoleRow, FirstSwap),
select(RowSwap, FirstSwap, NewSwapRow, Result).
% Print the path
trackPath(_, []) :- !.
trackPath(ActualTower, [Move|Moves]) :-
(
tryRotation(ActualTower, NewTower, Move);
changeHole(ActualTower, NewTower, Move)
),
getDimensions(ActualTower, Rows, _),
atom_concat('A', Rows, Hole),
putHole(NewTower, Rows, Solved, Hole, '**'),
nl,
printMatrix(Solved), !,
trackPath(NewTower, Moves).
%%%%%%%%% Helpers %%%%%%%%%
% Delete last element in a list
% In my case some pointer to a memory - just some trash, I dont know how to solve it more elegant
%http://stackoverflow.com/questions/16174681/how-to-delete-the-last-element-from-a-list-in-prolog
list_butlast([X|Xs], Ys) :- % use auxiliary predicate ...
list_butlast_prev(Xs, Ys, X). % ... which lags behind by one item
list_butlast_prev([], [], _).
list_butlast_prev([X1|Xs], [X0|Ys], X0) :-
list_butlast_prev(Xs, Ys, X1). % lag behind by one
% Replace element in a list
%http://stackoverflow.com/questions/5850937/prolog-element-in-lists-replacement
replace(_, _, [], []).
replace(O, R, [O|T], [R|T2]) :- replace(O, R, T, T2).
replace(O, R, [H|T], [H|T2]) :- H \= O, replace(O, R, T, T2).