Skip to content

Commit

Permalink
Finished Chapter 18: Lazy Programming
Browse files Browse the repository at this point in the history
Add :dice-of-doom-v2 and :lazy packages.

What was covered on this chapter:
* Lazy Programming allows you to work with very large (and even infinite)
  data structures and do so efficiently
* Once you have a lazy macro and a force function, you can use them to
  build more sophisticated lazy operations, including building a lazy list
  library (DONE).
* Heuristics are imperfect algorithms that can be used to improve the
  performance of your code, with some creative thinking. In our example,
  we made some heuristic changes to how we score leaf nodes.
* Once we converted Dice of Doom to use a lazy tree, we were able to
  elegantly trim the game tree in order to limit how deep the AI thinks
  when contemplating its moves.
* Alpha-beta pruning lets us improve performance even more, by pruning
  branches that have no way of impacting the final scores on the moves
  being considered by the AI.
  • Loading branch information
ryukinix committed Mar 16, 2017
1 parent 1676e3a commit e2d57e1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ A personal repository for annotation about learning lisp patterns.
The current content are answers plus code covering of the book [Land of Lisp](http://www.landoflisp.com) and the insights at the MIT 6.001 Course: Structures and Interpretations of Computer Programs.


# Land of Lisp book (reading) [386/482]
![progress](http://progressed.io/bar/81)
# Land of Lisp book (reading) [400/482]
![progress](http://progressed.io/bar/82)


- [x] Section I: Lisp is Power
Expand All @@ -29,7 +29,7 @@ The current content are answers plus code covering of the book [Land of Lisp](ht
- [x] Chapter 15 (Dice of Doom, a Game Written in the Functional Style)
- [x] Chapter 16 (The Magic of Lisp Macros)
- [x] Chapter 17 (Domain-Specific Languages)
- [ ] Chapter 18 (Lazy Programming)
- [x] Chapter 18 (Lazy Programming)
- [ ] Chapter 19 (Creating a Graphical, Web-Based Version of Dice of Doom)
- [ ] Chapter 20 (Making Dice of Doom More Fun)

Expand Down
69 changes: 64 additions & 5 deletions land-of-lisp/cap18-dice-of-doom-v2.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@

(defparameter *ai-level* 4) ;; depth to look on tree of game

(defun handle-computer (tree)
(let ((ratings (get-ratings (limit-tree-depth tree *ai-level*)
(car tree))))
(cadr (lazy-nth (position (apply #'max ratings) ratings)
(caddr tree)))))
;; OLD NON-OPTIMIZED COMPUTER AI
;; (defun handle-computer (tree)
;; (let ((ratings (get-ratings (limit-tree-depth tree *ai-level*)
;; (car tree))))
;; (cadr (lazy-nth (position (apply #'max ratings) ratings)
;; (caddr tree)))))


(defun play-vs-computer (tree)
Expand Down Expand Up @@ -148,3 +149,61 @@
(get-ratings tree player))
(get-ratings tree player))
(score-board (cadr tree) player)))

;; The next functions will be just a optimization of AI algorithm
;; to exclude bad branches on game tree using the Alpha-beta technique


(defun ab-rate-position (tree player upper-limit lower-limit)
(let ((moves (caddr tree)))
(if (not (lazy-null moves))
(if (eq (car tree) player)
(apply #'max (ab-get-ratings-max tree
player
upper-limit
lower-limit))
(apply #'min (ab-get-ratings-min tree
player
upper-limit
lower-limit)))
(score-board (cadr tree) player))))


(defun ab-get-ratings-max (tree player upper-limit lower-limit)
(labels ((f (moves lower-limit)
(unless (lazy-null moves)
(let ((x (ab-rate-position (cadr (lazy-car moves))
player
upper-limit
lower-limit)))
(if (>= x upper-limit)
(list x)
(cons x (f (lazy-cdr moves)
(max x lower-limit))))))))
(f (caddr tree) lower-limit)))


(defun ab-get-ratings-min (tree player upper-limit lower-limit)
(labels ((f (moves upper-limit)
(unless (lazy-null moves)
(let ((x (ab-rate-position (cadr (lazy-car moves))
player
upper-limit
lower-limit)))
(if (<= x lower-limit)
(list x)
(cons x (f (lazy-cdr moves)
(min x upper-limit))))))))
(f (caddr tree) upper-limit)))


(defun handle-computer (tree)
(let ((ratings (ab-get-ratings-max (limit-tree-depth tree *ai-level*)
(car tree)
most-positive-fixnum
most-negative-fixnum)))
(cadr (lazy-nth (position (apply #'max ratings) ratings) (caddr tree)))))


(defparameter *board-size* 5)
(defparameter *board-hexnum* (* *board-size* *board-size*))

0 comments on commit e2d57e1

Please sign in to comment.