Advertisement
Guest User

Untitled

a guest
Oct 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 5.31 KB | None | 0 0
  1. % ======================================================================= %
  2. % Prolog Homework #4                                                      %
  3. % 05/10/2018                                                              %
  4. % Sam Titarsolej, 12206385, s.titarsolej@gmail.com                        %
  5. % ======================================================================= %
  6.  
  7. % ======================================================================= %
  8. % ||                                                                   || %
  9. % ||                          Exercise 1                               || %
  10. % ||                                                                   || %
  11. % ======================================================================= %
  12.  
  13. % ======================================================================= %
  14. %                                 A                                       %
  15. % ======================================================================= %
  16.  
  17. % State zoals in B?
  18.  
  19. % ======================================================================= %
  20. %                                 B                                       %
  21. % ======================================================================= %
  22.  
  23. /*
  24. Using the implementations for the different possible arithmetic operations,
  25. we can implement the move/2 predicate. First we select two unique integers
  26. from the current state list, using select/3 and member/2
  27. */
  28.  
  29. move(Target:State, Target:NewState) :-
  30.     select(A, State, State1),
  31.     member(B, State1),
  32.     (move(Target:State, [A + B], Target:NewState); % Either +
  33.     move(Target:State, [A - B], Target:NewState); % Or -
  34.     move(Target:State, [A * B], Target:NewState); % Or *
  35.     move(Target:State, [A / B], Target:NewState)). % Or B
  36.  
  37. /*
  38. Different implementations for each of the possible arithmetic operations.
  39. For each operation we expect three arguments: the current state, the
  40. arithmetic operation we want to execute, and the resulting state. The
  41. second argument must be in the form [A {OPERATION} B], where A and B are
  42. numbers from the current state. This way we can simply add the operation to
  43. the head of the resulting state. We also have to remove A and B from the
  44. resulting state list. We can do this using the select/3 predicate.
  45. */
  46.  
  47. move(Target:State, [A + B], Target:[A + B | State3]) :-
  48.     select(A, State, State2),
  49.     select(B, State2, State3).
  50.  
  51. move(Target:State, [A - B], Target:[A - B | State3]) :-
  52.     select(A, State, State2),
  53.     select(B, State2, State3).
  54.  
  55. move(Target:State, [A * B], Target:[A * B | State3]) :-
  56.     select(A, State, State2),
  57.     select(B, State2, State3).
  58.  
  59. /*
  60. For division we need to do two more checks; whether the arthitmetic
  61. expression in B is not zero (to prevent a zero divisor), and whether A/B
  62. results in an integer.
  63. */
  64.  
  65. move(Target:State, [A / B], Target:[A / B | State3]) :-
  66.     \+ (0 =:= B),
  67.     0 =:= A mod B,
  68.     select(A, State, State2),
  69.     select(B, State2, State3).
  70.  
  71. % ======================================================================= %
  72. %                                  c                                      %
  73. % ======================================================================= %
  74.  
  75. goal(Target:State) :-
  76.     nth1(1, State, X),
  77.     Goal =.. [is, Target, X],
  78.     call(Goal).
  79.  
  80. % ======================================================================= %
  81. %                                   D                                     %
  82. % ======================================================================= %
  83.  
  84. solve(Target, State, Solution) :-
  85.     id(Target:State, Path),
  86.     last(Path, Target:SolutionList),
  87.     nth1(1, SolutionList, Solution).
  88.  
  89. % ======================================================================= %
  90. % ||                                                                   || %
  91. % ||                        Search algorithms                          || %
  92. % ||                                                                   || %
  93. % ======================================================================= %
  94.  
  95. solve_depthfirst(Node, [Node|Path]) :-
  96.     depthfirst(Node, Path).
  97.  
  98. depthfirst(Node, []) :-
  99.     goal(Node).
  100.  
  101. depthfirst(Node, [NextNode|Path]) :-
  102.     move(Node, NextNode),
  103.     depthfirst(NextNode, Path).
  104.  
  105. solve_depthfirst_cyclefree(Node, Path) :-
  106.     depthfirst_cyclefree([Node], Node, RevPath),
  107.     reverse(RevPath, Path).
  108.  
  109. depthfirst_cyclefree(Visited, Node, Visited) :-
  110.     goal(Node).
  111.  
  112. depthfirst_cyclefree(Visited, Node, Path) :-
  113.     move_cyclefree(Visited, Node, NextNode),
  114.     depthfirst_cyclefree([NextNode|Visited], NextNode, Path).
  115.  
  116. move_cyclefree(Visited, Node, NextNode) :-
  117.     move(Node, NextNode),
  118.     \+ member(NextNode, Visited).
  119.  
  120. solve_depthfirst_bound(Bound, Node, Path) :-
  121.     depthfirst_bound(Bound, [Node], Node, RevPath),
  122.     reverse(RevPath, Path).
  123.  
  124. depthfirst_bound(_, Visited, Node, Visited) :-
  125.     goal(Node).
  126.  
  127. depthfirst_bound(Bound, Visited, Node, Path) :-
  128.     Bound > 0,
  129.     move_cyclefree(Visited, Node, NextNode),
  130.     NewBound is Bound - 1,
  131.     depthfirst_bound(NewBound, [NextNode|Visited], NextNode, Path).
  132.  
  133. id(Node, Path) :-
  134.     id(Node, 0, Path).
  135.  
  136. id(Node, N, Path) :-
  137.     solve_depthfirst_bound(N, Node, Path).
  138.  
  139. id(Node, N, Path) :-
  140.     \+ solve_depthfirst_bound(N, Node, Path),
  141.     N1 is N + 1,
  142.     id(Node, N1, Path).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement