Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem Solver has 2 basic components:  Search engine (uses.

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
Advertisements

CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
A problem with functions: arguments must always have values that can be worked out Whenever we call a function we give it arguments. Lisp then works out.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Project 1: Background Informed search : 8- puzzle world BFS, DFS and A* algorithms Heuristics : Manhattan distance, Number of misplaced tiles Lisp programming.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
Returning values from functions You can return a value from a function by using the built- in function : ( return-from Function_name value) For example:
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PRACTICAL COMMON LISP Peter Seibel 1.
Introduction to C Programming
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
Genetic Programming.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
ISBN Chapter 15 Functional Programming Languages.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Another CPS example: mathematical reasoning The problem: given a mathematical equation, find its solution. Problem space: nodes represent mathematical.
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Problem Solving in Common Lisp Common Lisp J.E. Spragg May 1997.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 COSC generating functions, templates, and macros Yves Lespérance Adapted from Peter Roosen-Runge.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
Search as a problem solving technique. Consider an AI program that is capable of formulating a desired goal based on the analysis of the current world.
1.4 Solving Absolute Value Equations Evaluate and solve Absolute Value problems.
Macros Forms that the compiler expands into code ● Decide if the macro is really necessary ● Write down the syntax of the macro ● Figure out what the macro.
Functional Programming Languages
Section 15.4, 15.6 plus other materials
Defining Macros in Lisp
Example of formula (defun roots (a b c) (list
Getting Started with Lisp
J.E. Spragg Mitthögskolan 1997
Bindings, Scope, and Extent
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
Defining Macros in Lisp
Defining Functions with DEFUN
Abstraction and Repetition
Lisp: Using Functions as Data
Bindings, Scope, and Extent
Bindings, Scope, and Extent
Common Lisp II.
Chapter 15 Functional Programming 6/1/2019.
The general format of case is the following: (case <key form>
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
LISP primitives on sequences
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem Solver has 2 basic components:  Search engine (uses breadth-first, depth-first, best-first, etc. general purpose search through a specified problem space)  Interface for user-supplied problem spaces, which includes procedures for manipulating states and procedures for manipulating operators. Procedures for manipulating states perform the following operations: 1.Check if the goal criterion is satisfied in a given state. 2.Check if two states are the same. 3.Print out a state. Procedures for manipulating operators perform the following operations: 1.Define available operators. 2.Decide if a given operator is applicable to a given state, and if yes – generate the resulting state.

Problem specification To define a problem space, we must create a representation for states and operators; to define a particular problem in this problem space, we must define the initial state and the goal criterion. Let our representation include only the goal criterion, but not the initial state. Then, the following structure can be used to represent a problem: (defstruct name ; identified with the goal (goal-recognizer nil) (states-identical? nil) ; fields describing problem states (state-printer nil) (operators nil) ; fields describing operators (operator-applier nil) (path-filter nil) ; fields containing search related information (distance-remaining nil)) Another data type, path, is used to represent queue elements: (defstruct (problem nil) (current nil) (so-far nil) (distance nil))

To set up a specific problem, we must define the fields of the problem structure with a specific problem in mind. Consider the Boston subway problem (book, pages 35 – 39) Problems are defined by the following function, make-problem: (make-problem :NAME goal-state :GOAL-RECOGNIZER #'(lambda (state) (subway-states-identical? state goal-state)) :OPERATOR-APPLIER 'subway-operator-finder :OPERATORS '(TAKE-LINE) :STATES-IDENTICAL? 'subway-states-identical? :PATH-FILTER 'prune-subway-path? :STATE-PRINTER #'(lambda (f) (format nil "~A" f)) :SOLUTION-ELEMENT-PRINTER 'print-path-element :DISTANCE-REMAINING #'(lambda (state) (subway-distance state `(,goal-state))))

The back quote ` mechanism The normal quote isolates an entire expression from evaluation, while the back quote is not so absolute. Whenever a comma appears inside a back quoted expression, the sub-expression immediately following the comma is replaced by its value. Example: * (setf variable 'test) TEST * `(this is a,variable) (THIS IS A TEST) * (setf variable '(another example)) (ANOTHER EXAMPLE) * `(this is a,variable) ; because the value of variable is a list, (another example) (THIS IS A (ANOTHER EXAMPLE)) ; it is inserted as a list * `(this is ; to remove parentheses, use directive. (THIS IS A ANOTHER EXAMPLE)

Macros in Lisp Macro procedures do not evaluate their arguments; evaluation of the body of a macro procedure produces a form, which is then evaluated to produce a value. Initial macro Lisp form Value of the lisp form (in a transparent form) The general format of a macro procedure is the following: (defmacro ( … ) … )

Example Consider the following function which prints “Alarm” when a variable, pressure, becomes greater than zero. * (defun when-plusp (number result) (when (plusp number) result)) WHEN-PLUSP * (setf pressure -3) -3 * (when-plusp pressure (print 'Alarm)) ALARM ; side effect producing incorrect value NIL However, the following form if used alone, will produce the correct result: * (when (plusp pressure) result) ; assume that result is already bound to (print ‘alarm) NIL We can achieve this result by defining when-plusp as a macro.

Example (cont.) * (defmacro when-plusp (number result) (list 'when (list 'plusp number) result)) WHEN-PLUSP * (when-plusp pressure (print 'Alarm)) NIL Macros’ mechanism exploits the fact that Lisp forms are expressions meant to be evaluated; therefore Lisp forms can be constructed with the same primitives that manipulate Lisp expressions.

The labels primitive To define one or more procedures inside another procedure, we can use the labels primitive. Its general form is the following: (labels (( )... ( ) Embedded procedures are defined only within the labels’ body, which is why they are said to be lexically bound.

Examples: * (defun first-of-first (list) (labels ((procedure1 (list) (first list))) ; procedure1 is defined here (first (procedure1 list)))) ; labels body uses procedure1 FIRST-OF-FIRST * (first-of-first '((a b) (c d))) A * (defun inside (x e) (labels ((inside-aux (e) (cond ((atom e) (eq x e)) ((endp e) nil) (t (or (inside-aux (first e)) (inside-aux (rest e))))))) (inside-aux e))) INSIDE * (inside 'a '(s w (d f g (a b c)))) T The difference between the labels primitive and the let primitive is that let defines parameter values, while the labels primitive defines procedures. In the examples above, procedure1 and inside-aux are defined within a labels form and used in a labels' body (but they are undefined outside a labels form).

The Boston subway example continued To represent a graph defining a subway (see book, fig.3.3), we must define stations and lines. In subways.lsp, stations are represented as structures, called subway-station, and stored in *stations*. Lines are also represented as structures, called subway-line, and stored in *lines*. (defstruct (subway-station (:PRINT-FUNCTION (lambda (inst str ignore) (format str " " (subway-station-name inst))))) (name nil) (lines nil) (coordinates nil)) ;; For advanced versions of CPS (defstruct (subway-line (:PRINT-FUNCTION (lambda (inst str ignore) (format str " " (subway-line-name inst))))) (name nil) (stations nil))

The Boston subway example (contd.) The macros, defline and defstation, allow the user to define a particular subway instance. (defmacro defline (line-name) `(progn (setq,line-name (make-subway-line :NAME ',line-name)) (push ',line-name *lines*))) (defmacro defstation (name lines &optional (x 0) (y 0)) `(progn (setq,name (make-subway-station :NAME ',name :LINES ',lines :COORDINATES (mapcar #'(lambda (line) `(push ',name (subway-line-stations,line))) lines) (push ',name *stations*)))

The Boston subway example (contd.) A subway instance is defined in boston.lsp (see code). Now, to solve a particular subway problem, we must first define it and then run a search engine to solve it. Example: * (setup-subway-problem 'harvard-square) ; Harvard Square is where we want to go * (bsolve 'boston-u (setup-subway-problem 'harvard-square)) ; Go from Boston University to Harvard Sq CPS: State explored: BOSTON-U CPS: New operator instances: ; The only operator is TAKE-LINE, but it (TAKE-LINE BOSTON-U GREEN-LINE HAYMARKET) ; can be instantiated in different ways. (TAKE-LINE BOSTON-U GREEN-LINE NORTH-STATION) (TAKE-LINE BOSTON-U GREEN-LINE COPLEY-SQUARE) (TAKE-LINE BOSTON-U GREEN-LINE GOVERNMENT-CENTER) (TAKE-LINE BOSTON-U GREEN-LINE PARK-STREET). CPS: State explored: HAYMARKET CPS: New operator instances: (TAKE-LINE HAYMARKET ORANGE-LINE WASHINGTON) (TAKE-LINE HAYMARKET ORANGE-LINE NORTH-STATION) (TAKE-LINE HAYMARKET ORANGE-LINE STATE). CPS: State explored: NORTH-STATION CPS: New operator instances: (TAKE-LINE NORTH-STATION ORANGE-LINE WASHINGTON) (TAKE-LINE NORTH-STATION ORANGE-LINE HAYMARKET) (TAKE-LINE NORTH-STATION ORANGE-LINE STATE).

CPS: State explored: COPLEY-SQUARE CPS: New operator instances:. CPS: State explored: GOVERNMENT-CENTER CPS: New operator instances: (TAKE-LINE GOVERNMENT-CENTER BLUE-LINE STATE) (TAKE-LINE GOVERNMENT-CENTER BLUE-LINE WOOD-ISLAND) (TAKE-LINE GOVERNMENT-CENTER BLUE-LINE AQUARIUM) (TAKE-LINE GOVERNMENT-CENTER BLUE-LINE AIRPORT). CPS: State explored: PARK-STREET CPS: New operator instances: (TAKE-LINE PARK-STREET RED-LINE HARVARD-SQUARE) (TAKE-LINE PARK-STREET RED-LINE CENTRAL-SQUARE) (TAKE-LINE PARK-STREET RED-LINE KENDALL-SQUARE) (TAKE-LINE PARK-STREET RED-LINE WASHINGTON) (TAKE-LINE PARK-STREET RED-LINE SOUTH-STATION). CPS: State explored: WASHINGTON CPS: New operator instances: (TAKE-LINE WASHINGTON RED-LINE HARVARD-SQUARE) (TAKE-LINE WASHINGTON RED-LINE CENTRAL-SQUARE) (TAKE-LINE WASHINGTON RED-LINE KENDALL-SQUARE) (TAKE-LINE WASHINGTON RED-LINE SOUTH-STATION) (TAKE-LINE WASHINGTON RED-LINE PARK-STREET). CPS: State explored: NORTH-STATION CPS: New operator instances: (TAKE-LINE NORTH-STATION GREEN-LINE COPLEY-SQUARE) (TAKE-LINE NORTH-STATION GREEN-LINE GOVERNMENT- CENTER) (TAKE-LINE NORTH-STATION GREEN-LINE PARK-STREET). CPS: State explored: STATE CPS: New operator instances: (TAKE-LINE STATE BLUE-LINE GOVERNMENT-CENTER) (TAKE-LINE STATE BLUE-LINE WOOD-ISLAND) (TAKE-LINE STATE BLUE-LINE AQUARIUM) (TAKE-LINE STATE BLUE-LINE AIRPORT). CPS: State explored: WASHINGTON CPS: New operator instances: (TAKE-LINE WASHINGTON RED-LINE HARVARD-SQUARE) (TAKE-LINE WASHINGTON RED-LINE CENTRAL-SQUARE) (TAKE-LINE WASHINGTON RED-LINE KENDALL-SQUARE) (TAKE-LINE WASHINGTON RED-LINE SOUTH-STATION) (TAKE-LINE WASHINGTON RED-LINE PARK-STREET). CPS: State explored: HAYMARKET CPS: New operator instances: (TAKE-LINE HAYMARKET GREEN-LINE COPLEY-SQUARE) (TAKE-LINE HAYMARKET GREEN-LINE GOVERNMENT-CENTER) (TAKE-LINE HAYMARKET GREEN-LINE PARK-STREET). CPS: State explored: STATE CPS: New operator instances: (TAKE-LINE STATE BLUE-LINE GOVERNMENT-CENTER) (TAKE-LINE STATE BLUE-LINE WOOD-ISLAND) (TAKE-LINE STATE BLUE-LINE AQUARIUM) (TAKE-LINE STATE BLUE-LINE AIRPORT). CPS: State explored: STATE CPS: New operator instances: (TAKE-LINE STATE ORANGE-LINE WASHINGTON) (TAKE-LINE STATE ORANGE-LINE HAYMARKET) (TAKE-LINE STATE ORANGE-LINE NORTH-STATION). CPS: State explored: WOOD-ISLAND CPS: New operator instances:. CPS: State explored: AQUARIUM CPS: New operator instances:. CPS: State explored: AIRPORT CPS: New operator instances:. CPS: Found goal state: HARVARD-SQUARE 17