computing science (cmput) 496 - university of albertammueller/courses/496-winter-2017/... ·...

27
Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department of Computing Science University of Alberta [email protected] Winter 2017

Upload: dinhnga

Post on 09-Sep-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

Computing Science (CMPUT) 496Search, Knowledge, and Simulations

Martin Müller

Department of Computing ScienceUniversity of Alberta

[email protected]

Winter 2017

Page 2: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Part I

Intro - Problem Solving for humans andcomputers

Page 3: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

496 Today - Jan 12

Topics:Eyes and Life in GoBasic data structures and algorithms for Go ProgramsTools for Go programming: GoGui, attaching Goprograms, GTP text interfaceWhy talk about Go in the “Problem-solving” topic?I am doing this early for two reasons:

You need it for Assignment 1For many of the (general purpose) algorithmsthroughout the course, we will use Go as example

Page 4: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

About Python Code for Go and Assignment 1

I start using Python 3 code as example todaySo far, examples are only on slidesSource code not released yet

I want to do a final check for quality and consistencythis weekend

You will get the code for our Go1 programI will announce it when ready

Plays random moves, but does not fill “one point eyes”

This is also the code you will modify for Assignment 1(Random NoGo Player)

Page 5: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

What are Eyes and Why are they Useful?

End of last class, we saw a problem witha true random playerIt keeps playing and playing, and evenvery strong stones keep dyingGames would last VERY long, andresult would have no meaningSolution: remove some of the moststupid moves, to make sure the gameends in reasonable time

We remove so-called “eye-filling”moves

Page 6: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Eyes and Capturing

An eye is a point that issurrounded by one colorFor the opponent:(illegal) suicide to play there...(A, B, C in top picture)...unless that pointis the last libertyof the surrounding stones(bottom picture)- then it becomes a (legal)capture

Page 7: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Two Eyes are Safe

Here, Black has one blocksurrounding two eyes A and BWhite cannot attack, both A andB are suicideBlack is safe as long as Blackleaves the eyes alone

Remember you can alwayspass, no need to hurt your ownstones

Black should NEVER play A or B

If Black fills second-last eyethen White can capture

Page 8: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

What is an Eye?

Simple definition used hereThere are other kinds of eyes (later)

1 Single empty point2 All neighbors occupied by stones of the

same color3 All these stones are connected

Examples: eye in corner, edge of board,and center

Page 9: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Detecting Eyes Locally

Most simple eyes can bedetected locally

Only look at neighbors anddiagonalsCorner, edge: need all (1 or 2)diagonals to connectCenter: need at least 3 of 4diagonals

In principle, can connect alongsome longer path

Pretty rare, ignore in programfor nowExample: A7 eyeStones A6 and B7 connectedover a long path

Page 10: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Simple Eyes - Summary

Random player keeps playing senselessly......unless we stop it from filling its eyesAn eye is an empty point,surrounded by a connected block of stonesA simple local definition of eye is good enough for nowVery fast to check in program, only look at 8 neighborsand diagonals (at most)Having two (or more) eyesmakes a block safe from captureNext topic: implementing Go board and rules

Page 11: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Implementing a Go Board and Go Rules

Representing the boardUpdating the board after a move

Recognize captureChecking for legal moves

Recognize suicide and repetition (simple ko)

Page 12: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Why Bother with an Efficient BoardRepresentation?

Most game programs are based on search andsimulationBillions of moves played and taken back during a gamePlaying strength strongly depends on amount of searchSo, make it as fast as possible

Our first Python codes may be 100.000 times slowerthan state of the artMostly, that is due to algorithms and data structures, notPython...

Page 13: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Representing State of a Point

Three possible states: empty, black or whiteWe could use the new-ish Python 3 enumeration typehttps://docs.python.org/3/library/enum.html

class BoardColor(Enum):EMPTY = 0BLACK = 1WHITE = 2

In current program we just use integer codes for colors

EMPTY = 0BLACK = 1WHITE = 2

Page 14: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Representing the Go Board - 2d Array

MAXSIZE = 7board = [[EMPTY for x in range(MAXSIZE)]

for y in range(MAXSIZE)]print(board)board[3][4] = BLACKprint(board)

Most direct representation: 2-dimensional arrayStore a point on the board at coordinates [x][y] inarray

Page 15: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Drawbacks of Two-dimensional Array

Overhead from 2-d address calculationNeed two variables (x, y) to represent a single pointOften need two computations, for x, y separatelyComplex checking for boundary casesif x > 0 and y > 0and x <= MAXSIZE and y <= MAXSIZE

if statements introduce conditional branchesand slow down execution

Page 16: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Go Board as One-dimensional Array

Solution: use a simple1-dimensional arrayFrom (x,y) to single indexp = x + y * MAXSIZE

Back from p to x, y byinteger division andmodulo operators

x = p % MAXSIZEy = p // MAXSIZE

0 1 2 3 4 5 67 8 9 10 11 12 1314 15 16 17 18 19 20...

Can also precompute thecalculations

Lookup tables, e.g. x= xCoord[p]

Frequent operations usesimple offset, constanttime

Go to neighbors anddiagonalsCheck if on border, orhas neighborMany more..

Page 17: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Drawbacks of Simple One-dimensional Array

Edges of board still needs special case treatment(lots of if statements)

0 1 2 3 4 5 67 8 9 10 11 12 13

Index 6 and 7 are not neighbors...There is no neighbor upwards from 4...Similar for bottom edge

Page 18: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Solution: Add Padding

Image source:

https://www.gnu.org/software/

gnugo/gnugo_15.html

Solution: add extra “padding”Above boardBelow boardBetween rows

For these points, store a newcode"off the board" BORDER = 3

Many special edge cases can beavoided by only checking thecontent of the point

Page 19: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Summary and Comments for BoardRepresentation

Standard way for Go uses 1-d board with extra paddingOther special purpose representations are possible:

Bitsets, one set per colorList of stonesCover board with small patterns, e.g. 3 × 3 squares

Will use this as “simple features” later

Optional - to learn more: see e.g.https://chessprogramming.wikispaces.com/Board+Representation for detailed discussions forchessNext: Playing and Undoing moves

Page 20: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Playing and Undoing Moves

play(p, color)Put stone of given color on point pSimplest case: just needboard[p] = color

Major complication:recognize captures and removecaptured stonesClosely related: check if move on pis legal, before playing it...

Page 21: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Capturing Stones

How to find which opponent stonesare captured?Black move A captures one stoneBlack move B does not captureanything...To decide whether B is a capture, wemust look at the neighbors of thewhole blockWe must find the liberty at C todecide it’s no captureUpdate board:

for stone in capturedBy(p, color):board[stone] = EMPTY

Page 22: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Capturing Stones Algorithm

How to find which opponent stones arecaptured?Look at all neighbors nb of p which arestones of opponentCheck if nb is part of a block that losesits last libertyAlgorithm is similar to floodfill ingraphicsLook at all stones connected to nb

If any stone has a liberty (other than p),stop: no captureIf no stone in the block has anotherliberty, then all are captured

Page 23: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Checking If Move is Legal

isLegal(p, color):1 board[p] == EMPTY

2 not isSuicide(p, color)

3 not repetition(p, color)

Page 24: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Checking Suicide

Very similar to checkingcapture for the othercolorMain difference: themove can connectseveral blocks, and noneof them may haveanother libertySee examples: Black Ais suicide, Black B is notbecause liberty at C

Page 25: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Checking Repetition

For now, only check for simple koAfter capture of a single stone s:set kopoint = s

After any other move: setkopoint = NULL

When checkingisLegal(p, color):

if p == koPointand “p would capture a singlestone”:p is illegal

Page 26: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Software Demo

GoGuiAttaching different Go programsTools that come with GoGuiGTP text commands

Page 27: Computing Science (CMPUT) 496 - University of Albertammueller/courses/496-Winter-2017/... · Computing Science (CMPUT) 496 Search, Knowledge, and Simulations Martin Müller Department

CMPUT 496

Summary and Outlook

Discussed most of the basics of implementing GoStill to come: scoring at the end, taking back movesduring searchDiscussed useful tools and GTP text interface