Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I failed to write a Prolog program using RBFS algorithm to solve a task - how to measure exactly 3 liters of water using 5-liter and 9-liter water jugs? I have a solution, but it does not use best-first search algorithm and works blindly (without heuristics). Can you please provide some pieces of code to apply RBFS algorithm to my task?

What I have tried:

Tried to use blind searching without heuristics
My code
volume(9,5).

program(A,B,W):-
    volume(VA,VB),
    A>=0,
    A=<VA,
    B>=0,
    B=<VB,
    change(A,B,[[A,B]],W),
    write1(W).
	
write1([]).
write1([X|L]):-
    writeln(X),
    write1(L).

element(A,B,W,W1):-
    not(member([A,B],W)),
    add1([A,B],W,W2),
    change(A,B,W2,W1).
              
change(3,_,W,W).
change(_,3,W,W).

change(A,B,W,W1):-
    volume(VA,_),
    B>0,
    C is VA-A,
    C>0,
    min(B,C,Min),
    A1 is A+Min,
    B1 is B-Min,
    element(A1,B1,W,W1).

change(A,B,W,W1):-
    volume(_,VB),
    A>0,
    C is VB-B,
    C>0,
    min(A,C,Min),
    B1 is B+Min,
    A1 is A-Min,
    element(A1,B1,W,W1).

change(A,B,W,W1):-
    volume(VA,_),
    A=VA,
    B>0,
    element(0,B,W,W1).

change(A,B,W,W1):-
    volume(_,VB),
    A>0,
    B=VB,
    element(A,0,W,W1).

change(A,B,W,W1):-
    volume(VA,_),
    A=0,
    element(VA,B,W,W1).

change(A,B,W,W1):-
    volume(_,VB),
    B=0,
    element(A,VB,W,W1).
    
min(A,B,A):- A=<B.
min(A,B,B):-A>B.

add(X,L,[X|L]). 

add1(X,[],[X]).
add1(X,[X1|L],[X1|L1]):-
    add1(X,L,L1).
Posted
Updated 25-Feb-21 2:25am
v3
Comments
Patrice T 23-Feb-21 19:17pm    
Show your work
NickResh777 24-Feb-21 10:08am    
I just updated my question and added my code

Quote:
I failed to write a Prolog program using RBFS algorithm to solve a task

For RBFS, you need a function to estimate which option is the most promising, but for this problem, I see no such obvious function.
So my approach would be an arbitrary function such as
A and B are the contains of both jugs
Func is the difference with target
Func= min(abs(A-3),abs(B-3))
 
Share this answer
 
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
NickResh777 24-Feb-21 10:10am    
Sorry, I forgot to post my code before. Now the question has it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900