0% found this document useful (0 votes)
60 views13 pages

AI Lab Programs

The document contains code snippets written in LISP to implement various graph algorithms and data structures including: 1. BFS and DFS graph searches using lists and queues to track nodes. 2. Dijkstra's algorithm for shortest path search using hash tables to store distances and predecessors. 3. Best-first search using a priority queue to order nodes by heuristic value. 4. Water jug problem solving using a state-space search approach. 5. Additional examples like Fibonacci series, string replacement, and factorial calculation.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views13 pages

AI Lab Programs

The document contains code snippets written in LISP to implement various graph algorithms and data structures including: 1. BFS and DFS graph searches using lists and queues to track nodes. 2. Dijkstra's algorithm for shortest path search using hash tables to store distances and predecessors. 3. Best-first search using a priority queue to order nodes by heuristic value. 4. Water jug problem solving using a state-space search approach. 5. Additional examples like Fibonacci series, string replacement, and factorial calculation.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab Record File Of Artificial Intelligence

(CEST, Lucknow)

Submitted By : Name :

Class : B.Tech IV year Branch : IT Roll No :

;;; WAP to implement BFS using LISP


(let ((adjacency-info (make-hash-table :size 20)) (path-predecessor-info (make-hash-table :size 20)) ) (defun set-adj (x y) (setf (gethash x adjacency-info) y) ) (defun get-adj (x) (gethash x adjacency-info) ) (defun set-predecessor (x y) (setf (gethash x path-predecessor-info) y) ) (defun get-predecessor (x) (gethash x path-predecessor-info) ) )

;;; WAP to implement DFS using LISP.


(defun depth-first-graph-search (start-node goal-node) " erforms a depth-first search from !"#$"-%&'( for )&#*-%&'(+" (let ((open (list start-node)) (closed nil) n l) (loop (if (null open)(return .failure)) (setf n (pop open)) (push n closed) (increment-count) (if (e0l n goal-node) (return (extract-path n)) ) (setf l (successors n)) (setf l (list-difference l closed)) (setf open (append l (list-difference open l))) (dolist (x l)(set-predecessor x n) ) , end of loop -------- this is implicitly step3 ))) ,step2 ,step1 ,step2 ,step/ ,step-

;;; WAP to extract atoms from a given list.


(defun ne45line () (format t "67")) (defun is-element-of-list (element input-list) (cond ((member element input-list) t) (t nil))) (defun select-atoms (input-list output-list) , copy output-list into atom-list (set0 atom-list (copy-list output-list)) ,, -+ process first element of the list , if the first element is an atom (cond ((atom (car input-list)) , if the first element is not in the atom-list (cond ((not (is-element-of-list (car input-list) atom-list)) , add atom-element to the atom-list (set0 atom-list (cons (car input-list) atom-list)) , uncoment to see the results ,(print atom-list)))) , else if the first element is not an atom (t (select-atoms (car input-list) atom-list))) ,, 2+ process rest of list (cond ((not (null (cdr input-list))) (select-atoms (cdr input-list) atom-list ))) ,, at end return atom-list (re8erse atom-list)) (defun extract-atoms (input-list) , call select-atoms 4ith empty output-list (select-atoms input-list .())) ,,, 999 :ain program 999 (set0 input5list .(a b (c d) (e f (c d b)))) (format t "67input-list ; 6#" input5list) (ne45line)

(set0 atom5list (extract-atoms input5list)) (format t "67atom-list ; 6#" atom5list)

;;; WAP of a Fibonacci Series in LISP.


(defun fibonacci (%) "<ompute the %.th =ibonacci number+" (if (or (zerop %) (; % -)) (> (fibonacci (- % -)) (fibonacci (- % 2)))))

;;; WAP to find a factorial of a given num


(defun fibonacci (%) "<ompute the %.th =ibonacci number+" (if (or (zerop %) (; % -)) (let((=- (fibonacci (- % -))) (=2 (fibonacci (- % 2)))) (> =- =2))))

;;;WAP of Best First Searc using LISP


(defun (defun (defun (defun hval-of (node) (car node)) state-of (node) (cadr node)) path-of (node) (cdr node)) depth-of (node) (length (cddr node)))

(defvar *visited* nil) (defvar *heur-mult* 2) (defun best (state limit) (let ((nodes 0) (expanded 0) (branches 0) (limit limit) (open (list (list (heur-value state) state)))) (setf *visited* nil) (loop (cond ((null open) (print (list 'nodes nodes expanded branches)) (return (list 'no 'solution 'found)))) (incf nodes) (cond ((goalp (state-of (car open))) (print (list 'nodes nodes expanded branches)) (print (list 'length 'of 'soln (depth-of (car open)))) (return (path-of (car open))))) (cond ((> nodes limit) (print (list 'nodes nodes expanded branches)) (return (list 'closest 'was (car open))))) (let ((children (new-states (state-of (car open))))) (incf expanded) (setf branches (+ (length children) branches)) (setf open (combine- ueue children (car open) (cdr open)))))))

(defun combine- ueue (new-states node ueue) (push (state-of node) *visited*) (dolist (state new-states) (if (not (member state *visited* !test "'e ual)) (push (cons (- (* *heur-mult* (heur-value state)) (depth-of node)) (cons state (cdr node))) ueue))) (sort ueue "'> !#e$ "'car))

;;; WAP of A! Searc for a s ortest pat using LISP.


(let ((distance-info (make-hash-table :size 20)) (path-predecessor-info (make-hash-table :size 20)) ) (defun set-distances (x y) (setf (gethash x distance-info) y) ) (defun get-distances (x) (gethash x distance-info) ) (defun set-predecessor (x y) (setf (gethash x path-predecessor-info) y) ) (defun get-predecessor (x) (gethash x path-predecessor-info) ) )

;;;WAP for Water "ug Solution using LISP


(in-package "?!($") (def8ar 9start9 .(0 0)) (defun first-jug (state) (car state)) (defun second-jug (state) (cadr state)) (defun mk-state (f s) (list f s)) (defun goalp (state) (e0 (first-jug state) 2)) (defun ne4-states (state) (remo8e-null (list (fill-first state) (fill-second state) (pour-first-second state) (pour-second-first state) (empty-first state) (empty-second state)))) (defun remo8e-null (x)

(cond((null x) nil) ((null (car x)) (remo8e-null (cdr x))) ((cons (car x) (remo8e-null (cdr x)))))) (defun fill-first (state) (cond ((@ (first-jug state) 1) (mk-state 1 (second-jug state)))))) (defun fill-second (state) (cond ((@ (second-jug state) /) (mk-state (first-jug state) /)))) (defun pour-first-second (state) (let ( (f (first-jug state)) (s (second-jug state))) (cond ((zerop f) nil) ((; s /) nil) ((@; (> f s) /) (mk-state 0 (> f s))) (t (mk-state (- (> f s) /) /))))) (defun pour-second-first (state) (let ( (f (first-jug state)) (s (second-jug state))) (cond ((zerop s) nil) ((; f 1) nil) ((@; (> f s) 1) (mk-state (> f s) 0)) (t(mk-state 1 (- (> f s) 1)))))) , =ill first from second , <ant pour nothing , =irst full , (mpty second into first , =ill second from first , <ant pour nothing , !econd full , (mpty first into second

(defun empty-first (state)

(cond((A (first-jug state) 0) (mk-state 0 (second-jug state)))))

(defun empty-second (state) (cond((A (second-jug state) 0) (mk-state (first-jug state) 0))))

;;;WAP to replace a String #it anot er String in LISP.


(defun string-replace (str- sub- sub2) (let ((str- (string str-)) (str2 "") (sub- (string sub-)) (sub2 (string sub2)) (index- 0)) (loop (if (string-e0ual str- sub:start- index:end- (min (length str-) (> index- (length sub-)))) (progn (set0 str2 (concatenate .string str2 sub2)) (incf index- (length sub-))) (progn (set0 str2 (concatenate .string str2 (subse0 str- index- (-> index-)))) (incf index-))) (unless (@ index- (length str-)) (return str2)))))

$$$ WAP to implement DFS using P%&L&'.


go(!tartB )oal) :empty5stack((mpty5been5list)B stack(!tartB (mpty5been5listB Ceen5list)B path(!tartB )oalB Ceen5list)+ 7 path implements a depth first search in $&*&) 7 <urrent state ; goalB print out been list path()oalB )oalB Ceen5list):re8erse5print5stack(Ceen5list)+ path(!tateB )oalB Ceen5list) :mo8(!tateB %ext)B 7 not(unsafe(%ext))B not(member5stack(%extB Ceen5list))B stack(%extB Ceen5listB %e45been5list)B path(%extB )oalB %e45been5list)B D+ re8erse5print5stack(!) :empty5stack(!)+ re8erse5print5stack(!) :stack((B $estB !)B re8erse5print5stack($est)B 4rite(()B nl+

$$$ WAP to implement BFS using P%&L&'.


state5record(!tateB arentB E!tateB arentF)+ go(!tartB )oal) :empty50ueue((mpty5open)B state5record(!tartB nilB !tate)B add5to50ueue(!tateB (mpty5openB &pen)B empty5set(<losed)B path(&penB <losedB )oal)+ path(&penB5B5) :- empty50ueue(&pen)B 4rite(.graph searchedB no solution found.)+ path(&penB <losedB )oal) :remo8e5from50ueue(%ext5recordB &penB 5)B state5record(!tateB 5B %ext5record)B !tate ; )oalB 4rite(.!olution path is: .)B nlB printsolution(%ext5recordB <losed)+ path(&penB <losedB )oal) :remo8e5from50ueue(%ext5recordB &penB $est5of5open)B (bagof(<hildB mo8es(%ext5recordB &penB <losedB <hild)B <hildren),<hildren ; EF)B add5list5to50ueue(<hildrenB $est5of5openB %e45open)B add5to5set(%ext5recordB <losedB %e45closed)B path(%e45openB %e45closedB )oal)BD+ mo8es(!tate5recordB &penB <losedB <hild5record) :state5record(!tateB 5B !tate5record)B mo8(!tateB %ext)B 7 not (unsafe(%ext))B state5record(%extB 5B "est)B

not(member50ueue("estB &pen))B not(member5set("estB <losed))B state5record(%extB !tateB <hild5record)+ printsolution(!tate5recordB 5):state5record(!tateBnilB !tate5record)B 4rite(!tate)B nl+ printsolution(!tate5recordB <losed) :state5record(!tateB arentB !tate5record)B state5record( arentB 5B arent5record)B member( arent5recordB <losed)B printsolution( arent5recordB <losed)B 4rite(!tate)B nl+ add5list5to50ueue(EFB GueueB Gueue)+ add5list5to50ueue(EHI"FB GueueB %e450ueue) :add5to50ueue(HB GueueB "emp50ueue)B add5list5to50ueue("B "emp50ueueB %e450ueue)+

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy