Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem...

13
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.

Transcript of Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem...

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

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.

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

Problem specificationTo 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 <problem> 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 <path> (problem nil) (current nil) (so-far nil) (distance nil))

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

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

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

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 a ,@variable) ; to remove parentheses, use the @ directive.

(THIS IS A ANOTHER EXAMPLE)

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

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 <macro name> (<parameter 1> … <parameter k>)

<form1>

<form 2>

<form N> )

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

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.

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

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.

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

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

((<first embedded procedure’s name>

<first embedded procedure’s parameters>

<first embedded procedure’s body>)

. . .

(<n-th embedded procedure’s name>

<n-th embedded procedure’s parameters>

<n-th embedded procedure’s body>)

<body of labels> )

Embedded procedures are defined only within the labels’ body, which is why

they are said to be lexically bound.

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

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

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

The Boston subway example continued

To represent a graph defining a subway (see book, fig.3.3) , we must definestations 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 "<Station ~A>"

(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 ~A>"

(subway-line-name inst))))) (name nil) (stations nil))

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

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 (cons ,x ,y)))

,@ (mapcar #'(lambda (line)

`(push ',name (subway-line-stations ,line))) lines)

(push ',name *stations*)))

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

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<Problem: HARVARD-SQUARE> * (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).

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

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<path HARVARD-SQUARE> 17