SHARE
TWEET

Untitled

a guest Oct 21st, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. position(0). /*Let 0 indiciate 'Not Elected'*/
  2. position(1). /*Let 1 indicate the 'President' position*/
  3. position(2). /*Let 2 indicate the 'Vice President' position*/
  4. position(3). /*Let 3 indicate the 'Treasurer' position*/
  5. position(4). /*Let 4 indicate the 'Secretary' position*/
  6. /*This rule finds the four ideal Executive Council members out of six candidates (Arthur,Bart,Colleen,Donna,Eva,Frank)*/
  7. solve([Arthur,Bart,Colleen,Donna,Eva,Frank]) :- position(Arthur), position(Bart), position(Colleen), position(Donna), position(Eva), position(Frank),
  8.                         clause6(Frank,Colleen), clause7(Arthur,Bart,Colleen,Donna,Eva,Frank),       /*Order of clauses explained:                       */
  9.                                                                 /*Clause #6 must be checked first, as Frank can only be either 1 or 0.  */
  10.                         clause1(Arthur,Bart), clause2(Bart), clause3(Colleen,Bart,Frank),       /*Clause #7 is then checked, as no one else can be 1 if Frank is 1. */
  11.                                                                 /*Clauses 1, 2, 3, 4, and 5 are then checked based off of the results.  */
  12.                         clause4(Donna,Eva,Frank), clause5(Eva,Arthur,Bart).             /*of clauses 6 and 7.                           */
  13. /*This rule prints out the the name and position of each candidate.*/
  14. print_solution([Arthur,Bart,Colleen,Donna,Eva,Frank]) :- solve([Arthur,Bart,Colleen,Donna,Eva,Frank]), write('Arthur is '), txtPos(Arthur), nl,
  15.                             write('Bart is '), txtPos(Bart), nl, write('Colleen is '), txtPos(Colleen), nl,
  16.                             write('Donna is '), txtPos(Donna), nl, write('Eva is '), txtPos(Eva), nl,
  17.                             write('Frank is '), txtPos(Frank), nl.
  18. /*txtPos matches a candiates number with their role and outputs their role instead of the number.*/
  19. txtPos(0) :- write('not elected').
  20. txtPos(1) :- write('the President').
  21. txtPos(2) :- write('the Vice President').
  22. txtPos(3) :- write('the Treasurer').
  23. txtPos(4) :- write('the Secretary').
  24. /*Clause #1 handles the various election cases between Arthur and Bart where Arthur will only serve if Bart is also serving*/
  25. clause1(Arthur,Bart) :- Bart=0, Arthur=0. /*If Bart is not serving, then neither will Arthur*/
  26. clause1(Arthur,Bart) :- not Bart=0, not Arthur=0, not Arthur=2, not Arthur=Bart. /*If Bart is serving, then Arthur is fine with serving. They cannot be elected to the same position.*/
  27. clause1(Arthur,Bart) :- not Bart=0, Arthur=0. /*Just because Bart is serving does not mean that Arthur MUST serve.*/
  28. /*Clause #2 handles the possible positions for Bart where Bart will either serve or not serve.*/
  29. clause2(Bart) :- Bart=0. /*The possibility that Bart is not serving*/
  30. clause2(Bart) :- not Bart=2, not Bart=4. /*Bart will serve, but refuses to be Vice President or Secretary*/
  31. /*Clause #3 handles the various elections cases between Colleen, Bart, and Frank where Colleen refuses to work with Bart unless Frank is also elected.*/
  32. clause3(Colleen,Bart,Frank) :- not Bart=0, not Frank=0, not Colleen=0, not Colleen=Bart, not Colleen=Frank. /*Colleen will only serve alongside Bart is Frank is also serving. None of them can be elected to the same position*/
  33. clause3(Colleen,Bart,Frank) :- not Bart=0, Frank=0, Colleen=0. /*If Bart is serving, and Frank is not serving, then Colleen refuses to serve alongside Bart.*/
  34. clause3(Colleen,Bart,Frank) :- Bart=0, Frank=position(Frank), not Colleen=0. /*If Bart is not serving, then Colleen is willing to serve no matter what position Frank is in.*/
  35. /*Clause #4 handles the various election cases between Donna, Eva, and Frank where Donna refuses to work with Eva OR Frank.*/
  36. clause4(Donna,Eva,Frank) :- Eva=0, Frank=0, not Donna=0. /*Donna will serve as long as Eva and Frank are not serving*/
  37. clause4(Donna,Eva,Frank) :- not Eva=0, not Frank=0, not Eva=Frank, Donna=0. /*Donna will not serve if Eva and Frank are serving. Eva and Frank cannot be elected to the same position.*/
  38. clause4(Donna,Eva,Frank) :- not Eva=0, Frank=0, Donna=0. /*If Donna is not serving, Eva can still serve without Frank.*/
  39. clause4(Donna,Eva,Frank) :- Eva=0, not Frank=0, Donna=0. /*If Donna is not serving, Frank can stil serve without Eva.*/
  40. /*Clause #5 handles the various election cases between Eva, Arthur, and Bart where Eva refuses to serve if both Bart AND Arthur are in the Executive Council.*/
  41. clause5(Eva,Arthur,Bart) :- not Arthur=0, not Bart=0, Eva=0, not Arthur=Bart. /*If Arthur and Bart are serving, then Eva will not serve. Arthur and Bart cannot be elected to the same position.*/
  42. clause5(Eva,Arthur,Bart) :- Arthur=0, Bart=0, not Eva=0. /*Eva will serve if neither Arthur and Bart are serving.*/
  43. clause5(Eva,Arthur,Bart) :- not Arthur=0, Bart=0, not Eva=0, not Arthur=Eva. /*Eva is willing to serve alongside Arthur if Bart is not serving. Arthur and Eva cannot be elected to the same position.*/
  44. clause5(Eva,Arthur,Bart) :- Arthur=0, not Bart=0, not Eva=0, not Bart=Eva. /*Eva is willing to serve alongside Bart if Arthur is not serving. Bart and Eva cannot be elected to the same position.*/
  45. /*Clause #6 handles the situation that if Frank is elected, he will only accept being elected as President with the additional condition that Colleen is not the Vice President. They cannot be elected to the same position.*/
  46. clause6(Frank,Colleen) :- Frank=1, not Colleen=2, not Frank=Colleen.
  47. /*Clause #7 handles the situation where if Frank is elected as President, then no one else can be elected as President.*/
  48. clause7(Arthur,Bart,Colleen,Donna,Eva,Frank) :- Frank=1, not Arthur=Frank, not Bart=Frank, not Colleen=Frank, not Donna=Frank, not Eva=Frank.
  49. /*              QUERIES AND ANSWERS                     */
  50. /* ------------------------------------------------------------------------------------ */
  51. /*                                              */
  52. /*  The query solve(L). was entered. The following results were returned:       */
  53. /*                                          */
  54. /*          ?- solve(L).                            */
  55. /*          L = [0, 3, 4, 0, 2, 1]                      */
  56. /*          Yes (0.00s cpu, solution 1, maybe more)             */
  57. /*                                          */
  58. /*  The query print_solution(L). was entered. The following results were outputted: */
  59. /*                                          */
  60. /*          ?- print_solution(L).                       */
  61. /*          L = [0, 3, 4, 0, 2, 1]                      */
  62. /*          Yes (0.00s cpu, solution 1, maybe more)             */
  63. /*                                          */
  64. /*          Arthur is not elected                       */
  65. /*          Bart is the Treasurer                       */
  66. /*          Colleen is the Secretary                    */
  67. /*          Donna is not elected                        */
  68. /*          Eva is the Vice President                   */
  69. /*          Frank is the President                      */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top