Sei sulla pagina 1di 17

Weekly activity 1: Lisp and AIMA.

Installation:

Simple functions:

Load AIMA and search:


Test search:

The Dribble output for this work is as follows:


;; Dribble of #<IO TERMINAL-STREAM> started on 2015-09-09 00:27:37.
#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"log.txt">
[2]> (load 'aima.lisp')
*** - READ from
#<INPUT CONCATENATED-STREAM #<INPUT STRING-INPUT-STREAM>
#<IO TWO-WAY-STREAM
#<IO ECHO-STREAM #<IO TERMINAL-STREAM>
#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"log.txt">>
#<OUTPUT BROADCAST-STREAM #<IO TERMINAL-STREAM>
#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"log.txt">>>>
: an object cannot start with #\)
The following restarts are available:
ABORT
:R1
Abort main loop
Break 1 [3]> :r1
[4]> (aima-load 'search)
*** - EVAL: undefined function AIMA-LOAD
The following restarts are available:
USE-VALUE
:R1
Input a value to be used instead of (FDEFINITION 'AIMALOAD).
RETRY
:R2
Retry
STORE-VALUE
:R3
Input a new value for (FDEFINITION 'AIMA-LOAD).
ABORT
:R4
Abort main loop
Break 1 [5]> :r2
*** - EVAL: undefined function AIMA-LOAD
The following restarts are available:
USE-VALUE
:R1
Input a value to be used instead of (FDEFINITION 'AIMALOAD).
RETRY
:R2
Retry
STORE-VALUE
:R3
Input a new value for (FDEFINITION 'AIMA-LOAD).
ABORT
:R4
Abort main loop
Break 1 [6]> :r3
New (FDEFINITION 'AIMA-LOAD)> ()
*** - EVAL: undefined function AIMA-LOAD
The following restarts are available:
USE-VALUE
:R1
Input a value to be used instead of (FDEFINITION 'AIMALOAD).
RETRY
:R2
Retry
STORE-VALUE
:R3
Input a new value for (FDEFINITION 'AIMA-LOAD).
ABORT
:R4
Abort main loop
Break 1 [7]> :a
[8]>
Bye.
;; Dribble of #<IO TERMINAL-STREAM> started on 2015-09-09 00:33:22.
#<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"log.txt">
[2]> (load "aima.lisp")
;; Loading file aima.lisp ...
;; Loading file
/Users/atm/_code/coursework/cs6364/code/utilities/utilities.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/utilities/utilities.lisp
;; Loading file /Users/atm/_code/coursework/cs6364/code/utilities/binarytree.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/utilities/binarytree.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/utilities/queue.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/utilities/queue.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/utilities/cltl2.lisp ...

;; Loaded file /Users/atm/_code/coursework/cs6364/code/utilities/cltl2.lisp


;; Loading file /Users/atm/_code/coursework/cs6364/code/utilities/testutilities.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/utilities/testutilities.lisp
;; Loaded file aima.lisp
T
[3]> (aima-load 'search)
;; Loading file /Users/atm/_code/coursework/cs6364/code/agents/testagents.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/agents/test-agents.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/agents/environments/basic-env.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/agents/environments/basic-env.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/agents/environments/grid-env.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/agents/environments/gridenv.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/agents/environments/vacuum.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/agents/environments/vacuum.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/agents/environments/wumpus.lisp ...
** - Continuable Error
DEFUN/DEFMACRO(KILL): #<PACKAGE POSIX> is locked
If you continue (by typing 'continue'): Ignore the lock and proceed
The following restarts are also available:
SKIP
:R1
skip (DEFUN KILL # ...)
RETRY
:R2
retry (DEFUN KILL # ...)
STOP
:R3
stop loading file
/Users/atm/_code/coursework/cs6364/code/agents/environments/wumpus.lisp
ABORT
:R4
Abort main loop
Break 1 [4]> :r3
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/agents/environments/wumpus.lisp
;; Loading file /Users/atm/_code/coursework/cs6364/code/agents/agents/agent.lisp
...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/agents/agents/agent.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/agents/agents/vacuum.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/agents/agents/vacuum.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/agents/agents/wumpus.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/agents/agents/wumpus.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/agents/algorithms/grid.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/agents/algorithms/grid.lisp
;; Loading file /Users/atm/_code/coursework/cs6364/code/search/testsearch.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/search/test-search.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/problems.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/problems.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/simple.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/simple.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/repeated.lisp ...
;; Loaded file

/Users/atm/_code/coursework/cs6364/code/search/algorithms/repeated.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/csp.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/csp.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/ida.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/ida.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/iterative.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/iterative.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/sma.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/sma.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/minimax.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/algorithms/minimax.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/environments/games.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/environments/games.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/environments/prob-solve.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/search/environments/probsolve.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/domains/cannibals.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/domains/cannibals.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/domains/ttt.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/search/domains/ttt.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/domains/cognac.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/domains/cognac.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/domains/nqueens.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/domains/nqueens.lisp
;; Loading file /Users/atm/_code/coursework/cs6364/code/search/domains/pathplanning.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/search/domains/pathplanning.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/domains/puzzle8.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/domains/puzzle8.lisp
;; Loading file /Users/atm/_code/coursework/cs6364/code/search/domains/routefinding.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/search/domains/routefinding.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/domains/tsp.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/search/domains/tsp.lisp
;; Loading file
/Users/atm/_code/coursework/cs6364/code/search/domains/vacuum.lisp ...
;; Loaded file
/Users/atm/_code/coursework/cs6364/code/search/domains/vacuum.lisp
;; Loading file /Users/atm/_code/coursework/cs6364/code/search/agents/ps-

agents.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/search/agents/psagents.lisp
;; Loading file /Users/atm/_code/coursework/cs6364/code/search/agents/tttagent.lisp ...
;; Loaded file /Users/atm/_code/coursework/cs6364/code/search/agents/tttagent.lisp
T
[5]> (test 'search)
Testing System SEARCH
;;; Test the code for Solving Problems by Searching
;;; Start with a trivial version of the missionaries and cannibals puzzle.
>
(SETQ P1
(MAKE-CANNIBAL-PROBLEM :INITIAL-STATE (MAKE-CANNIBAL-STATE :M1 2 :C1 1)))
#<a CANNIBAL-PROBLEM>
;;; We search for a solution node:
> (SETQ RESULT (BREADTH-FIRST-SEARCH P1))
#<NODE f(3) = g(3) + h(0) state:(0 0 0 2 1 1)>
;;; We can get information out of that solution:
> (SOLUTION-ACTIONS RESULT)
((2 0 1) (-1 0 -1) (1 1 1))
> (SOLUTION-NODES RESULT)
(#<NODE f(0) = g(0) + h(0) state:(2 1 1 0 0 0)>
#<NODE f(1) = g(1) + h(0) state:(0 1 0 2 0 1)>
#<NODE f(2) = g(2) + h(0) state:(1 1 1 1 0 0)>
#<NODE f(3) = g(3) + h(0) state:(0 0 0 2 1 1)>)
;;;
Or we can use SOLVE to print the results nicely. By default, SOLVE
uses A*-search, but you can give it another algorithm as the second arg.
> (SOLVE P1)
Action
State
======
=====
(2 1 1 0 0 0)
(1 1 1)
(1 0 0 1 1 1)
(-1 0 -1)
(2 0 1 0 1 0)
(2 0 1)
(0 0 0 2 1 1)
======
=====
Total of 7 nodes expanded.
#<NODE f(3) = g(3) + h(0) state:(0 0 0 2 1 1)>
;;; For the full 3 missionary and 3 cannibal problem, breadth-first-search
;;; is very inefficient. Better to use something that handles repeated states,
;;; like A*-search or no-duplicates-breadth-first-search:
> (SOLVE (MAKE-CANNIBAL-PROBLEM) 'A*-SEARCH)
Action
State
======
=====
(3 3 1 0 0 0)
(1 1 1)
(2 2 0 1 1 1)
(-1 0 -1)
(3 2 1 0 1 0)
(0 2 1)
(3 0 0 0 3 1)
(0 -1 -1)
(3 1 1 0 2 0)
(2 0 1)
(1 1 0 2 2 1)
(-1 -1 -1)
(2 2 1 1 1 0)
(2 0 1)
(0 2 0 3 1 1)
(0 -1 -1)
(0 3 1 3 0 0)
(0 2 1)
(0 1 0 3 2 1)
(0 -1 -1)
(0 2 1 3 1 0)
(0 2 1)
(0 0 0 3 3 1)
======
=====
Total of 14 nodes expanded.
#<NODE f(11) = g(11) + h(0) state:(0 0 0 3 3 1)>
> (SOLVE (MAKE-CANNIBAL-PROBLEM) 'NO-DUPLICATES-BREADTH-FIRST-SEARCH)
Action
State
======
=====

(3 3 1 0 0 0)
(1 1 1)
(2 2 0 1 1 1)
(-1 0 -1)
(3 2 1 0 1 0)
(0 2 1)
(3 0 0 0 3 1)
(0 -1 -1)
(3 1 1 0 2 0)
(2 0 1)
(1 1 0 2 2 1)
(-1 -1 -1)
(2 2 1 1 1 0)
(2 0 1)
(0 2 0 3 1 1)
(0 -1 -1)
(0 3 1 3 0 0)
(0 2 1)
(0 1 0 3 2 1)
(0 -1 -1)
(0 2 1 3 1 0)
(0 2 1)
(0 0 0 3 3 1)
======
=====
Total of 14 nodes expanded.
#<NODE f(11) = g(11) + h(0) state:(0 0 0 3 3 1)>
;;; Here is how to get a problem-solving agent to find the solution,
;;; and then go ahead and execute the actions that comprise the solution.
> (RUN-ENVIRONMENT (PROBLEM->ENVIRONMENT P1))
At Time step 0:
#<PROBLEM-SOLVING-ENVIRONMENT; State: (2 1 1 0 0 0)>
At Time step 1:
Agent [AGENT-BODY> = 0] perceives (2 1 1 0 0 0)
and does (1 1 1)
#<PROBLEM-SOLVING-ENVIRONMENT; State: (1 0 0 1 1 1)>
At Time step 2:
Agent [AGENT-BODY> = 0] perceives (1 0 0 1 1 1)
and does (-1 0 -1)
#<PROBLEM-SOLVING-ENVIRONMENT; State: (2 0 1 0 1 0)>
At Time step 3:
Agent [AGENT-BODY> = 1] perceives (2 0 1 0 1 0)
and does (2 0 1)
#<PROBLEM-SOLVING-ENVIRONMENT; State: (0 0 0 2 1 1)>
#<PROBLEM-SOLVING-ENVIRONMENT; State: (0 0 0 2 1 1)>
;;; Now we look at the route-finding domain.
;;; First, solve the Arad-to-Bucharest problem with A*-search:
> (SOLVE (MAKE-ROMANIAN-PROBLEM :INITIAL-STATE 'ARAD :GOAL 'BUCHAREST))
Action
State
======
=====
ARAD
SIBIU
SIBIU
RIMNICU
RIMNICU
PITESTI
PITESTI
BUCHAREST
BUCHAREST
======
=====
Total of 5 nodes expanded.
#<NODE f(418) = g(418) + h(0) state:BUCHAREST>
;;; Now turn it around:
> (SOLVE (MAKE-ROMANIAN-PROBLEM :GOAL 'ARAD :INITIAL-STATE 'BUCHAREST))
Action
State
======
=====
BUCHAREST
PITESTI
PITESTI
RIMNICU
RIMNICU
SIBIU
SIBIU
ARAD
ARAD
======
=====
Total of 4 nodes expanded.
#<NODE f(418) = g(418) + h(0) state:ARAD>
;;; Let's just see the actions:
> (SOLUTION-ACTIONS (A*-SEARCH (MAKE-ROMANIAN-PROBLEM)))

(SIBIU RIMNICU PITESTI BUCHAREST)


;;; Now on a random map:
> (SOLVE (MAKE-ROUTE-FINDING-PROBLEM))
Action
State
======
=====
A
J
J
D
D
B
B
======
=====
Total of 7 nodes expanded.
#<NODE f(154) = g(154) + h(0) state:B>
;;; Here's how to compare several algorithms.
>
(SETQ SEARCHERS
'(A*-SEARCH NO-CYCLES-DEPTH-FIRST-SEARCH NO-DUPLICATES-BREADTH-FIRST-SEARCH))
(A*-SEARCH NO-CYCLES-DEPTH-FIRST-SEARCH NO-DUPLICATES-BREADTH-FIRST-SEARCH)
> (COMPARE-SEARCH-ALGORITHMS #'MAKE-ROUTE-FINDING-PROBLEM SEARCHERS)
Solved Cost Length Nodes Algorithm
====== ====== ====== ======= =========
10
54.4
1.6
1.9 A*-SEARCH
10
147.0
2.7
4.3 NO-CYCLES-DEPTH-FIRST-SEARCH
10
55.7
1.5
5.0 NO-DUPLICATES-BREADTH-FIRST-SEARCH
NIL
> (COMPARE-SEARCH-ALGORITHMS #'MAKE-ROMANIAN-PROBLEM SEARCHERS :N 1)
Solved Cost Length Nodes Algorithm
====== ====== ====== ======= =========
1
418.0
4.0
5.0 A*-SEARCH
1
607.0
5.0
5.0 NO-CYCLES-DEPTH-FIRST-SEARCH
1
450.0
3.0
11.0 NO-DUPLICATES-BREADTH-FIRST-SEARCH
NIL
>
(COMPARE-SEARCH-ALGORITHMS #'MAKE-CANNIBAL-PROBLEM
'(NO-RETURNS-BREADTH-FIRST-SEARCH A*-SEARCH
NO-DUPLICATES-BREADTH-FIRST-SEARCH)
:N 1)
Solved Cost Length Nodes Algorithm
====== ====== ====== ======= =========
1
11.0
11.0
55.0 NO-RETURNS-BREADTH-FIRST-SEARCH
1
11.0
11.0
14.0 A*-SEARCH
1
11.0
11.0
14.0 NO-DUPLICATES-BREADTH-FIRST-SEARCH
NIL
>
(COMPARE-SEARCH-ALGORITHMS #'MAKE-ROMANIAN-PROBLEM
'(TREE-A*-SEARCH A*-SEARCH TREE-IDA*-SEARCH) :N 1)
Solved Cost Length Nodes Algorithm
====== ====== ====== ======= =========
1
418.0
4.0
5.0 TREE-A*-SEARCH
1
418.0
4.0
5.0 A*-SEARCH
1
418.0
4.0
20.0 TREE-IDA*-SEARCH
NIL
;;; We'll look at the iterative improvement algorithms on a harder map problem.
> (SETQ SEARCHERS '(A*-SEARCH HILL-CLIMBING-SEARCH SIMULATED-ANNEALING-SEARCH))
(A*-SEARCH HILL-CLIMBING-SEARCH SIMULATED-ANNEALING-SEARCH)
>
(COMPARE-SEARCH-ALGORITHMS #'(LAMBDA NIL (MAKE-ROMANIAN-PROBLEM :GOAL 'IASI))
SEARCHERS :N 1)
Solved Cost Length Nodes Algorithm
====== ====== ====== ======= =========
1
737.0
7.0
14.0 A*-SEARCH
0
0.0
0.0
3.0 HILL-CLIMBING-SEARCH
0
0.0
0.0
9.0 SIMULATED-ANNEALING-SEARCH
NIL
;;; Let's take a look at the 8-puzzle:

> (SOLVE (MAKE-8-PUZZLE-PROBLEM))


Action
State
======
=====
233451332
V
233333396
<
233543348
V
219214124
>
27895364
^
199846052
<
200003516
<
200038508
^
200044332
>
200044324
>
200044180
V
199867276
V
27916588
<
219235348
^
247893796
======
=====
Total of 36 nodes expanded.
#<NODE f(14) = g(14) + h(0) state:247893796>
> (COMPARE-SEARCH-ALGORITHMS 'MAKE-8-PUZZLE-PROBLEM '(A*-SEARCH) :N 2)
Solved Cost Length Nodes Algorithm
====== ====== ====== ======= =========
2
20.0
20.0
162.5 A*-SEARCH
NIL
;;; And the path-planning problem among polygonal obstacles:
> (SOLVE (MAKE-PATH-PLANNING-PROBLEM :SCENE *SCENE-4.17*))
Action
State
======
=====
#<V 112,660>
(39 10)
#<V 151,670>
(47 -35)
#<V 198,635>
(22 -19)
#<V 220,616>
(60 -33)
#<V 280,583>
(59 -5)
#<V 339,578>
(14 -5)
#<V 353,573>
======
=====
Total of 7 nodes expanded.
#<NODE f(270.48468) = g(270.48468) + h(0) state:#<V 353,573>>
;;; Now the 8-queens problem
> (SOLVE (MAKE-NQUEENS-PROBLEM) 'CSP-BACKTRACKING-SEARCH)
Action
State
======
=====
#<CSP NIL>
(0 . 7)
#<CSP ((0 (0 1 2 3 4 5 6 7) 7 NIL))>
(1 . 3)
#<CSP ((1 (0 1 2 3 4 5 6 7) 3 NIL) (0 (0 1 2 3 4 5 6 7) 7
NIL))>
(2 . 0)
#<CSP ((2 (0 1 2 3 4 5 6 7) 0 NIL) (1 (0 1 2 3 4 5 6 7) 3 NIL)
(0 (0 1 2 3 4 5 6 7) 7 NIL))>
(3 . 2)
#<CSP ((3 (0 1 2 3 4 5 6 7) 2 NIL) (2 (0 1 2 3 4 5 6 7) 0 NIL)
(1 (0 1 2 3 4 5 6 7) 3 NIL) (0 (0 1 2 3 4 5 6 7) 7 NIL))>
(4 . 5)
#<CSP ((4 (0 1 2 3 4 5 6 7) 5 NIL) (3 (0 1 2 3 4 5 6 7) 2 NIL)
(2 (0 1 2 3 4 5 6 7) 0 NIL) (1 (0 1 2 3 4 5 6 7) 3 NIL)
(0 (0 1 2 3 4 5 6 7) 7 NIL))>
(5 . 1)
#<CSP ((5 (0 1 2 3 4 5 6 7) 1 NIL) (4 (0 1 2 3 4 5 6 7) 5 NIL)
(3 (0 1 2 3 4 5 6 7) 2 NIL) (2 (0 1 2 3 4 5 6 7) 0 NIL)
(1 (0 1 2 3 4 5 6 7) 3 NIL) (0 (0 1 2 3 4 5 6 7) 7 NIL))>
(6 . 6)
#<CSP ((6 (0 1 2 3 4 5 6 7) 6 NIL) (5 (0 1 2 3 4 5 6 7) 1 NIL)

(4 (0 1 2 3 4 5 6 7) 5 NIL) (3 (0 1 2 3 4 5 6 7) 2 NIL)
(2 (0 1 2 3 4 5 6 7) 0 NIL) (1 (0 1 2 3 4 5 6 7) 3 NIL)
(0 (0 1 2 3 4 5 6 7) 7 NIL))>
(7 . 4)
#<CSP ((7 (0 1 2 3 4 5 6 7) 4 NIL) (6 (0 1 2 3 4 5 6 7) 6 NIL)
(5 (0 1 2 3 4 5 6 7) 1 NIL) (4 (0 1 2 3 4 5 6 7) 5 NIL)
(3 (0 1 2 3 4 5 6 7) 2 NIL) (2 (0 1 2 3 4 5 6 7) 0 NIL)
(1 (0 1 2 3 4 5 6 7) 3 NIL) (0 (0 1 2 3 4 5 6 7) 7 NIL))>
======
=====
Total of 113 nodes expanded.
#<NODE f(8) = g(8) + h(0) state:#<CSP ((7 (0 1 2 3 4 5 6 7) 4 NIL)
(6 (0 1 2 3 4 5 6 7) 6 NIL) (5 (0 1 2 3 4 5 6 7) 1 NIL)
(4 (0 1 2 3 4 5 6 7) 5 NIL) (3 (0 1 2 3 4 5 6 7) 2 NIL)
(2 (0 1 2 3 4 5 6 7) 0 NIL) (1 (0 1 2 3 4 5 6 7) 3 NIL)
(0 (0 1 2 3 4 5 6 7) 7 NIL))>>
>
(COMPARE-SEARCH-ALGORITHMS 'MAKE-NQUEENS-PROBLEM
'(CSP-BACKTRACKING-SEARCH CSP-FORWARD-CHECKING-SEARCH) :N 1)
Solved Cost Length Nodes Algorithm
====== ====== ====== ======= =========
1
8.0
8.0
113.0 CSP-BACKTRACKING-SEARCH
1
8.0
8.0
53.0 CSP-FORWARD-CHECKING-SEARCH
NIL
;;; Here is the Travelling Salesperson Problem (TSP).
> (SOLVE (MAKE-TSP-PROBLEM))
Action
State
======
=====
((F) (E D C B A))
B
((B F) (E D C A))
D
((D B F) (E C A))
C
((C D B F) (E A))
A
((A C D B F) (E))
E
((E A C D B F) NIL)
F
((F E A C D B F) NIL)
======
=====
Total of 26 nodes expanded.
#<NODE f(213) = g(213) + h(0) state:((F E A C D B F) NIL)>
>
(COMPARE-SEARCH-ALGORITHMS 'MAKE-TSP-PROBLEM
'(A*-SEARCH GREEDY-SEARCH UNIFORM-COST-SEARCH) :N 5)
Solved Cost Length Nodes Algorithm
====== ====== ====== ======= =========
5
307.2
6.0
12.6 A*-SEARCH
5
361.4
6.0
6.2 GREEDY-SEARCH
5
458.8
6.0
326.0 UNIFORM-COST-SEARCH
NIL
;;; Now we test the environments for 2-player and 3-player games:
> (RUN-GAME (MAKE-TTT-GAME))
|---|---|---|
2 | - | - | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | - | - | - |
|---|---|---|
0
1
2
X to move:
X moved to (1 2).
|---|---|---|
2 | - | X | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | - | - | - |

|---|---|---|
0
1
2
O to move:
O moved to (0 0).
|---|---|---|
2 | - | X | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | O | - | - |
|---|---|---|
0
1
2
X to move:
X moved to (0 2).
|---|---|---|
2 | X | X | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | O | - | - |
|---|---|---|
0
1
2
O to move:
O moved to (1 0).
|---|---|---|
2 | X | X | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | O | O | - |
|---|---|---|
0
1
2
X to move:
X moved to (2 0).
|---|---|---|
2 | X | X | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | O | O | X |
|---|---|---|
0
1
2
O to move:
O moved to (2 1).
|---|---|---|
2 | X | X | - |
|---|---|---|
1 | - | - | O |
|---|---|---|
0 | O | O | X |
|---|---|---|
0
1
2
X to move:
X moved to (1 1).
|---|---|---|
2 | X | X | - |
|---|---|---|
1 | - | X | O |
|---|---|---|
0 | O | O | X |
|---|---|---|
0
1
2
O to move:
#<GAME-ENVIRONMENT; Step: 7, Agents: [X = 1] [O = -1]>

> (RUN-GAME (MAKE-COGNAC-GAME :PLAYERS '(X O @)))


|---|---|---|
2 | - | - | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | - | - | - |
|---|---|---|
0
1
2
X to move:
X moved to (1 0).
|---|---|---|
2 | - | - | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | - | X | - |
|---|---|---|
0
1
2
O to move:
O moved to (2 0).
|---|---|---|
2 | - | - | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | - | X | O |
|---|---|---|
0
1
2
@ to move:
@ moved to (2 1).
|---|---|---|
2 | - | - | - |
|---|---|---|
1 | - | - | @ |
|---|---|---|
0 | - | X | O |
|---|---|---|
0
1
2
X to move:
X moved to (0 0).
|---|---|---|
2 | - | - | - |
|---|---|---|
1 | - | - | @ |
|---|---|---|
0 | X | X | O |
|---|---|---|
0
1
2
O to move:
O moved to (2 2).
|---|---|---|
2 | - | - | O |
|---|---|---|
1 | - | - | @ |
|---|---|---|
0 | X | X | O |
|---|---|---|
0
1
2
@ to move:
@ moved to (0 1).
|---|---|---|
2 | - | - | O |
|---|---|---|

1 | @ | - | @ |
|---|---|---|
0 | X | X | O |
|---|---|---|
0
1
2
X to move:
X moved to (1 1).
|---|---|---|
2 | - | - | O |
|---|---|---|
1 | @ | X | @ |
|---|---|---|
0 | X | X | O |
|---|---|---|
0
1
2
O to move:
O moved to (1 2).
|---|---|---|
2 | - | O | O |
|---|---|---|
1 | @ | X | @ |
|---|---|---|
0 | X | X | O |
|---|---|---|
0
1
2
@ to move:
@ moved to (0 2).
|---|---|---|
2 | @ | O | O |
|---|---|---|
1 | @ | X | @ |
|---|---|---|
0 | X | X | O |
|---|---|---|
0
1
2
X to move:
#<GAME-ENVIRONMENT; Step: 9, Agents: [X = 0] [O = 0] [@ = 0]>
;;; Now we see that 2x2 tic-tac-toe is a win for the first player, X.
>
(RUN-GAME (MAKE-TTT-GAME :N 2) :AGENTS
'(ALPHA-BETA-TTT-AGENT ALPHA-BETA-TTT-AGENT))
|---|---|
1 | - | - |
|---|---|
0 | - | - |
|---|---|
0
1
X to move:
X moved to (1 1).
|---|---|
1 | - | X |
|---|---|
0 | - | - |
|---|---|
0
1
O to move:
O moved to (1 0).
|---|---|
1 | - | X |
|---|---|
0 | - | O |
|---|---|
0
1
X to move:

X moved to (0 1).
|---|---|
1 | X | X |
|---|---|
0 | - | O |
|---|---|
0
1
O to move:
#<GAME-ENVIRONMENT; Step: 3, Agents: [X = 1] [O = -1]>
;;; In a full 3x3 game, alpha-beta search (playing O) often wins.
> (RUN-GAME (MAKE-TTT-GAME) :AGENTS '(RANDOM-GAME-AGENT ALPHA-BETA-TTT-AGENT))
|---|---|---|
2 | - | - | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | - | - | - |
|---|---|---|
0
1
2
X to move:
X moved to (0 2).
|---|---|---|
2 | X | - | - |
|---|---|---|
1 | - | - | - |
|---|---|---|
0 | - | - | - |
|---|---|---|
0
1
2
O to move:
O moved to (1 1).
|---|---|---|
2 | X | - | - |
|---|---|---|
1 | - | O | - |
|---|---|---|
0 | - | - | - |
|---|---|---|
0
1
2
X to move:
X moved to (0 0).
|---|---|---|
2 | X | - | - |
|---|---|---|
1 | - | O | - |
|---|---|---|
0 | X | - | - |
|---|---|---|
0
1
2
O to move:
O moved to (0 1).
|---|---|---|
2 | X | - | - |
|---|---|---|
1 | O | O | - |
|---|---|---|
0 | X | - | - |
|---|---|---|
0
1
2
X to move:
X moved to (2 2).
|---|---|---|
2 | X | - | X |
|---|---|---|

1 | O | O | - |
|---|---|---|
0 | X | - | - |
|---|---|---|
0
1
2
O to move:
O moved to (2 1).
|---|---|---|
2 | X | - | X |
|---|---|---|
1 | O | O | O |
|---|---|---|
0 | X | - | - |
|---|---|---|
0
1
2
X to move:
#<GAME-ENVIRONMENT; Step: 6, Agents: [X = -1] [O = 1]>
0 errors on system SEARCH
0
[6]> (dribble)
;; Dribble of #<IO TERMINAL-STREAM> finished on 2015-09-09 00:33:50.

Potrebbero piacerti anche