ai lab programs

15
Lab Record File Of Artificial Intelligence (CEST, Lucknow)

Upload: virendrak11

Post on 16-Apr-2015

48 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: AI Lab Programs

Lab Record FileOf

Artificial Intelligence

(CEST, Lucknow)

Submitted By : Name :

Page 2: AI Lab Programs

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) )

)

Page 3: AI Lab Programs

;;; WAP to implement DFS using LISP.

(defun depth-first-graph-search (start-node goal-node)

"Performs a depth-first search from START-NODE for GOAL-NODE."

(let ((open (list start-node)) ;step1

(closed nil) n l)

(loop (if (null open)(return 'failure)) ;step2

(setf n (pop open)) ;step3

(push n closed)

(increment-count)

(if (eql n goal-node)

(return (extract-path n)) )

(setf l (successors n)) ;step4

(setf l (list-difference l closed))

(setf open (append l (list-difference open l))) ;step5

(dolist (x l)(set-predecessor x n) )

; end of loop -------- this is implicitly step6

) ) )

Page 4: AI Lab Programs

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

(defun new_line ()   (format t "~%"))(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  (setq atom-list (copy-list output-list))  ;; 1. 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           (setq 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  (reverse atom-list))(defun extract-atoms (input-list)  ; call select-atoms with empty output-list  (select-atoms input-list '()))

;;; *** Main program ***(setq input_list '(a b (c d) (e f (c d b))))(format t "~%input-list = ~A" input_list)(new_line)

Page 5: AI Lab Programs

(setq atom_list (extract-atoms input_list))(format t "~%atom-list = ~A" atom_list)

;;; WAP of a Fibonacci Series in LISP.

(defun fibonacci (N) "Compute the N'th Fibonacci number." (if (or (zerop N) (= N 1)) 1 (+ (fibonacci (- N 1)) (fibonacci (- N 2)))))

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

(defun fibonacci (N) "Compute the N'th Fibonacci number." (if (or (zerop N) (= N 1)) 1 (let((F1 (fibonacci (- N 1)))

(F2 (fibonacci (- N 2)))) (+ F1 F2))))

Page 6: AI Lab Programs

;;;WAP of Best First Search using LISP

(defun hval-of (node) (car node))(defun state-of (node) (cadr node))(defun path-of (node) (cdr node))(defun 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-queue children (car open) (cdr open)))))))

Page 7: AI Lab Programs

(defun combine-queue (new-states node queue) (push (state-of node) *visited*) (dolist (state new-states)

(if (not (member state *visited* :test #'equal)) (push (cons (- (* *heur-mult* (heur-value state)) (depth-of node))

(cons state (cdr node)))queue)))

(sort queue #'> :key #'car))

;;; WAP of A* Search for a shortest path 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) ) )

Page 8: AI Lab Programs

;;;WAP for Water Jug Solution using LISP

(in-package "USER")

(defvar *start* '(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)

(eq (first-jug state) 2))

(defun new-states (state)

(remove-null (list

(fill-first state)

(fill-second state)

(pour-first-second state)

(pour-second-first state)

(empty-first state)

(empty-second state))))

(defun remove-null (x)

Page 9: AI Lab Programs

(cond((null x) nil)

((null (car x)) (remove-null (cdr x)))

((cons (car x) (remove-null (cdr x))))))

(defun fill-first (state)

(cond ((< (first-jug state) 4) (mk-state 4 (second-jug state))))))

(defun fill-second (state)

(cond ((< (second-jug state) 3) (mk-state (first-jug state) 3))))

(defun pour-first-second (state)

(let ( (f (first-jug state))

(s (second-jug state)))

(cond ((zerop f) nil) ; Cant pour nothing

((= s 3) nil) ; Second full

((<= (+ f s) 3) ; Empty first into second

(mk-state 0 (+ f s)))

(t (mk-state (- (+ f s) 3) 3))))) ; Fill second from first

(defun pour-second-first (state)

(let ( (f (first-jug state))

(s (second-jug state)))

(cond ((zerop s) nil) ; Cant pour nothing

((= f 4) nil) ; First full

((<= (+ f s) 4) ; Empty second into first

(mk-state (+ f s) 0))

(t(mk-state 4 (- (+ f s) 4)))))) ; Fill first from second

(defun empty-first (state)

Page 10: AI Lab Programs

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

(defun empty-second (state)

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

;;;WAP to replace a String with another String in LISP.

(defun string-replace (str1 sub1 sub2)  (let ((str1 (string str1))        (str2 "")        (sub1 (string sub1))        (sub2 (string sub2))        (index1 0))    (loop        (if (string-equal str1 sub1                          :start1 index1                          :end1 (min (length str1)                                     (+ index1 (length sub1))))            (progn              (setq str2 (concatenate 'string str2 sub2))              (incf index1 (length sub1)))            (progn              (setq str2 (concatenate 'string                                      str2                                      (subseq str1 index1 (1+ index1))))              (incf index1)))        (unless (< index1 (length str1))          (return str2)))))

Page 11: AI Lab Programs

%%% WAP to implement DFS using PROLOG.

go(Start, Goal) :-empty_stack(Empty_been_list),stack(Start, Empty_been_list, Been_list),path(Start, Goal, Been_list).

% path implements a depth first search in PROLOG

% Current state = goal, print out been listpath(Goal, Goal, Been_list):-

reverse_print_stack(Been_list).

path(State, Goal, Been_list) :-mov(State, Next),% not(unsafe(Next)),not(member_stack(Next, Been_list)),stack(Next, Been_list, New_been_list),path(Next, Goal, New_been_list), !.

reverse_print_stack(S) :-empty_stack(S).

reverse_print_stack(S) :-stack(E, Rest, S),reverse_print_stack(Rest),write(E), nl.

Page 12: AI Lab Programs

%%% WAP to implement BFS using PROLOG.

state_record(State, Parent, [State, Parent]).

go(Start, Goal) :- empty_queue(Empty_open), state_record(Start, nil, State), add_to_queue(State, Empty_open, Open), empty_set(Closed), path(Open, Closed, Goal).

path(Open,_,_) :- empty_queue(Open), write('graph searched, no solution found'). path(Open, Closed, Goal) :- remove_from_queue(Next_record, Open, _), state_record(State, _, Next_record), State = Goal, write('Solution path is: '), nl, printsolution(Next_record, Closed). path(Open, Closed, Goal) :- remove_from_queue(Next_record, Open, Rest_of_open), (bagof(Child, moves(Next_record, Open, Closed, Child), Children);Children = []), add_list_to_queue(Children, Rest_of_open, New_open), add_to_set(Next_record, Closed, New_closed), path(New_open, New_closed, Goal),!.

moves(State_record, Open, Closed, Child_record) :- state_record(State, _, State_record), mov(State, Next), % not (unsafe(Next)), state_record(Next, _, Test),

Page 13: AI Lab Programs

not(member_queue(Test, Open)), not(member_set(Test, Closed)), state_record(Next, State, Child_record).

printsolution(State_record, _):- state_record(State,nil, State_record), write(State), nl.printsolution(State_record, Closed) :- state_record(State, Parent, State_record), state_record(Parent, _, Parent_record), member(Parent_record, Closed), printsolution(Parent_record, Closed), write(State), nl. add_list_to_queue([], Queue, Queue).add_list_to_queue([H|T], Queue, New_queue) :- add_to_queue(H, Queue, Temp_queue), add_list_to_queue(T, Temp_queue, New_queue).