在 Prolog 中获取规则的最小值
Get min value of a rule in Prolog
我正在尝试解决 Knight Tour 问题。我需要获得移动量最少的位置。我有计算某个位置移动次数的规则 (search_path),但我需要获取该规则的最小值 (search_path_min)。
moves(X, Y, Xi, Yi, Size) :-
( Xi is X - 1, Yi is Y - 2
; Xi is X - 1, Yi is Y + 2
; Xi is X + 1, Yi is Y - 2
; Xi is X + 1, Yi is Y + 2
; Xi is X - 2, Yi is Y - 1
; Xi is X - 2, Yi is Y + 1
; Xi is X + 2, Yi is Y - 1
; Xi is X + 2, Yi is Y + 1 ),
Xi > 0,
Yi > 0,
Xi =< Size*Size,
Yi =< Size*Size.
posibles_moves(Size, X, Y, Tour, Xi, Yi):-
moves(X, Y, Xi, Yi, Size),
\+ member((Xi,Yi),Tour).
posibles_moves_count(Size, X, Y, Tour, Count):-
findall(_, posibles_moves(Size, X, Y, Tour, Xi, Yi), Moves),
length(Moves, Count).
search_path(Size, X, Y, Tour, Xi, Yi, Count):-
posibles_moves(Size, X, Y, Tour, Xi, Yi),
posibles_moves_count(Size, Xi, Yi, [(Xi,Yi)|Tour],Count).
search_path_min(Size, X, Y, Tour, Xi, Yi):-
search_path(Size, X, Y, Tour, Xi, Yi, Count),
?
?
?
?
knight_tour(Size, X, Y, Tour, Path) :-
length(Tour, L),
L =:= Size * Size - 1,
_Tour = [(X, Y) | Tour],
reverse(_Tour, Path).
knight_tour(Size, X, Y, Tour, Path) :-
length(Visits, L),
L < Size * Size - 1,
search_path_min(Size, X, Y, Tour, Xi, Yi),
_Tour = [(X, Y) | Tour],
knight_tour(Size, Xi, Yi, _Tour, Path).
我的规则search_path 我需要获得具有 Count 变量最小值的结果。
谢谢
我能解决
search_path_min(Size, X, Y, Tour, Minimum):-
findall(min(Xi,Yi,Count),search_path(Size,X,Y,Tour,Xi,Yi,Count),[H|Rest]),
search_path_min(H,Rest,Minimum).
search_path_min(Minimum, [], Minimum).
search_path_min(min(X,Y,C),[min(X2,Y2,C2)|Rest],Minimum):-
C < C2 -> search_path_min(min(X,Y,C),Rest,Minimum);
search_path_min(min(X2,Y2,C2),Rest,Minimum).
我正在尝试解决 Knight Tour 问题。我需要获得移动量最少的位置。我有计算某个位置移动次数的规则 (search_path),但我需要获取该规则的最小值 (search_path_min)。
moves(X, Y, Xi, Yi, Size) :-
( Xi is X - 1, Yi is Y - 2
; Xi is X - 1, Yi is Y + 2
; Xi is X + 1, Yi is Y - 2
; Xi is X + 1, Yi is Y + 2
; Xi is X - 2, Yi is Y - 1
; Xi is X - 2, Yi is Y + 1
; Xi is X + 2, Yi is Y - 1
; Xi is X + 2, Yi is Y + 1 ),
Xi > 0,
Yi > 0,
Xi =< Size*Size,
Yi =< Size*Size.
posibles_moves(Size, X, Y, Tour, Xi, Yi):-
moves(X, Y, Xi, Yi, Size),
\+ member((Xi,Yi),Tour).
posibles_moves_count(Size, X, Y, Tour, Count):-
findall(_, posibles_moves(Size, X, Y, Tour, Xi, Yi), Moves),
length(Moves, Count).
search_path(Size, X, Y, Tour, Xi, Yi, Count):-
posibles_moves(Size, X, Y, Tour, Xi, Yi),
posibles_moves_count(Size, Xi, Yi, [(Xi,Yi)|Tour],Count).
search_path_min(Size, X, Y, Tour, Xi, Yi):-
search_path(Size, X, Y, Tour, Xi, Yi, Count),
?
?
?
?
knight_tour(Size, X, Y, Tour, Path) :-
length(Tour, L),
L =:= Size * Size - 1,
_Tour = [(X, Y) | Tour],
reverse(_Tour, Path).
knight_tour(Size, X, Y, Tour, Path) :-
length(Visits, L),
L < Size * Size - 1,
search_path_min(Size, X, Y, Tour, Xi, Yi),
_Tour = [(X, Y) | Tour],
knight_tour(Size, Xi, Yi, _Tour, Path).
我的规则search_path 我需要获得具有 Count 变量最小值的结果。 谢谢
我能解决
search_path_min(Size, X, Y, Tour, Minimum):-
findall(min(Xi,Yi,Count),search_path(Size,X,Y,Tour,Xi,Yi,Count),[H|Rest]),
search_path_min(H,Rest,Minimum).
search_path_min(Minimum, [], Minimum).
search_path_min(min(X,Y,C),[min(X2,Y2,C2)|Rest],Minimum):-
C < C2 -> search_path_min(min(X,Y,C),Rest,Minimum);
search_path_min(min(X2,Y2,C2),Rest,Minimum).