This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/examples/searching/hill_climbing1.lgt

43 lines
1.0 KiB
Plaintext
Raw Normal View History

:- object(hill_climbing(Threshold),
instantiates(heuristic_search(Threshold))).
:- info([
version is 1.0,
authors is 'Paulo Moura',
date is 1998/3/23,
comment is 'Hill climbing heuristic state space search strategy.',
parnames is ['Threshold']]).
:- uses(list).
:- private(hill/7).
search(Space, State, Threshold, Solution, Cost) :-
hill(Space, State, Threshold, [], Path, 0, Cost),
list::reverse(Path, Solution).
hill(Space, State, _, Path, [State| Path], Cost, Cost) :-
Space::goal_state(State).
hill(Space, State, Threshold, Path, Solution, SoFar, Total) :-
SoFar < Threshold,
findall(
(Estimate, Cost, Next),
(Space::next_state(State, Next, Cost),
\+ list::member(Next, [State| Path]),
Space::heuristic(Next, Guess),
Estimate is Guess + Cost),
States),
list::sort(States, SortedStates),
list::member((_, Cost, Next), SortedStates),
SoFar2 is SoFar + Cost,
hill(Space, Next, Threshold, [State| Path], Solution, SoFar2, Total).
:- end_object.