Lisp legal move generators & search

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

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

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

CS 3 Midterm 1 Review

6.001 Notes: Section 31.1

Types, Expressions, and States

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below.

6.001 Notes: Section 6.1

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Documentation Nick Parlante, 1996.Free for non-commerical use.

ACT-R RPC Interface Documentation. Working Draft Dan Bothell

NST: A Unit Test Framework for Common Lisp

CSC D84 Assignment 2 Game Trees and Mini-Max

Common LISP-Introduction

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we


Table of Contents. How to use this document. How to use the template. Page 1 of 9

Racket Style Guide Fall 2017

Permission to copy The CAD Academy

Microsoft Excel 2010 Training. Excel 2010 Basics

Basic Search Algorithms

MITOCW watch?v=kz7jjltq9r4

Putting the fun in functional programming

CIS 505: Software Systems

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

! Emacs Howto Tutorial!

Chapter 3: The IF Function and Table Lookup

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

6.001 Notes: Section 15.1

6.001 Notes: Section 7.1

6.001 Notes: Section 8.1

Title: Recursion and Higher Order Functions

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Assignment#2 Uninformed Search in Pac-Man i Due Wednesday, September 27

Every function machine takes its inputs from a bag called the Input bag. Here are some examples of this function machine's Input bag:

Introduction to Programming with RAPTOR

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

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Formatting: Cleaning Up Data

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

: Intro Programming for Scientists and Engineers Assignment 1: Turtle Graphics

Formulas, LookUp Tables and PivotTables Prepared for Aero Controlex

Haskell Refresher Informatics 2D

19 Machine Learning in Lisp

You should provide the best solutions you can think of.

Detecting and correcting mistakes

a b c d a b c d e 5 e 7

15100 Fall 2005 Final Project

Lecture Set 4: More About Methods and More About Operators

bindings (review) Topic 18 Environment Model of Evaluation bindings (review) frames (review) frames (review) frames (review) x: 10 y: #f x: 10

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

COSC345 Software Engineering. Documentation

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

CS3 Midterm 1 Fall 2006

Azon Master Class. By Ryan Stevenson Guidebook #7 Site Construction 2/3

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

APCS-AB: Java. Recursion in Java December 12, week14 1

Autumn 2012 October 29 th, 2012 CS106X Midterm Examination Solution

PAC-MAN is one of the most popular game

CSc 372, Fall 2006; Quiz 1 Time: 3:00; Value: 2 points. 1. What is a fundamental difference between the if-then-else constructs in Java (or C) and ML?

Spreadsheet definition: Starting a New Excel Worksheet: Navigating Through an Excel Worksheet

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

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

Functions that return lists

1 Introduction and Examples

Reliable programming

CS3 Midterm 1 Fall 2007 Standards and solutions

CS510 \ Lecture Ariel Stolerman

Full file at

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Human Computer Interface Design Chapter 7 User Interface Elements Design and Guidelines

Excel Basics: Working with Spreadsheets

CSCI-1200 Data Structures Fall 2017 Lecture 13 Problem Solving Techniques

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Introduction to: Computers & Programming Defining Identifiers: Objects with Names

Boot Camp. Dave Eckhardt Bruce Maggs

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

XC Total Max Score Grader

CS 360 Programming Languages Interpreters

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Lou Burnard Consulting

In either case, remember to delete each array that you allocate.

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

MITOCW watch?v=0jljzrnhwoi

Emptying the trash...18 Webmail Settings...19 Displayed Name...19 Sort by...19 Default font style...19 Service language...

BEGINNER PHP Table of Contents

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Let s Make a Front Panel using FrontCAD

University of Massachusetts Lowell

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

Homework: More Abstraction, Trees, and Lists

Introduction to Excel 2013

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Week 5 Creating a Calendar. About Tables. Making a Calendar From a Table Template. Week 5 Word 2010

Problem Set 4. Part A: Due Tuesday, November 2nd

Transcription:

Lisp legal move generators & search objectives 1. development of legal move generators 2. experimentation with search 3. exposure to pattern matching 4. exposure to new Lisp forms The work described here will lead you through different strategies for developing legal move generator functions and give you some experience of using search mechanisms. You should not expect to complete this work in a single session, in order to complete it you will have to experiment and (probably) check out notes and on-line Lisp guides. outline You have been introduced to the role of legal move generators (LMGs) in lectures. You have also seen an example of a LMG in Lisp. Reminder: a LMG is a function which takes a single state as its argument and returns a list of valid successor states. The most important activity in developing a solution using search is the design of a representation to capture state descriptions. Often there are no "right" and "wrong" ways to specify state descriptions but a set of alternatives with some better than others. Consider the jugs problem introduced in lectures. It is possible to specify state descriptions in various ways, eg: 1. as text: jug1 holds five pints, jug2 holds three pints, jug3 is empty 2. as a set of associations: ((jug1. 5) (jug2. 3) (jug3. 0 3. as a list of numbers: (5 3 0) 4. as a single number: 530...etc... The style of state description obviously influences the design of a LMG. A good style of state description will the LMG much easier to develop. In the example above... option-1 a very poor choice, option-2 ok assuming the LMG is to be based on adding & subtracting volumes but probably by using the symbols jug1,2,3 and the association nesting it becomes unnecessarily detailed and thereby more error prone, option-3 better than option-2, can be used as a style of state description for LMGs based on adding & subtracting volumes and also for an LMG which explicitly maps states onto successor states (see below); option-4 not such a good choice as option-3 for an LMG based on numeric manipulation (adding & subtracting volumes) but good for an LMG which explicitly maps states onto their successor states (see below). Example of a LMG which explicitly maps states to successor states

This LMG (which is very restricted) is for a word puzzle game where the goal is to get from one word to another through a series of intermediate steps such that (i) only one letter may be changed at each step and (ii) each step must spell a legal word. For example the word "BOAT" can be transformed into the word "LAST" in 4 steps as follows... Boat - Coat - Cost - Lost - Last A LMG for this puzzle could be written either as a collection of defmatch forms or using an association list with a small lookup function. Example1: using defmatch (defmatch lmg1 (coat) '((boat moat cost) (defmatch lmg1 (cost) '((most lost coat cast) (defmatch lmg1 (boat) '((moat coat boot)...etc... Example 2: using an association list (defparameter *words* '((coat boat moat cost) (cost most lost coat cast) (boat moat coat boot) (moat moot most boat) (moot soot boot loot) (lost last cast loot) ;;...etc... (defun lmg2 (word) (-> *words* word NB: the "->" operator in Utils.lsp, you will need to load this before using "->". You will also need to load search.cl to use the search routine. The search mechanism is called breadthsearch, it's called with 3 arguments: start-state, goal-state and the function body of the LMG (use the abbreviation #' when you pass functions as arguments). Using best-search... lisp prompt> result> (breadth-search 'boat 'last #'lmg2) (boat coat cost lost last) task 1 Think about the farmer-fox-goose-grain problem, design an appropriate state description and build a LMG for it. Try your LMG with the best-search mechanism provided.

task 2 This problem concentrates on the problem of navigating a robot around a maze (a simple example is shown below). 0 1 2 3 4 5 0 S X 1 X 2 X X X X X 3 4 X X X X 5 G The start position is marked S & the goal position marked G. Shaded squares are illegal but we will slowly build up to avoiding these positions. Each state descriptions for this problem is a pair of coordinates, to pull each coordinate pair into x & y parts you will need to write the legal move generator. Use a defmatch form with the first line looking something like this... (defmatch maze-lmg ( (?x?y) )...) you may then refer to then match variables in the body of the defmatch form as #?x and #?y. Your defmatch form should return all the points you can get to from any (x,y) point don't worry about the shaded squares or those outside of the grid just yet (don't allow diagonal moves though). When I developed this I wrote a small helper function a function that doesn't really do anything but help readability. My helper function worked like list but I used it to make new points... (defun point (a b) (list a b > (point 3 4) (3 4) Try out your legal move generator with the breadth first search and trace its action by setting the debug argument of breadth-search to t. > (breadth-search '(0 0) '(5 5) #'maze-lmg)... NB: if your call to breadth-search generates too much volume of output Lisp will abbreviate it by cutting it short. To get full output, surround your call to breadth-search with a pprint call. pprint is pretty-print... (pprint (breadth-search '(0 0) '(5 5) #'maze-lmg

task 3 When you read the output of the search you ran at the end of task 2, you'll notice that the search considers moving to points that are off the grid. To prevent with this we need to do two things (i) find which points are off the grid (ii) remove them from the list of points. Dealing with issue (ii) first... there are a suite of useful Lisp functions which prune different types of values from lists. An interesting pair of functions are called remove-if and remove-if-not. They both take two arguments, a predicate (a function returning true or false) and a list of values. For example: evenp is a predicate which returns true if its numeric argument is even. > (evenp 5) > (evenp 6) t remove-if returns a list after removing any items which satisfy the predicate, remove-if-not removes items which do not satisfy the predicate. > (remove-if #'evenp '(1 2 3 4 5 6 7 8 (1 3 5 7) > (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 (2 4 6 8) evenp is not much use for our purposes since it doesn't help determine which points are off the grid so the next step is to develop a predicate which does this. For the grid shown above this predicate needs to report if an x or y coordinate falls outside the range 0..5. We can develop this using the less than or equal operator... (setf x-ok 3) 3 > (setf x-bad 7) 7 > (>= 5 x-ok 0) t > (>= 5 x-bad 0) A possible in-bounds predicate is given below take time to examine it & understand how it is coded & how it works. (defmatch in-bounds ( (?x?y) ) (and (>= 5 #?x 0) (>= 5 #?y 0) (in-bounds '(3 4 t > (in-bounds '(3 7 > (in-bounds '(-3 4

Use the ideas above to modify your legal move generator so it does not generate points which are off the grid. The best way to do this is to generate all your points then reject (ie: use remove-if-not to filter out) all those which are not in-bounds. task 4 The next stage is to remove/reject coordinate points which are blocked-off in the grid (marked as a shaded X in the diagram above). This can be done in a similar way to dealing with points that are out of bounds but developing the predicate requires a different strategy. The first step is to build a structure containing all illegal (blocked) points... (defvar illegals) (setf illegals '((0 2)(1 4)(2 0)(2 1)...etc... then we can check to see if a point is a member of this structure. The most obvious way to do this is using the member function which returns non- if an item is a member of a list (remember that anything not is considered to be true in Lisp). > (member 'cat '(dog bat cat rat (cat rat) > (member 'cat '(herring haddock kipper carp The member function needs a bit of tweaking when we check for membership of structures (like coordinate pairs) rather than simple, atomic symbols... > (member '(2 3) '((1 2)(2 3)(3 4) > (member '(2 3) '((1 2)(2 3)(3 4 :test #'equal) ((2 3) (3 4 Wrap a call to the member function in the following function definition & then use it to filter out illegal states in your legal move generator. (defun illegal (p)...) Finally... check your legal move generator by inspecting the results of a search and ensuring its recommended path is valid.

task 5 The final modifications to your maze search will make it recommend the cheapest path in mazes where there is a cost associated with different cells in the grid. The maze below has some blocked (illegal) squares and other squares have costs associated with them. 0 1 2 3 4 5 6 0 S 4 X 4 X 4 5 1 3 1 1 2 2 1 5 2 X X 1 X X 8 X 3 1 1 2 5 9 8 1 4 1 7 X X 7 X 1 5 1 1 6 6 4 3 3 6 X 1 X 1 1 1 1 7 X 1 1 1 X 5 G If you look back at the code you have developed for the tasks above, you'll notice that some of the data & functions work together to describe the structure of the maze, you will need to modify some of these. Start by changing your code to define the bounds of the grid and the illegal (blocked) squares. The cost information can be encoded in a variety of ways, the code below deals with costs in a similar way to illegal states, with a data structure and a lookup function. The lookup function assumes any cell which does not have an entry in the data structure has a cost of 1. (defvar costs) (setf costs '((0 1 4) (0 3 4) (0 5 4) (0 6 5) (1 0 3) (1 3 2) (1 4 2) (1 6 5) (2 5 8) (3 2 2) (3 3 5) (3 4 9) (3 5 8) (4 1 7) (4 4 7) (5 2 6) (5 3 6) (5 4 4) (5 5 3) (5 6 3) (7 5 5) (defun get-cost (x y) (or (is-present (`(,x,y?n) costs) #?n) 1 The most important modification you need to make concerns the structure of state descriptions. With the previous maze (the one with no costs), state descriptions were points like (3 2). State descriptions now need to contain cost information as well. One representation you could use is as follows... (cost (X-coordinate Y-coordinate Write a new legal move generator which takes a state encoded as suggested and generates successor states with updated cost information, eg: > (cost-lmg '(7 (3 2) ((8 (2 2 (8 (3 1 (12 (3 3) When your LMG is complete you should try it with the search routine. This time your call to best-search needs to include a bit of extra information. Your state descriptions now include

cost details as well as the state information. The search routine needs to filter out the cost details when checking to see if it has reached the goal state, etc. Given states represented as... (cost (X-coordinate Y-coordinate the filter will be a function which returns (X-coordinate Y-coordinate) the function second will do this, eg: > (second '(12 (5 0) (5 0) Using second as a filter your call to best-first should result in something like this... > (pprint (best-search '(0 (0 0 '(7 6) #'cost-lmg :filter #'second ((41 (7 6 (40 (7 5 (35 (6 5 (34 (6 4 (33 (5 4 (29 (4 4 (22 (3 4 (13 (3 3 (8 (3 2 (6 (2 2 (5 (1 2 (4 (1 1 (3 (1 0 (0 (0 0) Notice that the route returned is the shortest (in terms of the number of grid-squares) but not the least-cost. task 6 To obtain the least cost path the search routine must select the least cost node on each cycle. If we want this to happen we must provide it with an appropriate selector function which selects the least cost node from a collection of nodes. A typical node (including all the path information) looks something like the one below which shows a path from (4 4) through (5 4) and (6 4) to (6 3) with an accumulated cost of 6. ((6 (6 3 (5 (6 4 (4 (5 4 (0 (4 4) One strategy for writing a selector function is to use the in-built function sort which takes the following arguments: 1. a list to be sorted; 2. a comparison function; 3. an optional function key to extract the values to be compared. see examples below & note the use of the :key argument. > (sort '(2 7 5 9 4) #'>) (9 7 5 4 2) > (sort '(2 7 5 9 4) #'<) (2 4 5 7 9) > (sort '((2 the) (7 a) (5 chased) (9 mouse) (4 cat #'< :key #'first) ((2 the) (4 cat) (5 chased) (7 a) (9 mouse Write a suitable function path-cost to extract the accumulated cost from a path... > (path-cost '((6 (6 3 (5 (6 4 (4 (5 4 (0 (4 4 6...check this works with sort...

> (sort '(((25 (2 5 (17 (3 5 (9 (3 4 (0 (4 4) ((18 (3 6 (17 (3 5 (9 (3 4 (0 (4 4) ((6 (6 3 (5 (6 4 (4 (5 4 (0 (4 4) ((11 (6 3 (10 (5 3 (4 (5 4 (0 (4 4) ((16 (5 2 (10 (5 3 (4 (5 4 (0 (4 4) ((8 (6 5 (7 (5 5 (4 (5 4 (0 (4 4) ((10 (5 6 (7 (5 5 (4 (5 4 (0 (4 4) ) #'< :key #'path-cost) (((6 (6 3 (5 (6 4 (4 (5 4 (0 (4 4) ((8 (6 5 (7 (5 5 (4 (5 4 (0 (4 4) ((10 (5 6 (7 (5 5 (4 (5 4 (0 (4 4) ((11 (6 3 (10 (5 3 (4 (5 4 (0 (4 4) ((16 (5 2 (10 (5 3 (4 (5 4 (0 (4 4) ((18 (3 6 (17 (3 5 (9 (3 4 (0 (4 4) ((25 (2 5 (17 (3 5 (9 (3 4 (0 (4 4...and use this as a basis for writing a selector function to choose the path with least associated cost from a list of paths... > (selector '(((25 (2 5 (17 (3 5 (9 (3 4 (0 (4 4) ((18 (3 6 (17 (3 5 (9 (3 4 (0 (4 4) ((6 (6 3 (5 (6 4 (4 (5 4 (0 (4 4) ((11 (6 3 (10 (5 3 (4 (5 4 (0 (4 4) ((16 (5 2 (10 (5 3 (4 (5 4 (0 (4 4) ((8 (6 5 (7 (5 5 (4 (5 4 (0 (4 4) ((10 (5 6 (7 (5 5 (4 (5 4 (0 (4 4) ((6 (6 3 (5 (6 4 (4 (5 4 (0 (4 4) Now you are finally ready to run a best-first search... > (pprint (best-search '(0 (0 0 '(7 6) #'cost-lmg :filter #'second :selector #'selector ((22 (7 6 (21 (6 6 (20 (6 5 (19 (6 4 (18 (6 3 (17 (7 3 (16 (7 2 (15 (7 1 (14 (6 1 (13 (5 1 (12 (5 0 (11 (4 0 (10 (3 0 (9 (3 1 (8 (3 2 (6 (2 2 (5 (1 2 (4 (1 1 (3 (1 0 (0 (0 0) Look at the results & check that the search has now produced the least cost route. Check your solution again by adjusting the costs of the grid and checking the results (changing the two 8 values to 1's works).