Homework #2. Source code with description. Tzewen Wang ECE578 Winter 2005

Similar documents
Modern Programming Languages. Lecture LISP Programming Language An Introduction

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

Recursion & Iteration

Lisp Basic Example Test Questions

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

(defvar *state* nil "The current state: a list of conditions.")

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

Lisp. Versions of LISP

Structure Programming in Lisp. Shared Structure. Tailp. Shared Structure. Top-Level List Structure

Scheme: Strings Scheme: I/O

Department of Computer and information Science Norwegian University of Science and Technology

A little bit of Lisp

1. Problem Representation

User-defined Functions. Conditional Expressions in Scheme

ALISP interpreter in Awk

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Project 2: Scheme Interpreter

Common Lisp. Blake McBride

Lecture #2 Kenneth W. Flynn RPI CS

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Meet the Macro. a quick introduction to Lisp macros. Patrick Stein / TC Lisp Users Group /

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Lecture Notes on Lisp A Brief Introduction

15 Unification and Embedded Languages in Lisp

19 Machine Learning in Lisp

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Allegro CL Certification Program

CSCI337 Organisation of Programming Languages LISP

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Scheme Quick Reference

Imperative, OO and Functional Languages A C program is

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Associative Database Managment WIlensky Chapter 22

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Associative Database Managment

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Simplifying Logical Formulas

Functional Programming Languages (FPL)

Common LISP-Introduction

Structure and Interpretation of Computer Programs

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Midterm Examination (Sample Solutions), Cmput 325

Debugging in LISP. trace causes a trace to be printed for a function when it is called

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

Functional Programming. Pure Functional Languages

EE 352 Lab 3 The Search Is On

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Functional Programming. Pure Functional Programming

Announcements. Today s Menu

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM

Building a system for symbolic differentiation

CS 314 Principles of Programming Languages

Structure and Interpretation of Computer Programs

Mechanized Operational Semantics

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Functional programming with Common Lisp

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

GPS: The general problem solver. developed in 1957 by Alan Newel and Herbert Simon. (H. Simon, 1957)

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

A Genetic Algorithm Implementation

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

CS 842 Ben Cassell University of Waterloo

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Review of Functional Programming

Problem Solving Agents

Problem 1: Binary Trees: Flatten

Intro. Scheme Basics. scm> 5 5. scm>

Functional Programming. Pure Functional Languages

Building a system for symbolic differentiation

Scheme Quick Reference

CS450 - Structure of Higher Level Languages

Introduction to Functional Programming and basic Lisp

implementing the breadth-first search algorithm implementing the depth-first search algorithm

by the evening of Tuesday, Feb 6

Functional Programming - 2. Higher Order Functions

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

A Brief Introduction to Scheme (II)

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

Formal Languages and Automata Theory, SS Project (due Week 14)

Common LISP Tutorial 1 (Basic)

CSc 520. Principles of Programming Languages 7: Scheme List Processing

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

UMBC CMSC 331 Final Exam Section 0101 December 17, 2002

Vectors in Scheme. Data Structures in Scheme

Heap storage. Dynamic allocation and stacks are generally incompatible.

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Functional programming techniques

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Transcription:

Tzewen Wang ECE578 Winter 2005 Source code with description Homework #2 Homework 2 Tzewen Wang ECE578 Winter 2005 In addition to the symbols for obstacle and corridor in a labyrinth, five more symbols are used and described including those as the following: 0: corridor. 1: obstacle. 2: visited corridor. 3: robot - facing north. 4: robot - facing east. 5: robot - facing south. 6: robot - facing west. The robot in this program will try to do learning first and memorize every single turn in the final path. The algorithm assumes the learning phase is done on a high-speed computer. Once the path is found, the robot performs actual movements and makes turns at particular location given by the path. As a result, the data structure for the path is the following: ( (X Y DIR (X Y DIR... This project uses a simple recursive algorithm in the learning phase. Each step is a recursive call. The termination occurs on either a door or a a dead-end. A door is the outmost (edge of labyrinth. The labyrinth is constructed with a 2D array. If a corridor appears on the edge of the labyrinth, it is concerned as a door. For example, for a 10*10 labyrinth, a corridor is at (9, 5, then (9, 5 is the door. This should work fine with multiple doors. The direction precedence to move to the next step is is the following: i forward, ii right, iiileft, and iv reverse. The algorithm returns TRUE if a door is found. It returns NIL if a a dead-end is found. The backtracking is handled by language mechanism. Thus, the backtracking will not be shown explicitly during the learning process. As the robot makes turn, the coordinate and direction will be pushed into PathStack. The stack will be popped when a backtracking occurs. This algorithm operates directly on two data structure: i a 2D array representing the labyrinth, and ii a list of list consisting of coordinates and facing directions. One can image the execution tree is to be a quadnary tree, a node with four branches. The algorithm traverses down to the leaf where it is a "door". As traversing going, the PathStack will be expanded and shrunk. Once the door is found, the execution of the program is ended. Then, the robot is guided by the coordinates in the PathStack. An additional function is implemented to do pretty-printing. It just converts those symbols into a human readable notations. The shortest path can be obtained in the learning process with a little modification of the algorithm. As mentioned earlier, the basic idea of the algorithm is to find a door leaf on the big execution-tree. Now, with modified termination condition, the entire tree is possibly traversed. The modification is as follows: if a door is found, we put the stack aside and return a NIL instead of TRUE. In addition, a weight should be assigned with each coordinate recorded in the PathStack. The weight is the distance from start point to current location. Once the learning process is done, a number of paths will be returned and the robot can pick the lightest one.

learn-labyrinth (L X Y DIR Walk through a labyrinth (learning phase. Parameters: L - labyrinth in a 2-D array. X - current position X. Y - current position Y. DIR - robot's facing directions. Returns T if the door is found. NIL, otherwise (defun learn-labyrinth (L X Y DIR (dump-labyrinth L (cond ; when reaching the door... ((or (equal X 0 (equal X (- (array-dimension L 1 1 (equal Y 0 (equal Y (- (array-dimension L 0 1 T ; when facing North... ((equal DIR 3 (let ((FLAG nil (cond ((equal (aref L (- Y 1 X 0 (setf (aref L (- Y 1 X 3 (setf FLAG (learn-labyrinth L X (- Y 1 3 (cond ((and (null FLAG (equal (aref L Y (+ X 1 0 (setf (aref L Y (+ X 1 4 (pusht X Y 4 (setf FLAG (learn-labyrinth L (+ X 1 Y 4 (cond ((and (null FLAG (equal (aref L Y (- X 1 0 (setf (aref L Y (- X 1 6 (pusht X Y 6 (setf FLAG (learn-labyrinth L (- X 1 Y 6 (cond ((and (null FLAG (equal (aref L (+ Y 1 X 0 (setf (aref L (+ Y 1 X 5 (pusht X Y 5 (setf FLAG (learn-labyrinth L X (+ Y 1 5 FLAG ; when facing East... ((equal DIR 4 (let ((FLAG nil (cond ((equal (aref L Y (+ X 1 0 (setf (aref L Y (+ X 1 4 (setf FLAG (learn-labyrinth L (+ X 1 Y 4 (cond ((and (null FLAG (equal (aref L (+ Y 1 X 0 (setf (aref L (+ Y 1 X 5 (pusht X Y 5 (setf FLAG (learn-labyrinth L X (+ Y 1 5 (cond ((and (null FLAG (equal (aref L (- Y 1 X 0 (setf (aref L (- Y 1 X 3

(pusht X Y 3 (setf FLAG (learn-labyrinth L X (- Y 1 3 (cond ((and (null FLAG (equal (aref L Y (- X 1 0 (setf (aref L Y (- X 1 6 (pusht X Y 6 (setf FLAG (learn-labyrinth L (- X 1 Y 6 FLAG ; when facing South... ((equal DIR 5 (let ((FLAG nil (cond ((equal (aref L (+ Y 1 X 0 (setf (aref L (+ Y 1 X 5 (setf FLAG (learn-labyrinth L X (+ Y 1 5 (cond ((and (null FLAG (equal (aref L Y (- X 1 0 (setf (aref L Y (- X 1 6 (pusht X Y 6 (setf FLAG (learn-labyrinth L (- X 1 Y 6 (cond ((and (null FLAG (equal (aref L Y (+ X 1 0 (setf (aref L Y (+ X 1 4 (pusht X Y 4 (setf FLAG (learn-labyrinth L (+ X 1 Y 4 (cond ((and (null FLAG (equal (aref L (- Y 1 X 0 (setf (aref L (- Y 1 X 3 (pusht X Y 3 (setf FLAG (learn-labyrinth L X (- Y 1 3 FLAG ; when facing West... ((equal DIR 6 (let ((FLAG nil (cond ((equal (aref L Y (- X 1 0 (setf (aref L Y (- X 1 6 (setf FLAG (learn-labyrinth L (- X 1 Y 6 (cond ((and (null FLAG (equal (aref L (- Y 1 X 0 (setf (aref L (- Y 1 X 3 (pusht X Y 3 (setf FLAG (learn-labyrinth L X (- Y 1 3 (cond ((and (null FLAG (equal (aref L (+ Y 1 X 0 (setf (aref L (+ Y 1 X 5 (pusht X Y 5 (setf FLAG (learn-labyrinth L X (+ Y 1 5 (cond ((and (null FLAG (equal (aref L Y (+ X 1 0 (setf (aref L Y (+ X 1 4 (pusht X Y 4 (setf FLAG (learn-labyrinth L (+ X 1 Y 4

FLAG walk-labyrinth (L X Y DIR Walks through a labyrinth by a given path. (This is not a argument-safe nor path-safe funciton. Parameters: L - the labyrinth in a 2D array. X - current position X. Y - current position Y. DIR - robot's facing direction. PATH - learned path. Returns T if success. (defun walk-labyrinth (L X Y DIR PATH (dump-labyrinth L (cond ; when reaching the door... ((or (equal X 0 (equal X (- (array-dimension L 1 1 (equal Y 0 (equal Y (- (array-dimension L 0 1 T ; when reaching a turn point... ((and (not (null PATH (equal (first (first PATH X (equal (second (first PATH Y (setf (aref L Y X (third (first PATH (walk-labyrinth L X Y (third (first PATH (rest PATH ; when facing North... ((equal DIR 3 (setf (aref L (- Y 1 X 3 (walk-labyrinth L X (- Y 1 3 PATH ; when facing East... ((equal DIR 4 (setf (aref L Y (+ X 1 4 (walk-labyrinth L (+ X 1 Y 4 PATH ; when facing South... ((equal DIR 5 (setf (aref L (+ Y 1 X 5 (walk-labyrinth L X (+ Y 1 5 PATH ; when facing West... ((equal DIR 6 (setf (aref L Y (- X 1 6 (walk-labyrinth L (- X 1 Y 6 PATH (T (print "ERROR in walk-labyrinth!" Dumps a labyrithn in human readable notations. (This is not a argument-safe function. L - the labyrinth to be dumped. Returns T if success.

(defun dump-labyrinth (L (dotimes (Y (array-dimension L 0 (dotimes (X (array-dimension L 1 (write-char (cond ((equal (aref L Y X 0 #\space ((equal (aref L Y X 1 #\* ((equal (aref L Y X 2 #\. ((equal (aref L Y X 3 #\^ ((equal (aref L Y X 4 #\> ((equal (aref L Y X 5 #\ ((equal (aref L Y X 6 #\< (write-char #\newline (write-char #\newline pusht (X Y DIR Push a turn into path list. Parameters: X, Y - cordinates. DIR - robot's facing direction. Returns a list of cordinates on the stack. (defun pusht (X Y DIR (cond ((atom PATH (setf PATH (list (list X Y DIR ((and (equal X (first (first (reverse PATH (equal Y (first (second (reverse PATH (popt (setf PATH (append PATH (list (list X Y DIR (T (setf PATH (append PATH (list (list X Y DIR pop ( Pop a turn into path list. Parameters: X, Y - cordinates. DIR - robot's facing direction. Returns a list of cordinates on the stack. (defun popt ( (cond ((equal (length PATH 1 (sef nil (T (setf PATH (reverse (rest (reverse PATH copy (S Clone an arbitrary structure. Parameters:

S - The structure to be replicated. Returns a clone copy of the structure. (defun copy (S (cond ((atom S S (T (cons (copy (car S (copy (cdr S Main program. Find a way to get out of the labyrinth. (defun main ( (setf PATH nil (setf LAB (make-array `(10 10 :initial-contents `((1 1 1 1 1 1 1 1 1 1 (1 0 0 0 0 0 0 0 0 1 (1 0 1 0 1 0 0 1 0 1 (1 0 1 0 1 1 1 1 0 1 (1 5 1 0 0 0 0 0 0 1 (1 0 1 0 1 1 1 1 0 0 (1 0 1 0 0 0 0 0 0 1 (1 0 1 0 1 1 1 1 0 1 (1 0 1 0 0 0 0 0 0 1 (1 1 1 1 1 1 1 1 1 1 (write-char #\newline (print "Learning phase" (write-char #\newline (learn-labyrinth (copy LAB 1 4 5 ; In case of COPY does not work. A manual copy instead. (setf LAB (make-array `(10 10 :initial-contents `((1 1 1 1 1 1 1 1 1 1 (1 0 0 0 0 0 0 0 0 1 (1 0 1 0 1 0 0 1 0 1 (1 0 1 0 1 1 1 1 0 1 (1 5 1 0 0 0 0 0 0 1 (1 0 1 0 1 1 1 1 0 0 (1 0 1 0 0 0 0 0 0 1 (1 0 1 0 1 1 1 1 0 1 (1 0 1 0 0 0 0 0 0 1 (1 1 1 1 1 1 1 1 1 1 (write-char #\newline (print "Solving phase" (write-char #\newline (walk-labyrinth LAB 1 4 5 PATH ; Main program invocation. (main Program output (next page

"Learning phase" * * * * * * * * * * * * * * * * * * * * * * **** * * * **** * * * **** * * * **** * * * **** * * * * *.* * *.* * *.* * *.* * * * **** * * **** *.* **** *.* **** *.* **** * * * * * * * * * *.* * *.* * * * **** * * * **** * * * **** * * * **** * *.* **** * * * * * * * * * * * * * * * * * * * * * * *^ * *.> * * * * * * * * * * * *^* * * * *.* * * * *.* * * * * * **** * *^* **** * *.* **** * *.* **** * *.* **** * *.* **** *.* **** *.* **** *.* **** *.* **** * * * *.* * *.* * *.* * *.* * *..> * *...> * *...> * *...> * *...> * *.* * * * *.* * * * *.* * * * *.* * * * *.* * * * *.* **** *.* **** *.* **** *.* **** *.* **** *...>* *...* *...* *...* *...* *.* * * * *.* * * * *.* * *.* *.* * *.* *.* * *.* *.* **** * *.* **** * *.* **** * *.* ****.* *.* ****.* *.* * *.* * *.* * *.* * *.*.* *.* **** *.* **** *.* **** *.* **** *.* ****

*...* *...* *...* *...* *...* *.* * *.* *.* * *.* *.* * *.* *.* * *.* *.* * *.* *.* ****.* *.* ****.* *.* ****.* *.* ****.* *.* ****.* *.*.* *.*.* *.*.* *.*.* *.*.* *.* ****. *.* ****. *.* ****. *.* ****. *.* ****. *.* * *.*.* *.*.* *.*.* *.*.* *.* **** * *.* **** * *.* ****.* *.* ****.* *.* ****.* *.* * *.* * *.* * *.* <.* *.* <..* *...* *...* *...* *...* *...* *.* * *.* *.* * *.* *.* * *.* *.* * *.* *.* * *.* *.* ****.* *.* ****.* *.* ****.* *.* ****.* *.* ****.* *.*.* *.*.* *.*.* *.*.* *.*.* *.* ****. *.* ****. *.* ****. *.* ****. *.* ****. *.*.* *.*.* *.*.* *.*.* *.*^.* *.* ****.* *.* ****.* *.* ****.* *.*^****.* *.*.****.* *.* <...* *.* <...* *.*<...* *.*...* *.*...* *...* *...* *...* *...* *...* *.* * *.* *.* * *.* *.* * *.* *.*^* *.* *.*.* *.* *.* ****.* *.* ****.* *.*^****.* *.*.****.* *.*.****.* *.*.* *.*^.* *.*..* *.*..* *.*.>.* *.*^****. *.*.****. *.*.****. *.*.****. *.*.****. *.*..* *.*..* *.*..* *.*..* *.*..* *.*.****.* *.*.****.* *.*.****.* *.*.****.* *.*.****.* *.*...* *.*...* *.*...* *.*...* *.*...* *...* *...* *...* *...* *...* *.*.* *.* *.*.* *.* *.*.* *.* *.*.* *.* *.*.* *.* *.*.****.* *.*.****.* *.*.****.* *.*.****.* *.*.****.* *.*..>.* *.*...>.* *.*...>.* *.*...* *.*...* *.*.****. *.*.****. *.*.****. *.*.****. *.*.****. *.*..* *.*..* *.*..* *.*.>.* *.*..>.* *.*.****.* *.*.****.* *.*.****.* *.*.****.* *.*.****.* *.*...* *.*...* *.*...* *.*...* *.*...*

********** ********** ********** *...* *...* *...* *.*.* *.* *.*.* *.* *.*.* *.* *.*.****.* *.*.****.* *.*.****.* *.*...* *.*...* *.*...* *.*.****. *.*.****. *.*.****.> *.*...>.* *.*...>.* *.*...* *.*.****.* *.*.****.* *.*.****.* *.*...* *.*...* *.*...* ********** ********** ********** "Solving phase" * * * * * * * * *^ * *^* * * * *.* * * * * * **** * * * **** * *^* **** * *.* **** * *.* **** * * * * *^* * *.* * *.* * *.* * * * **** * * **** * * **** * * **** * * **** * * **** * * * **** * * * **** * * * **** * * * **** * *> * *.> * *..> * *...> * *...> * *.* * * * *.* * * * *.* * * * *.* * * * *.* * * * * * **** * * **** * * **** * * **** * * **** * * **** * * * **** * * * **** * * * **** * * * **** * *...> * *...> * *...>* *... * *...* *.* * * * *.* * * * *.* * * * *.* * * * *.* * * * * * **** * * **** * * **** * * **** * * **** * * **** * * * **** * * * **** * * * **** * * * **** *

*...* *...* *...* *...* *...* *.* * *.* *.* * *.* *.* * *.* *.* * *.* *.* * *.* *.* **** * *.* ****.* *.* ****.* *.* ****.* *.* ****.* *.* * *.* * *.*.* *.*.* *.*.* * * **** * * **** * * **** * * ****> * * ****.> * * **** * * * **** * * * **** * * * **** * * * **** *