% Define the problem domain
% Sample state transitions between cities.
% You can adapt this for your specific problem.
transition(city_a, city_b, 10).
transition(city_a, city_c, 5).
transition(city_b, city_d, 15).
transition(city_b, city_e, 20).
transition(city_c, city_e, 10).
transition(city_d, city_f, 5).
transition(city_e, city_f, 8).
transition(city_e, city_g, 12).
% Define the heuristic function. This is problem-specific.
% In this example, it's the straight-line distance between cities.
heuristic(city_a, H) :- H is 15. % Assuming city_a to city_f is 15 units away.
heuristic(city_b, H) :- H is 12. % Assuming city_b to city_f is 12 units away.
heuristic(city_c, H) :- H is 20. % Assuming city_c to city_f is 20 units away.
heuristic(city_d, H) :- H is 8. % Assuming city_d to city_f is 8 units away.
heuristic(city_e, H) :- H is 5. % Assuming city_e to city_f is 5 units away.
heuristic(city_f, H) :- H is 0. % The goal state.
% best_first_search/3 is the main predicate.
best_first_search(Start, Goal, Path) :-
best_first_search([[Start, 0]], [], Goal, Path).
% best_first_search/4 handles the BFS algorithm.
best_first_search([[Node, _] | _], _, Goal, [Node | []]) :- Node = Goal.
best_first_search([[Node, Cost] | Rest], Visited, Goal, Path) :-
findall([Child, NewCost], (
transition(Node, Child, StepCost),
NewCost is Cost + StepCost,
heuristic(Child, H),
NewCost + H, Child
), Children),
append(Children, Rest, NewOpen),
best_first_search(NewOpen, [Node | Visited], Goal, Path).
% Sample query:
% best_first_search(city_a, city_f, Path).
Write, Run & Share Prolog code online using OneCompiler’s Prolog online compiler for free. It’s a simple and intuitive platform to experiment with logic programming in Prolog. OneCompiler supports standard Prolog syntax, great for learning, prototyping, and practicing logic-based problems.
Prolog (Programming in Logic) is a logic programming language associated with artificial intelligence and computational linguistics. It works through facts, rules, and queries, using a form of symbolic reasoning known as backward chaining. Prolog is declarative, meaning you describe what you want instead of how to compute it.
The following is a simple Prolog program that prints a greeting:
:- initialization(main).
main :-
write('Hello, World!').
Facts represent basic assertions about the world.
likes(alice, pizza).
likes(bob, pasta).
Rules define logical relationships using facts.
friends(X, Y) :- likes(X, Z), likes(Y, Z).
Queries are used to find information based on facts and rules.
?- likes(alice, What).
| Operator | Description |
|---|---|
:- | Rule definition |
, | Logical AND |
; | Logical OR |
= | Unification |
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
Prolog heavily relies on recursion.
factorial(0, 1).
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
This guide provides a quick reference to Prolog programming syntax and features. Start writing Prolog code using OneCompiler’s Prolog online compiler today!