SlideShare a Scribd company logo
1 of 6
Download to read offline
Cse section A
Lab-9
Write a program to solve water jug problem using LISP.
Program:
;returns the quantity in first jug (defun
get-first-jug (state) (car state))
;returns the quantity in second jug
(defun get-second-jug (state) (cadr state))
;returns the state of two jugs (defun
get-state (f s) (list f s)) (defun dfs
(start-state depth lmt))
;checks whether a given state is a goal
; GOAL IS TO GET 4 IN SECOND JUG
(defun is-goal (state)
(eq (get-second-jug state) 4))
;returns all possible states that can be derived
;from a given state (defun
child-states (state)
(remove-null
(list
(fill-first-jug state)
(fill-second-jug state)
(pour-first-second state)
(pour-second-first state)
(empty-first-jug state)
(empty-second-jug state))))
;remove the null states (defun
remove-null (x) (cond ((null x)
nil)
((null (car x)) (remove-null (cdr x)))
((cons (car x) (remove-null (cdr x))))))
;return the state when the first jug is filled(first jug can hold 3) (defun
fill-first-jug (state)
(cond
((< (get-first-jug state) 3) (get-state 3 (get-second-jug state)))))
;returns the state when the second jug is filled (second jug can hold 5) (defun
fill-second-jug (state)
(cond
((< (get-second-jug state) 5) (get-state (get-first-jug state) 5))))
;returns the state when quantity in first
;is poured to second jug (defun
pour-first-second (state)
(let ( (f (get-first-jug state)) (s (get-second-jug state))) (cond
((zerop f) nil) ; first jug is empty ((= s 5) nil)
; Second jug is full ((<= (+ f s) 5)
(get-state 0 (+ f s)))
(t ; pour to first from second
(get-state (- (+ f s) 5) 5))))
;returns the state when second jug is poured to first
(defun pour-second-first (state)
(let ( (f (get-first-jug state)) (s (get-second-jug state))) (cond
((zerop s) nil) ; second jug is empty ((= f
3) nil) ; second jug is full
((<= (+ f s) 3)
(get-state (+ f s) 0))
(t ;pour to second from first
(get-state 3 (- (+ f s) 3))))))
;returns the state when first jug is emptied
(defun empty-first-jug (state)
(cond
((> (get-first-jug state) 0) (get-state 0 (get-second-jug state)))))
;returns the state when second jug is emptied (defun
empty-second-jug (state)
(cond
((> (get-second-jug state) 0) (get-state (get-first-jug state) 0))))
;;;MAIN FUNCTION
(defun dfs (start-state depth lmt) (setf
*node* 0) (setf *limit* lmt)
(dfs-node start-state depth))
;dfs-node expands a node and calls dfs-children to recurse on it
(defun dfs-node (state depth)
(setf *node* (+ 1 *node*))
(cond
((is-goal state) (list state)) ((zerop depth) nil) ((>
*node* *limit*) nil)
((let ((goal-child (dfs-children (child-states state) (- depth 1))))
(and goal-child (cons state goal-child)))))) ;for back-tracking if the branch don't
have a goal state
;dfs-children expands each node recursively and give it
;to dfs-node to process
(defun dfs-children (states depth)
(cond
((null states) nil)
((dfs-node (car states) depth))
((dfs-children (cdr states) depth))))
;(princ (dfs))
(print "ENTER YOUR INPUT AS") (print "(dfs start_state depth limit)")
Water jug

More Related Content

What's hot

Introduction to the lambda calculus
Introduction to the lambda calculusIntroduction to the lambda calculus
Introduction to the lambda calculusJack Fox
 
Zend VMにおける例外の実装
Zend VMにおける例外の実装Zend VMにおける例外の実装
Zend VMにおける例外の実装Yoshio Hanawa
 
20170127 tokyoserversideswiftmeetup資料
20170127 tokyoserversideswiftmeetup資料20170127 tokyoserversideswiftmeetup資料
20170127 tokyoserversideswiftmeetup資料jugemjugemjugem
 
Ejercicios Scilab Completo
Ejercicios Scilab CompletoEjercicios Scilab Completo
Ejercicios Scilab CompletoRicardo Grandas
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานknang
 
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)jon_bell
 

What's hot (11)

Introduction to the lambda calculus
Introduction to the lambda calculusIntroduction to the lambda calculus
Introduction to the lambda calculus
 
SLIME
SLIMESLIME
SLIME
 
Taller De Scilab
Taller De ScilabTaller De Scilab
Taller De Scilab
 
Zend VMにおける例外の実装
Zend VMにおける例外の実装Zend VMにおける例外の実装
Zend VMにおける例外の実装
 
20170127 tokyoserversideswiftmeetup資料
20170127 tokyoserversideswiftmeetup資料20170127 tokyoserversideswiftmeetup資料
20170127 tokyoserversideswiftmeetup資料
 
Ejercicios Scilab Completo
Ejercicios Scilab CompletoEjercicios Scilab Completo
Ejercicios Scilab Completo
 
Periodic pattern mining
Periodic pattern miningPeriodic pattern mining
Periodic pattern mining
 
Trabajo Scilab
Trabajo ScilabTrabajo Scilab
Trabajo Scilab
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐาน
 
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
CROCHET - Checkpoint Rollback in JVM (ECOOP 2018)
 
Scope and closures
Scope and closuresScope and closures
Scope and closures
 

Recently uploaded

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 

Recently uploaded (20)

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 

Water jug

  • 1. Cse section A Lab-9 Write a program to solve water jug problem using LISP. Program: ;returns the quantity in first jug (defun get-first-jug (state) (car state)) ;returns the quantity in second jug (defun get-second-jug (state) (cadr state)) ;returns the state of two jugs (defun get-state (f s) (list f s)) (defun dfs (start-state depth lmt)) ;checks whether a given state is a goal ; GOAL IS TO GET 4 IN SECOND JUG (defun is-goal (state) (eq (get-second-jug state) 4)) ;returns all possible states that can be derived ;from a given state (defun child-states (state) (remove-null (list
  • 2. (fill-first-jug state) (fill-second-jug state) (pour-first-second state) (pour-second-first state) (empty-first-jug state) (empty-second-jug state)))) ;remove the null states (defun remove-null (x) (cond ((null x) nil) ((null (car x)) (remove-null (cdr x))) ((cons (car x) (remove-null (cdr x)))))) ;return the state when the first jug is filled(first jug can hold 3) (defun fill-first-jug (state) (cond ((< (get-first-jug state) 3) (get-state 3 (get-second-jug state))))) ;returns the state when the second jug is filled (second jug can hold 5) (defun fill-second-jug (state) (cond ((< (get-second-jug state) 5) (get-state (get-first-jug state) 5))))
  • 3. ;returns the state when quantity in first ;is poured to second jug (defun pour-first-second (state) (let ( (f (get-first-jug state)) (s (get-second-jug state))) (cond ((zerop f) nil) ; first jug is empty ((= s 5) nil) ; Second jug is full ((<= (+ f s) 5) (get-state 0 (+ f s))) (t ; pour to first from second (get-state (- (+ f s) 5) 5)))) ;returns the state when second jug is poured to first (defun pour-second-first (state) (let ( (f (get-first-jug state)) (s (get-second-jug state))) (cond ((zerop s) nil) ; second jug is empty ((= f 3) nil) ; second jug is full ((<= (+ f s) 3) (get-state (+ f s) 0)) (t ;pour to second from first (get-state 3 (- (+ f s) 3)))))) ;returns the state when first jug is emptied (defun empty-first-jug (state)
  • 4. (cond ((> (get-first-jug state) 0) (get-state 0 (get-second-jug state))))) ;returns the state when second jug is emptied (defun empty-second-jug (state) (cond ((> (get-second-jug state) 0) (get-state (get-first-jug state) 0)))) ;;;MAIN FUNCTION (defun dfs (start-state depth lmt) (setf *node* 0) (setf *limit* lmt) (dfs-node start-state depth)) ;dfs-node expands a node and calls dfs-children to recurse on it (defun dfs-node (state depth) (setf *node* (+ 1 *node*)) (cond ((is-goal state) (list state)) ((zerop depth) nil) ((> *node* *limit*) nil) ((let ((goal-child (dfs-children (child-states state) (- depth 1)))) (and goal-child (cons state goal-child)))))) ;for back-tracking if the branch don't have a goal state
  • 5. ;dfs-children expands each node recursively and give it ;to dfs-node to process (defun dfs-children (states depth) (cond ((null states) nil) ((dfs-node (car states) depth)) ((dfs-children (cdr states) depth)))) ;(princ (dfs)) (print "ENTER YOUR INPUT AS") (print "(dfs start_state depth limit)")