%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% File: hpsc.pro %%%%
%%%% Line: Heuristic Problem Solver for Cryptos %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Load Files
:- consult('gv2.pro').
:- consult('combosets.pro').
%%%% END Load Files
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN rcg, dp, sp, ds
establishCryptoProblemParameters :-
declare(low,0),declare(high,9).
:- establishCryptoProblemParameters.
generateRandomCryptoNumber(N) :-
valueOf(low,Lo),
valueOf(high,Hi),
HighPlus is Hi + 1,
random(Lo,HighPlus,N).
generateRandomCryptoProblem :-
generateRandomCryptoNumber(N1),
generateRandomCryptoNumber(N2),
generateRandomCryptoNumber(N3),
generateRandomCryptoNumber(N4),
generateRandomCryptoNumber(N5),
generateRandomCryptoNumber(GoalNum),
addCryptoProblemToKB(N1,N2,N3,N4,N5,GoalNum).
addCryptoProblemToKB(N1,N2,N3,N4,N5,G) :-
retract(problem(_,_)),
assert(problem(numbers(N1,N2,N3,N4,N5),goal(G))).
addCryptoProblemToKB(N1,N2,N3,N4,N5,G) :-
assert(problem(numbers(N1,N2,N3,N4,N5),goal(G))).
displayResult(ex(A,O,B)) :-
number(A),number(B),
write('( '),write(A),write(' '),write(O),
write(' '),write(B),write(' )').
displayResult(ex(A,O,B)) :-
number(A), B = ex(A1,O1,B1),
write('( '),write(A),write(' '),write(O),write(' '),
displayResult(ex(A1,O1,B1)),write(' )').
displayResult(ex(A,O,B)) :-
number(B), A = ex(A1,O1,B1),
write('( '),displayResult(ex(A1,O1,B1)),write(' '),
write(O),write(' '),write(B),write(' )').
displayResult(ex(A,O,B)) :-
A = ex(A1,O1,B1), B = ex(A2,O2,B2),
write('( '),displayResult(ex(A1,O1,B1)),write(' '),write(O),
write(' '),displayResult(ex(A2,O2,B2)),write(' )').
displayProblem :-
problem(numbers(N1,N2,N3,N4,N5),goal(G)),
write('Problem: Numbers = { '),write(N1),write(', '),
write(N2),write(', '),write(N3),write(', '),
write(N4),write(', '),write(N5),write(' } and Goal = '),
write(G),nl.
%%%% END rcg, dp, sp, ds
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN heuristic stuff
rule(1,situation1,action1).
rule(2,situation2,action2).
rule(3,situation3,action3).
rule(4,situation4,action4).
rule(5,situation5,action5).
rule(6,situation6,action6).
rule(7,situation7,action7).
rule(8,situation8,action8).
solveProblemHeuristically :-
rule(Number,Situation,Action),
write('considering rule '),write(Number),
write(' ...'),nl,Situation,
write('application of rule '),write(Number),
write(' produces '),Action.
solveProblemHeuristically.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heuristic 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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)))))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heuristic 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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)))))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heuristic 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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))))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heuristic 4
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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))))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heuristic 5
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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)))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heuristic 6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heuristic 7
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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)))).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Heuristic 8
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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),
checker,
assert(solution(ex(GExpr,+,ex(ZExpr,*,E)))).
%%%% END heuristic stuff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN heuristic support
doubleton :-
problem(numbers(N1,N2,N3,N4,N5),_),
combos(set(N1,N2,N3,N4,N5),combo(A,B),_),
A=B.
doubleton(doubleton(A,B),rest(C,D,E)) :-
problem(numbers(N1,N2,N3,N4,N5),_),
combos(set(N1,N2,N3,N4,N5),combo(A,B),extras(C,D,E)),
A=B.
other_numbers(special(N1),others(N2,N3,N4,N5)) :-
problem(numbers(N1,N2,N3,N4,N5),goal(_)).
other_numbers(special(N2),others(N1,N3,N4,N5)) :-
problem(numbers(N1,N2,N3,N4,N5),goal(_)).
other_numbers(special(N3),others(N1,N2,N4,N5)) :-
problem(numbers(N1,N2,N3,N4,N5),goal(_)).
other_numbers(special(N4),others(N1,N2,N3,N5)) :-
problem(numbers(N1,N2,N3,N4,N5),goal(_)).
other_numbers(special(N5),others(N1,N2,N3,N4)) :-
problem(numbers(N1,N2,N3,N4,N5),goal(_)).
checker :-
retract(solution(_)).
checker.
zeroP(A,B) :-
crypto(A,B,0,_).
zeroPEx(A,B,Ex) :-
crypto(A,B,0,Ex).
goalP(A,B,G) :-
crypto(A,B,G,_).
goalPEx(A,B,G,Ex) :-
crypto(A,B,G,Ex).
setsequence(N1,N2,N3,N4,N5) :-
retract(sequence(_)),
asserta(sequence(N1,N2,N3,N4,N5)).
setsequence(N1,N2,N3,N4,N5) :-
asserta(sequence(N1,N2,N3,N4,N5)).
%%%% END heuristic support
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Crypto Predicate Solver
crypto(N1,N2,Goal,ex(N1,+,N2)) :-
Goal is ( N1 + N2 ).
crypto(N1,N2,Goal,ex(N1,*,N2)) :-
Goal is ( N1 * N2 ).
crypto(N1,N2,Goal,ex(N1,-,N2)) :-
Goal is ( N1 - N2 ).
crypto(N1,N2,Goal,ex(N2,-,N1)) :-
Goal is ( N2 - N1 ).
crypto(N1,N2,Goal,ex(N1,/,N2)) :-
N2 > 0,
Goal is ( N1 / N2 ).
crypto(N1,N2,Goal,ex(N2,/,N1)) :-
N1 > 0,
Goal is ( N2 / N1 ).
%%%% END Crypto Predicate Solver
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Establish Specific Crypto Problem
establishSpecificCryptoProblem(N1,N2,N3,N4,N5,G) :-
addCryptoProblemToKB(N1,N2,N3,N4,N5,G).
%%%% END Establish Specific Crypto Problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Crypto Functionality Support
getProblemFromKnowledgeBase(N1,N2,N3,N4,N5,G) :-
problem(numbers(N1,N2,N3,N4,N5),goal(G)).
%%%% END Crypto Functionality Support
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Display the Solution IF it is Solved
displaySolution :-
solution(S),
displayResult(S),
nl.
displaySolution.
%%%% END Display the Solution IF it is Solved
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Solver with Random Problem
solve :-
retract(solution(_)),
generateRandomCryptoProblem,
displayProblem,
solveProblemHeuristically,
displaySolution.
solve :-
generateRandomCryptoProblem,
displayProblem,
solveProblemHeuristically,
displaySolution.
%%%% END Solver with Random Problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Solver with Specific Problem
solve(numbers(N1,N2,N3,N4,N5),goal(G)) :-
retract(solution(_)),
establishSpecificCryptoProblem(N1,N2,N3,N4,N5,G),
displayProblem,
solveProblemHeuristically,
displaySolution.
solve(numbers(N1,N2,N3,N4,N5),goal(G)) :-
establishSpecificCryptoProblem(N1,N2,N3,N4,N5,G),
displayProblem,
solveProblemHeuristically,
displaySolution.
%%%% END Solver with Specific Problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Demo
demo(0).
demo(N) :-
solve,
K is N - 1,
demo(K).
%%%% END Demo
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% BEGIN Iterator
repeat(0,_).
repeat(N,P) :-
P,
K is N - 1,
repeat(K,P).
%%%% END Iterator
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% END File: hpsc.pro
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%