Advertisement
Luninariel

Python Office Space Logic - WIP

Nov 6th, 2019
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.63 KB | None | 0 0
  1. ##############################################
  2. # CSC 246 - Fall 2019
  3. # Missouri Western State University
  4. # Homework #4 - Logic Programming
  5. # Starter Template
  6. # Student(s): 1
  7.  
  8. from kanren import *
  9.  
  10. allgoals = []   # goals used to solve the logic puzzle
  11.  
  12.  
  13. def anon_employee():
  14.   return (var(), var(), var())
  15.  
  16. def set_fname(employee, name):
  17.   return (name, employee[1], employee[2])
  18.  
  19. def set_lname(employee, name):
  20.   return (employee[0], name, employee[2])
  21.  
  22. def set_job(employee, job):
  23.   return (employee[0], employee[1], job)
  24.  
  25.  
  26. # TODO: you can make the starting variables more specific
  27. #       this will make your program run more efficiently
  28. #       but be careful! don't solve the problem in your code...
  29. #       ... have your code solve the problem!
  30.  
  31.  
  32. # ex. Curtis ended up with a wall office.
  33. curtis = set_fname(anon_employee(), "curtis")
  34.  
  35. #Ms. Singer ended up with a window seat
  36. ms_singer = set_lname(anon_employee(), "singer")
  37.  
  38. #Harriet ended up with a Window seat
  39. harriet = set_fname(anon_employee(), "harriet")
  40.  
  41. #A Product manager ended up with a window seat
  42. window_PM = set_job(anon_employee(), "product manager")
  43.  
  44. #A Product Manager ended up with a  Wall seat
  45. wall_pm = set_job(anon_employee(), "product manager")
  46.  
  47. wallseats = (curtis, var(), wall_pm)    # employees with seats by the wall
  48. windowseats = (harriet, ms_singer, window_PM)   # employees with seats by the window
  49. employees = wallseats + windowseats # all employees (same vars)
  50.  
  51.  
  52.  
  53.  
  54. # TODO: create more goal generating functions for use below
  55.  
  56. def has_wall_seat(employee):
  57.   """produces a goal expecting the employee to have a wall seat"""
  58.   return (membero, employee, wallseats)
  59.  
  60. def has_window_seat(employee):
  61.   """produces a goal expecting the employee to have a window seat"""
  62.   return (membero, employee, windowseats)
  63.  
  64. def is_employee(employee):
  65.   """produces a goal asserting that 'employee' is one of the six employees in question"""
  66.   return (membero, employee, employees)
  67.  
  68. def is_male(employee):
  69.   """produces a goal asserting that the employee has one of the male first or last names"""
  70.   return (lany,
  71.             is_employee(set_fname(employee, "steve")),
  72.             is_employee(set_fname(employee, "curtis")),
  73.             is_employee(set_fname(employee, "dick")))
  74.  
  75. def is_female(employee):
  76.   """produces a goal asserting that the employee has one of the female first names"""
  77.   return (lany,
  78.             is_employee(set_fname(employee, "harriet")),
  79.             is_employee(set_fname(employee, "ellen")),
  80.             is_employee(set_fname(employee, "amelia")))
  81.  
  82.  
  83. def does_not_have_lname(employee, lname):
  84.   "produces a goal asserting that an employee cannot have a specific last name"
  85.   return(lany,) + tuple(map(lambda n: is_employee(set_lname(employee, n)),
  86.   filter(lambda x : x != lname, ["fairview", "singer", "macklin", "radcliffe", "weathervane", "chase"])))
  87.  
  88.  
  89. def does_not_have_job(employee, job):
  90.   "produces a goal asserting that an employee cannot have a specific job"
  91.   return(lany,) + tuple(map(lambda n: is_employee(set_job(employee, n)),
  92.   filter(lambda x : x != job, ["product manager", "applications engineer", "marketing analyst", "publication writer", "marketing manager"])))
  93.  
  94. def gender_balance():
  95.   "produces a goal asserting that the windowseats must contain a mix of male and female employees"
  96.   return (lall,
  97.     (lany, is_male(windowseats[0]), is_male(windowseats[1]), is_male(windowseats[2])),
  98.     (lany, is_female(windowseats[0]), is_female(windowseats[1]), is_female(windowseats[2])))
  99.  
  100. # TODO: add more goals below to force a solution to the puzzle
  101. #       you should end up with one unique solution if you do everything correctly
  102. #       however, your program will run much faster when you are further from the correct answer!
  103.  
  104.  
  105. #The marketing analyst had a last name of Radcliffe.
  106. mx_radcliffe = set_lname(anon_employee(), "radcliffe")
  107. mx_radcliffe = set_job(mx_radcliffe, "marketing analyst")
  108. allgoals.append(is_employee(mx_radcliffe))
  109.  
  110. #The Applications Engineer had a last name of Fairview.
  111. me_fairview = set_lname(anon_employee(), "fairview")
  112. me_fairview = set_job(me_fairview, "applications engineer")
  113. allgoals.append(is_employee(me_fairview))
  114.  
  115. #Mrs. Singer is female and has a window seat
  116. allgoals.append(is_female(ms_singer))
  117. allgoals.append(has_window_seat(ms_singer))
  118. allgoals.append(is_employee(ms_singer))
  119.  
  120. #The Publication writer had a first name of Steve & Steve's last name wasn't radcliffe
  121. pw_steve = set_fname(anon_employee(), "steve")
  122. pw_steve = set_job(pw_steve, "publication writer")
  123. allgoals.append(does_not_have_lname(pw_steve, "radcliffe"))
  124. allgoals.append(is_male(pw_steve))
  125. allgoals.append(is_employee(pw_steve))
  126.  
  127. #Ellen Weathervane Wasn't the marketing manager
  128. ellen_weathervane = set_fname(anon_employee(), "ellen")
  129. ellen_weathervane = set_lname(ellen_weathervane, "weathervane")
  130. allgoals.append(does_not_have_job(ellen_weathervane, "marketing manager"))
  131. allgoals.append(is_female(ellen_weathervane))
  132. allgoals.append(is_employee(ellen_weathervane))
  133.  
  134. #Curtis' last name was not fairview
  135. allgoals.append(does_not_have_lname(curtis, "fairview"))
  136. allgoals.append(is_male(curtis))
  137. allgoals.append(has_wall_seat(curtis))
  138. allgoals.append(is_employee(curtis))
  139.  
  140. #Dick's last name isn't Macklin
  141. dick = set_fname(anon_employee(),"dick")
  142. allgoals.append(does_not_have_lname(dick, "macklin"))
  143. allgoals.append(is_male(dick))
  144. allgoals.append(is_employee(dick))
  145.  
  146. #Mr. Chase wasn't the applications engineer
  147. mr_chase = set_lname(anon_employee(), "chase")
  148. allgoals.append(does_not_have_job(mr_chase, "applications engineer"))
  149. allgoals.append(is_male(mr_chase))
  150. allgoals.append(is_employee(mr_chase))
  151.  
  152. #Amelia wasn't the application engineer
  153. amelia = set_fname(anon_employee(),"amelia")
  154. allgoals.append(does_not_have_job(amelia, "applications engineer"))
  155. allgoals.append(is_female(amelia))
  156. allgoals.append(is_employee(amelia))
  157.  
  158. #An Employee is a MarketingManager
  159. marketManager = set_job(anon_employee(),"marketing manager")
  160. allgoals.append(is_employee(marketManager))
  161.  
  162. #Harriet is Female
  163. allgoals.append(is_female(harriet))
  164. allgoals.append(has_window_seat(harriet))
  165. allgoals.append(is_employee(harriet))
  166.  
  167. #One product manager had a window seat
  168. allgoals.append(has_window_seat(window_PM))
  169.  
  170. #One Product Manager had a wall seat
  171. allgoals.append(has_wall_seat(wall_pm))
  172.  
  173. #One product manager was male
  174. pm1 = set_job(anon_employee(), "product manager")
  175. allgoals.append(is_male(pm1))
  176.  
  177. #One product manager was female
  178. pm2 = set_job(anon_employee(), "product manager")
  179. allgoals.append(is_female(pm2))
  180.  
  181. #Every first name must be assigned
  182. allgoals.append(is_employee(set_fname(anon_employee(), "steve")))
  183. allgoals.append(is_employee(set_fname(anon_employee(), "curtis")))
  184. allgoals.append(is_employee(set_fname(anon_employee(), "dick")))
  185. allgoals.append(is_employee(set_fname(anon_employee(), "ellen")))
  186. allgoals.append(is_employee(set_fname(anon_employee(), "amelia")))
  187. allgoals.append(is_employee(set_fname(anon_employee(), "harriet")))
  188.  
  189. #Every Last Name must be assigned
  190. allgoals.append(is_employee(set_lname(anon_employee(), "chase")))
  191. allgoals.append(is_employee(set_lname(anon_employee(), "macklin")))
  192. allgoals.append(is_employee(set_lname(anon_employee(), "radcliffe")))
  193. allgoals.append(is_employee(set_lname(anon_employee(), "weathervane")))
  194. allgoals.append(is_employee(set_lname(anon_employee(), "fairview")))
  195. allgoals.append(is_employee(set_lname(anon_employee(), "singer")))
  196.  
  197. #Every Job must be assigned
  198. allgoals.append(is_employee(set_job(anon_employee(), "product manager")))
  199. allgoals.append(is_employee(set_job(anon_employee(), "applications engineer")))
  200. allgoals.append(is_employee(set_job(anon_employee(), "marketing analyst")))
  201. allgoals.append(is_employee(set_job(anon_employee(), "publication writer")))
  202. allgoals.append(is_employee(set_job(anon_employee(), "marketing manager")))
  203.  
  204. #Every Gender Must be Balanced.
  205. allgoals.append(gender_balance())
  206.  
  207.  
  208. # TODO: nothing to change here, but consider setting the number of solutions to find to 1
  209. # if you want your program to run quickly for testing! A setting of 0 will determine if you're
  210. # 100% correct or not, but it may take a long time (or literally forever if you're wrong).
  211. print("**********************************")
  212. results = run(0, (wallseats, windowseats), (lall,) + tuple(allgoals))
  213. results = set(map(lambda x: (frozenset(x[0]), frozenset(x[1])), results))  # eliminate duplicates
  214. for (walls, windows) in results:
  215.   print("wall seats: ")
  216.   for employee in walls:
  217.     print(employee)
  218.   print("window seats: ")
  219.   for employee in windows:
  220.     print(employee)
  221.   print("-----------")
  222.  
  223.  
  224. print("number of results: " + str(len(results)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement