Skip to content

Commit d83cbb2

Browse files
committed
Finished A3
1 parent d843714 commit d83cbb2

File tree

3 files changed

+954
-0
lines changed

3 files changed

+954
-0
lines changed

CS612/a3/a3.tex

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
\documentclass{article}
2+
3+
\usepackage{url}
4+
\usepackage{tikz}
5+
\usepackage{float}
6+
\usepackage{amsmath}
7+
\usepackage{amssymb}
8+
\usepackage{enumitem}
9+
10+
\usetikzlibrary{matrix, shapes, snakes, arrows}
11+
\tikzset{>=triangle 45}
12+
13+
\title{CS 612: Assignment 3\\Spring 2013}
14+
15+
\author{Dustin Ingram}
16+
17+
\date{\today}
18+
19+
\begin{document}
20+
21+
\maketitle
22+
23+
\section*{Written}
24+
25+
\begin{enumerate}
26+
27+
\item{} % 1
28+
29+
\textbf{Ontology for Cooking Utensils:} I developed my ontology based on the
30+
principle that the key differences in kitchen utensils arise from their intended
31+
uses, their materials, and the types of ``ends'' that they have. I further broke
32+
down the materials (e.g., heat-resistant or non-heat-resistant) as well as the
33+
uses as these were the dominant features driving the delineation between utensil
34+
types. The kitchen utensils I used were all considered ``unitaskers'' in the
35+
sense that they had only one intended use. For example, you \emph{could} cut a
36+
piece of meat with a candy thermometer, but you probably wouldn't if you
37+
actually had your wits about you. Furthermore, I considered a utensil's material
38+
to simply be it's most important material (usually that which makes up the
39+
business end), e.g. the material which makes up the handle on a steak knife does
40+
not affect it's usefulness very much.
41+
42+
\item{} % 2
43+
44+
\textbf{Representing sentences using modal logic:}
45+
Here, I'm considering the $\in$ symbol to represent being ``in'' a place,
46+
whether it is swimming in a pool or sitting in a cabana.
47+
\begin{enumerate}
48+
\item $K_{Chris}(John \in robots)$
49+
\item $K_{John}(K_{Chris}(John \in robots)$
50+
\item $\lozenge robots \neg \in waterproof$
51+
\item $\square \phi \neg \in waterproof \rightarrow \phi \square \neg \in pool$
52+
\item $\square \phi \neg \in pool \rightarrow \lozenge \phi \in cabana$
53+
\end{enumerate}
54+
55+
\end{enumerate}
56+
57+
\section*{Programming}
58+
59+
I based my rules off of the \texttt{washington.edu} example \texttt{.clp} file.
60+
I adapted it to work in JESS. It can be run by doing:
61+
62+
\begin{verbatim}
63+
$ jess dsi23_cam.clp
64+
\end{verbatim}
65+
66+
I also modified the program to support arbitrary numbers of cannibals and
67+
missionaries. It can be modified by changing the following lines:
68+
69+
\begin{verbatim}
70+
;;;***********************
71+
;;;* ARBITRARY VARIABLES *
72+
;;;***********************
73+
74+
(defglobal ?*n-missionaries* = 3)
75+
(defglobal ?*n-cannibals* = 3)
76+
\end{verbatim}
77+
78+
The output for a 3-missionary 3-cannibal problem is as follows:
79+
80+
\begin{verbatim}
81+
Jess, the Rule Engine for the Java Platform
82+
Copyright (C) 2008 Sandia Corporation
83+
Jess Version 7.1p2 11/5/2008
84+
85+
This copy of Jess will expire in 29 day(s).
86+
87+
Solution found:
88+
89+
Move 1 missionary and 1 cannibal to shore-2.
90+
Move 1 missionary to shore-1.
91+
Move 2 cannibals to shore-2.
92+
Move 1 cannibal to shore-1.
93+
Move 2 missionaries to shore-2.
94+
Move 1 missionary and 1 cannibal to shore-1.
95+
Move 2 missionaries to shore-2.
96+
Move 1 cannibal to shore-1.
97+
Move 2 cannibals to shore-2.
98+
Move 1 missionary to shore-1.
99+
Move 1 missionary and 1 cannibal to shore-2.
100+
101+
Solution found:
102+
103+
Move 1 missionary and 1 cannibal to shore-2.
104+
Move 1 missionary to shore-1.
105+
Move 2 cannibals to shore-2.
106+
Move 1 cannibal to shore-1.
107+
Move 2 missionaries to shore-2.
108+
Move 1 missionary and 1 cannibal to shore-1.
109+
Move 2 missionaries to shore-2.
110+
Move 1 cannibal to shore-1.
111+
Move 2 cannibals to shore-2.
112+
Move 1 cannibal to shore-1.
113+
Move 2 cannibals to shore-2.
114+
115+
Solution found:
116+
117+
Move 2 cannibals to shore-2.
118+
Move 1 cannibal to shore-1.
119+
Move 2 cannibals to shore-2.
120+
Move 1 cannibal to shore-1.
121+
Move 2 missionaries to shore-2.
122+
Move 1 missionary and 1 cannibal to shore-1.
123+
Move 2 missionaries to shore-2.
124+
Move 1 cannibal to shore-1.
125+
Move 2 cannibals to shore-2.
126+
Move 1 missionary to shore-1.
127+
Move 1 missionary and 1 cannibal to shore-2.
128+
129+
Solution found:
130+
131+
Move 2 cannibals to shore-2.
132+
Move 1 cannibal to shore-1.
133+
Move 2 cannibals to shore-2.
134+
Move 1 cannibal to shore-1.
135+
Move 2 missionaries to shore-2.
136+
Move 1 missionary and 1 cannibal to shore-1.
137+
Move 2 missionaries to shore-2.
138+
Move 1 cannibal to shore-1.
139+
Move 2 cannibals to shore-2.
140+
Move 1 cannibal to shore-1.
141+
Move 2 cannibals to shore-2.
142+
\end{verbatim}
143+
144+
\end{document}

CS612/a3/dsi23_cam.clp

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
2+
;;;======================================================
3+
;;; Cannibals and Missionaries Problem
4+
;;;
5+
;;; Another classic AI problem. The point is
6+
;;; to get three cannibals and three missionaries
7+
;;; across a stream with a boat that can only
8+
;;; hold two people. If the cannibals outnumber
9+
;;; the missionaries on either side of the stream,
10+
;; then the cannibals will eat the missionaries.
11+
;;;
12+
;;; CLIPS Version 6.01 Example
13+
;;;
14+
;;; To execute, merely load, reset and run.
15+
;;;======================================================
16+
17+
;;;***********************
18+
;;;* ARBITRARY VARIABLES *
19+
;;;***********************
20+
21+
(defglobal ?*n-missionaries* = 3)
22+
(defglobal ?*n-cannibals* = 3)
23+
24+
;;;*************
25+
;;;* TEMPLATES *
26+
;;;*************
27+
28+
;;; The status facts hold the state
29+
;;; information of the search tree.
30+
31+
(deftemplate MAIN::status
32+
(slot search-depth)
33+
(slot parent)
34+
(slot shore-1-missionaries)
35+
(slot shore-1-cannibals)
36+
(slot shore-2-missionaries)
37+
(slot shore-2-cannibals)
38+
(slot boat-location)
39+
(slot last-move))
40+
41+
;;;*****************
42+
;;;* INITIAL STATE *
43+
;;;*****************
44+
45+
(deffacts MAIN::initial-positions
46+
(status (search-depth 1)
47+
(parent no-parent)
48+
(shore-1-missionaries ?*n-missionaries*)
49+
(shore-2-missionaries 0)
50+
(shore-1-cannibals ?*n-cannibals*)
51+
(shore-2-cannibals 0)
52+
(boat-location shore-1)
53+
(last-move "No move.")))
54+
55+
(deffacts MAIN::boat-information
56+
(boat-can-hold 2))
57+
58+
;;;****************************************
59+
;;;* FUNCTION FOR MOVE DESCRIPTION STRING *
60+
;;;****************************************
61+
62+
(deffunction MAIN::move-string (?missionaries ?cannibals ?shore)
63+
(if (eq ?missionaries 0) then
64+
(if (eq ?cannibals 1)
65+
then (format nil "Move 1 cannibal to %s.%n" ?shore)
66+
else (format nil "Move %d cannibals to %s.%n" ?cannibals ?shore))
67+
else
68+
(if (eq ?missionaries 1) then
69+
(if (eq ?cannibals 0) then
70+
(format nil "Move 1 missionary to %s.%n" ?shore) else
71+
(if (eq ?cannibals 1) then
72+
(format nil "Move 1 missionary and 1 cannibal to %s.%n" ?shore)
73+
else
74+
(format nil "Move 1 missionary and %d cannibals to %s.%n"
75+
?cannibals ?shore)))
76+
else
77+
(if (eq ?cannibals 0) then
78+
(format nil "Move %d missionaries to %s.%n" ?missionaries ?shore)
79+
else
80+
(if (eq ?cannibals 1) then
81+
(format nil "Move %d missionaries and 1 cannibal to %s.%n"
82+
?missionaries ?shore)
83+
else
84+
(format nil "Move %d missionary and %d cannibals to %s.%n"
85+
?missionaries ?cannibals ?shore))))))
86+
87+
;;;***********************
88+
;;;* GENERATE PATH RULES *
89+
;;;***********************
90+
91+
(defrule MAIN::shore-1-move
92+
?node <- (status (search-depth ?num)
93+
(boat-location shore-1)
94+
(shore-1-missionaries ?s1m)
95+
(shore-1-cannibals ?s1c)
96+
(shore-2-missionaries ?s2m)
97+
(shore-2-cannibals ?s2c))
98+
(boat-can-hold ?limit)
99+
=>
100+
(bind ?max-missionaries (min ?s1m ?limit))
101+
(bind ?missionaries 0)
102+
(while (<= ?missionaries ?max-missionaries)
103+
(bind ?min-cannibals (max 0 (- 1 ?missionaries)))
104+
(bind ?max-cannibals (min ?s1c (- ?limit ?missionaries)))
105+
(bind ?cannibals ?min-cannibals)
106+
(while (<= ?cannibals ?max-cannibals)
107+
(duplicate ?node (search-depth (+ 1 ?num))
108+
(parent ?node)
109+
(shore-1-missionaries (- ?s1m ?missionaries))
110+
(shore-1-cannibals (- ?s1c ?cannibals))
111+
(shore-2-missionaries (+ ?s2m ?missionaries))
112+
(shore-2-cannibals (+ ?s2c ?cannibals))
113+
(boat-location shore-2)
114+
(last-move (MAIN::move-string ?missionaries ?cannibals shore-2)))
115+
(++ ?cannibals))
116+
(++ ?missionaries)))
117+
118+
(defrule MAIN::shore-2-move
119+
?node <- (status (search-depth ?num)
120+
(boat-location shore-2)
121+
(shore-1-missionaries ?s1m)
122+
(shore-1-cannibals ?s1c)
123+
(shore-2-missionaries ?s2m)
124+
(shore-2-cannibals ?s2c))
125+
(boat-can-hold ?limit)
126+
=>
127+
(bind ?max-missionaries (min ?s2m ?limit))
128+
(bind ?missionaries 0)
129+
(while (<= ?missionaries ?max-missionaries)
130+
(bind ?min-cannibals (max 0 (- 1 ?missionaries)))
131+
(bind ?max-cannibals (min ?s2c (- ?limit ?missionaries)))
132+
(bind ?cannibals ?min-cannibals)
133+
(while (<= ?cannibals ?max-cannibals)
134+
(duplicate ?node (search-depth (+ 1 ?num))
135+
(parent ?node)
136+
(shore-1-missionaries (+ ?s1m ?missionaries))
137+
(shore-1-cannibals (+ ?s1c ?cannibals))
138+
(shore-2-missionaries (- ?s2m ?missionaries))
139+
(shore-2-cannibals (- ?s2c ?cannibals))
140+
(boat-location shore-1)
141+
(last-move (MAIN::move-string ?missionaries ?cannibals shore-1)))
142+
(++ ?cannibals))
143+
(++ ?missionaries)))
144+
145+
;;;******************************
146+
;;;* CONSTRAINT VIOLATION RULES *
147+
;;;******************************
148+
149+
(defmodule CONSTRAINTS)
150+
151+
(defrule CONSTRAINTS::cannibals-eat-missionaries
152+
(declare (auto-focus TRUE))
153+
?node <- (status (shore-1-missionaries ?s1m)
154+
(shore-1-cannibals ?s1c)
155+
(shore-2-missionaries ?s2m)
156+
(shore-2-cannibals ?s2c))
157+
(test (or (and (> ?s2c ?s2m) (<> ?s2m 0))
158+
(and (> ?s1c ?s1m) (<> ?s1m 0))))
159+
=>
160+
(retract ?node))
161+
162+
(defrule CONSTRAINTS::circular-path
163+
(declare (auto-focus TRUE))
164+
(status (search-depth ?sd1)
165+
(boat-location ?bl)
166+
(shore-1-missionaries ?s1m)
167+
(shore-1-cannibals ?s1c)
168+
(shore-2-missionaries ?s2m)
169+
(shore-2-cannibals ?s2c))
170+
?node <- (status (search-depth ?sd2&:(< ?sd1 ?sd2))
171+
(boat-location ?bl)
172+
(shore-1-missionaries ?s1m)
173+
(shore-1-cannibals ?s1c)
174+
(shore-2-missionaries ?s2m)
175+
(shore-2-cannibals ?s2c))
176+
=>
177+
(retract ?node))
178+
179+
;;;*********************************
180+
;;;* FIND AND PRINT SOLUTION RULES *
181+
;;;*********************************
182+
183+
(defmodule SOLUTION)
184+
185+
(deftemplate SOLUTION::moves
186+
(slot id)
187+
(multislot moves-list))
188+
189+
(defrule SOLUTION::recognize-solution
190+
(declare (auto-focus TRUE))
191+
?node <- (status (parent ?parent)
192+
(shore-2-missionaries ?m&:(= ?m ?*n-missionaries*))
193+
(shore-2-cannibals ?c&:(= ?c ?*n-cannibals*))
194+
(last-move ?move))
195+
=>
196+
(retract ?node)
197+
(assert (moves (id ?parent) (moves-list ?move))))
198+
199+
(defrule SOLUTION::further-solution
200+
?node <- (status (parent ?parent)
201+
(last-move ?move))
202+
?mv <- (moves (id ?node) (moves-list $?rest))
203+
=>
204+
(modify ?mv (id ?parent) (moves-list ?move ?rest)))
205+
206+
(defrule SOLUTION::print-solution
207+
?mv <- (moves (id no-parent) (moves-list "No move." $?m))
208+
=>
209+
(retract ?mv)
210+
(printout t crlf "Solution found: " crlf crlf)
211+
(bind ?length (length$ ?m))
212+
(bind ?i 1)
213+
(while (<= ?i ?length)
214+
(bind ?move (nth$ ?i ?m))
215+
(printout t ?move)
216+
(bind ?i (+ 1 ?i))))
217+
218+
(reset)
219+
(run)

0 commit comments

Comments
 (0)