Solution to the water jug problem in my way.
There are two jugs of volume A litre and B litre. Now, we have to trace a proper path to get x litre of water in jug A. We have unlimited supply of water.
[ id, parent_id, water in A, water in B]
So, I have divided the execution in two major steps:
- First of all initial node
[0,'null', 0, 0]is added to the graph. - Then, taking the state
(0, 0)as the initial state, all the procedures of classmake_states.pyis called with the help of a local object and the return states are saved as the child node of the initial node. - Repeating second step by makeing all the child as initial node, a full graph is generated.
- In this program I have limited generations to
256 iterationswhich do generates~1481states.
- Graph is traversed index wise till the node watnted is reached.
- Now, when the desired node is reached, this node is backtracked to the initial node or
root nodeand the path followed while backtracking is printed. - For every instance of the desired node this path is printed.
( 2 , 0 )<-( 0 , 2 )<-( 4 , 2 )<-( 3 , 3 )<-( 3 , 0 )<-( 0 , 3 )<-( 0 , 0 )
( 2 , 3 )<-( 2 , 0 )<-( 0 , 2 )<-( 4 , 2 )<-( 3 , 3 )<-( 3 , 0 )<-( 0 , 3 )<-( 0 , 0 )
( 2 , 0 )<-( 0 , 2 )<-( 4 , 2 )<-( 3 , 3 )<-( 3 , 0 )<-( 3 , 3 )<-( 3 , 0 )<-( 0 , 3 )<-( 0 , 0 )
( 2 , 0 )<-( 0 , 2 )<-( 4 , 2 )<-( 3 , 3 )<-( 4 , 2 )<-( 3 , 3 )<-( 3 , 0 )<-( 0 , 3 )<-( 0 , 0 )
( 2 , 0 )<-( 2 , 3 )<-( 2 , 0 )<-( 0 , 2 )<-( 4 , 2 )<-( 3 , 3 )<-( 3 , 0 )<-( 0 , 3 )<-( 0 , 0 )
( 2 , 0 )<-( 0 , 2 )<-( 4 , 2 )<-( 0 , 2 )<-( 4 , 2 )<-( 3 , 3 )<-( 3 , 0 )<-( 0 , 3 )<-( 0 , 0 )
( 2 , 0 )<-( 0 , 2 )<-( 4 , 2 )<-( 3 , 3 )<-( 3 , 0 )<-( 0 , 3 )<-( 4 , 3 )<-( 0 , 3 )<-( 0 , 0 )
( 2 , 0 )<-( 0 , 2 )<-( 4 , 2 )<-( 3 , 3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