Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##############################################
- # CSC 246 - Fall 2019
- # Missouri Western State University
- # Homework #4 - Logic Programming
- # Starter Template
- # Student(s): 1
- from kanren import *
- allgoals = [] # goals used to solve the logic puzzle
- def anon_employee():
- return (var(), var(), var())
- def set_fname(employee, name):
- return (name, employee[1], employee[2])
- def set_lname(employee, name):
- return (employee[0], name, employee[2])
- def set_job(employee, job):
- return (employee[0], employee[1], job)
- # TODO: you can make the starting variables more specific
- # this will make your program run more efficiently
- # but be careful! don't solve the problem in your code...
- # ... have your code solve the problem!
- # ex. Curtis ended up with a wall office.
- curtis = set_fname(anon_employee(), "curtis")
- #Ms. Singer ended up with a window seat
- ms_singer = set_lname(anon_employee(), "singer")
- #Harriet ended up with a Window seat
- harriet = set_fname(anon_employee(), "harriet")
- #A Product manager ended up with a window seat
- window_PM = set_job(anon_employee(), "product manager")
- #A Product Manager ended up with a Wall seat
- wall_pm = set_job(anon_employee(), "product manager")
- wallseats = (curtis, var(), wall_pm) # employees with seats by the wall
- windowseats = (harriet, ms_singer, window_PM) # employees with seats by the window
- employees = wallseats + windowseats # all employees (same vars)
- # TODO: create more goal generating functions for use below
- def has_wall_seat(employee):
- """produces a goal expecting the employee to have a wall seat"""
- return (membero, employee, wallseats)
- def has_window_seat(employee):
- """produces a goal expecting the employee to have a window seat"""
- return (membero, employee, windowseats)
- def is_employee(employee):
- """produces a goal asserting that 'employee' is one of the six employees in question"""
- return (membero, employee, employees)
- def is_male(employee):
- """produces a goal asserting that the employee has one of the male first or last names"""
- return (lany,
- is_employee(set_fname(employee, "steve")),
- is_employee(set_fname(employee, "curtis")),
- is_employee(set_fname(employee, "dick")))
- def is_female(employee):
- """produces a goal asserting that the employee has one of the female first names"""
- return (lany,
- is_employee(set_fname(employee, "harriet")),
- is_employee(set_fname(employee, "ellen")),
- is_employee(set_fname(employee, "amelia")))
- def does_not_have_lname(employee, lname):
- "produces a goal asserting that an employee cannot have a specific last name"
- return(lany,) + tuple(map(lambda n: is_employee(set_lname(employee, n)),
- filter(lambda x : x != lname, ["fairview", "singer", "macklin", "radcliffe", "weathervane", "chase"])))
- def does_not_have_job(employee, job):
- "produces a goal asserting that an employee cannot have a specific job"
- return(lany,) + tuple(map(lambda n: is_employee(set_job(employee, n)),
- filter(lambda x : x != job, ["product manager", "applications engineer", "marketing analyst", "publication writer", "marketing manager"])))
- def gender_balance():
- "produces a goal asserting that the windowseats must contain a mix of male and female employees"
- return (lall,
- (lany, is_male(windowseats[0]), is_male(windowseats[1]), is_male(windowseats[2])),
- (lany, is_female(windowseats[0]), is_female(windowseats[1]), is_female(windowseats[2])))
- # TODO: add more goals below to force a solution to the puzzle
- # you should end up with one unique solution if you do everything correctly
- # however, your program will run much faster when you are further from the correct answer!
- #The marketing analyst had a last name of Radcliffe.
- mx_radcliffe = set_lname(anon_employee(), "radcliffe")
- mx_radcliffe = set_job(mx_radcliffe, "marketing analyst")
- allgoals.append(is_employee(mx_radcliffe))
- #The Applications Engineer had a last name of Fairview.
- me_fairview = set_lname(anon_employee(), "fairview")
- me_fairview = set_job(me_fairview, "applications engineer")
- allgoals.append(is_employee(me_fairview))
- #Mrs. Singer is female and has a window seat
- allgoals.append(is_female(ms_singer))
- allgoals.append(has_window_seat(ms_singer))
- allgoals.append(is_employee(ms_singer))
- #The Publication writer had a first name of Steve & Steve's last name wasn't radcliffe
- pw_steve = set_fname(anon_employee(), "steve")
- pw_steve = set_job(pw_steve, "publication writer")
- allgoals.append(does_not_have_lname(pw_steve, "radcliffe"))
- allgoals.append(is_male(pw_steve))
- allgoals.append(is_employee(pw_steve))
- #Ellen Weathervane Wasn't the marketing manager
- ellen_weathervane = set_fname(anon_employee(), "ellen")
- ellen_weathervane = set_lname(ellen_weathervane, "weathervane")
- allgoals.append(does_not_have_job(ellen_weathervane, "marketing manager"))
- allgoals.append(is_female(ellen_weathervane))
- allgoals.append(is_employee(ellen_weathervane))
- #Curtis' last name was not fairview
- allgoals.append(does_not_have_lname(curtis, "fairview"))
- allgoals.append(is_male(curtis))
- allgoals.append(has_wall_seat(curtis))
- allgoals.append(is_employee(curtis))
- #Dick's last name isn't Macklin
- dick = set_fname(anon_employee(),"dick")
- allgoals.append(does_not_have_lname(dick, "macklin"))
- allgoals.append(is_male(dick))
- allgoals.append(is_employee(dick))
- #Mr. Chase wasn't the applications engineer
- mr_chase = set_lname(anon_employee(), "chase")
- allgoals.append(does_not_have_job(mr_chase, "applications engineer"))
- allgoals.append(is_male(mr_chase))
- allgoals.append(is_employee(mr_chase))
- #Amelia wasn't the application engineer
- amelia = set_fname(anon_employee(),"amelia")
- allgoals.append(does_not_have_job(amelia, "applications engineer"))
- allgoals.append(is_female(amelia))
- allgoals.append(is_employee(amelia))
- #An Employee is a MarketingManager
- marketManager = set_job(anon_employee(),"marketing manager")
- allgoals.append(is_employee(marketManager))
- #Harriet is Female
- allgoals.append(is_female(harriet))
- allgoals.append(has_window_seat(harriet))
- allgoals.append(is_employee(harriet))
- #One product manager had a window seat
- allgoals.append(has_window_seat(window_PM))
- #One Product Manager had a wall seat
- allgoals.append(has_wall_seat(wall_pm))
- #One product manager was male
- pm1 = set_job(anon_employee(), "product manager")
- allgoals.append(is_male(pm1))
- #One product manager was female
- pm2 = set_job(anon_employee(), "product manager")
- allgoals.append(is_female(pm2))
- #Every first name must be assigned
- allgoals.append(is_employee(set_fname(anon_employee(), "steve")))
- allgoals.append(is_employee(set_fname(anon_employee(), "curtis")))
- allgoals.append(is_employee(set_fname(anon_employee(), "dick")))
- allgoals.append(is_employee(set_fname(anon_employee(), "ellen")))
- allgoals.append(is_employee(set_fname(anon_employee(), "amelia")))
- allgoals.append(is_employee(set_fname(anon_employee(), "harriet")))
- #Every Last Name must be assigned
- allgoals.append(is_employee(set_lname(anon_employee(), "chase")))
- allgoals.append(is_employee(set_lname(anon_employee(), "macklin")))
- allgoals.append(is_employee(set_lname(anon_employee(), "radcliffe")))
- allgoals.append(is_employee(set_lname(anon_employee(), "weathervane")))
- allgoals.append(is_employee(set_lname(anon_employee(), "fairview")))
- allgoals.append(is_employee(set_lname(anon_employee(), "singer")))
- #Every Job must be assigned
- allgoals.append(is_employee(set_job(anon_employee(), "product manager")))
- allgoals.append(is_employee(set_job(anon_employee(), "applications engineer")))
- allgoals.append(is_employee(set_job(anon_employee(), "marketing analyst")))
- allgoals.append(is_employee(set_job(anon_employee(), "publication writer")))
- allgoals.append(is_employee(set_job(anon_employee(), "marketing manager")))
- #Every Gender Must be Balanced.
- allgoals.append(gender_balance())
- # TODO: nothing to change here, but consider setting the number of solutions to find to 1
- # if you want your program to run quickly for testing! A setting of 0 will determine if you're
- # 100% correct or not, but it may take a long time (or literally forever if you're wrong).
- print("**********************************")
- results = run(0, (wallseats, windowseats), (lall,) + tuple(allgoals))
- results = set(map(lambda x: (frozenset(x[0]), frozenset(x[1])), results)) # eliminate duplicates
- for (walls, windows) in results:
- print("wall seats: ")
- for employee in walls:
- print(employee)
- print("window seats: ")
- for employee in windows:
- print(employee)
- print("-----------")
- print("number of results: " + str(len(results)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement