artificial intelligence ies 503 asst. prof. dr. senem kumova metin

57
Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Upload: rosa-king

Post on 13-Jan-2016

228 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Artificial IntelligenceIES 503

Asst. Prof. Dr. Senem Kumova Metin

Page 2: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Artificial Intelligence – IES 503• Instructor : Asst. Prof. Dr. Senem Kumova Metin• E mail : [email protected]• Lectures : Mondays 18:30—21:30

• Course web page: http://homes.ieu.edu.tr/skumova – Up to date information– Relevant dates, links, etc.

• Course material:– [AIMA] Artificial Intelligence: A Modern Approach, by

Stuart Russell and Peter Norvig. (2nd ed)

Page 3: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Lisp : Functional High Level Language• LISP(LISt Processing language) is a functional language:

compute by evaluating nested functional expressions.

• An AI language developed in 1958 (J. McCarthy at MIT)

• Special focus on symbolic processing and symbol manipulation– Linked list structures– Also programs, functions are represented as lists

• LISP today:– Many AI programs now are written in C,C++, Java– List manipulation libraries are available

• Using and Dowloading CLISP ttp:::p opl : s:m ssour : u: ~s:u:h e eci i edb

m::::: l sp: tmic c i h

Page 4: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Lisp : Functional High Level Language

• The main characteristic of LISP is its capability for symbolic computation.

• Symbols (atoms) are the principal data type.• The operations that can be performed on

symbols include equality testing and building into symbol structures.

• Putting two symbols together creates a structure, which can then be accessed and taken apart.

Page 5: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Lisp Language

• LISP Competitors:– Prolog– Python

• Current LISP: The most widely-known general-purpose Lisp dialects– Common Lisp– Scheme

• Common LISP:– Interpreter and compiler– CLOS: object oriented programming

Page 6: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Typical applications of Lisp

• Language processing, – using words as symbols; – using lists for sentences, – using trees for grammatical structure.

• Mathematics, involving expressions and equations; trees for expressions.

Page 7: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

1. Lisp is interactive

• There is an interpreter that evaluates inputs. An input is processes in 3 steps:1. Reads input and construct expression from the input.

2. Evaluates the expression for meaning.3. Prints the results of the evaluation, including signaling of

errors if necessary.

• These 3 steps can be customized by the programmer.

Page 8: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

2. Lisp is a dynamic language

• Programs are developed incrementally, by making small changes to the source code.

• Interpreter evaluates the changed definitions and then immediately run the results.

• New definitions and data structures can be added at any time.

• This features are ideal for prototyping.

Page 9: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

3. Lisp has symbols

• Symbols are the basic type of data in use.• Symbols are used to build bigger, more

complex expressions.• Example of symbols:

– HELLO – 23-worldofsports

Page 10: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

4. LISP has lists

• Lists are delimited using parenthesis (…). Anything can be placed in a list, including other lists (nested lists). For example:(1 orange 2 3)

(once (upon a) time)

• Empty list is represented as ()• Caution: elements within a list are separated with a

white space, and NOT a comma ,

Page 11: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

5. Lisp classifies data

• It does not classify variables. • A variable is just a symbol. It can hold any type of

value. • A variable do not have to be declared before it is

used. • Lisp defines different types of data (rather than

defining different types of variables)

Page 12: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

5. Lisp classifies data - continues

Lisp Expression

number symbol sequence

integer

float

ratio

keyword

list vector

string

Page 13: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

5. Lisp classifies data - continues

• Integer - a counting number like 1, 2 ,3 …100, -23• float - real number. Example 1.59, -100.3• ratio - a fraction, example 99/23, 4/5• symbol - a sequence of alphanumeric characters, eg: BOO,

ID4…• keyword - a symbol prefixed with a colon. Eg- :BOO, :ID4• list - a collection of zero or more expressions inside (..)• vector - 1 dimensional collection of expressions in sequential

memory• string - a vector of 0 or more characters inside “ ”

Page 14: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

6. Lisp uses prefix notation

• Operators appear in front of their operands.• The infix (10 + 3) is written as (+ 10 3)

Page 15: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

7. Lisp is functional

• Lisp functions take data, operates on it, and return the results. The returned results are called function values.

• Functions can return any number of values.

• To call a function, place it as the first element of an input list, followed by its operands. Example:(+ 100 99 88)

(setf x (+ 2 3))

• All operations are done through functions

Page 16: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

8. Programs and data

• Lisp makes no distinction between programs and data. • A program can be treated as a set of instruction or as a list of

symbols. • This makes it possible to write programs that generate

another program, or programs that analyze other programs.

Page 17: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

9. Lisp evaluation is easy to understand

• Rule 1 : If an expression is a constant, the interpreter will return the value of the constant. No more rules apply.

Examples: ‘socrates, 4.5

• Rule 2:If Rule 1 is not applicable, and the expression is a symbol, then Lisp treats the symbol as a variable and no more rules are considered. If the variable has a value, Lisp will return that value. Otherwise it will report an error.

Page 18: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

9. Lisp evaluation is easy to understand

• Rule 3: If Rule 1 and Rule 2 do not apply and the expression is a LIST,then Lisp treats it as a function call and no more rules are considered.

– You should at this point remember that the first element in the list is assumed to be a defined a function. The remaining elements are data. Each expression in the data is evaluated left to right.

• Rule 4:If Rules 1 to 3 do not apply, then there is an error!

Page 19: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Data Structures

S-Expression - Symbolic expression. It can be an Atom, a List or a collection of S-Expression enclosed by (…)

Atom - String of characters beginning with a letter, digit. Eg: Artificial, intelligence, 31416..etc.

Page 20: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Data Structures

List - a collection of S-Expression enclosed by ( …. ). Eg:(One of these days)(one ((two) three) four)

Page 21: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Dealing with variables(1)

Explicit evaluation call is possible !! > (setq a 12) ;; setq means SET QUOTE

a=1212> (setq b (+ a 4)) ;; b=a+4 16> (setq b ‘(+ a 4)) ;; b=(+ a 4)(+ a 4)> (eval b) ;; explicit evaluation call16

Page 22: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Dealing with variables(2)• Sometimes we want to pass an argument directly,

without evaluation. To do this we need an identity function QUOTE serves this purpose.

>(+ 2 3)

5

>(quote (+ 2 3))

(+ 2 3)

>‘( + 2 3)

(+ 2 3)

(+ '4 '4) returns 8 but (+ '(+ 2 2) '(+ 1 3)) returns what?

Page 23: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Dealing with variables(3)

> (setf a 12) ;; setf means SET FIELD a=1212> (setf b (+ a 4)) ;; b=a+4 16> (setf b ‘(+ a 4)) ;; b=(+ a 4)(+ a 4)> (eval b) ;; explicit evaluation call16------(setf (rest list) nil ) ;; list.rest=nil(setf (aref A i j) 0) ;; A[i][j]=0;

Page 24: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Dealing with variables(3)

• Special symbols:

> t ;; true

T

> nil ;; nil stands for false or

NIL

> ( ) ;; an empty list

NIL

Page 25: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Dealing with variables(4)• the let expression is a special form in lisp that you will need to

use in most function definitions.• let is used to attach or bind a symbol to a value in such a way

that the lisp interpreter will not confuse the variable with a variable of the same name that is not part of the function.

>( setq x ‘(+ 3 4))(+ 3 4)>(let (( x 40) (y (+ 1 3))) (+ x y ))44

> x(+ 3 4)

Page 26: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Operations on list structure• List structure is made by putting symbols together. The function that

puts things together is CONS.

> (cons (cons 'a 'b) 'c) > ((a . b) . c)

• These are s-expressions and two s-expressions CONSed together make an s-expression.

• Tree data structure representing the s-expression for  (* 2 (+ 3 4))

Page 27: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Operations on list structure• Lists are special kinds of s-expressions whose rightmost element is

the special atom nil.

> (cons 'a (cons 'b nil))> (a b)

• It's a list if CDRing down it eventually returns nil. 

CDR returns the elements following the first element

>(cdr (cons 'a (cons 'b nil))) B

>(cdr (cdr (cons 'a (cons 'b nil)))) NIL

A B nil

Page 28: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Operations on list structure• Lists can be constructed using list as well as cons. • The functions first and rest are the same as car and cdr but more

readable:

> (setq l (list 'a 'b '(c d)))

(a b (c d))

> (first l)

a

> (rest l)

(b (c d))

> (second l)

b

Page 29: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Operations on list structure

• Some useful functions and predicates:> (setq a ‘(1 2 3 4 5))(1 2 3 4 5)> (length a) ;; gives the list length of the argument5> (atom ‘a) ;; checks if the argument is an atomT> (atom a)NIL> (listp ‘a) ;; checks if the argument is a listNIL> (listp a)T

Page 30: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Defining Structures (1)(defstruct structure name “optional documentation” slot….)>(defstruct person name age sex) ;; structure definition

You can create a new person by calling make-person, which takes keyword arguments :name, :age, and :sex to specify the initial values of these slots in the new object.

>(setq dave (make-person :name "Dave" :sex "male"))>(person-name dave) ;; Dave>(person-age dave) ;; NIL

Page 31: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Defining Structures (2)

>(setq dave (make-person :name "Dave" :sex "male"))>(person-name dave) ;; Dave>(person-age dave) ;; NIL>dave#S(PERSON :NAME "Dave" :AGE NIL :SEX "male")

>(setf (person-age dave) 27) ;; 27>dave#S(PERSON :NAME "Dave" :AGE 27 :SEX "male")

Page 32: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Function definition (1)

Definition of a function(defun <f-name> <parameter-list> <body>)

>(defun square (x)(* x x))SQUARE>(square 2)4>(square (square 2))16

Page 33: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Function definition (2)(defun <f-name> <parameter-list> <body>)<body> can be a sequence of function calls, the function

returns the value of the last call in the sequence

> (defun foo (a)(setq b (+ a 1))(setq c (+ a 2))c)

FOO> (foo 2)4

Page 34: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Conditional statements (1)• Cond statement: sequentially tests conditions, the call

associated with the first true condition is executed

> (defun abs (a)(cond ((> a 0) a)(t (* a -1))))ABS> (abs 2)2> (abs -3)3

> (defun func (a)(cond ((< a 10) 1) ((< a 50) 2) ((< a 100)

3)))FUNC>(func 40) 2>(func 60) 3

Page 35: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Conditional statements (2)if statement:(if <test> <then> <else>)

> (defun abs (a)(if( > a 0) a (* a -1)))ABS> (abs 5)5> (abs -5)5

Page 36: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Lisp: Equality operators4 equality predicates: =, equal, eq, eql

> (= 2 4/2) ;; used for numerical values onlyT> (setf a '(1 2 3 4))(1 2 3 4)>(setf b '(1 2 3 4))(1 2 3 4)>(setf c b)(1 2 3 4)> (equal a b) ;; equal is true if the two objects are isomorphicT> (equal c b)T

Page 37: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

LISP : Recursion

• Recursive function definitions are very common in LISP> (defun factorial (num) (cond ((<= num 0) 1)

(t (* (factorial (- num 1)) num))))FACTORIAL> (factorial 4)24

Page 38: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

LISP: Global versus Local variables

> (setq a 12) ;; global a12> (defun foo (n)

(setq a 14)(+ n 2))

FOO> a12

> (foo 3)5> a14

Page 39: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Functions Revisited (1)

Standard function – all parameters defined(defun fact (x)

(if (> x 0) (* x (fact (- x 1))) 1))

But it is possible to define functions:• with variable number of parameters,• optional parameters and• keyword-based parameters

Page 40: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Functions Revisited (2)

Functions with optional parameters

> (defun func (x &optional y) (if y (+ x y ) 0))FUNC>(func 3 4)7>(func 3)0

Page 41: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Functions Revisited (3)Functions with optional parameters

> (defun func (&optional (x 3) (z 10)) (+ x z))FUNC> (func)13> (func 5 6)11> (func 5)15

Page 42: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Functions Revisited (4)

• Functions with variable number of parameters> (defun foo (&rest y) y) ;; all but the first parameters are put into a

listFOO> (foo 3)3> (foo 1 2 3)(1 2 3)> (foo 1 2 3 4 5)(1 2 3 4 5)

Page 43: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Functions Revisited (4)• Functions with ‘keyword’ parameters• All of the required arguments (req-arg) must precede the &KEY marker.

The key-args name the variable that you'll reference from within your function's definition; the same key-arg name in the keyword package (i.e. preceded with a colon) is used in the call as a prefix for the keyword value.

>(defun foo (x &key y z) (list x y z)) ;; y and z are keys>(foo 1 2 3)(1 2 3)>(foo 4 :y 5)(4 5 NIL)>(foo 4 :z 6)( 4 NIL 6)

Page 44: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Defining Arrays

(setf a (make-array ‘(3 2)) ;; make a 3 by 2 array#2a((NIL NIL) (NIL NIL) (NIL NIL))> (aref a 1 1)NIL> (setf (aref a 1 1) 2)2> a#2A((NIL NIL) (NIL 2) (NIL NIL))

Page 45: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Iterations (1)Many ways to define iterations

» loop» dolist» dotimes» do, do*

Also we can write compactly the code for repeated application of function to elements of the list:

»mapc, mapcar

Page 46: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Iterations (2)• Iterations: loop> (setq a 4)4> (loop (setq a (+ a 1))(when (> a 7) (return a))) ;; return exists the loop8> (loop (setq a (- a 1))(when (< a 3) (return)))NIL

Page 47: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Iterations(3)

• Iterations: dolist> (dolist (x '(1 2 5 4)) (print x))1254NIL ;; NIL is returned by dolist>

Page 48: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Iterations(4)

Iterations: dotimes> (dotimes (i 4) (print i)) ;; starts from 0 and continues till limit 401234NIL ;; returns NIL

Page 49: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Iterations (5)MAPCAR applies function FN to elements of lists with same index. Each application result is put into resulting list. Length of resulting list is the length of the shortest list argument.

> (mapcar #’oddp ‘(1 2 3 4 5)) => (T NIL T NIL T)> (mapcar (lambda (x) (+ x 10)) '(1 2 3 4)) => (11 12 13 14) > (mapcar #'round '(1.3 2.7 3.4 4.5)) => (1 3 3 4) > (mapcar #'list '(123 symbol "string" 345) '(1 2 3))

=> ((123 1) (SYMBOL 2) ("string" 3))

> (mapcar #'* '(3 4 5) '(4 5 6)) => (12 20 30)

Page 50: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Input-Output (1)

You can input/output data to:• standard input/output,• string or• fileA number of functions supported by the Lisp:• (read) ;; reads the input from the standard input• (print ‘a) ;; prints to the standard output• (scanf…) (printf…) (format …) for formatted input and output• (open ..) (close ..) for opening and closing the files• (load ..) reads and executes the file

Page 51: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Input-Output (2)

(with-open-file (stream "D:/lisp_test.txt" :direction :output)(print ' (hello there) stream)(princ 'goodbye stream ))

;; (HELLO THERE) GOODBYE is written in the file

(with-open-file (stream "D:/lisp_test.txt" :direction :input)(list (read stream) (read-char stream) (read stream)(read stream nil ' eof)))

Page 52: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Program calls

Assume you have your lisp code ready in the .lisp (sentence1.lisp) file; hhis is how you load

it

>(load "sentence1.lisp")>(sentence) ;; call for the function

Page 53: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

A Simple Lisp program : Sentence generator !!

A grammar for a Subset of English– Sentence : Noun-phrase + Verb-phrase– Noun-phrase: Article + Noun– Verb-phrase: Verb + Noun-phrase

– Article: the, a– Noun: man , ball, woman, table– Verb: hit, took saw, liked

Page 54: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

A Simple Lisp program

• To get a sentence append a Noun-Phrase and a Verb phrase– To get a noun phrase append an article and a noun

• Choose “the” for the Article• Choose “man” for the Noun

– The resulting Noun-phrase is “the man”

• To get a Verb-phrase, append a Verb and a Noun phrase– Choose “hit” for the verb– To get a noun phrase append an article and a noun

• Choose “the” for the Article• Choose “ball” for the Noun

– The resulting Noun-phrase is “the ball”– The resulting verb-phrase is “hit the ball”

• The resulting Sentence is “The man hit the ball”

Page 55: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

A Simple Lisp program

(defun sentence () (append (noun-phrase)(verb-phrase)))

(defun noun-phrase () (append (Article)(Noun)))

(defun verb-phrase () (append (Verb) (noun-phrase)))

(defun Article () (one-of '(the a)))

(defun Noun () (one-of '(man ball woman table)))

(defun Verb () (one-of '(hit took saw liked)))

Page 56: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

A Simple Lisp program

(defun one-of (set) (list (random-elt set )))

;; Picks one element of set, and make a list of list

(defun random-elt (choices)(elt choices (random (length choices))))

;; Chooses an element from a list of random"

Page 57: Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin

Your homework

Arabayı kimle aldı?Evi niye sattı?Masayı neyle yıkadın?

Forbidden case: XXX neyle sattı ?