SlideShare a Scribd company logo

知能工学実験 last report.pdf

2
2

実験レポート

知能工学実験 last report.pdf

1 of 15
Download to read offline
知能工学実験Ⅱ
最終課題
学籍番号 2120072 後藤颯希
1. lisp のプログラム
(defconstant Board-Size 8)
(defconstant Board-Dimensions (list Board-Size Board-Size))
(defconstant Board-Sente 1)
(defconstant Board-Gote -1)
(defconstant Board-Empty 0)
(defconstant Board-Sente-String "*")
(defconstant Board-Gote-String "o")
(defconstant Board-Empty-String "-")
(setq upcount 0)
(defconstant Board-Initial
#2A((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 -1 1 0 0 0) (0 0 0 1 -1 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0
0 0)))
;20 手目までの評価値
(defconstant Board-judge<=20
#2A((30 0.1 1 1 1 1 0.1 30) (0.1 0.01 10 10 10 10 0.01 0.1) (2 10 20 20 20 20 10 2) (2 10 20 -1 1 20 10 2) (2 10 20 1 -1 20 10 2) (2 10
20 20 20 20 10 2) (0.1 0.01 10 10 10 10 0.01 0.1) (30 0.1 1 1 1 1 0.1 30)))
;40 手目までの評価値
(defconstant Board-judge<=40
#2A((30 0.1 5 10 5 10 0.1 30) (0.1 0.01 10 15 15 10 0.01 0.1) (5 10 20 20 20 20 10 5) (10 15 20 -1 1 20 15 10) (5 15 20 1 -1 20 15 5)
(10 10 20 20 20 20 0 10) (0.1 0.01 10 15 15 10 0.01 0.1) (30 0.1 5 10 5 10 0.1 30)))
;40 手目以降の評価値
(defconstant Board-judge>40
#2A((30 0.1 15 10 10 15 0.1 30) (0.1 0.01 10 15 15 10 0.01 0.1) (15 10 20 20 20 20 10 15) (10 15 20 -1 1 20 15 10) (10 15 20 1 -1 20 15
10) (15 10 20 20 20 20 10 15) (0.1 0.01 10 15 15 10 0.01 0.1) (30 0.1 15 10 10 15 0.1 30)))
(defun change-turn (turn)
(cond ((equal turn Board-Sente) Board-Gote)
(t Board-Sente)))
(defun symbol-turn (turn)
(cond ((equal turn Board-Sente) Board-Sente-String)
((equal turn Board-Gote) Board-Gote-String)
(t Board-Empty-String)))
(defun make-board () (make-array Board-Dimensions))
(defun ref-board (board row col)
(aref board row col))
(defun set-board (board row col turn)
(setf (aref board row col) turn))
(defun copy-board (board)
(let ((new-board (make-board)))
(dotimes (row Board-Size new-board)
(dotimes (col Board-Size)
(set-board new-board row col (ref-board board row col))))))
(defun print-board (board)
(format t "R/C 0 1 2 3")
(dotimes (row Board-Size nil)
(format t "~%")
(format t " ~A " row)
(dotimes (col Board-Size nil)
(format t "~A " (symbol-turn (ref-board board row col)))
)
)
)
(defconstant Board-Directions '((0 1) (1 1) (1 0) (1 -1) (0 -1) (-1 -1) (-1 0) (-1 1)))
(defun sample-scan (board row0 col0 row-inc col-inc)
(do ((row row0 (+ row row-inc))
(col col0 (+ col col-inc))
(length 0 (1+ length)))
((or (< row 0) (= row Board-Size) (< col 0) (= col Board-Size)
(equal (ref-board board row col) Board-Empty))
length)))
(defun board-scan (board row0 col0 row-inc col-inc turn)
(do ((row (+ row0 row-inc) (+ row row-inc))
(col (+ col0 col-inc) (+ col col-inc))
(length 0 (1+ length)))
((or (< row 0) (= row Board-Size) (< col 0) (= col Board-Size)
(eql (ref-board board row col) Board-Empty)) 0)
(cond ((equal (ref-board board row col) turn) (return length))
)))
(defun board-movable (board row col turn)
(and (equal (ref-board board row col) Board-Empty)
(dolist (dir Board-Directions nil)
(cond ((> (board-scan board row col (car dir) (cadr dir) turn) 0)
(return t))))))
(defun board-movable-any (board turn)
(let ((judge nil))
(dotimes (row Board-Size judge)
(dotimes (col Board-Size)
(cond ((board-movable board row col turn)
(setq judge t))
)
)
)
)
)
(defun othello-by-human ()
(do ((board (copy-board Board-Initial))
(turn Board-Sente (change-turn turn))
(count 1 (1+ count))
state)
((numberp (setq state (board-state board turn)))
(game-result board state))
(print-board board)
(cond ((equal state 'pass)
(format t "(~A) Pass. ~%" count))
(t (play-by-human board turn count))))
)
(defun board-state (board turn)
(cond ((board-movable-any board turn) nil)
((and (not (board-movable-any board turn)) (board-movable-any board (change-turn turn))) 'pass)
(t (board-eval board))
)
)
(defun board-eval (board)
(let ((black 0) (white 0) (score 0))
(dotimes (row Board-Size score)
(dotimes (col Board-Size)
(cond ((equal (ref-board board row col) 1) (setq black (1+ black)))
((equal (ref-board board row col) -1) (setq white (1+ white)))
)
(cond ((and (= black 0) (= white 0)) (setq score 0))
(t (setq score (/ (- black white) (+ black white))))
)
)
)
)
)
(defun game-result (board state)
(print-board board)
(format t "~%")
(cond ((< state 0) (format t "Game won by o ~%"))
((> state 0) (format t "Game won by * ~%"))
(t (format t "Draw ~%"))
)
(let ((black 0) (white 0) (scoreresult 0))
(dotimes (row Board-Size scoreresult)
(dotimes (col Board-Size)
(cond ((equal (ref-board board row col) 1) (setq black (1+ black)))
((equal (ref-board board row col) -1) (setq white (1+ white)))
)
))
(setq scoreresult (format t "Score is ~A - ~A" black white ))
)
)
(defun play-by-human (board turn count)
(format t "(~A) Enter row and col for ~A:" count (symbol-turn turn))
(do ((row (read) (read))
(col (read) (read)))
((and (board-number-check row col)
(board-movable board row col turn))
(board-move board row col turn))
(format t "Re-enter row and col for ~A:" (symbol-turn turn))))
(defun board-move (board row col turn)
(dolist (dir Board-Directions board)
(let ((row row) (col col))
(set-board board row col turn)
(dotimes (num (board-scan board row col (car dir) (cadr dir) turn))
(set-board board (+ (car dir) row) (+ (cadr dir) col) turn)
(setq row (+ row (car dir)))
(setq col (+ col (cadr dir)))
)
)
)
)
(defun board-number-check (row col)
(cond ((or (not (numberp row)) (not (numberp col))) nil)
((or (< (- Board-Size 1) row) (< (- Board-Size 1) col)) nil)
(t t)))
(defstruct node
row col
board
val
)
(defun eval-node (node turn count) (+ (board-judge (node-board node) count) (board-judge2 (node-board node))
(board-judge3 (node-board node) count turn) ;それぞれの関数を用いて導いた評価値を足し合わせ、どれが最善手かを計算する
Ad

Recommended

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 

More Related Content

Featured

Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...Palo Alto Software
 
9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free VacationWeekdone.com
 
I Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from PrinceI Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from PrinceEmpowered Presentations
 
How to Map Your Future
How to Map Your FutureHow to Map Your Future
How to Map Your FutureSlideShop.com
 
Beyond Pride: Making Digital Marketing & SEO Authentically LGBTQ+ Inclusive -...
Beyond Pride: Making Digital Marketing & SEO Authentically LGBTQ+ Inclusive -...Beyond Pride: Making Digital Marketing & SEO Authentically LGBTQ+ Inclusive -...
Beyond Pride: Making Digital Marketing & SEO Authentically LGBTQ+ Inclusive -...AccuraCast
 
Read with Pride | LGBTQ+ Reads
Read with Pride | LGBTQ+ ReadsRead with Pride | LGBTQ+ Reads
Read with Pride | LGBTQ+ ReadsKayla Martin-Gant
 
Exploring ChatGPT for Effective Teaching and Learning.pptx
Exploring ChatGPT for Effective Teaching and Learning.pptxExploring ChatGPT for Effective Teaching and Learning.pptx
Exploring ChatGPT for Effective Teaching and Learning.pptxStan Skrabut, Ed.D.
 
How to train your robot (with Deep Reinforcement Learning)
How to train your robot (with Deep Reinforcement Learning)How to train your robot (with Deep Reinforcement Learning)
How to train your robot (with Deep Reinforcement Learning)Lucas García, PhD
 

Featured (20)

Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
The Pixar Way: 37 Quotes on Developing and Maintaining a Creative Company (fr...
 
9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation9 Tips for a Work-free Vacation
9 Tips for a Work-free Vacation
 
I Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from PrinceI Rock Therefore I Am. 20 Legendary Quotes from Prince
I Rock Therefore I Am. 20 Legendary Quotes from Prince
 
How to Map Your Future
How to Map Your FutureHow to Map Your Future
How to Map Your Future
 
Beyond Pride: Making Digital Marketing & SEO Authentically LGBTQ+ Inclusive -...
Beyond Pride: Making Digital Marketing & SEO Authentically LGBTQ+ Inclusive -...Beyond Pride: Making Digital Marketing & SEO Authentically LGBTQ+ Inclusive -...
Beyond Pride: Making Digital Marketing & SEO Authentically LGBTQ+ Inclusive -...
 
Read with Pride | LGBTQ+ Reads
Read with Pride | LGBTQ+ ReadsRead with Pride | LGBTQ+ Reads
Read with Pride | LGBTQ+ Reads
 
Exploring ChatGPT for Effective Teaching and Learning.pptx
Exploring ChatGPT for Effective Teaching and Learning.pptxExploring ChatGPT for Effective Teaching and Learning.pptx
Exploring ChatGPT for Effective Teaching and Learning.pptx
 
How to train your robot (with Deep Reinforcement Learning)
How to train your robot (with Deep Reinforcement Learning)How to train your robot (with Deep Reinforcement Learning)
How to train your robot (with Deep Reinforcement Learning)
 

知能工学実験 last report.pdf

  • 2. 1. lisp のプログラム (defconstant Board-Size 8) (defconstant Board-Dimensions (list Board-Size Board-Size)) (defconstant Board-Sente 1) (defconstant Board-Gote -1) (defconstant Board-Empty 0) (defconstant Board-Sente-String "*") (defconstant Board-Gote-String "o") (defconstant Board-Empty-String "-") (setq upcount 0) (defconstant Board-Initial #2A((0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 -1 1 0 0 0) (0 0 0 1 -1 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0) (0 0 0 0 0 0 0 0))) ;20 手目までの評価値 (defconstant Board-judge<=20 #2A((30 0.1 1 1 1 1 0.1 30) (0.1 0.01 10 10 10 10 0.01 0.1) (2 10 20 20 20 20 10 2) (2 10 20 -1 1 20 10 2) (2 10 20 1 -1 20 10 2) (2 10 20 20 20 20 10 2) (0.1 0.01 10 10 10 10 0.01 0.1) (30 0.1 1 1 1 1 0.1 30))) ;40 手目までの評価値 (defconstant Board-judge<=40 #2A((30 0.1 5 10 5 10 0.1 30) (0.1 0.01 10 15 15 10 0.01 0.1) (5 10 20 20 20 20 10 5) (10 15 20 -1 1 20 15 10) (5 15 20 1 -1 20 15 5) (10 10 20 20 20 20 0 10) (0.1 0.01 10 15 15 10 0.01 0.1) (30 0.1 5 10 5 10 0.1 30))) ;40 手目以降の評価値 (defconstant Board-judge>40 #2A((30 0.1 15 10 10 15 0.1 30) (0.1 0.01 10 15 15 10 0.01 0.1) (15 10 20 20 20 20 10 15) (10 15 20 -1 1 20 15 10) (10 15 20 1 -1 20 15 10) (15 10 20 20 20 20 10 15) (0.1 0.01 10 15 15 10 0.01 0.1) (30 0.1 15 10 10 15 0.1 30))) (defun change-turn (turn) (cond ((equal turn Board-Sente) Board-Gote) (t Board-Sente))) (defun symbol-turn (turn) (cond ((equal turn Board-Sente) Board-Sente-String) ((equal turn Board-Gote) Board-Gote-String) (t Board-Empty-String)))
  • 3. (defun make-board () (make-array Board-Dimensions)) (defun ref-board (board row col) (aref board row col)) (defun set-board (board row col turn) (setf (aref board row col) turn)) (defun copy-board (board) (let ((new-board (make-board))) (dotimes (row Board-Size new-board) (dotimes (col Board-Size) (set-board new-board row col (ref-board board row col)))))) (defun print-board (board) (format t "R/C 0 1 2 3") (dotimes (row Board-Size nil) (format t "~%") (format t " ~A " row) (dotimes (col Board-Size nil) (format t "~A " (symbol-turn (ref-board board row col))) ) ) ) (defconstant Board-Directions '((0 1) (1 1) (1 0) (1 -1) (0 -1) (-1 -1) (-1 0) (-1 1))) (defun sample-scan (board row0 col0 row-inc col-inc) (do ((row row0 (+ row row-inc)) (col col0 (+ col col-inc)) (length 0 (1+ length))) ((or (< row 0) (= row Board-Size) (< col 0) (= col Board-Size) (equal (ref-board board row col) Board-Empty)) length))) (defun board-scan (board row0 col0 row-inc col-inc turn) (do ((row (+ row0 row-inc) (+ row row-inc)) (col (+ col0 col-inc) (+ col col-inc)) (length 0 (1+ length))) ((or (< row 0) (= row Board-Size) (< col 0) (= col Board-Size) (eql (ref-board board row col) Board-Empty)) 0) (cond ((equal (ref-board board row col) turn) (return length))
  • 4. ))) (defun board-movable (board row col turn) (and (equal (ref-board board row col) Board-Empty) (dolist (dir Board-Directions nil) (cond ((> (board-scan board row col (car dir) (cadr dir) turn) 0) (return t)))))) (defun board-movable-any (board turn) (let ((judge nil)) (dotimes (row Board-Size judge) (dotimes (col Board-Size) (cond ((board-movable board row col turn) (setq judge t)) ) ) ) ) ) (defun othello-by-human () (do ((board (copy-board Board-Initial)) (turn Board-Sente (change-turn turn)) (count 1 (1+ count)) state) ((numberp (setq state (board-state board turn))) (game-result board state)) (print-board board) (cond ((equal state 'pass) (format t "(~A) Pass. ~%" count)) (t (play-by-human board turn count)))) ) (defun board-state (board turn) (cond ((board-movable-any board turn) nil) ((and (not (board-movable-any board turn)) (board-movable-any board (change-turn turn))) 'pass)
  • 5. (t (board-eval board)) ) ) (defun board-eval (board) (let ((black 0) (white 0) (score 0)) (dotimes (row Board-Size score) (dotimes (col Board-Size) (cond ((equal (ref-board board row col) 1) (setq black (1+ black))) ((equal (ref-board board row col) -1) (setq white (1+ white))) ) (cond ((and (= black 0) (= white 0)) (setq score 0)) (t (setq score (/ (- black white) (+ black white)))) ) ) ) ) ) (defun game-result (board state) (print-board board) (format t "~%") (cond ((< state 0) (format t "Game won by o ~%")) ((> state 0) (format t "Game won by * ~%")) (t (format t "Draw ~%")) ) (let ((black 0) (white 0) (scoreresult 0)) (dotimes (row Board-Size scoreresult) (dotimes (col Board-Size) (cond ((equal (ref-board board row col) 1) (setq black (1+ black))) ((equal (ref-board board row col) -1) (setq white (1+ white))) ) )) (setq scoreresult (format t "Score is ~A - ~A" black white )) ) )
  • 6. (defun play-by-human (board turn count) (format t "(~A) Enter row and col for ~A:" count (symbol-turn turn)) (do ((row (read) (read)) (col (read) (read))) ((and (board-number-check row col) (board-movable board row col turn)) (board-move board row col turn)) (format t "Re-enter row and col for ~A:" (symbol-turn turn)))) (defun board-move (board row col turn) (dolist (dir Board-Directions board) (let ((row row) (col col)) (set-board board row col turn) (dotimes (num (board-scan board row col (car dir) (cadr dir) turn)) (set-board board (+ (car dir) row) (+ (cadr dir) col) turn) (setq row (+ row (car dir))) (setq col (+ col (cadr dir))) ) ) ) ) (defun board-number-check (row col) (cond ((or (not (numberp row)) (not (numberp col))) nil) ((or (< (- Board-Size 1) row) (< (- Board-Size 1) col)) nil) (t t))) (defstruct node row col board val ) (defun eval-node (node turn count) (+ (board-judge (node-board node) count) (board-judge2 (node-board node)) (board-judge3 (node-board node) count turn) ;それぞれの関数を用いて導いた評価値を足し合わせ、どれが最善手かを計算する
  • 7. )) (defun board-judge (board turn) (let((count 0)) ;評価値 (dotimes (row Board-Size count);行 (dotimes (col Board-size);列 (cond ((<= turn 20) ;20 手目まで (setq count (+ count (* (ref-board Board-judge<=20 row col) (ref-board board row col)))));count に自分で設定した評価値を 与える ((<= turn 40);40 手目まで (setq count (+ count (* (ref-board Board-judge<=40 row col) (ref-board board row col))))) ;count に自分で設定した評価値を 与える (t ;それ以降 (setq count (+ count (* (ref-board Board-judge>40 row col) (ref-board board row col))))) ;count に自分で設定した評価値を与 える ))))) (defun board-judge2 (board) (let((count 0)) (dotimes (row Board-Size count) ;count=評価値 (dotimes (col Board-Size) (cond ((and (= row 0) (= col 0)) (setq count (+ count (* (ref-board Board-judge<=40 row col) (ref-board board row col)))));端の時評価値を与える ((and (= row 0) (= col 7)) (setq count (+ count (* (ref-board Board-judge<=40 row col) (ref-board board row col))))) ;端の時評価値を与える ((and (= row 7) (= col 0)) (setq count (+ count (* (ref-board Board-judge<=40 row col) (ref-board board row col))))) ;端の時評価値を与える ((and (= row 7) (= col 7)) (setq count (+ count (* (ref-board Board-judge<=40 row col) (ref-board board row col))))) ;端の時評価値を与える )))))) (defun board-judge3 (board count turn) (let((hyouka 0) (maisuu 0)) (dotimes (row Board-Size hyouka) ;hyouka=評価値 (dotimes (col Board-Size)
  • 8. (cond (= row turn) (setq (1+ maisuu))) ;自分のコマを数える (cond ((<= count 20) (setq hyouka (+ hyouka (* (ref-board Board-judge<=20 row col) (ref-board board row col) (/ 2 (1+ maisuu))))));与えた評価 値に 2/(1+コマ数)をかけた値を評価値に加える ((> count 44)) (setq hyouka (+ hyouka (* (ref-board Board-judge>40 row col) (ref-board board row col) (* 3 maisuu))));与えた評価値に 3*コ マ数をかけた値を評価値に加える ))) (setq maisuu 0)));コマ数のリセット (defun expand-node (node turn) (let ((result nil)) (dotimes (row Board-Size (reverse result)) (dotimes (col Board-Size) (cond ((board-movable (node-board node) row col turn) (setq result (cons (make-node :row row :col col :board (board-move (copy-board (node-board node)) row col turn) :val nil ) result)) ) ) ) ) ) ) (defun node-state (node turn count) (board-state (node-board node) turn)) ;count 手数 ;depth 先読みの先の数 (defun minimax-children (children turn count depth) (let ((new-turn (change-turn turn) (v 0)) (new-depth (1- depth)) (new-count (1+ count)))
  • 9. (do ((val nil) (result nil)) ((null children) result) (setq v (node-val (minimax (car children) new-turn new-count new-depth))) (cond ((or (null val) (and (equal turn Board-Sente) (> val)) (and (equal turn Board-Gote) (< val)) ) (setq val v) (setq result (make-node :row (node-row (car children));子の row :col (node-col (car children));子の col :board (node-board (car children));子の board :val val)) ) ) (setq children (cdr children)) ) ) ) (defun minimax (node turn count depth) (setq upcount (1+ upcount)) (let ((state (node-state node turn count))) (cond ((numberp state) (setf (node-val node) state) node) ((zerop depth) (setf (node-val node) (eval-node node turn count)) node) ((equal state 'pass) (minimax node (change-turn turn) count depth)) (t (minimax-children (expand-node node turn) turn count depth))))) (defun othello (sente-gote yomi-depth) (do ((board (copy-board Board-Initial)) (turn Board-Sente (change-turn turn)) (count 1 (1+ count)) state) ((numberp (setq state (board-state board turn))) (game-result board state)) (print-board board) (cond ((equal state 'pass) (format t "(~A) Pass.~%" count) (setq count (1- count)))
  • 10. ((or (equal sente-gote Board-Empty) (equal turn sente-gote)) (play-by-machine2 board turn count yomi-depth)) (t (play-by-human board turn count)) ) ) ) (defun play-by-machine (boardA turn count depth) (format t "My move is ~A ~A.~%" (node-row (minimax (make-node :board boardA) turn count depth)) (node-col (minimax (make-node :board boardA) turn count depth))) (board-move boardA (node-row (minimax (make-node :board boardA) turn count depth)) (node-col (minimax (make-node :board boardA) turn count depth)) turn ) (format t "~A nodes checked." upcount)) (defun play-by-machine2 (boardA turn count depth) (setq node (alpha-beta (make-node :board boardA) turn count depth nil nil)) (format t "(~A) My move is ~A ~A.~%" count (node-row node) (node-col node)) (board-move boardA (node-row node) (node-col node) turn) (format t "~A nodes checked." upcount) ) ; Lisp 側でのインタフェース (defvar out-filename nil) (defvar lock-out-filename nil) (defvar in-filename nil) (defvar lock-in-filename nil) ; *** C からのデータ(盤面)をファイルから読み込む関数 *** (defun read-board-from-file (board) (let ((eos (cons nil nil)) hand) (do () ((and (probe-file in-filename) (not (probe-file lock-in-filename)))) (sleep 1)) (with-open-file (i-file in-filename) (dotimes (row Board-Size) (dotimes (col Board-Size)
  • 11. (setq hand (read i-file nil eos)) (cond ((eq hand eos) (set-board board 0 0 nil) (return-from read-board-from-file board))) (set-board board row col hand)))) (delete-file in-filename))) ; *** C へのデータ(次の打ち手)をファイルへ書き出す関数 *** (defun write-move-to-file (row col) (with-open-file (o-file lock-out-filename :direction :output) (print 'lock o-file)) (with-open-file (o-file out-filename :direction :output) (format o-file "~A ~A" row col)) (delete-file lock-out-filename)) ; *** ファイル名称 *** (defconstant Owner "g20072") (defun initialize (sente-gote) (let ((saki-ato (if (equal sente-gote Board-Sente) "saki" "ato"))) (setq out-filename (format nil "/tmp/te.~A.~A" Owner saki-ato)) (setq lock-out-filename (format nil "/tmp/te.~A.~A.lock" Owner saki-ato)) (setq in-filename (format nil "/tmp/ban.~A.~A" Owner saki-ato)) (setq lock-in-filename (format nil "/tmp/ban.~A.~A.lock" Owner saki-ato)) )) (defun main-loop (sente-gote yomi-depth) (initialize sente-gote) (let ((board (make-board)) (turn sente-gote)) (read-board-from-file board) (do ((count (cond ((equal turn Board-Sente) 1) (t 2)) (+ count 2))
  • 12. (lst (ref-board board 0 0) (ref-board board 0 0)) (state (board-state board turn)) node) ((and (/= lst Board-Sente) (/= lst Board-Gote) (/= lst Board-Empty))) (setq state (board-state board turn)) (cond ((or (equal state 'pass) (numberp state)) (write-move-to-file -1 0)) ;(t (setq node (minimax (make-node :board board) ; turn count ; yomi-depth))) ;;alpha-beta 法の場合 (t (setq node (alpha-beta (make-node :board board) turn count yomi-depth nil nil)) (write-move-to-file (node-row node) (node-col node)))) (read-board-from-file board) ))) (defun alpha-beta-children (children turn count depth alpha beta) (let ((new-turn (change-turn turn)) (new-depth (1- depth)) (new-count (1+ count)) (v 0) (alpha alpha) (beta beta)) (cond ((null alpha) (setq alpha -10000)));極端に小さくして nil を避ける (cond ((null beta) (setq beta 10000))) ; 極端に大きくして nil を避ける (do ((val nil) (result nil)) ((or (null children) (>= alpha beta)) result);終了条件 (setq v (node-val (alpha-beta (car children) new-turn new-count new-depth alpha beta)));評価値 (cond ((null v) (setq v 0))) (cond ((and (equal turn Board-Sente) (> v alpha));1つずつ評価値を見ていく α カット (setq alpha v)) ;alpha を更新 ((and (equal turn Board-Gote) (< v beta)) ;1つずつ評価値を見ていく β カット (setq beta v)) ;beta を更新 ) (cond ((equal turn Board-Sente) (setq result (make-node :row (node-row (car children));子の row :col (node-col (car children));子の col :board (node-board (car children));子の board :val alpha)))
  • 13. ((equal turn Board-Gote) (setq result (make-node :row (node-row (car children));子の row :col (node-col (car children));子の col :board (node-board (car children));子の board :val beta))) ) (setq children (cdr children)))) ) (defun alpha-beta (node turn count depth alpha beta) (setq upcount (1+ upcount)) (let ((state (node-state node turn count))) (cond ((numberp state) (setf (node-val node) state) node) ((zerop depth) (setf (node-val node) (eval-node node turn count)) node) ((equal state 'pass) (alpha-beta node (change-turn turn) count depth alpha beta)) (t (alpha-beta-children (expand-node node turn) turn count depth alpha beta))))) 2. 評価関数のプログラムの説明 ・20 手目まで、40 手目まで、それ以降の手目によって評価値を変更させる ・角の評価値を上げる ・20 手目までの場合の評価値をひっくり返すコマの数が少なければ少ない ほど上げ、後半はひっくり返すコマが多ければ多いほど上げた 3. 評価関数の工夫した点 ・20 手目までは内側の盤面の評価値が高いように設定した ・40 手目までは 4,5 列目,4,5 行目の盤面の評価値を上げることで中抜きしや すいようにした
  • 14. ・40 手目以降はできるだけ角付近は避けたいのでそれ以外の評価値を平等 に与えた ・角をとれるなら必ずとれるようにするために評価値を特に上げた ・序盤はひっくり返すコマを少なくすることで次のターンに自分が置けるコ マの数を多くし、終盤はより多くのコマを取れるようにした。序盤は与えた 評価値と 2/(1+返せるコマ数)の掛け算をし、終盤は与えた評価値と 3*返せ るコマ数の掛け算をした。 4. 考察 ・αβ法を用いることでより深く探索できるので minimax 法を用いるよりも強 くなりやすい ・評価値を変更、 上書きする際はマイナスなどにも対応しやすい掛け算を使うの が有効だと考える ・ 最善の手をうつために 10^100 以上の場合分けを行わずとも計算で求めること ができる 追加できれば強くなると考える関数とその理由 ・相手のコマと相手のコマの間に置く/相手のコマとコマの間を自分のコマで埋 める
  • 15. 自分が置いたコマとひっくり返したコマが上書きされないようにするため。ま た端の列、行にそのような場所にコマを置くことで置いたコマは取られないよ うにする。 ・もし自分のコマが角に置いたとき、その周辺の評価値を上げる 角の周辺の評価値を上げ、優先してその場所に置くことで相手は取ることがで きない確定子を増やすことができる。 ・ 5.感想 2,3 週間の間悩みに悩んでαβ法のプログラムを完成させたときの達成感が忘 れられない。 目標を達成することができ、 より一層これからも頑張ろうと思えた。 ボードゲームの AI がどのようにして作られているのかがわかり、将棋やチェス は遥かに難しいプログラムになると感じた。頭ではやりたいことがあったとし てもそれを lisp のプログラムにする難しさがわかっただけでなく python のプロ グラムの書きやすさがわかった。