classical problem solving
Download
Skip this Video
Download Presentation
Classical Problem Solving

Loading in 2 Seconds...

play fullscreen
1 / 87

Classical Problem Solving - PowerPoint PPT Presentation


  • 214 Views
  • Uploaded on

Classical Problem Solving. Design of Computer Problem Solvers CS 344 Winter 2000. Last time. AI programming versus conventional programming Emphasis on knowledge representation Overview of systems Homework 0 Preview of homework 1. This week’s themes.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Classical Problem Solving' - Roberta


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
classical problem solving

Classical Problem Solving

Design of Computer

Problem Solvers

CS 344

Winter 2000

last time
Last time
  • AI programming versus conventional programming
    • Emphasis on knowledge representation
  • Overview of systems
  • Homework 0
  • Preview of homework 1
this week s themes
This week’s themes
  • Problem-solvers see the world in terms of problem spaces
  • Describe the problem in its own terms, and let Lisp make up the difference
components of a problem space
Components of a Problem Space
  • States (conditions of the world)
    • Initial state (where we are)
    • Goal state (where we want to be)
  • Operators (actions that can change the current state)
  • Problem solving
    • Construct a path of states, using available operators, from initial to goal state

Problem-solvers

see the world

in terms of

problem spaces

sample problem spaces
Sample Problem Spaces
  • Subway travel
    • Initial state: Davis Street
    • Goal state: Morse Street
    • Operators: Take purple line, take red line
  • Chess
    • Initial state: Board setup
    • Goal state: Checkmate condition
    • Operators: Chess moves

Problem-solvers

see the world

in terms of

problem spaces

how to characterize a problem space
How to characterize a problem space
  • Characterizing states
    • Often symbols or propositional expressions
  • Comparing states
  • Choosing operators
    • Operator applicability (can we use it?)
    • Operator preference (is it the best one?)
  • Search strategy
    • When do we backtrack? Breadth or depth-first? Use means-end analysis?

Problem-solvers

see the world

in terms of

problem spaces

methods for choosing operators
Methods for choosing operators
  • When is an operator applicable?
    • Preconditions
  • Which applicable operator is best?
    • First available
      • May assume that operators written in order of preference (e.g., travel ops ordered by speed)
    • Random
      • Choose one--if it doesn’t work out, try other.
    • Distance metric
      • Pick a metric that tells us which is best

Problem-solvers

see the world

in terms of

problem spaces

why study the classical method of problem solving
Why study the classical method of problem solving?
  • Historically early method in AI
    • GPS (Newell & Simon, 1963)
    • STRIPS (Fikes & Nilsson, 1971)
  • Descendants still viable: SOAR, Deep Blue
  • Provides a vocabulary for discussing problem solving
    • “States”, “operators”, and “problem spaces”

Problem-solvers

see the world

in terms of

problem spaces

classical problem solving9
Classical Problem Solving
  • Separation between the search method and the problem space
    • Search method: generic search methods
    • Problem space: representation of problem as set of states and operators

Problem

Space

Search

Engine

how problem spaces provide modularity and explicitness

Programming Knowledge

Reasoning

Engine

Knowledge of the domain

Computer Program

Representation

of Domain Knowledge

How Problem Spaces provide modularity and explicitness

Search Engine

Problem Space

Representation

basic search methods
Basic search methods
  • Breadth-first
    • Try all applicable operators before choosing secondary operators
  • Depth-first
    • On the operator chosen, continue searching until either goal is reached or search fails
  • Best-first
    • Use a distance metric to choose best operator each time
building cps
Building CPS
  • Need a problem space “API” between a problem space and the search engine
  • Requirements:
    • Way to describe basic characteristics of states and operators in problem space
    • Should provide multiple, interchangable search engines
    • Should work with novel problem spaces

Describe the

problem in

its own terms,

and let Lisp

make up

the difference.

constructing a pluggable classical problem solver
Constructing a “pluggable” classical problem solver
  • What’s a pluggable data structure?
  • Why build a pluggable problem space obj?
    • Search engines, to be general, need a consistent API for the problem space
    • No guarantee that all problem spaces will respect that API
    • Use a pluggable class, called Problem, which mediates between the two and allow an arbitrary problem space to be used with many different search engines.

Describe the

problem in

its own terms,

and let Lisp

make up

the difference.

problem defstruct

Code

PROBLEM Defstruct
  • Each problem object defines a particular problem space
    • E.g., taking subway from MIT to aquarium
  • Requires following functions to define problem space:
    • Goal recognizer
    • State identicality test
    • Print states
    • Applicable operators for given state
    • Distance function

Once we have these, we can create “generic” search engines that will work with any problem space.

using pluggable function slots
Using pluggable function slots

Function description:

PROBLEM slot:

When has goal been found?

Return set of applicable operators

Are states the same?

Remove path from possible paths?

Estimate of distance to goal

Print state

Print solution path segment

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

Code

path object structure

Code

Path object structure
  • Records a particular solution path in terms of intermediate states and operators
  • Has links back to the problem space, so that a path struct can be taken and “extended” easily.
state and operator defstructs
State and Operator defstructs?
  • Where’s the state defstruct?
  • Where’s the op defstruct?
  • CPS code doesn’t define them.
  • Instead
    • Operator and state data structures are implicit in the comparison functions:
      • Operator-applier, states-identical?, goal-recognizer, distance-remaining, state-printer

Describe the

problem in

its own terms,

and let Lisp

make up

the difference.

basic search methods18
Basic search methods
  • Breadth-first
    • Try all applicable operators before choosing secondary operators
  • Depth-first
    • On the operator chosen, continue searching until either goal is reached or search fails
  • Best-first
    • Use a distance metric to choose best operator each time
bsolve function simplified
Bsolve function (simplified)

(defun bsolve (initial &aux curr-path new-paths)

“Apply breadth-first search to problem space.”

(do ((queue (list (list initial initial))

(append (cdr queue) new-paths)))

((null queue))

(setq curr-path (car queue))

(when (goal-recognized? (car curr-path)

(return curr-path))

(setq new-paths (extend-path curr-path))

extend path simplified
Extend-path (simplified)

(defun extend-path (path &aux new-paths new-path pr)

“Extend the given path, and return a list of possible paths+1”

(setq pr (path-pr path))

(dolist (op (pr-operators pr) new-paths)

(dolist (op-pair (apply-op path op pr))

(setq new-path

(make-path

(add-to-path path (cdr op-pair) (car op-pair))))

(unless (path-has-loop? new-path) ;avoid loops

(unless (and (pr-path-filter pr)

(funcall (pr-path-filter pr) new-path))

(push new-path new-paths))))))

extend path simplified21
Extend-path (simplified)

(defun extend-path (path &aux new-paths new-path pr)

“Extend the given path, and return a list of possible paths+1”

(setq pr (path-pr path))

(dolist (op (pr-operators pr) new-paths)

(dolist (op-pair (apply-op path op pr))

(setq new-path

(make-path

(add-to-path path (cdr op-pair) (car op-pair))))

(unless (path-has-loop? new-path) ;avoid loops

(unless (and (pr-path-filter pr)

(funcall (pr-path-filter pr) new-path))

(push new-path new-paths))))))

Apply each op to path’s end state.

extend path simplified22
Extend-path (simplified)

(defun extend-path (path &aux new-paths new-path pr)

“Extend the given path, and return a list of possible paths+1”

(setq pr (path-pr path))

(dolist (op (pr-operators pr) new-paths)

(dolist (op-pair (apply-op path op pr))

(setq new-path

(make-path

(add-to-path path (cdr op-pair) (car op-pair))))

(unless (path-has-loop? new-path) ;avoid loops

(unless (and (pr-path-filter pr)

(funcall (pr-path-filter pr) new-path))

(push new-path new-paths))))))

Apply domain-specific filter.

bsolve function simplified23
Bsolve function (simplified)

(defun bsolve (initial &aux curr-path new-paths)

“Apply breadth-first search to problem space.”

(do ((queue (list (list initial initial))

(append (cdr queue) new-paths)))

((null queue))

(setq curr-path (car queue))

(when (goal-recognized? (car curr-path)

(return curr-path))

(setq new-paths (extend-path curr-path))

slide24

Bsolve function (simplified)

Fifo queue

(defun bsolve (initial &aux curr-path new-paths)

“Apply breadth-first search to problem space.”

(do ((queue (list (list initial initial))

(append (cdr queue) new-paths)))

((null queue))

(setq curr-path (car queue))

(when (goal-recognized? (car curr-path)

(return curr-path))

(setq new-paths (extend-path curr-path))

slide25

Code

Bsolve function (simplified)

Fifo queue

(defun bsolve (initial &aux curr-path new-paths)

“Apply breadth-first search to problem space.”

(do ((queue (list (list initial initial))

(append (cdr queue) new-paths)))

((null queue))

(setq curr-path (car queue))

(when (goal-recognized? (car curr-path)

(return curr-path))

(setq new-paths (extend-path curr-path))

Check current state to see if goal reached:

slide26

Bsolve function (simplified)

Fifo queue

(defun bsolve (initial &aux curr-path new-paths)

“Apply breadth-first search to problem space.”

(do ((queue (list (list initial initial))

(append (cdr queue) new-paths)))

((null queue))

(setq curr-path (car queue))

(when (goal-recognized? (car curr-path)

(return curr-path))

(setq new-paths (extend-path curr-path))

Dsolve

LIFO

(append new-paths (cdr queue)))

the agenda type determines the search characteristics
The Agenda type determines the search characteristics

FIFO == Breadth-first

LIFO == Depth-first

more search variants variants lsp
More search variants (variants.lsp)
  • Best-first search
    • Estimate distance from goal for each solution path
    • Sort queue so that closest solution paths tried first
    • How good must the distance metric be?
  • Beam search
    • Same as best-first search, but limit size of queue to a fixed “beam width”
example 1 subway navigation
Example 1: Subway navigation

Let’s use our search engine to tackle a commonly-used search space: the Boston “T”

building the representation
Building the representation
  • Representation
    • Stations: the lines they are on, x-y map coordinates
    • Lines: the stations on that line
  • Problem space mapping
    • State = station
    • Operator: Take the line to another station
  • Definition mechanism
  • T definitions

Describe the

problem in

its own terms,

and let Lisp

make up

the difference.

stations
Stations

(defstruct (subway-station (:PRINT-FUNCTION subway-station-print-procedure))

"Data structure representating a single subway station."

(name nil) ;; Name of station.

(lines nil) ;; Subways lines it is on.

(coordinates nil)) ;; For advanced CPS versions which use a distance metric.

(defun subway-station-print-procedure (pr str ignore)

"Print name of station."

(declare (ignore ignore))

(format str "<Station ~A>" (subway-station-name pr)))

defining a station
Defining a station

(defvar KENDALL-SQUARE)

(setq KENDALL-SQUARE

(make-subway-station

:name ‘Kendall-square

:lines ‘(RED-LINE)

:coordinates ‘(1 . 0))

(push ‘Kendall-square (subway-line-stations RED-LINE))

(push ‘Kendall-square *stations*)

defining a station33
Defining a station

(defmacro defstation (name lines &optional (x 0) (y 0))

"Define a subway station."

`(progn

(defvar ,name)

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

defining a station34
Defining a station

(defvar KENDALL-SQUARE)

(setq KENDALL-SQUARE

(make-subway-station

:name ‘Kendall-square

:lines ‘(RED-LINE)

:coordinates ‘(1 . 0))

(push ‘Kendall-square (subway-line-stations RED-LINE))

(push ‘Kendall-square *stations*)

(defstation Kendall-Square (Red-Line) 1.0 0.0)

Describe the

problem in

its own terms,

and let Lisp

make up

the difference.

defining a station35
Defining a station

(defstation South-Station (Red-Line) 3.0 -1.0)

(defstation Washington (Red-Line Orange-Line) 2.75 -0.75)

(defstation Kendall-Square (Red-Line) 1.0 0.0)

(defstation Central-Square (Red-Line) -1.0 0.0)

(defstation Harvard-Square (Red-Line) -2.0 1.0)

(defline Red-Line)

(defline Green-Line)

(defline Orange-Line)

(defline Blue-Line)

Describe the

problem in

its own terms,

and let Lisp

make up

the difference.

slide36

Setting up Subway problem

PROBLEM:

?

?

?

?

?

?

?

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

setting up subway problem
Setting up Subway problem

PROBLEM:

Subway-states-identical?

Subway-operator-finder

Subway-states-identical?

Prune-subway-path

Subway-distance

Format

Print-subway-path

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

RUN

next time algebra
Next time: Algebra!

Brady’s example:

log (x+1) + log(x-1) = c.

classical problem solving ii

Classical Problem Solving, II

Design of Computer

Problem Solvers

CS 344

Winter 2000

lessons from last time
Lessons from last time
  • Constructing problem spaces
    • See a problem in terms of a problem space
    • How to construct a “pluggable” problem space
    • Represent the problem in its own terms
  • Search engines
    • Breadth, Depth, and Best-First
  • Fitting the representation to the task
    • DefLine, DefStation.
today solving algebraic expressions
Today: Solving algebraic expressions
  • Algebra as a problem space
    • Alan Bundy’s model for heuristic-based simplification of algebra problems.
  • Building the system
    • Representing states and operators
    • Pattern matching
      • How to build it
      • Making canonical expressions
  • Homework 1
  • Preview of homework 2 (Natural Deduction)
a quick example bundy s challenge
A quick example: Bundy’s challenge

1. loge(x+1) + loge (x-1) = c.

2. loge[(x+1) * (x-1)] = c.

3. loge[x2-1] = c.

4. x2-1=ec.

5. x2 = ec +1.

6. x = +/- sqrt(ec + 1).

algebra as a problem space
Algebra as a Problem Space
  • The problem space is the transformational grammar
    • States: valid sentences within that grammar
    • Operators: transformation operators
  • Solving algebraic expressions
    • 3X = 5X - 2. What is the value for X?
    • Significant expert-novice differences
    • What do expects know that novices don’t?
algebra as a problem space45

Standard Lisp expressions forstates...

…named functionsfor operators (takeequation, returns transformed equation).

Algebra as a Problem Space
  • States:Algebraic equations
  • Initial state:Starting equation.
  • Goal state:Version of equation with X (unknown) on the left.
  • Operators: Transformations of the equation using various algebraic laws.
  • Path:Sets of transformations.
  • Distance metric: ??

Data

structures?

bundy s claim

Note: There’s a distance metric hiding in here that we can use later...

Bundy’s Claim
  • Experts have control knowledge that lets them avoid many false paths
    • This knowledge limits the needed search
  • Transform equations to reduce the depth and frequency of unknown variable.
    • Each transformation either reduces the number of unknowns, or brings them closer together.
    • Three kinds of methods: isolation methods, collection methods, and attraction methods.
    • This is best understood by graphing the equation.
slide47

Isolation methods

  • Reduces the depth of the occurrences of the unknown
  • Examples
    • U - W = Y ®U = Y + W
    • log(U,w) = Y ®U = WY
  • Assumptions
    • U contains x, Y & W do not.
slide48

=

log

-

=

c

e

-

expt

1

expt

1

expt

x

2

e

c

x

2

Isolation methods

Reduce the depth of unknowns.

3. loge[x2-1] = c.

4. x2-1=ec.

Depth=4.

Depth=3.

slide49

Collection methods

  • Reduce the number of occurrences of the unknown
  • Examples
    • UW + UY ® U(W+Y)
    • (U + V)(U - V) ® U2 - V2
  • Assumptions
    • U and V containx, W & Y do not.
slide50

=

log

c

*

e

-

-

expt

1

+

x

2

x

1

x

1

Collection methods

Collect together multiple instances of the unknown:

2. loge[(x+1) * (x-1)] = c.

3. loge[x2-1] = c.

One less X.

slide51

Attraction methods

  • Bring occurrences of the unknown “closer together”
  • Examples:
    • WU + WV® W(U + V)
    • log(U,w) + log(V,w) ® log(UV,w)
  • Assumptions:
    • U and V expressions contain x
    • W expression does not.
slide52

+

log

log

U

W

V

W

log

*

W

U

V

Attraction methods

Bring occurrences of the unknown “closer together”

Distance=4.

1. loge(x+1) + loge (x-1) = c.

2. loge[(x+1) * (x-1)] = c.

Distance=2.

a quick derivation revisited
A quick derivation, revisited.

1. loge(x+1) + loge (x-1) = c.

2. loge[(x+1) * (x-1)] = c.

3. loge[x2-1] = c.

4. x2-1=ec.

5. x2 = ec +1.

6. x = +/- sqrt(ec + 1).

1. Initial

2. Attract log sum.

3. Collect prod diffs

4. Isolate log.

5. Isolate difference.

6. Isolate square

algebra as a problem space54

Standard Lisp expressions forstates...

Isolation methodsCollection methodsAttraction methods

…named functionsfor operators (takeequation, returns transformed equation).

Measure number and depths of unknowns in equation...

Algebra as a problem space
  • States:Algebraic equations
  • Initial state:Starting equation.
  • Goal state:Version of equation with X (unknown) on the left.
  • Operators: Transformations of the equation using various algebraic laws.
  • Path:Sets of transformations.
  • Distance metric: ??

Data

structures?

implementing bundy s idea
Implementing Bundy’s idea
  • States: equations in Lisp form
    • (= (- U W) Y) for U - W = Y.
  • Operators:
    • Functions which perform individual isolation, collection and attraction methods
  • Distance metric:
    • Function that measures depth and frequence of X.
  • Glue:
    • Flexible pattern matcher for operators and simplifier rules
    • Simplifier rules run after each operation
    • Canonicalize expressions
pluggable problem space
“Pluggable” problem space

Function description:

PROBLEM slot:

When has goal been found?

Return set of applicable operators

Are states the same?

Remove path from possible paths?

Estimate of distance to goal

Print state

Print solution path segment

Operator list

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

Operators

pluggable problem space57

Problem space

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

Operators

Algebra domainStates: Lisp expressionOperators: Isolation, collection, & attraction methods

“Pluggable” problem space
  • Search Engine:
  • bsolve
  • dsolve
  • best-solve
setting up algebra problem solver
Setting up Algebra problem solver

PROBLEM:

?

?

?

?

?

?

?

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

setting up algebra problem solver59
Setting up Algebra problem solver

PROBLEM:

Got-algebra-goal?

Find-algebra-operator

Equal

NIL

Algebra-distance

Format

Print-derivative-step

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

Operators

some of these functions are easy
Some of these functions are easy
  • Got-algebra-goal?
  • Find-algebra-operator (misnamed, btw).
  • States-identical = EQUAL
    • But wait, what about:
      • (* (+ x y) z) vs. (* z (+ y x))?
  • This leaves:
    • The operators themselves
    • The pattern matcher
    • The distance metric
got algebra goal
Got algebra goal

(defun occurs-in? (exp1 exp2)

"True if expression 1 is contained somewhere in expression 2."

(cond ((equal exp1 exp2) t)

((null exp2) nil)

((listp exp2)

(or (occurs-in? exp1 (car exp2))

(occurs-in? exp1 (cdr exp2))))))

(defun has-unknown? (exp)

"True if expression contains unknown value."

(occurs-in? \'x exp))

(defun no-unknown? (exp)

"True if expression contains no unknown values."

(not (occurs-in? \'x exp)))

(defun got-algebra-goal? (state)

"Has goal of algebra

problem been met?"

(and (eq (cadr state) \'x) ;; LHS=X

(no-unknown? (rhs state))))

canonicalization
Canonicalization
  • Equations must be canonicalized to allow for better matching of expressions
    • Get rid of trivial cases:
      • (+ x 0) => x.
      • (+ 3 7) => 10.
    • Flatten trees:
      • (+ (x y) z) => (+ x y z).
    • Sort the terms:
      • (+ z x b) => (+ b x z).
building the operators
Building the operators
  • Requires ability to match particular patterns and then transform them.
    • We’ll need a pattern matcher
  • Operators:
    • Isolation, collection & attraction
    • Also a “canonicalization” operator
      • Will canonicalize the expression, if needed.
      • Will throw in a bunch of “trivial” simplifications.
      • Also needs a pattern matcher
creating a pattern matcher
Creating a pattern matcher
  • For matching
    • (? x) : Matches symbol, binding to pattern var X
    • (?? x) : Matches sequence, binding to segment var X
    • (? x test) : Matches single symbol x that also returns true when passed to function test.
  • Matcher returns either a set of bindings or :FAIL
    • (match ‘(+ (? A) (? B) (? C)) ‘(+ 1 2 3)) returns ((c 3) (b 2) (a 1))
  • For simplification
    • subst function for instantiating matched patterns.
    • (:eval (+ (? X) (? Y))) : Matches X and Y, and then adds them and returns that value.
    • :splice act similarly, but splices result into list.
tree walking for dummies
Tree-walking for dummies

Tree-walking: Apply Foo to every element in tree thing.

Function foo-treewalk (thing)

Null thing? Return nil.

Atom thing? Then FooThing.

List thing? Foo-treewalk the car(thing).Foo-treewalk the cdr(thing).

creating a pattern matcher66
Creating a pattern matcher

(defun match (pat dat &optional (dict nil))

"Take a single pattern and data, and return any matches based on the pattern. Dictionary contains binding list."

(cond ((eq dict :FAIL) :FAIL) ;; Propagate lossage

((eq pat dat) dict)

((element-var? pat)

(match-element-var pat dat dict))

((not (consp pat))

(if (equal? pat dat) dict :FAIL))

((segment-var? (car pat))

(match-segment-var pat dat dict))

((not (consp dat)) :FAIL)

(t (match (cdr pat) (cdr dat)

(match (car pat) (car dat) dict)))))

creating a pattern matcher67
Creating a pattern matcher

(defun match-element-var (pat dat dict &aux entry pred)

"Match single element pattern variable to given data, using

the bindings in dictionary. Returns either :FAIL or updated binding dictionary."

(setq entry (lookup-var pat dict))

(cond (entry

(if (equal? (cadr entry) dat) dict :FAIL))

(t (setq pred (var-restriction pat))

(cond ((or (not pred)

(funcall pred dat))

(bind-element-var (var-name pat) dat dict))

(t :FAIL)))))

creating a pattern matcher68
Creating a pattern matcher

(defun match-segment-var (pat dat dict &aux entry rest)

"Given sequence pattern variable, attempt matching. Returns either bindings or :FAIL."

(setq entry (lookup-var (car pat) dict))

(cond (entry ;; check for match

(setq rest

(check-segment dat (segment-beg entry)

(segment-end entry)))

(if (eq rest :FAIL) :FAIL

(match (cdr pat) rest dict)))

(t ;; Search for alternate segment bindings

(try-segment-bindings (car pat) (cdr pat) dat dict))))

uses of the pattern matcher
Uses of the pattern matcher
  • Two uses:
    • Bundy’s method operators
    • Simplifier rules for canonicalization operator
simplifier rules
Simplifier rules
  • Simplifier rules are used by the canonicalization operator
    • Identities: x + 0 = x; x * 1 = x.
    • Canonicalization: Ordering operands for consistant matching
    • Evaluation: computing the function of constants
  • Canonicalization operator runs all the simplifier rules each time it is called.
simplifier rules71
Simplifier rules
  • Rules have simple before/after pattern.

;; Flush degenerate cases

((? op +/*?) (? e)) (? e))

((+ (? zero zero?) (?? e)) (+ (?? e)))

;; Combine numerical constants

(((? op +/*?) (? e1 numberp) (? e2 numberp) (?? e3))

((? op) (:EVAL ((? op) (? e1) (? e2))) (?? e3)))

((- (? e1 numberp) (? e2 numberp)) (:EVAL (- (? e1) (? e2))))

((- (? e1 numberp)) (:EVAL (- (? e1))))

;; Flatten +,*

(((? op +/*?) (?? e1) ((? op) (?? e2) (?? e3)))

((? op) (?? e1) (?? e2) (?? e3)))

;; Canonicalize +,*

(((? op +/*?)

(?? terms,#\'(lambda (terms) (not (sorted? terms #\'alg<)))))

((? op) (:SPLICE (:EVAL (sort (quote (? terms)) #\'alg<)))))

canonicalization operator
Canonicalization operator

TRY-CANONICALIZATION

+-> SIMPLIFY

+-> SIMPLIFY-IT

+-> TRY-MATCHER-RULES

(defun try-matcher-rules (exp rules)

;; Return the original expression by default

(dolist (rule rules exp)

(let ((bindings (match (rule-pattern rule) exp nil)))

(unless (eq bindings :FAIL)

(return-from try-matcher-rules

(substitute-in (rule-result rule)

bindings))))))

setting up algebra problem solver73
Setting up Algebra problem solver

PROBLEM:

Got-algebra-goal?

Find-algebra-operator

Equal

NIL

Algebra-distance

Format

Print-derivative-step

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

Operators

setting up the operators in the problem space
Setting up the operators in the problem space

(defun setup-algebra-problem ()

"Create an generic algebra \'problem space\'."

(make-problem

:NAME \'Algebra

:GOAL-RECOGNIZER \'got-algebra-goal?

:OPERATOR-APPLIER \'find-algebra-operator

:STATE-PRINTER #\'(lambda (f) (format nil "~A" f))

:SOLUTION-ELEMENT-PRINTER #\'print-derivation-step

:STATES-IDENTICAL? \'equal

:DISTANCE-REMAINING \'algebra-distance

:OPERATORS \'((Isolate-Log try-isolate-log)

(Isolate-Sum try-isolate-sum)

(Isolate-Difference try-isolate-difference)

(Isolate-Square try-isolate-square)

(Collect-Product-Difference try-collect-prod-diff)

(Attract-Log-Sum try-attract-log-sum)

(Canonicalize try-canonicalization))))

(defun find-algebra-operator (state operator)

;; Operators take the form (<name> <procedure>)

(funcall (cadr operator) state))

building the rest of the operators
Building the rest of the operators
  • Attempt to match the current expression
  • If it fails, return :FAIL
  • If it succeeds, do the substitution and simplify.
operators
Operators
  • Example:

(defun try-isolate-square (form &aux bindings)

(setq bindings

(match \'(= (sqr (? arg has-unknown?))

(? rhs no-unknown?))

form))

(unless (eq bindings :FAIL)

`(,(cons `(isolate-square ,form)

(simplify (substitute-in `(= (? arg) (sqrt (? rhs)))

bindings))))))

operators77
Operators

Operators

  • Example:

(defun try-isolate-square (form &aux bindings)

(setq bindings

(match \'(= (sqr (? arg has-unknown?))

(? rhs no-unknown?))

form))

(unless (eq bindings :FAIL)

`(,(cons `(isolate-square ,form)

(simplify (substitute-in `(= (? arg) (sqrt (? rhs)))

bindings))))))

Attempt to match X^2 = expression.

operators78
Operators

Operators

  • Example:

(defun try-isolate-square (form &aux bindings)

(setq bindings

(match \'(= (sqr (? arg has-unknown?))

(? rhs no-unknown?))

form))

(unless (eq bindings :FAIL)

`(,(cons `(isolate-square ,form)

(simplify (substitute-in `(= (? arg) (sqrt (? rhs)))

bindings))))))

Attempt to match X^2 = expression.

If success, substitute bindings into new expression

attraction and collection ops
Attraction and Collection Ops

(defun try-collect-prod-diff (form &aux bindings results)

(dolist (ldt (find-least-dominating-terms form) results)

(setq bindings

(match \'(* (+ (? v no-unknown?)

(? u has-unknown?))

(- (? u) (? v)))

ldt))

(unless (eq bindings :FAIL)

(push (cons `(collect-product-sum ,ldt)

(simplify

(subst (substitute-in

`(- (sqr (? U)) (sqr (? V)))

bindings)

ldt form)))

results))))

setting up algebra problem solver80
Setting up Algebra problem solver

PROBLEM:

Got-algebra-goal?

Find-algebra-operator

Equal

NIL

Algebra-distance

Format

Print-derivative-step

Goal-recognizer

Operator-applier

States-identical

Path-filter

Distance-remaining

State-printer

Solution-element-printer

Operators

algebra distance
Algebra-distance

(defun algebra-distance (expr)

"Estimate how close this expression is to solution, return number."

(labels ((sum-tree-depth

(exp depth)

(cond ((null exp) 0)

((eq exp \'X) depth)

((not (listp exp)) 0)

(t (+ (sum-tree-depth (car exp) (1+ depth))

(sum-tree-depth (cdr exp) depth))))))

(+ (sum-tree-depth (lhs expr) 1)

(sum-tree-depth (rhs expr) 1))))

moral for working through a problem space control knowledge is essential
Moral: For working through a problem space, control knowledge is essential
  • Bundy’s insights:
    • It is not enough to know what the rules are
    • Control knowledge needed to know when to apply particular rules
      • Careful analysis of a domain can avoid a lot of search
      • More leverage in domain-specific knowledge than in domain-general search techniques
  • Imposing a problem-space on a problem
  • Represent the problem in its own terms
    • Not done here as much, but in Subway problem.
    • Homework: fix this.
homework 1
Homework 1:

Due: January 21 by midnight!

Build a better algebra system.

Chapter 3, Problem 5 (parts a & b), then extend system to solve 10 problems given on web page.

better operator definitions
Better operator definitions

(defun try-collect-prod-diff (form &aux bindings results)

(dolist (ldt (find-least-dominating-terms form) results)

(setq bindings

(match \'(* (+ (? v no-unknown?)

(? u has-unknown?))

(- (? u) (? v)))

ldt))

(unless (eq bindings :FAIL)

(push (cons `(collect-product-sum ,ldt)

(simplify

(subst (substitute-in

`(- (sqr (? U)) (sqr (? V)))

bindings)

ldt form)))

results))))

  • Before:
  • After:

(defAlgebraOperator collect-prod-diff COLLECTION;;; (U+v)*(U-v) => (U^2 - V^2).

(* (+ (? v no-unknown?)

(? u has-unknown?))

(- (? u) (? v)))

(- (sqr (? U)) (sqr (? V))))

reading for next week
Reading for next week
  • Chapters 4-5 on Pattern-Directed Rule Systems
  • Download code for TRE and FTRE
  • Note:
    • Natural deduction system implemented for FTRE, but not TRE.
homework 2 preview
Homework 2 Preview

Extend the Natural Deduction system to use premises and goals.

BPS, pp. 148-149:

  • Exercise 17, (a) and (b)
  • Exercise 18, (a), (b), and (c)

Hints:

  • Start early if possible.
  • Run the ND examples with the debug flag on, and contemplate the output carefully before modifying the rule set.
  • Building a driver loop that automates the testing process (and turns the debug flag on and off cleverly) will make your life easier.

Due: January 28 by midnight!

ad