1 / 57

Artificial Intelligence IES 503

Artificial Intelligence IES 503. Asst . Prof. Dr. Senem Kumova Metin. Artificial Intelligence – IES 503. Instructor : Asst . Prof. Dr. Senem Kumova Metin E mail : senem.kumova@ieu.edu.tr Lectures : Mondays 1 8 :30— 21 : 3 0 Course web page: http://homes.ieu.edu.tr/skumova

eldora
Download Presentation

Artificial Intelligence IES 503

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Artificial IntelligenceIES 503 Asst. Prof. Dr. Senem Kumova Metin

  2. Artificial Intelligence – IES 503 • Instructor:Asst. Prof. Dr. Senem Kumova Metin • E mail : senem.kumova@ieu.edu.tr • 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. (2nded)

  3. 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 symbolmanipulation • 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  http://people.cs.missouri.edu/~skubicm/375/clisp.htm

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

  5. Lisp Language • LISP Competitors: • Prolog • Python • Current LISP: The most widely-knowngeneral-purpose Lisp dialects • Common Lisp • Scheme • Common LISP: • Interpreter and compiler • CLOS: object oriented programming

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

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

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

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

  10. 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 ,

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

  12. 5. Lisp classifies data - continues Lisp Expression number symbol sequence keyword float list vector ratio integer string

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

  14. 6. Lisp uses prefix notation • Operators appear in front of their operands. • The infix (10 + 3) is written as (+ 10 3)

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

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

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

  18. 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!

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

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

  21. Dealing with variables(1) Explicit evaluation call is possible !! > (setq a 12) ;; setq means SET QUOTE  a=12 12 > (setq b (+ a 4)) ;; b=a+4 16 > (setq b ‘(+ a 4)) ;; b=(+ a 4) (+ a 4) > (eval b) ;; explicit evaluation call 16

  22. 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 8but (+ '(+ 2 2) '(+ 1 3)) returns what?

  23. Dealing with variables(3) > (setf a 12) ;; setf means SET FIELD  a=12 12 > (setf b (+ a 4)) ;; b=a+4 16 > (setfb ‘(+ a 4)) ;; b=(+ a 4) (+ a 4) > (eval b) ;; explicit evaluation call 16 ------ (setf (rest list) nil ) ;; list.rest=nil (setf (aref A i j) 0) ;; A[i][j]=0;

  24. Dealing with variables(3) • Special symbols: > t ;; true T > nil ;; nil stands for false or NIL > ( ) ;; an empty list NIL

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

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

  27. 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 returnstheelementsfollowingthefirst element • >(cdr (cons 'a (cons'b nil)))  B • >(cdr(cdr (cons 'a (cons'b nil))))  NIL A B nil

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

  29. 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 argument 5 > (atom ‘a) ;; checks if the argument is an atom T > (atom a) NIL > (listp ‘a) ;; checks if the argument is a list NIL > (listp a) T

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

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

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

  33. Function definition (2) (defun <f-name> <parameter-list> <body>) <body> can be a sequence of function calls, thefunction returnsthe value of the last call in the sequence > (defun foo (a) (setq b (+ a 1)) (setq c (+ a 2)) c) FOO > (foo 2) 4

  34. Conditional statements (1) • Cond statement: sequentially tests conditions, the callassociated with the first true condition is executed > (defun abs (a) (cond ((> a 0) a) (t (* a -1)))) ABS > (abs 2) 2 > (abs -3) 3 • > (defunfunc (a) • (cond ((< a 10) 1) ((< a 50) 2) ((< a 100) 3))) • FUNC • >(func 40) • 2 • >(func 60) • 3

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

  36. Lisp: Equality operators 4 equality predicates: =, equal, eq, eql > (= 2 4/2) ;; used for numerical values only T > (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 isomorphic T > (equal c b) T

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

  38. LISP: Global versus Local variables > (setq a 12) ;; global a 12 > (defun foo (n) (setq a 14) (+ n 2)) FOO > a 12 > (foo 3) 5 > a 14

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

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

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

  42. Functions Revisited (4) • Functions with variable number of parameters > (defun foo (&rest y) y) ;; all but the first parameters are putinto a list FOO > (foo 3) 3 > (foo 1 2 3) (1 2 3) > (foo 1 2 3 4 5) (1 2 3 4 5)

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

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

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

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

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

  48. Iterations(4) Iterations: dotimes > (dotimes (i 4) (print i)) ;; starts from 0 and continues tilllimit 4 0 1 2 3 4 NIL ;; returns NIL

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

  50. Input-Output (1) You can input/output data to: • standard input/output, • string or • file A 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

More Related