Solving a complex logic puzzle with CLIPS IDE and specific rules [closed]
Please help me 馃檹
This is a complex logic puzzle where we need to figure out Uncle J贸zsi's seven daily activities, taking into account the information provided in various instructions and hints.
Before we get started, let's look at the kind of information we're searching for:
Day: Monday through Friday (5 days)
Families: P谩szti, D茅ri, Feh茅r, Berkes, Kardos (5 families)
Street: Telepi Street, M谩lna Street, Jenei Street, R贸zsika Street, Bukta Street (5 streets)
Activities: spring spraying, washing machine repair, fence painting, TV antenna adjustment, gutter cleaning (5 activities)
Based on the information, we know the following:
-
Uncle J贸zsi did not do the spring spraying on Telepi Street. (The name of the family is longer than that of the family living on M谩lna Street.)
-
The P谩szti family does not live on Jenei Street, and they called Uncle J贸zsi on Wednesday.
-
The D茅ri family lives on R贸zsika Street, but Uncle J贸zsi did not work for them on Monday.
-
One day, the Feh茅r family entrusted him with something, and the next day he was painting a fence.
-
On Monday, he repaired a washing machine.
-
He worked on Bukta Street two days later than at the Berkes family, where he adjusted the TV antenna.
-
He did not do the gutter cleaning at the Kardos family.
You gave rules for trying to solve a logic puzzle where there are several families, streets, tasks, and days, and they have to be matched according to certain rules. Your rules are specific and create a relationship between the variables. This code needs to be written in the CLIPS IDE, The program should solve the puzzle during the trials and print it out in a table.
I I tried to use gpt chat but it did not lead to anything.
(deffacts starting-facts
(day "Monday") (day "Tuesday") (day "Wednesday") (day "Thursday") (day "Friday")
(family "P谩szti") (family "D茅ri") (family "Feh茅r") (family "Berkes") (family "Kardos")
(street "Telepi") (street "M谩lna") (street "Jenei") (street "R贸zsika") (street "Bukta")
(activity "Spring spraying") (activity "Washing machine repair") (activity "Fence painting")
(activity "TV antenna adjustment") (activity "Gutter cleaning")
)
(defrule rule1
?f1 <- (day ?d)
?f2 <- (family ?f)
?f3 <- (street ?s)
?f4 <- (activity "Spring spraying")
(test (neq ?s "Telepi"))
=>
(assert (activity ?f ?s ?d "Spring spraying"))
(retract ?f1 ?f2 ?f3 ?f4)
)
(defrule rule2
?f1 <- (family "P谩szti")
?f2 <- (day "Wednesday")
?f3 <- (street ?s)
?f4 <- (activity ?a)
(test (neq ?s "Jenei"))
=>
(assert (activity "P谩szti" ?s "Wednesday" ?a))
(retract ?f1 ?f2 ?f3 ?f4)
)
(defrule rule3
?f1 <- (family "D茅ri")
?f2 <- (street "R贸zsika")
?f3 <- (day ?d)
?f4 <- (activity ?a)
(test (neq ?d "Monday"))
=>
(assert (activity "D茅ri" "R贸zsika" ?d ?a))
(retract ?f1 ?f2 ?f3 ?f4)
)
(defrule rule4
?f1 <- (family "Feh茅r")
?f2 <- (street ?s)
?f3 <- (day ?d)
?f4 <- (activity ?a)
(test (neq ?a "Fence painting"))
=>
(assert (activity "Feh茅r" ?s ?d ?a))
(retract ?f1 ?f2 ?f3 ?f4)
)
(defrule rule5
?f1 <- (day "Monday")
?f2 <- (activity "Washing machine repair")
?f3 <- (family ?f)
?f4 <- (street ?s)
=>
(assert (activity ?f ?s "Monday" "Washing machine repair"))
(retract ?f1 ?f2 ?f3 ?f4)
)
(defrule rule6
?f1 <- (family "Berkes")
?f2 <- (activity "TV antenna adjustment")
?f3 <- (day ?d)
?f4 <- (street ?s)
(test (neq ?s "Bukta"))
=>
(assert (activity "Berkes" ?s ?d "TV antenna adjustment"))
(retract ?f1 ?f2 ?f3 ?f4)
)
(defrule rule7
?f1 <- (family "Kardos")
?f2 <- (street ?s)
?f3 <- (day ?d)
?f4 <- (activity ?a)
(test (neq ?a "Gutter cleaning"))
=>
(assert (activity "Kardos" ?s ?d ?a))
(retract ?f1 ?f2 ?f3 ?f4)
)
(defrule print-result
?f <- (activity ?f ?s ?d ?a)
=>
(printout t "On " ?d ", Uncle J贸zsi worked with the " ?f " family, at " ?s " street, where he carried out the following activity: " ?a crlf)
(retract ?f)
)
Here is the second code, but for some reason it does not give any result and does not write errors.
(deffacts initial-facts
(day Monday) (day Tuesday) (day Wednesday) (day Thursday) (day Friday)
(job Spring_spraying) (job Washing_machine_repair) (job Fence_painting) (job TV_antenna_adjustment) (job Gutter_cleaning)
(family P谩szti) (family D茅ri) (family Feh茅r) (family Berkes) (family Kardos)
(street Telepi) (street M谩lna) (street Jenei) (street R贸zsika) (street Bukta)
(day-after Monday Tuesday) (day-after Tuesday Wednesday) (day-after Wednesday Thursday) (day-after Thursday Friday))
(deffunction next_day (?day) (switch ?day (case "Monday" then "Tuesday") (case "Tuesday" then "Wednesday") (case "Wednesday" then "Thursday") (case "Thursday" then "Friday") (case "Friday" then "Monday")))
(deffunction day_after_next (?day) (next_day (next_day ?day)))
(defrule rule1
(day ?day1&:(eq ?day1 "Monday") ?work1&:(eq ?work1 "washing_machine_repair") ?family1 ?street1)
(family ?family1&:(eq ?family1 "Berkes") ?street1)
=>
(assert (day_work_family_street ?day1 ?work1 ?family1 ?street1)))
(defrule rule2
(day ?day2&:(eq ?day2 "Tuesday") ?work2&:(eq ?work2 "spring_spraying") ?family2 ?street2)
(family ?family2&:(eq ?family2 "Kardos") ?street2)
=>
(assert (day_work_family_street ?day2 ?work2 ?family2 ?street2)))
(defrule rule3
(day ?day3&:(eq ?day3 "Wednesday") ?work3&:(eq ?work3 "TV_antenna_adjustment") ?family3 ?street3)
(family ?family3&:(eq ?family3 "Paszti") ?street3)
=>
(assert (day_work_family_street ?day3 ?work3 ?family3 ?street3)))
(defrule rule4
(day ?day4&:(eq ?day4 "Thursday") ?work4&:(eq ?work4 "gutter_cleaning") ?family4 ?street4)
(family ?family4&:(eq ?family4 "Feh茅r") ?street4)
=>
(assert (day_work_family_street ?day4 ?work4 ?family4 ?street4)))
(defrule rule5
(day ?day5&:(eq ?day5 "Friday") ?work5&:(eq ?work5 "fence_painting") ?family5 ?street5)
(family ?family5&:(eq ?family5 "D茅ri") ?street5)
=>
(assert (day_work_family_street ?day5 ?work5 ?family5 ?street5)))
; J贸zsi b谩csi didn't do spring spraying on Telepi street.
(defrule rule6
(job "spring_spraying" ?family)
(not (family ?family "Telepi street"))
=>
(assert (family_job ?family "spring_spraying")))
; P谩szti family doesn't live on Jenei street, and they called J贸zsi b谩csi on Wednesday.
(defrule rule7
(day "Wednesday" ?job)
(family "P谩szti" ?street&:(neq ?street "Jenei street"))
=>
(assert (family_day_job "P谩szti" "Wednesday" ?job))
(assert (family_street "P谩szti" ?street)))
; D茅ri family lives on R贸zsika street, but J贸zsi b谩csi didn't work for them on Monday.
(defrule rule8
(day ?day&:(neq ?day "Monday") ?job)
(family "D茅ri" "R贸zsika street")
=>
(assert (family_day_job "D茅ri" ?day ?job))
(assert (family_street "D茅ri" "R贸zsika street")))
; Feh茅r family hired J贸zsi b谩csi for something, and the next day he painted a fence.
(defrule rule9
(day ?day1 ?job1)
(day ?day2&:(eq ?day2 (next_day ?day1)) "fence_painting")
(family "Feh茅r" ?street)
=>
(assert (family_day_job "Feh茅r" ?day1 ?job1))
(assert (family_day_job "Feh茅r" ?day2 "fence_painting"))
(assert (family_street "Feh茅r" ?street)))
; J贸zsi b谩csi worked in Bukta street two days later than at the Berkes family. He adjusted the TV antenna in Bukta street.
(defrule rule10
(day ?day1 ?job1)
(day ?day2&:(eq ?day2 (day_after_next ?day1)) "TV_antenna_adjustment")
(family "Berkes" ?street1)
(family ?family2 "Bukta street")
=>
(assert (family_day_job "Berkes" ?day1 ?job1))
(assert (family_day_job ?family2 ?day2 "TV_antenna_adjustment"))
(assert (family_street "Berkes" ?street1))
(assert (family_street ?family2 "Bukta street")))
; J贸zsi b谩csi didn't do gutter cleaning at the Kardos family.
(defrule rule11
(job "gutter_cleaning" ?family&:(neq ?family "Kardos"))
(family ?family ?street)
=>
(assert (family_job ?family "gutter_cleaning"))
(assert (family_street ?family ?street)))
; Rules for ordering days and services
(deffunction day_to_number (?day)
(switch ?day
(case "Monday" then 1)
(case "Tuesday" then 2)
(case "Wednesday" then 3)
(case "Thursday" then 4)
(case "Friday" then 5)))
(defrule rule12
?f <- (family_day_job ?fam1 ?day1 ?job1)
?f2 <- (family_day_job ?fam2 ?day2&:(neq ?day1 ?day2) ?job2&:(neq ?job1 ?job2))
(test (< (day_to_number ?day1) (day_to_number ?day2)))
=>
(retract ?f ?f2)
(assert (family_day_job ?fam1 ?day2 ?job2))
(assert (family_day_job ?fam2 ?day1 ?job1)))
(defrule rule13
?f <- (family_day_job ?family ?day1 ?job1 ?day2 ?job2)
?f2 <- (family_day_job ?family ?day3&:(and (neq ?day1 ?day3) (neq ?day2 ?day3)) ?job3&:(and (neq ?job1 ?job3) (neq ?job2 ?job3)))
(test (< (day_to_number ?day1) (day_to_number ?day3)))
=>
(retract ?f ?f2)
(assert (family_day_job ?family ?day3 ?job3 ?day1 ?job1))
(assert (family_day_job ?family ?day2 ?job2)))
(defrule rule14
?f <- (family_day_job ?family ?day1 ?job1 ?day2 ?job2 ?day3 ?job3)
?f2 <- (family_day_job ?family ?day4&:(and (neq ?day1 ?day4) (neq ?day2 ?day4) (neq ?day3 ?day4)) ?job4&:(and (neq ?job1 ?job4) (neq ?job2 ?job4) (neq ?job3 ?job4)))
(test (< (day_to_number ?day3) (day_to_number ?day4)))
=>
(retract ?f ?f2)
(assert (family_day_job ?family ?day4 ?job4 ?day1 ?job1 ?day2 ?job2 ?day3 ?job3))
(defrule rule15
?f <- (family_day_job ?family ?day1 ?job1 ?day2 ?job2 ?day3 ?job3 ?day4 ?job4)
?f2 <- (family_day_job ?family ?day5&:(and (neq ?day1 ?day5) (neq ?day2 ?day5) (neq ?day3 ?day5) (neq ?day4 ?day5)) ?job5&:(and (neq ?job1 ?job5) (neq ?job2 ?job5) (neq ?job3 ?job5) (neq ?job4 ?job5)))
=>
(retract ?f ?f2)
(assert (family_day_job ?family ?day5 ?job5 ?day1 ?job1 ?day2 ?job2 ?day3 ?job3 ?day4 ?job4)))
(deffunction number_to_day (?number)
(switch ?number
(case 1 then "H茅tf艖")
(case 2 then "Kedd")
(case 3 then "Szerda")
(case 4 then "Cs眉t枚rt枚k")
(case 5 then "P茅ntek")))
; Overrides the previously defined facts if new facts are created based on the rules
(defrule overwrite-facts
?old <- (family ?family ?street)
?new <- (family_day_job ?family ?day ?job ?street)
=>
(retract ?old)
(assert (family ?family ?street ?day ?job)))
(defrule print-solution
(family ?family ?street ?day ?job)
=>
(printout t ?day ": " ?job " - " ?family " family - " ?street " street" crlf))
(defrule table
?f <- (activity ?c ?u ?n ?t)
=>
(printout t "Family: " ?c " Street: " ?u " Day: " ?n " Activity: " ?t crlf)
(retract ?f)
)
Comments
Post a Comment