1 / 28

Languages for Artificial Intelligence

Languages for Artificial Intelligence. Lecturer: J. Kolar Laboratory: J. Janousek, V. Rokos. J. Kolar Cooperation: J. Travnik Czech Technical University , Department of CS & Eng. Course References. S. Russell - P. Norvig: Artificial Intelligence: A Modern Approach. Prentice Hall, 2003.

edana
Télécharger la présentation

Languages for Artificial Intelligence

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. Languages for Artificial Intelligence Lecturer: J. Kolar Laboratory: J. Janousek, V. Rokos. J. Kolar Cooperation: J. Travnik Czech Technical University, Department of CS & Eng Languages for AI – Lesson 1

  2. Course References • S. Russell - P. Norvig: Artificial Intelligence: A Modern Approach. Prentice Hall, 2003. • http://aima.cs.berkeley.edu/ • S. Slade: Object-Oriented Common Lisp. Prentice-Hall 1998. • http://www.paulgraham.com/index.html (ANSI Common Lisp) • Bratko, I.:PROLOG Programming for Artificial Intelligence (3rd Ed). Addison-Wesley 2000. • http://cwx.prenhall.com/bookbind/pubbooks/bratko3_ema/ • ... and other resources, see http://service.felk.cvut.cs/courses/36JUI Languages for AI – Lesson 1

  3. A Brief Course Overview • Introduction to Lisp • AI & Intelligent Agents • Processing symbolic data & recursion • Search and its implementation in Lisp • Lisp control structures & functionals • Lisp special functions & data structures • Game playing • Prolog • Constraint Satisfaction Problems Languages for AI – Lesson 1

  4. Languages for AI Languages for AI – Lesson 1

  5. The Role of Languages for AI Levels of knowledge-based systems Knowledge level Symbol level Algorithm & data structure level Programming language level Assembly language, Machine instruction, Microcode, and Hardware levels Languages for AI – Lesson 1

  6. General Features of AI Languages • Support of symbolic computation • Flexibility of control • Support of exploratory programming methodologies • AI problems are initially poorly specified • solving approaches are domain-specific • heuristic methods are inherently empirical • structures & formalisms for AI representations are refined • modularity, extensibility, high-level constructs, fast prototyping, program readability, interpreted & compiled modes, ... • Dynamic binding and constraint propagation • A clear and well-defined semantics Languages for AI – Lesson 1

  7. As an Example: Factorial Forever … • Versions 1 and 2: Iterative & recursive (Pascal) functionFactIt(N:integer):integer; var F,I:integer; begin end; functionFactRe(N:integer):integer; begin end; F:=1; for I:=1 to N do F:=F*I; FactIt:=F if N = 0 then fact:=1 else FactRe:=N*FactRe(N-1) Languages for AI – Lesson 1

  8. (defun fact (N) (cond ((= N 0) 1) (T (* N (fact (- N 1)))) ) ) • Version 3:Lisp (recursive, of course!) • Version 4: Prolog (recursive, as well!) fact(1,0). fact(F,N):- N>0, N1 is N-1, fact(F1,N1), F is N*F1. Languages for AI – Lesson 1

  9. A Lisp session example: • CL-USER 1 >(defun fact (n) • (cond ((= n 0) 1) • (T (* n (fact (- n 1)))) • ) ) • FACT • CL-USER 2 >(fact 5) • 120 • CL-USER 3 >(fact 10) • 3628800 • CL-USER 4 >(fact 120) • 6689502913449127057588118054090372586752746333138029810295671352301633557244962989366874165271984981308157637893214090552534408589408121859898481114389650005964960521256960000000000000000000000000000 Languages for AI – Lesson 1

  10. Why Lisp ? • 1958-9 John McCarthy at MIT, a formal model of computation • Lisp attributes • simple (trivial) syntax • concise semantics • excellent medium for programming constructs & data structures • procedures can be manipulated as data, so ideal for writing interpreters & compilers • erases difference between “passive” data and “active” processes • Quoting Abelson & Sussman (Structure and Interpretation of Computer Programs): • Pascalis good for pyramids, static structures built by armies pushing heavy blocks into place • Lispis for building organisms, dynamic structures built by squads fitting fluctuating myriads of simpler organisms into place Languages for AI – Lesson 1

  11. What Programming Is About? • Mastering complexity! • 3 foci that are involved • human mind • collections of programs • computer • "Programsmust be written for people to read, and only incidentally for machines to execute." • "Programming languageis NOT just a way of getting a computer to perform operations, BUT a novel formal medium for expressing ideas about methodology." • ... quoting Abelson & Sussman again Languages for AI – Lesson 1

  12. Programming in Lisp • As any other powerful language, Lisp has • primitive expressions – represent the simplest entities that the language is concerned with 123 JOSEF ″This is a string″ • means of combination – needed to build compound expressions from simpler ones (+ 123 456) (cons ′JOSEF ′(IS MY NAME)) • means of abstraction – to name and manipulate compound objects, both for data and procedures (defun square (X) (* X X)) Languages for AI – Lesson 1

  13. Primitive Expressions of Lisp • Primitive expressions of Lisp are calledatoms. • Atoms are of various kinds (types): • numbers are of the following types • real • float (short, single, double, long) • rational • ratio • integer (fixnum, bignum) • complex • symbols • strings • characters • ... Languages for AI – Lesson 1

  14. (Arithmetic) Expressions in Lisp • (+ 1 2 3 4 5 6) • (* 1 2 3 4 5 6) • (/ 1 2 3) (* (+ 1 2) (- 3 4 5)) • +, -, 1+, 1-, INCF, DECF, *, /, EXPT, MOD, REM, FLOOR, CEILING, ROUND, ABS, SIGNUM, MAX, MIN • =, /=, <, >, <=, >=, MINUSP, PLUSP, ZEROP, EVENP, ODDP • SQRT, SIN, COS, TAN, EXP, LOG, ASIN, ACOS, ATAN • SINH, COSH, TANH, ASINH, ACOSH, ATANH • GCD, LCM, RANDOM • … and more … Languages for AI – Lesson 1

  15. -18 (* 3-6) Evaluation Rules • When evaluating an (arithmetic) expression: • numbers evaluate to themselves • in a compound expression • arguments (sub-expressions) are evaluated first • then the operation / function is applied (* (+ 1 2) (- 3 4 5)) (+ 1 2) (- 3 4 5) Languages for AI – Lesson 1

  16. Evaluation Rules • Lisp (as a rule) evaluates all arguments before the main function is applied to them. • ?How can we avoid that (if necessary for some reason)? • quote is the tool behind the apostrophe! • > (defvar X '(A B C)) • X • > X • (A B C) • > (setf X (C B A)) • Error: The variable B is unbound. • > (setf X (C 1 2)) • Error: Undefined function C called with arguments (1 2). • Syntactic sugar: • ' exp (quoteexp) same as(quote (A B C)) attempt to evaluateB Cconsidered a function Languages for AI – Lesson 1

  17. Symbolic Expressions in Lisp • atoms123 MY_NAME "Josef " #\space • lists(1 2 3) (MY NAME IS (Josef Kolar)) () (s1 s2 ... sn) where s1, s2, ... sn (n0) are symbolic expressions of any kind • dotted-pairs (A . 1) ((FROM POPO TO) . IXTA) (s1 . s2 ) where s1, s2 are symbolic expressions of any kind • (* (+ 1 2) (- 5 3)) ...arithmetic expression / list • Both dataandprograms in Lisp have a form of • symbolic expressions Languages for AI – Lesson 1

  18. dotted-pair:(A . B) A B (A B C) "cons-cell" A B C (A ((B) C)) A C B Data Representation in Lisp empty list:() = NIL Languages for AI – Lesson 1

  19. Lisp Variables • (defvar A 11) • (defvar B 22) • (+ A B) • (/ (+ A B) (- A B)) • (1+ A) • (incf A) • (setf A (+ A 5)) • variable – a name used to refer some object (value) • (global) environment - memory that keeps track of name → value pairs • many different environments are established during program execution • evaluation rule for a variable – the object associated with the name at the actual time in the (corresponding/actual) environment Languages for AI – Lesson 1

  20. Function Definition in Lisp • (defun square (X) (* X X)) • To square something, multiply it by itself • (defun name (formal_parameters) body) • (defun sum-of-squares (X Y) • (+ (square X) (square Y))) • (defun hipotenusa (A B) • (sqrt (sum-of-squares A B))) • We needmore constructs in order to extend the expressive power of procedures we define. Languages for AI – Lesson 1

  21. Conditional Expressions and Predicates • x if x>0 • abs(x) =0 if x=0 • -x if x<0 • (defun abs (X) (defun abs (X) • (cond ((> X 0) X) (if (> X 0) X • ((= X 0) 0) X • ((< X 0) (- X)) (if (= X 0) 0 (- X)) • ) ) ) ) • (cond (p1 e1) (if predicate • (p2 e2) consequent • ... alternative ) • (pn en) ) • =, /=, <, >, <=, >=, MINUSP, PLUSP, ZEROP, EVENP, ODDP Languages for AI – Lesson 1

  22. More about Predicates • Predicate (Boolean) valuesin Lisp : • NIL (false) T or anything  NIL(true) • 10  X  20: (and (<= 10 X) (<= X 20)) or directly (<= 10 X 20) • (and p1 p2 ... pn) (or p1 p2 ... pn) (not p) • Predicates and, ortake any number of arguments and have • a special rule of evaluation: • and– evaluate arguments from the left until the first NIL value or arguments exhausted, the resulting value is NIL or the last argument • or– evaluate arguments from the left until the first non-NIL value or arguments exhausted, the resulting value is the first non-NIL or NIL • Examples ... Languages for AI – Lesson 1

  23. Example: Calculating Square Roots • Declarative description (what is): • x = the y such that y  0 and y2 = x • Imperative description (how to): • Newton's method – successive approximations, a guessed y is improved averaging y and x / y • x = 2 : guess y 1 1.5 1.4167 1.4142 • quotient x/y 2 1.3333 1.4118 • average 1.5 1.4167 1.4142 • (defun sqrt-iter (guess x) • ;(format t "~5t x: ~5t~f~5tGuess: ~5t~f~%" x guess) • (if (good-enough? guess x) • guess • (sqrt-iter (improve guess x) x) • ) ) • Lisp is not case-sensitive! Languages for AI – Lesson 1

  24. (defun improve (guess x) • (average guess (/ x guess)) • ) • (defun average (x y) • (/ (+ x y) 2) • ) • (defun good-enough? (guess x) • (< (abs (- (square guess) x)) 0.001) • ) • (defun my-sqrt (x) • (sqrt-iter 1.0 x) ; what if we put just 1 instead? • ) ; ratio arithmetics • We can play a bit with Lisp ...what happens if ... • (defun my-if (p c e) • (cond (p c) • (t e) • ) ) Languages for AI – Lesson 1

  25. Processing Symbolic Expressions • How to process lists and dotted pairs? • (1 2 3 4) (A B C D E) (MI . MA) • We have • selectors(cars)(cdrs)(nth ns )and more ... • (AB C D E) • constructors(conselm s)(lista b…)and more ... • predicates(atom s) (eq a b) or eql, equal (null s) = (eq s NIL) ... • Examples: Languages for AI – Lesson 1

  26. (defun swap1 (S) ; interchange the first and second • (cons (car (cdr S)) ; cadr • (cons (car S) (cdr (cdr S))) ; cddr • ) ) • (defun swap2 (S) ; interchange the first and second • (cond ((null S) NIL) ; if S empty • ((null (cdr S)) S) ; if just one elm • (T (cons (car (cdr S)) ; now safe • (cons (car S) • (cdr (cdr S)) • ) ) ) ) ) • (defun swap3 (S) ; interchange the first and second • (cond ((null S) NIL) • ((null (cdr S)) S) • (T (cons (cadr S) • (cons (car S) • (cddr S)) • ) ) ) ) ) Languages for AI – Lesson 1

  27. (defun select-atoms (S) ; select all atoms of S • (cond ((null S) NIL) • ((atom (car S)) • (cons (car S) (select-atoms (cdr S)))) • (T (select-atoms (cdr S))) • ) ) • (defun test-positive (S) ; test if all elms >0 • (cond ((null S) T) • ((plusp (car S)) • (test-positive (cdr S))) • (T NIL) ; can be omitted • ) ) • And what about looking for the maximal element ... Languages for AI – Lesson 1

  28. (defun my-max1 (S) ; calculate maximum elm • (if (null S) • 'ErrorMax • (my-max-iter (car S) (cdr S)) • ) ) • (defun my-max-iter (TempMax S) • (cond ((null S) TempMax) • ((> (car S) TempMax) • (my-max-iter (car S) (cdr S))) • (T (my-max-iter TempMax (cdr S))) • ) ) • ;; now find the difference • (defun my-max2 (S) ; calculate maximum elm • (cond ((null (cdr S)) (car S)) • ((> (car S) (my-max (cdr S)) • (car S)) • (T (my-max (cdr S))) • ) ) Languages for AI – Lesson 1

More Related