Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Installation:
Simple functions:
/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)))
(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]>
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.