COG 366 Assignment 17 Part 3: Individual Heuristics Demo

Heuristic 1 – Zeros

Prolog Code

            situation1 :- 
                problem(Numbers,Goal), 
                Goal = goal(0),
                Numbers = numbers(N1,N2,N3,N4,N5),
                member(0,[N1,N2,N3,N4,N5]).
            action1 :-
                problem(Numbers,_),
                Numbers = numbers(N1,N2,N3,N4,N5),
                assert(solution(ex(N1,*,ex(N2,*,ex(N3,*,ex(N4,*,N5)))))).
            

Prolog Demo

            ?- solve(numbers(0,1,2,3,4),goal(0)).
            Problem: Numbers = { 0, 1, 2, 3, 4 } and Goal = 0
            considering rule 1 ...
            application of rule 1 produces ( 0 * ( 1 * ( 2 * ( 3 * 4 ) ) ) )
            true .
            ?- solve(numbers(2,4,6,8,0),goal(0)).
            Problem: Numbers = { 2, 4, 6, 8, 0 } and Goal = 0
            considering rule 1 ...
            application of rule 1 produces ( 2 * ( 4 * ( 6 * ( 8 * 0 ) ) ) )
            true .
            ?- solve(numbers(1,3,0,5,7),goal(0)).
            Problem: Numbers = { 1, 3, 0, 5, 7 } and Goal = 0
            considering rule 1 ...
            application of rule 1 produces ( 1 * ( 3 * ( 0 * ( 5 * 7 ) ) ) )
            true .
            

Heuristic 2 – Zero and Goal

Prolog Code

            situation2 :-
                problem(numbers(N1,N2,N3,N4,N5),goal(G)),
                member(G,[N1,N2,N3,N4,N5]),
                member(0,[N1,N2,N3,N4,N5]),
                not(G=0).
            action2 :-
                problem(_,goal(G)),
                other_numbers(special(G),others(A,B,C,D)),
                assert(solution(ex(G,+,ex(A,*,ex(B,*,ex(C,*,D)))))).
            

Prolog Demo

            ?- solve(numbers(1,5,9,0,3),goal(5)).
            Problem: Numbers = { 1, 5, 9, 0, 3 } and Goal = 5
            considering rule 1 ...
            considering rule 2 ...
            application of rule 2 produces ( 5 + ( 1 * ( 9 * ( 0 * 3 ) ) ) )
            true .
            ?- solve(numbers(3,7,6,0,2),goal(7)).
            Problem: Numbers = { 3, 7, 6, 0, 2 } and Goal = 7
            considering rule 1 ...
            considering rule 2 ...
            application of rule 2 produces ( 7 + ( 3 * ( 6 * ( 0 * 2 ) ) ) )
            true .
            ?- solve(numbers(9,1,7,4,0),goal(1)).
            Problem: Numbers = { 9, 1, 7, 4, 0 } and Goal = 1
            considering rule 1 ...
            considering rule 2 ...
            application of rule 2 produces ( 1 + ( 9 * ( 7 * ( 4 * 0 ) ) ) )
            true .
            

Heuristic 3 – Zero Goal and Pair

Prolog Code

            situation3 :-
                problem(_,goal(0)),
                doubleton.
            action3 :-
                doubleton(doubleton(A,B),rest(C,D,E)),
                assert(solution(ex(ex(A,-,B),*,ex(C,*,ex(D,*,E))))).
            

Prolog Demo

            ?- solve(numbers(1,1,3,2,6),goal(0)).
            Problem: Numbers = { 1, 1, 3, 2, 6 } and Goal = 0
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            application of rule 3 produces ( ( 1 - 1 ) * ( 3 * ( 2 * 6 ) ) )
            true .
            ?- solve(numbers(1,3,3,2,6),goal(0)).
            Problem: Numbers = { 1, 3, 3, 2, 6 } and Goal = 0
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            application of rule 3 produces ( ( 3 - 3 ) * ( 1 * ( 2 * 6 ) ) )
            true .
            ?- solve(numbers(1,3,2,2,6),goal(0)).
            Problem: Numbers = { 1, 3, 2, 2, 6 } and Goal = 0
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            application of rule 3 produces ( ( 2 - 2 ) * ( 1 * ( 3 * 6 ) ) )
            true .
            

Heuristic 4 – Same Goal and Numbers

Prolog Code

            situation4 :-
                problem(numbers(N1,N2,N3,N4,N5),goal(G)),
                N1=N2,N2=N3,N3=N4,N4=N5,
                N5=G.
            action4 :-
                problem(numbers(A,B,C,D,E),_),
                assert(solution(ex(A,+,ex(ex(B,-,C),+,ex(D,-,E))))).
            

Prolog Demo

            ?- solve(numbers(1,1,1,1,1),goal(1)).
            Problem: Numbers = { 1, 1, 1, 1, 1 } and Goal = 1
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            application of rule 4 produces ( 1 + ( ( 1 - 1 ) + ( 1 - 1 ) ) )
            true .
            ?- solve(numbers(9,9,9,9,9),goal(9)).
            Problem: Numbers = { 9, 9, 9, 9, 9 } and Goal = 9
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            application of rule 4 produces ( 9 + ( ( 9 - 9 ) + ( 9 - 9 ) ) )
            true .
            ?- solve(numbers(5,5,5,5,5),goal(5)).
            Problem: Numbers = { 5, 5, 5, 5, 5 } and Goal = 5
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            application of rule 4 produces ( 5 + ( ( 5 - 5 ) + ( 5 - 5 ) ) )
            true .
            

Heuristic 5 – Pairs Plus Two

Prolog Code

            situation5 :-
                problem(numbers(N1,N2,N3,N4,N5),goal(G)),
                combos(set(N1,N2,N3,N4,N5),combo(A,B),extras(C,D,E)),
                A=1,B=1,C=D,Test1 is E + 2,G = Test1,setsequence(A,B,C,D,E).
            action5 :-
                sequence(A,B,C,D,E),
                assert(solution(ex(ex(ex(A,+,B),+,E),+,ex(C,-,D)))).
            

Prolog Demo

            ?- solve(numbers(1,1,4,4,5),goal(7)).
            Problem: Numbers = { 1, 1, 4, 4, 5 } and Goal = 7
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            application of rule 5 produces ( ( ( 1 + 1 ) + 5 ) + ( 4 - 4 ) )
            true .
            ?- solve(numbers(1,3,1,3,7),goal(9)).
            Problem: Numbers = { 1, 3, 1, 3, 7 } and Goal = 9
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            application of rule 5 produces ( ( ( 1 + 1 ) + 7 ) + ( 3 - 3 ) )
            true .
            ?- solve(numbers(4,1,4,1,1),goal(3)).
            Problem: Numbers = { 4, 1, 4, 1, 1 } and Goal = 3
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            application of rule 5 produces ( ( ( 1 + 1 ) + 1 ) + ( 4 - 4 ) )
            true .
            

Heuristic 6 – Two Adjacents and Goal

Prolog Code

            situation6 :-
                problem(numbers(N1,N2,N3,N4,N5),goal(G)),
                combos(set(N1,N2,N3,N4,N5),combo(A,B),extras(C,D,E)),
                Test1 is B - 1,A = Test1,
                Test2 is D + 1,C = Test2,E=G,setsequence(A,B,C,D,E).
            action6 :-
                sequence(A,B,C,D,E),
                assert(solution(ex(ex(ex(B,-,A),*,ex(C,-,D)),*,E))).
            

Prolog Demo

            ?- solve(numbers(3,4,7,6,2),goal(2)).
            Problem: Numbers = { 3, 4, 7, 6, 2 } and Goal = 2
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            application of rule 6 produces ( ( ( 4 - 3 ) * ( 7 - 6 ) ) * 2 )
            true .
            ?- solve(numbers(7,8,3,2,6),goal(6)).
            Problem: Numbers = { 7, 8, 3, 2, 6 } and Goal = 6
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            application of rule 6 produces ( ( ( 8 - 7 ) * ( 3 - 2 ) ) * 6 )
            true .
            ?- solve(numbers(1,2,6,5,9),goal(9)).
            Problem: Numbers = { 1, 2, 6, 5, 9 } and Goal = 9
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            application of rule 6 produces ( ( ( 2 - 1 ) * ( 6 - 5 ) ) * 9 )
            true .
            

Heuristic 7 – Two Pairs and Goal of One

Prolog Code

            situation7 :-
                problem(numbers(N1,N2,N3,N4,N5),goal(1)),
                combos(set(N1,N2,N3,N4,N5),combo(A,B),extras(C,D,E)),
                A=B,C=D,setsequence(A,B,C,D,E).
            action7 :-
                sequence(A,B,C,D,E),
                assert(solution(ex(ex(ex(A,-,B),*,E),+,ex(C,/,D)))).
            

Prolog Demo

            ?- solve(numbers(3,3,7,5,7),goal(1)).
            Problem: Numbers = { 3, 3, 7, 5, 7 } and Goal = 1
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            considering rule 7 ...
            application of rule 7 produces ( ( ( 7 - 7 ) * 5 ) + ( 3 / 3 ) )
            true .
            ?- solve(numbers(6,6,4,4,9),goal(1)).
            Problem: Numbers = { 6, 6, 4, 4, 9 } and Goal = 1
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            considering rule 7 ...
            application of rule 7 produces ( ( ( 6 - 6 ) * 9 ) + ( 4 / 4 ) )
            true .
            ?- solve(numbers(8,7,2,7,8),goal(1)).
            Problem: Numbers = { 8, 7, 2, 7, 8 } and Goal = 1
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            considering rule 7 ...
            application of rule 7 produces ( ( ( 8 - 8 ) * 9 ) + ( 4 / 4 ) )
            true .
            

Heuristic 8 – Make Zero and Goal

Prolog Code

            situation8 :-
                problem(numbers(N1,N2,N3,N4,N5),goal(G)),
                combos(set(N1,N2,N3,N4,N5),combo(A,B),extras(C,D,E)),
                goalP(A,B,G),
                zeroP(C,D),
                setsequence(A,B,C,D,E).
            action8 :-
                problem(_,goal(G)),
                sequence(A,B,C,D,E),
                goalPEx(A,B,G,GExpr),
                zeroPEx(C,D,ZExpr),
                assert(solution(ex(GExpr,+,ex(ZExpr,*,E)))).
            

Prolog Demo

            ?- solve(numbers(3,4,0,2,5),goal(7)).
            Problem: Numbers = { 3, 4, 0, 2, 5 } and Goal = 7
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            considering rule 7 ...
            considering rule 8 ...
            application of rule 8 produces ( ( 3 + 4 ) + ( ( 0 * 2 ) * 5 ) )
            true .
            ?- solve(numbers(6,2,1,1,3),goal(8)).
            Problem: Numbers = { 6, 2, 1, 1, 3 } and Goal = 8
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            considering rule 7 ...
            considering rule 8 ...
            application of rule 8 produces ( ( 6 + 2 ) + ( ( 1 - 1 ) * 3 ) )
            true .
            ?- solve(numbers(5,3,9,0,7),goal(2)).
            Problem: Numbers = { 5, 3, 9, 0, 7 } and Goal = 2
            considering rule 1 ...
            considering rule 2 ...
            considering rule 3 ...
            considering rule 4 ...
            considering rule 5 ...
            considering rule 6 ...
            considering rule 7 ...
            considering rule 8 ...
            application of rule 8 produces ( ( 5 - 3 ) + ( ( 9 * 0 ) * 7 ) )
            true .