Tuesday, October 6, 2009

Crypto.pro Lab3 part4

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-%
%
% FILE: crypto.pro in directory xsolve3.
% TYPE: Prolog source code.
% LINE: Crypto.
% DATE: 09/29/2009.
%
%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-%

% Load of combosets.pro

:- consult('gv.pro').
:- consult('combosets.pro').

establishCryptoProblemParameters :-
declare(lo,0),
declare(hi,15).

generateRandomCryptoNumber(R) :-
valueOf(lo, Lo),
valueOf(hi, Hi),
Hip is Hi + 1,
random(Lo,Hip,R).

generateRandomCryptoProblem :-
generateRandomCryptoNumber(N1),
generateRandomCryptoNumber(N2),
generateRandomCryptoNumber(N3),
generateRandomCryptoNumber(N4),
generateRandomCryptoNumber(N5),
generateRandomCryptoNumber(G),
addCryptoProblemToKnowledgeBase(N1,N2,N3,N4,N5,G).

addCryptoProblemToKnowledgeBase(N1,N2,N3,N4,N5,G) :-
retract(problem(_,_)),
assert(problem(numbers(N1,N2,N3,N4,N5) ,goal(G))).
addCryptoProblemToKnowledgeBase(N1,N2,N3,N4,N5,G) :-
assert(problem(numbers(N1,N2,N3,N4,N5) ,goal(G))).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-

% Display the problem after it has been internalized.

displayProblem :-
problem(numbers(N1,N2,N3,N4,N5) ,goal(G)),
write('Crypto Problem: numbers = {'),
write(N1), write(', '),
write(N2), write(', '),
write(N3), write(', '),
write(N4), write(', '),
write(N5), write('} Goal = '),
write(G), nl.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-

% Internalize the problem.

internalizeProblem :-
problem(numbers(N1,N2,N3,N4,N5) ,goal(G)),
eraseProblemBindings,
eraseProblem,
eraseSolution,
assert( problem( numbers(N1,N2,N3,N4,N5), goal(G) ) ),
assert( binding(n1, N1 ) ),
assert( binding(n2, N2 ) ),
assert( binding(n3, N3 ) ),
assert( binding(n4, N4 ) ),
assert( binding(n5, N5 ) ),
assert( binding(g, G ) ).

eraseProblem :-
retract( problem( _,_ ) ),
fail.
eraseProblem.

eraseSolution :-
retract( solution( _ ) ),
fail.
eraseSolution.

eraseProblemBindings :-
retract( binding( n1, _) ),
retract( binding( n2, _) ),
retract( binding( n3, _) ),
retract( binding( n4, _) ),
retract( binding( n5, _) ),
retract( binding( g, _) ),
fail.
eraseProblemBindings.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-

% Solve the problem decompositionally after internalization.

solveProblemDecompositionally :-
problem(numbers(N1,N2,N3,N4,N5) ,goal(G)),
crypto(N1,N2,N3,N4,N5,G, Expression),
assert(solution(Expression)).

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).

crypto( N1, N2, N3, G, Expr ) :-
combos( set(N1,N2,N3), combo(A,B), extra(C) ),
crypto( A, B, SG, SGE ),
crypto( C, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr ).

crypto( N1, N2, N3, N4, G, Expr ) :-
combos( set(N1,N2,N3,N4), combo(A,B), extras(C,D) ),
crypto( A, B, SG, SGE ),
crypto( C, D, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr).

crypto( N1, N2, N3, N4, N5, G, Expr ) :-
combos( set(N1,N2,N3,N4,N5), combo(A,B), extras(C,D,E) ),
crypto( A, B, SG, SGE ),
crypto( C, D, E, SG, G, UGE ),
substitute( SGE, SG, UGE, Expr).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Key substitution code.

substitute( New, Old, ex( Old, O, Z ), ex( New, O, Z ) ).
substitute( New, Old, ex( X, O, Old ), ex( X, O, New ) ).
substitute( New, Old, ex( X, O, Z ), ex( Q, O, Z) ) :-
substitute( New, Old, X, Q ).
substitute( New, Old, ex( X, O, Z), ex(X, O, Q ) ) :-
substitute( New, Old, Z, Q ).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Display the solution after being solved.

displaySolution :-
write( 'Possible Solution: '),
solution( S ),
displayResult( S ),
nl.
displaySolution.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Display the result after being solved.

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(' )').

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Demo program.

demo :-
generateRandomCryptoProblem,
internalizeProblem,
displayProblem,
solveProblemDecompositionally,
displaySolution.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Demo five times.

demo5 :-
demo,nl, demo,nl, demo,nl, demo,nl, demo.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Establish a particular crypto problem.

establishCrptoProblem(numbers(N1,N2,N3,N4,N5) ,goal(G)) :-
addCryptoProblemToKnowledgeBase(N1,N2,N3,N4,N5,G).

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Crypto Problem Solver.

solve(numbers(N1,N2,N3,N4,N5) ,goal(G)) :-
establishCryptoProblem(numbers(N1,N2,N3,N4,N5) ,goal(G)),
internalizeProblem,
displayProblem,
solveProblemDecompositionally,
displaySolution.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

% Initialize problem.

:- establishCryptoProblemParameters.

%-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-