algorithms for computer games - lecture slides 2009

Post on 28-Oct-2014

710 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

The course concentrates on algorithmic problems present in computer games. The aim of the course is to review common solution methods, analyse their usability, and describe possible improvements. The topics cover among other things random numbers, game trees, path finding, terrain generation, and decision-making for synthetic players.

TRANSCRIPT

Algorithms for Computer Games

Jouni Smed Department of Information Technology

University of Turku

http://www.iki.fi/smed

Course syllabus

n  credits: 5 cp (3 cu) n  prerequisites

n  fundamentals of algorithms and data structures (e.g., Cormen et al., Introduction to Algorithms)

n  knowledge in programming (e.g., with Java)

n  assessment n  examination only (no exercises)

Lectures

n  Lecture times n  Tuesdays 10–12 a.m. n  Thursdays 10–12 a.m.

n  September 8 – October 8, 2009 n  Lecture room B2033, ICT Building

Examinations 1(2)

n  examination dates (to be confirmed) 1.  ?? (possibly October 2009) 2.  ?? (possibly November 2009) 3.  ?? (possibly January 2010)

n  check the exact times and places at http://www.it.utu.fi/opetus/tentit/

n  remember to enrol! https://ssl.utu.fi/nettiopsu/

Examinations 2(2)

n  questions n  based on both lectures and the textbook n  two questions, à 5 points n  to pass the examination, at least 5 points (50%) are

required n  grade: g = ⎡p − 5⎤ n  questions are in English, but you can answer in

English or in Finnish

Web page

http://www.iki.fi/smed/a4cg

n  news and announcements n  slides, code examples, additional material n  discussion forum

Follow-up course: Multiplayer Computer Games

n  focus: networking in computer games n  credits: 5 cp (3 cu) n  schedule:

n  October 27 – November 26, 2009 n  Tuesdays 10–12 a.m., and Thursdays 10–12 a.m.

n  web page: http://www.iki.fi/smed/mcg

Textbook

n  Jouni Smed & Harri Hakonen: Algorithms and Networking for Computer Games, John Wiley & Sons, 2006.

n  http://www.wiley.com/go/smed

Computer games

In the beginning...

“If, when walking down the halls of MIT, you should happen to hear strange cries of ‘No! No! Turn! Fire! ARRRGGGHHH!!,’ do not be alarmed. Another western is not being filmed—MIT students and others are merely participating in a new sport, SPACEWAR!”

D. J. Edwards & J. M. Graetz, “PDP-1 Plays at Spacewar”, Decuscope, 1(1):2–4, April 1962

...and then

n  1962: Spacewar n  1971: Nutting: Computer Space n  1972: Atari: Pong n  1978: Midway: Space Invaders n  1979: Roy Trubshaw: MUD n  1980: Namco: Pac-Man n  1981: Nintendo: Donkey Kong n  1983: Commodore 64

n  1985: Alexei Pajitnov: Tetris n  1989: Nintendo Game Boy n  1993: id Software: Doom n  1994: Sony Playstation n  1997: Origin: Ultima Online n  2001: Microsoft Xbox n  2006: Nintendo Wii

Three academic perspectives to computer games

GAME

Humanistic perspective

Administrative/ business

perspective

Technical perspective

Game design

Game programming

Software development

n  rules n  graphics n  animation n  audio

n  gfx & audio n  simulation n  networking n  AI

n  design patterns n  architectures n  testing n  reuse

Game development is team work

source: Game Developer, 2005

Intention of this cource

n  to provide a glance into the world of computer games as seen from the perspective of a computer scientist

Contents

§1 Introduction §2 Random Numbers §3 Tournaments §4 Game Trees §5 Path Finding §6 Decision-making §7 Modelling Uncertainty

§1 Introduction

n  definitions: play, game, computer game n  anatomy of computer games n  synthetic players n  multiplaying n  games and story-telling n  other game design considerations

First, a thought game

n  what features are common to all games?

Components of a game

n  players: willing to participate for enjoyment, diversion or amusement

n  rules: define limits of the game n  goals: gives a sense of purpose n  opponents: give arise to contest and rivarly n  representation: concretizes the game

player

rules goal

opponent representation

definition

CHALLENGE

obstruction

CONFLICT

correspondence

PLAY

Components, relationships and aspects of a game

Definition for ‘computer game’

n  a game that is carried out with the help of a computer program

n  roles: n  coordinating the game process n  illustrating the situation n  participating as a player

→ Model–View–Controller architectural pattern

Model–View–Controller

control logic

driver

proto-view

rendering

state instance core structures

input device

action

configuration

instance data

synthetic view

synthetic player

script output device

human player

options

perception

model

view controller

Synthetic players

n  synthetic player = computer-generated actor in the game n  displays human-like features n  has a stance towards the human player

n  games are anthropocentric!

Humanness

n  human traits and characteristics n  fear and panic (Half-Life, Halo)

n  computer game comprising only synthetic players n  semi-autonomous actors (The Sims) n  fully autonomous actors (Core War, AOE2)

Stance towards the player

n  enemy

ally

neutral

Enemy

n  provides challenge n  opponent

n  must demonstrate intelligent (or at least purposeful) behaviour

n  cheating n  quick-and-dirty methods n  when the human player cannot observe enemy’s

actions

Ally

n  augmenting the user interface n  hints and guides

n  aiding the human player n  reconnaissance officer n  teammate, wingman

n  should observe the human point of view n  provide information in an accessible format n  consistency of actions

Neutral

n  commentator n  highlighting events and providing background

information

n  camera director n  choosing camera views, angles and cuts

n  referee n  judging the rule violations

n  should observe the context and conventions

Studying synthetic players: AIsHockey

n  simplified ice hockey: n  official IIHF rules n  realistic measures and

weights n  Newtonian physics

engine

n  distributed system n  client/server

architecture

n  implemented with Java n  source code available

(under BSD licence)

Example: MyAI.java

import fi.utu.cs.hockey.ai.*; public class MyAI extends AI implements Constants { public void react() { if (isPuckWithinReach()) { head(headingTo(0.0, THEIR_GOAL_LINE)); brake(0.5); shoot(1.0); say(1050L); } else { head(headingTo(puck())); dash(1.0); } } }

Try it yourself!

n  challenge: implement a team of autonomous collaborating synthetic players

n  the platform and ready-to-use teams available at: http://www.iki.fi/smed/aishockey

Multiplaying

n  multiple human players sharing the same game n  methods:

n  divide the screen n  divide the playtime n  networking

All this and more in the follow-up course Multiplayer Computer Games starting October 27, 2009.

Games and story-telling

n  traditional, linear story-telling n  events remain from time to time (almost) unchangeable n  books, theatre, cinema n  participant (reader, watcher) is passive

n  interactive story-telling n  events change and adapt to the choices the participant makes n  computer games n  participant (player) is active

A story is always told to human beings

n  story-telling is not about actions but reasons for actions n  humans use a story (i.e., a narrative) to understand

intentional behaviour n  how can we model and generate this?

n  story-telling is about humans n  humans humanize the characters’ behaviour and

understand the story through themselves n  how can we model and generate this?

All this and more in the course Interactive Storytelling lectured in the Autumn 2010.

Other game design considerations

n  customization n  tutorial n  profiles n  modification n  replaying

→ parameterization!

§2 Random Numbers

n  what is randomness? n  linear congruential method

n  parameter choices n  testing

n  random shuffling n  uses in computer games

What are random numbers good for (according to D.E.K.)

n  simulation n  sampling n  numerical analysis n  computer programming n  decision-making n  aesthetics n  recreation

Random numbers?

n  there is no such thing as a ‘random number’ n  is 42 a random number?

n  definition: a sequence of statistically independent random numbers with a uniform distribution n  numbers are obtained by chance n  they have nothing to do with the other numbers in

the sequence

n  uniform distribution: each possible number is equally probable

Methods

n  random selection n  drawing balls out of a ‘well-stirred urn’

n  tables of random digits n  decimals from π

n  generating data n  white noise generators n  cosmic background radiation

n  computer programs?

Generating random numbers with arithmetic operations

n  von Neumann (ca. 1946): middle square method n  take the square of previous number and extract the

middle digits

n  example: four-digit numbers n  ri = 8269 n  ri + 1 = 3763 (ri

2 = 68376361) n  ri + 2 = 1601 (ri + 1

2 = 14160169) n  ri + 3 = 5632 (ri + 2

2 = 2563201)

Truly random numbers?

n  each number is completely determined by its predecessor!

n  sequence is not random but appears to be n  → pseudo-random numbers n  all random generators based arithmetic

operation have their own in-built characteristic regularities

n  hence, testing and analysis is required

Middle square (revisited)

n  another example: n  ri = 6100 n  ri + 1 = 2100 (ri

2 = 37210000) n  ri + 2 = 4100 (ri + 1

2 = 4410000) n  ri + 3 = 8100 (ri + 2

2 = 16810000) n  ri + 4 = 6100 = ri (ri + 3

2 = 65610000)

n  how to counteract?

Words of the wise

n  ‘random numbers should not be generated with a method chosen at random’ — D. E. Knuth

n  ‘Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin.’ — J. von Neumann

Even the wise get humbled 1(2)

source: Knuth, 1998

Even the wise get humbled 2(2)

source: Knuth, 1998

Words of the more (or less) wise

n  ‘We guarantee that each number is random individually, but we don’t guarantee that more than one of them is random.’ — anonymous computer centre’s programming consultant (quoted in Numerical Recipes in C)

Other concerns

n  speed of the algorithm n  ease of implementation n  parallelization techniques n  portable implementations

Linear congruential method

n  D. H. Lehmer (1949) n  choose four integers

n  modulus: m (0 < m) n  multiplier: a (0 ≤ a < m) n  increment: c (0 ≤ c < m) n  starting value (or seed): X0 (0 ≤ X0 < m)

n  obtain a sequence 〈Xn〉 by setting Xn + 1 = (aXn + c) mod m (n ≥ 0)

Linear congruential method (cont’d)

n  let b = a – 1 n  generalization:

Xn + k = (akXn + (ak – 1) c/b) mod m (k ≥ 0, n ≥ 0)

n  random floating point numbers Un ∈ [0, 1): Un = Xn / m

Random integers from a given interval

n  Monte Carlo methods n  approximate solution n  accuracy can be improved at the cost

of running time

n  Las Vegas methods n  exact solution n  termination is not guaranteed

n  Sherwood methods n  exact solution, termination guaranteed n  reduce the difference between good

and bad inputs

Choice of modulus m

n  sequence of random numbers is finite → period (repeating cycle)

n  period has at most m elements → modulus should be large

n  recommendation: m is a prime n  reducing modulo: m is a power of 2

n  m = 2i : x mod m = x п (2i – 1)

Choice of multiplier a

n  period of maximum length n  a = c = 1: Xn + 1 = (Xn + 1) mod m n  hardly random: …, 0, 1, 2, …, m – 1, 0, 1, 2, …

n  results from Theorem 2.1.1 n  if m is a product of distinct primes, only a = 1 produces full

period n  if m is divisible by a high power of some prime, there is

latitude when choosing a

n  rules of thumb n  0.01m < a < 0.99m n  no simple, regular bit patterns in the binary representation

Choice of increment c

n  no common factor with m n  c = 1 n  c = a

n  if c = 0, addition operation can be eliminated n  faster processing n  period length decreases

Choice of starting value X0

n  determines from where in the sequence the numbers are taken

n  to guarantee randomness, initialization from a varying source n  built-in clock of the computer n  last value from the previous run

n  using the same value allows to repeat the sequence

Tests for randomness 1(2)

n  Frequency test n  Serial test n  Gap test n  Poker test n  Coupon collector’s test

Tests for randomness 2(2)

n  Permutation test n  Run test n  Collision test n  Birthday spacings test n  Spectral test

Spectral test

n  good generators will pass it n  bad generators are likely to fail it n  idea:

n  let the length of the period be m n  take t consecutive numbers n  construct a set of t-dimensional points:

{ (Xn, Xn + 1, …, Xn + t – 1) | 0 ≤ n < m }

n  when t increases the periodic accuracy decreases n  a truly random sequence would retain the accuracy

Spectral test results 1(3)

source: Knuth, 1998

Spectral test results 2(3)

source: Hellekalek, 1998

Spectral test results 3(3)

source: Hellekalek, 1998

Random shuffling

n  generate random permutation, where all permutations have a uniform random distribution

n  shuffling ≈ inverse sorting (!) n  ordered set S = 〈s1, …, sn〉 to be shuffled n  naïve solution

n  enumerate all possible n! permutations n  generate a random integer [1, n!] and select the corresponding

permutation n  practical only when n is small

Random sampling without replacement

n  guarantees that the distribution of permutations is uniform n  every element has a probability 1/n to become selected in the

first position n  subsequent position are filled with the remaining n – 1

elements n  because selections are independent, the probability of any

generated ordered set is 1/n · 1/(n – 1) · 1/(n – 2) · … · 1/1 = 1/n!

n  there are exactly n! possible permutations → generated ordered sets have a uniform distribution

Riffle shuffle

Perfect shuffle

Premo: Standard order

Premo: After a riffle shuffle and card insertion

Probability of success: 52 cards, m shuffles, n guesses

m n

2 3 4 5 6 7 8 9 10 11 12 ∞

1 997 839 288 088 042 028 023 021 020 020 019 019

2 1000 943 471 168 083 057 047 042 040 039 039 038

3 1000 965 590 238 123 085 070 063 061 059 058 058

13 1000 998 884 617 427 334 290 270 260 254 252 250

26 1000 999 975 835 688 596 548 524 513 505 503 500

cut-off

Cut-off phenomenon: distance d from the perfect shuffle, critical number k0

source: Aldous & Diaconis, 1986

Random numbers in games

n  terrain generation n  events n  character creation n  decision-making n  game world compression n  synchronized simulation

Game world compression

n  used in Elite (1984) n  finite and discrete galaxy n  enumerate the positions n  set the seed value n  generate a random value for each position

n  if smaller than a given density, create a star n  otherwise, space is void

n  each star is associated with a randomly generated number, which used as a seed when creating the star system details (name, composition, planets)

n  can be hierarchically extended

Example: Elite

Random game world generation

n  discrete game worlds n  example: Nethack, Age of Empires n  rooms, passages, item placements

n  continuous game worlds n  random world is not believable n  modular segments put together randomly

n  terrain generation

Example: Age of Empires 2

Terraing generation: height map

Terrain generation methods

n  simple random n  limited random n  particle deposition n  fault line n  circle hill n  midpoint displacement

Simple random terrain

Limited random terrain

Particle deposition terrain

Fault line terrain

Circle hill terrain

Midpoint displacement terrain

§3 Tournaments

n  rank adjustment (or challege) tournament n  each match is a challenge for a rank exchange n  types: ladder, hill climbing, pyramid, king of the hill

n  elimination tournament (or cup) n  each match eliminates the loser from the tournament n  types: single elimination

n  scoring tournament n  each match rewards the winner n  types: round robin

n  hybridizations

Other uses

n  game balancing n  duelling synthetic players n  adjusting point rewarding schemes

n  heuristic search n  selecting suboptimal candidates for a genetic algorithm

n  group behaviour n  modelling pecking order

n  learning player characteristics n  managing history knowledge

Example: Hill climbing tournament

Juhani

Tuomas

Aapo

Simeoni

Timo

Lauri

Eero

m0

m3

m1

m4

m2

m5

Example: Elimination tournament

Juhani

Tuomas

Aapo

Simeoni

Timo

Lauri

Eero

m0

m1

m2

m3

m4

m5

Example: Scoring tournament

Tuomas Aapo Simeoni Timo Lauri Eero

Juhani m0 m6 m11 m15 m18 m20

Tuomas m1 m7 m12 m16 m19

Aapo m2 m8 m13 m17

Simeoni m3 m9 m14

Timo m4 m10

Lauri m5

Terms

n  players: p0…pn − 1

n  match between pi and pj: match(i, j) n  outcome: WIN, LOSE, TIE

n  rank of pi: rank(i) n  players with the rank r: rankeds(r) n  round: a set of (possibly) concurrent matches n  bracket: diagram of match pairings and rounds

Rank adjustment tournaments

n  a set of already ranked players n  matches

n  independent from one another n  outcome affects only the participating players

n  suits on-going tournaments n  example: boxing

n  matches can be limited by the rank difference

Ladder and pyramid tournaments

pi: rank(i) = 0

pj: rank(j) = 1

pk: rank(k) = 2

pm: rank(m) = 3

pn: rank(n) = 4

pi: rank(i) = 0

pj: rank(j) = 1

pk: rank(k) = 2

pm: rank(m) = 2

pn: rank(n) = 2

rankeds(2) = { k, m, n }

Hill-climbing tournament

n  a.k.a. n  top-of-the-mountain tournament n  last man standing tournament

n  specialization of the ladder tournament n  reigning champion defends the title against challlengers n  similarly: king of the hill tournament specializes the pyramid

tournament

n  initialization n  based on previous competitions n  random

Elimination tournaments

n  loser of a match is eliminated from the tournament n  no ties! → tiebreak competition

n  winner of a match continues to the next round n  how to assign pairings for the first round?

n  seeding

n  examples n  football cups, snooker tournaments

Single elimination

pi

pj

pk

pm

pn

pq

pr

ps

final

semifinal

quarter-final

Bye

bye

pi

pj

pk

pm

pn

pq

pr

Seeding

n  some match pairing will not occur in a single elimination tournament

n  pairings for the first round (i.e., seeding) affects the future pairings

n  seeding can be based on existing ranking n  favour the top-ranked players n  reachability: give the best players an equal

opportunity to proceed the final rounds

Seeding methods

n  random n  does not favour any player n  does not fulfil reachability criterion

n  standard and ordered standard n  favours the top-ranked players n  ordered standard: matches are listed in increasing order

n  equitable n  in the first round, the rank difference between the players is

the same for each match

Byes and fairness

n  the byes have bottom ranks so that they get paired with best players

n  the byes appear only in the first round

Runners-up

n  we find only the champion n  how to determine the runners-up (e.g. silver and

bronze medallists)?

n  random pairing can reduce the effect of seeding n  best players are put into different sub-brackets n  the rest is seeded randomly

n  re-seed the players before each round n  previous matches indicate the current position

n  multiple matches per round (best-of-m)

Double elimination tournament

n  two brackets n  winners’ bracket n  losers’ (or consolation) bracket

n  initially everyone is in the winners’ bracket n  if a player loses, he is moved to the losers’ bracket n  if he loses again, he is out from the tournament

n  the brackets are combined at some point n  for example, the champion of the losers’ bracket

gets to the semifinal in the winners’ bracket

Scoring tournaments

n  round robin: everybody meets everybody else once

n  scoring table determines the tournament winner n  players are rewarded with scoring points n  winner and tie

n  matches are independent from one another

Reduction to a graph

n  n players n  clique Kn n  players as vertices, matches as edges n  how to organize the rounds?

n  a player has at most one match in a round n  a round has as many matches as possible

K5

Reduction to a graph (cont’d)

n  if n is odd, partition the edges of the clique to (n − 1) / 2 disjoint sets n  in each turn, one player is resting n  player pi rests in the round i

n  if n is even, reduce the problem n  player pn − 1 is taken out from the clique n  solve the pairings for n − 1 players as above n  for each round, pair the resting player pi with

player pn − 1

Round robin with seven players

round matches resting

0 1 – 6 2 – 5 3 – 4 0

1 2 – 0 3 – 6 4 – 5 1

2 3 – 1 4 – 0 5 – 6 2

3 4 – 2 5 – 1 6 – 0 3

4 5 – 3 6 – 2 0 – 1 4

5 6 – 4 0 – 3 1 – 2 5

6 0 – 5 1 – 4 2 – 3 6

Real-world tournament examples

n  boxing n  reigning champion and challengers

n  sport wrestling n  double elimination: consolation bracket

n  professional wrestling n  royal rumble

n  World Cup n  ice hockey championship n  snooker

matches (n = 15) 14 14 14 105

rounds 14 6 4 15

champion’s matches

1…14 1…6 3…4 14

match in a round 1 1…4 1…7 7

Practical considerations

n  home matches n  venue bookings n  travelling times n  risk management n  other costs

§4 Game Trees

n  perfect information games n  no hidden information

n  two-player, perfect information games n  Noughts and Crosses n  Chess n  Go

n  imperfect information games n  Poker n  Backgammon n  Monopoly

n  zero-sum property n  one player’s gain equals another player’s loss

Game tree

n  all possible plays of two-player, perfect information games can be represented with a game tree n  nodes: positions (or states) n  edges: moves

n  players: MAX (has the first move) and MIN

n  ply = the length of the path between two nodes n  MAX has even plies counting from the root node n  MIN has odd plies counting from the root node

Division Nim with seven matches

Problem statement

Given a node v in a game tree find a winning strategy for MAX (or MIN) from v

or (equivalently) show that MAX (or MIN) can force a win from v

Minimax

n  assumption: players are rational and try to win n  given a game tree, we know the outcome in the leaves

n  assign the leaves to win, draw, or loss (or a numeric value like +1, 0, –1) according to MAX’s point of view

n  at nodes one ply above the leaves, we choose the best outcome among the children (which are leaves) n  MAX: win if possible; otherwise, draw if possible; else loss n  MIN: loss if possible; otherwise, draw if possible; else win

n  recurse through the nodes until in the root

Minimax rules

1.  If the node is labelled to MAX, assign it to the maximum value of its children.

2.  If the node is labelled to MIN, assign it to the minimum value of its children.

n  MIN minimizes, MAX maximizes → minimax

MAX

MAX

MAX

MIN

MIN

MIN +1

–1

+1

+1

+1 –1

–1 +1 +1 –1

–1 –1 –1

–1

Analysis

n  simplifying assumptions n  internal nodes have the same branching factor b n  game tree is searched to a fixed depth d

n  time consumption is proportional to the number of expanded nodes n  1 — root node (the initial ply) n  b — nodes in the first ply n  b2 — nodes in the second ply n  bd — nodes in the dth ply

n  overall running time O(bd)

Rough estimates on running times when d = 5

n  suppose expanding a node takes 1 ms n  branching factor b depends on the game n  Draughts (b ≈ 3): t = 0.243 s n  Chess (b ≈ 30): t = 6¾ h

n  Go (b ≈ 300): t = 77 a

n  alpha-beta pruning reduces b

Controlling the search depth

n  usually the whole game tree is too large → limit the search depth → a partial game tree → partial minimax

n  n-move look-ahead strategy n  stop searching after n moves n  make the internal nodes (i.e., frontier nodes) leaves n  use an evaluation function to ‘guess’ the outcome

Evaluation function

n  combination of numerical measurements mi(s, p) of the game state n  single measurement: mi(s, p) n  difference measurement: mi(s, p) − mj(s, q) n  ratio of measurements: mi(s, p) / mj(s, q)

n  aggregate the measurements maintaining the zero-sum property

Example: Noughts and Crosses

n  heuristic evaluation function e: n  count the winning lines open to MAX

n  subtract the number of winning lines open to MIN

n  forced wins n  state is evaluated +∞, if it is a forced win for MAX

n  state is evaluated –∞, if it is forced win for MIN

Examples of the evaluation

e(•) = 6 – 5 = 1

e(•) = 4 – 5 = –1

e(•) = +∞

Drawbacks of partial minimax

n  horizon effect n  heuristically promising path can lead to an unfavourable

situation n  staged search: extend the search on promising nodes n  iterative deepening: increase n until out of memory or time n  phase-related search: opening, midgame, end game n  however, horizon effect cannot be totally eliminated

n  bias n  we want to have an estimate of minimax but get a minimax of

estimates n  distortion in the root: odd plies → win, even plies → loss

The deeper the better...?

n  assumptions: n  n-move look-ahead n  branching factor b, depth d, n  leaves with uniform random distribution

n  minimax convergence theorem: n  n increases → root value converges to f(b, d)

n  last player theorem: n  root values from odd and even plies not comparable

n  minimax pathology theorem: n  n increases → probability of selecting non-optimal move

increases (← uniformity assumption!)

Alpha-beta pruning

n  reduce the branching factor of nodes n  alpha value

n  associated with MAX nodes n  represents the worst outcome MAX can achieve n  can never decrease

n  beta value n  associated with MIN nodes n  represents the worst outcome MIN can achieve n  can never increase

Example

n  in a MAX node, α = 4 n  we know that MAX can make a move which will

result at least the value 4 n  we can omit children whose value is less than or

equal to 4

n  in a MIN node, β = 4 n  we know that MIN can make a move which will result

at most the value 4 n  we can omit children whose value is greater than or

equal to 4

Rules of pruning

1.  Prune below any MIN node having a beta value less than or equal to the alpha value of any of its MAX ancestors.

2.  Prune below any MAX node having an alpha value greater than or equal to the beta value of any of its MIN ancestors

Or, simply put: If α ≥ β, then prune below!

Example: αβ-pruning 1(3)

Example: αβ-pruning 2(3)

Example: αβ-pruning 3(3)

Best-case analysis

n  omit the principal variation n  at depth d – 1 optimum pruning: each node

expands one child at depth d n  at depth d – 2 no pruning: each node expands all

children at depth d – 1 n  at depth d – 3 optimum pruning n  at depth d – 4 no pruning, etc. n  total amount of expanded nodes: Ω(bd/2)

Principal variation search

n  alpha-beta range should be small n  limit the range artificially → aspiration search n  if search fails, revert to the original range

n  if we find a move between α and β, assume we have found a principal variation node n  search the rest of nodes the assuming they will not produce a

good move n  if the assumption fails, re-search the node

n  works well if the principal variation node is likely to get selected first

Games of chance

n  minimax trees assume determistic moves n  what about indeterministic events like tossing a coin,

casting a die or shuffling cards?

n  chance nodes: *-minimax tree n  expectiminimax

n  if node v is labelled to CHANCE, multiply the probability of a child with its expectiminimax value and return the sum over all v’s children

n  otherwise, act as in minimax

§5 Path Finding

n  common problem in computer games n  routing characters, troops etc.

n  computationally intensive problem n  complex game worlds n  high number of entities n  dynamically changing environments n  real-time response

Problem statement

n  given a start point s and a goal point r, find a path from s to r minimizing a given criterion

n  search problem formulation n  find a path that minimizes the cost

n  optimization problem formulation n  minimize cost subject to the constraint of the path

The three phases of path finding

1.  discretize the game world n  select the waypoints and connections

2.  solve the path finding problem in a graph n  let waypoints = vertices, connections = edges,

costs = weights n  find a minimum path in the graph

3.  realize the movement in the game world n  aesthetic concerns n  user-interface concerns

Discretization

n  waypoints (vertices) n  doorways, corners, obstacles, tunnels, passages, …

n  connections (edges) n  based on the game world geometry, are two

waypoints connected

n  costs (weights) n  distance, environment type, difference in altitude, …

n  manual or automatic process? n  grids, navigation meshes

Grid

n  regular tiling of polygons n  square grid n  triangular grid n  hexagonal grid

n  tile = waypoint n  tile’s neighbourhood =

connections

Navigation mesh

n  convex partitioning of the game world geometry n  convex polygons covering the game world n  adjacent polygons share only two points and one

edge n  no overlapping

n  polygon = waypoint n  middlepoints, centre of edges

n  adjacent polygons = connections

Solving the convex partitioning problem

n  minimize the number of polygons n  points: n n  points with concave interior angle (notches): r ≤ n − 3

n  optimal solution n  dynamic programming: O(r2n log n)

n  Hertel–Mehlhorn heuristic n  number of polygons ≤ 4 × optimum n  running time: O(n + r log r) n  requires triangulation

n  running time: O(n) (at least in theory) n  Seidel’s algorithm: O(n lg* n) (also in practice)

Path finding in a graph

n  after discretization form a graph G = (V, E) n  waypoints = vertices (V) n  connections = edges (E) n  costs = weights of edges (weight : E → R+)

n  next, find a path in the graph

Graph algorithms

n  breadth-first search n  running time: O(|V| + |E|)

n  depth-first search n  running time: Θ(|V| + |E|)

n  Dijkstra’s algorithm n  running time: O(|V|2) n  can be improved to O(|V| log |V| + |E|)

Heuristical improvements

n  best-first search n  order the vertices in the neighbourhood according to

a heuristic estimate of their closeness to the goal n  returns optimal solution

n  beam search n  order the vertices but expand only the most

promising candidates n  can return suboptimal solution

Evaluation function

n  expand vertex minimizing f(v) = g(s ~> v) + h(v ~> r)

n  g(s ~> v) estimates the minimum cost from the start vertex to v

n  h(v ~> r) estimates (heuristically) the cost from v to the goal vertex

n  if we had exact evaluation function f *, we could solve the problem without expanding any unnecessary vertices

Cost function g

n  actual cost from s to v along the cheapest path found so far n  exact cost if G is a tree n  can never underestimate the cost if G is a general

graph

n  f(v) = g(s ~> v) and unit cost → breadth-first search

n  f(v) = –g(s ~> v) and unit cost → depth-first search

Heuristic function h

n  carries information from outside the graph n  defined for the problem domain n  the closer to the actual cost, the less superfluous

vertices are expanded n  f(v) = g(s ~> v) → cheapest-first search n  f(v) = h(v ~> r) → best-first search

Admissibility

n  let Algorithm A be a best-first search using the evaluation function f

n  search algorithm is admissible if it finds the minimal path (if it exists) n  if f = f *, Algorithm A is admissible

n  Algorithm A* = Algorithm A using an estimate function h n  A* is admissible, if h does not overestimate the

actual cost

Monotonicity

n  h is locally admissible → h is monotonic n  monotonic heuristic is also admissible n  actual cost is never less than the heuristic cost

→ f will never decrease n  monotonicity → A* finds the shortest path to

any vertex the first time it is expanded n  if a vertex is rediscovered, path will not be shorter n  simplifies implementation

Optimality

n  Optimality theorem: The first path from s to r found by A* is optimal.

n  Proof: textbook p. 105

Informedness

n  the more closely h approximates h*, the better A* performs

n  if A1 using h1 will never expand a vertex that is not also expanded by A2 using h2, A1 is more informed that A2

n  informedness → no other search strategy with the same amount of outside knowledge can do less work than A* and be sure of finding the optimal solution

Algorithm A*

n  because of monotonicity n  all weights must be positive n  closed list can be omitted

n  the path is constructed from the mapping π starting from the goal vertex n  s → … → π(π(π(r))) → π(π(r)) → π(r) → r

A* example 1(6)

A* example 2(6)

A* example 3(6)

A* example 4(6)

A* example 5(6)

A* example 6(6)

Practical considerations

n  computing h n  despite the extra vertices expanded, less informed h

may yield computationally less intensive implementation

n  suboptimal solutions n  by allowing overestimation A* becomes

inadmissible, but the results may be good enough for practical purposes

Realizing the movement

n  movement through the waypoints n  unrealistic: does not follow the game world

geometry n  aesthetically displeasing: straight lines and sharp

turns

n  improvements n  line-of-sight testing n  obstacle avoidance

n  combining path finding to user-interface n  real-time response

Recapitulation

1.  discretization of the game world n  grid, navigation mesh n  waypoints, connections, costs

2.  path finding in a graph n  Algorithm A*

3.  realizing the movement n  geometric corrections n  aesthetic improvements

Alternatives?

n  Although this is the de facto approach in (commercial) computer games, are there alternatives?

n  possible answers n  AI processors (unrealistic?) n  robotics: reactive agents (unintelligent?) n  analytical approaches (inaccessible?)

§6 Decision-Making

n  decision-making and games n  levels of decision-making n  modelled knowledge n  method

n  example methods n  finite state machines n  flocking algorithms n  influence maps

n  this will not be a comprehensive guide into decision-making!

MVC (revisited)

control logic

driver

proto-view

rendering

state instance core structures

input device

action

configuration

instance data

synthetic view

synthetic player

script output device

human player

options

perception

model

view controller

Decision-making system

World Pattern recognition

Observed events and states

Decision-making system

Requested actions

Possible actions

Primitive events and states

Previous primitives

Three perspectives for decision-making in computer games

n  level of decision-making n  strategic, tactical, operational

n  use of the modelled knowledge n  prediction, production

n  methods n  optimization, adaptation

Level of decision-making

n  strategic n  what should be done

n  tactical n  how to actuate it

n  operational n  how to carry it out

Strategic level

n  long-term decisions n  infrequent → can be computed offline or in the

background

n  large amount of data, which is filtered to bring forth the essentials n  quantization problem?

n  speculative (what-if scenarios) n  the cost of a wrong decision is high

Tactical level

n  medium-term decisions n  intermediary between strategic and operational

levels n  follow the plan made on the strategic level n  convey the feedback from the operational level

n  considers a group of entities n  a selected set of data to be scrutinized n  co-operation within the group

Operational level

n  short-term decisions n  reactive, real-time response

n  concrete and closely connected to the game world

n  considers individual entities n  the cost of a wrong decision is relatively low

n  of course not to the entity itself

Use of the modelled knowledge

n  time series data n  world = a generator of events and states, which

can be labelled with symbols n  prediction

n  what the generator will produce next?

n  production n  simulating the output of the generator

n  how to cope with uncertainty?

Prediction

Modeller

maximum probability Generator

Production

Modeller

random selection from

probability distribution

Decision-making methods

n  optimization n  find an optimal solution for a given objective

function n  affecting factors can be modelled

n  adaption n  find a function behind the given solutions n  affecting factors are unknown or dynamic

Optimization

solution

optimality

local optimum

global optimum

objective function

Optimization methods

n  hill-climbing n  how to escape local optima?

n  tabu search n  simulated annealing n  genetic algorithms

n  multiple search traces

n  swarm algorithms

Adaptation

solution

feedback

sample cases

fitted function

Adaptation methods

n  neural networks n  training

n  supervised learning n unsupervised learning (e.g., self-organizing maps)

n  execution

n  hidden Markov model n  recurring structures

Finite state machine (FSM)

n  components: n  states n  transitions n  events n  actions

n  state chart: fully connected directed graph n  vertices = states n  edges = transitions

Properties of FSM

1.  acceptor n  does the input sequence fulfil given criteria?

2.  transducer n  what is the corresponding output sequence for a

given input sequence?

3.  computator n  what is the sequence of actions for a given input

sequence?

n  these properties are independent!

Mealy and Moore machines

n  theoretical cathegories for FSMs n  Mealy machine

n  actions are in transitions n  the next action is determined by the current state and the

occurring event n  more compact but harder to comprehend

n  Moore machine n  actions are in states n  the next action is determined by the next state

n  helps to understand and use state machines in UML

Implementation

n  design by contract n  two parties: the supplier and the client n  formal agreement using interfaces

n  FSM software components n  environment: view to the FSM (client) n  context: handles the dynamic aspects of the FSM

(supplier) n  structure: maintains the representation of the FSM

(supplier)

Noteworthy

n  structure is static n  hard to modify

n  reactivity n  memoryless representation of all possible walks from the

initial state n  states are mutually exclusive: one state at a time

n  not for continuous or multivalued values n  combinatorial explosion

n  if the states and events are independent n  risk of total rewriting

n  high cohesion of actions

Flocking

n  C. W. Reynolds: “Flocks, herds, and schools: A distributed behavioral model” (1987)

n  a flock seems to react as autonomous entity although it is a collection of individual beings

n  flocking algorithm emulates this phenomenon n  results resemble various natural group

movements n  boid = an autonomous agent in a flock

Rules of flocking

1.   Separation: Do not crowd flockmates. 2.   Alignment: Move in the same direction as

flockmates. 3.   Cohesion: Stay close to flockmates. 4.   Avoidance: Avoid obstacles and enemies. → boid’s behavioural urges

Observations

n  stateless algorithm n  no information needs to be maintained n  boid re-evaluates the environment on each update

cycle

n  no centralized control n  emergent behaviour

Other uses for flocking

n  swarm algorithms n  solution candidate = boid n  solution space = flying space n  separation prevents crowding the local optima

n  obstacle avoidance in path finding n  steer away from obstacles along the path

Influence maps

n  discrete representation of the synthetic player’s knowledge of the world

n  strategic and tactical information n  frontiers, control points, weaknesses

n  influence n  type n  repulsiveness/alluringness

n  recall path finding and terrain generation

Assumptions

n  a regular grid over the game world n  each tile holds numeric information of the

corresponding area n  positive values: alluringness n  negative values: repulsiveness

Construction

1.  initialization n  assign values to the tiles where the influence exists

2.  propagation n  spread the effect to the neighbouring tiles n  linear or exponential fall-off n  cut-off point

Example: Initialization and propagation

5 10 20 10 5

10 20 40 20 10

5 10 20 10 5

2 5 10 5 2

1 2 5 2 1

0 -1 -2 -2 -2

-1 -2 -6 -4 -6

-2 -6 -12 -10 -10

-5 -10 -20 -12 -10

-2 -5 -10 -6 -4

Aggregation

n  influence map can be combined n  the same (or compatible) granularity

n  example n  map 1 = my troops n  map 2 = enemy’s troops n  map 3 = map 1 + map 2 = battlefield

n  aggregation n  operator: sum, product n  weights: to balance the effects

Example: Aggregation

5 9 18 8 3

9 18 36 16 4

3 4 8 0 -5

-3 -5 -10 -7 -8

-1 -3 -5 -4 -3

Evaluation

n  static features: compute beforehand n  periodical updates

n  categorize the maps based on the rate of change n  lazy evaluation

Key questions for synthetic players

n  how to achieve real-time response? n  how to distribute the synthetic players in a

network? n  how autonomous the synthetic players should

be? n  how to communicate with other synthetic

players?

§7 Modelling Uncertainty

n  probabilistic uncertainty n  probability of an outcome n  dice, shuffled cards n  statistical reasoning

n Bayesian networks, Dempster-Shafer theory

n  possibilistic uncertainty n  possibility of classifying object n  sorites paradoxes n  fuzzy sets

Probabilistic or possibilistic uncertainty?

n  Is the vase broken? n  Is the vase broken by a burglar? n  Is there a burglar in the closet? n  Is the burglar in the closet a man? n  Is the man in the closet a burglar?

Bayes’ theorem

n  hypothesis H n  evidence E n  probability of the hypothesis P(H) n  probability of the evidence P(E) n  probability of the hypothesis based on the

evidence P(H|E) = (P(E|H) · P(H)) / P(E)

Example

n  H — there is a bug in the code n  E — a bug is detected in the test n  E|H — a bug is detected in the test given that

there is a bug in the code n  H|E — there is a bug in the code given that a

bug is detected in the test

Example (cont’d)

n  P(H) = 0.10 n  P(E|H) = 0.90 n  P(E|¬H) = 0.10 n  P(E) = P(E|H) · P(H) + P(E|¬H) · P(¬H)

= 0.18 n  from Bayes’ theorem:

P(H|E) = 0.5 n  conclusion: a detected bug has fifty-fifty chance

that it is not in the actual code

Bayesian networks

n  describe cause-and-effect relationships with a directed graph n  vertices = propositions or variables n  edges = dependencies as probabilities

n  propagation of the probabilities n  problems:

n  relationships between the evidence and hypotheses are known

n  establishing and updating the probabilities

Dempster-Shafer theory

n  belief about a proposition as an interval [ belief, plausability ] ⊆ [ 0, 1]

n  belief supporting A: Bel(A) n  plausability of A: Pl(A) = 1 − Bel(¬A) n  Bel(intruder) = 0.3, Pl(intruder) = 0.8

n  Bel(no intruder) = 0.2 n  0.5 of the probability range

is indeterminate

Belief interval

0 1 Bel(A) Pl(A)

Belief Uncertainty Non-belief

Plausability

Doubt

Example 1(5)

n  hypotheses: animal, weather, trap, enemy n Θ = { A, W, T, E}

n  task: assign a belief value for each hypothesis n  evidence can affect one or more hypotheses

n  mass function m(H) = current belief to the set H of hypotheses n  in the beginning m(Θ) = 1

n  evidence ‘noise’ supports A, W and E n  mass function mn({ A, W, E }) = 0.6, mn(Θ) = 0.4

Example 2(3)

n  evidence ‘footprints’ supports A, T, E n  mf({ A, T, E }) = 0.8, mf(Θ) = 0.2

n  combination with Dempster’s rule: n  mnf({A, E}) = 0.48, mnf({W, A, E}) = 0.12,

mnf({A, T, E}) = 0.32, mnf(Θ) = 0.08

n  enemy, trap, trap or enemy, weather, or animal? n  Bel(E) = 0, Pl(E) = 1 n  Bel(T) = 0, Pl(T) = 0.4 n  Bel(T, E) = 0, Pl(T, E) = 1 n  Bel(W) = 0, Pl(W) = 0.2 n  Bel(A) = 0, Pl(A) = 1

Example 3(3)

n  evidence ‘candy wrapper’ supports T, E n  mc({E}) = 0.6, mc({T}) = 0.3, mc(Θ) = 0.1

n  combination with Dempster’s rule: n  mnfc({E}) = 0.73, mnfc({T}) = 0.15,

mnfc({A, E}) = 0.06, mnfc({A, T, E}) = 0.04, mnfc({W, A, E}) = 0.01, mnfc(Θ) = 0.01

n  enemy, trap, trap or enemy, weather, or animal? n  Bel(E) = 0.73, Pl(E) = 0.85 n  Bel(T) = 0.15, Pl(T) = 0.2 n  Bel(T, E) = 0.88, Pl(T, E) = 1 n  Bel(W) = 0, Pl(W) = 0.02 n  Bel(A) = 0, Pl(A) = 0.03

Fuzzy sets

n  element x has a membership in the set A defined by a membership function μA(x) n  not in the set: μA(x) = 0 n  fully in the set: μA(x) = 1 n  partially in the set: 0 < μA(x) < 1

n  contrast to classical ‘crisp’ sets n  not in the set: χA(x) = 0 n  in the set: χA(x) = 1

Membership function

U

μ

1

0

A

x

μA(x)

How to assign membership functions?

n  real-word data n  physical measurements n  statistical data

n  subjective evaluation n  human experts’ cognitive knowledge n  questionnaires, psychological tests

n  adaptation n  neural networks, genetic algorithms

n  → simple functions usually work well enough as long as they model the general trend

Fuzzy operations

n  union: μC(x) = max{μA(x), μB(x)} n  intersection: μC(x) = min{μA(x), μB(x)} n  complement: μC(x) = 1 − μA(x)

n  note: operations can be defined differently

Fuzzy operations (cont’d)

U

μ 1

0

A B

A ∪ B

A ∩ B

A

Uses for fuzzy sets

n  approximate reasoning n  fuzzy constraint satisfaction problem n  fuzzy numbers n  almost any ‘crisp’ method can be fuzzified!

Constraint satisfaction problem

n  constraint satisfaction problem (CSP): n  a set of n variables X n  a domain Di for each variable xi in X n  a set of constraints restricting the feasibility of the

tuples (x0, x1,…, xn – 1) ∈ D0 × … × Dn – 1

n  solution: an assignment of value to each variable so that every constraint is satisfied n  no objective function → not an optimization

problem

Example: n queens problem as a CSP

n  problem: place n queens on a n × n chessboard so that they do not threat one another

n  CSP formulation n  variables: xi for each row i n  domain: Di = { 1, 2,…, n } n  constraints:

n xi ≠ xj n xi – xj ≠ i – j n xj – xi ≠ i – j

Fuzzy constraint satisfaction problem

n  fuzzy constraint satiscation problem (FCSP) is a five-tuple P = 〈 V, Cµ, W, T, U 〉 n  V: variables n  U: universes (domains) for the variables n  Cµ: constraints as membership functions n  W: weighting scheme n  T: aggregation function

Dog Eat Dog: Modelling the criteria as fuzzy sets

n  if the visual observation of the enemy is reliable, then avoid the enemy

n  if the visual observation of the prey is reliable, then chase the prey

n  if the olfactory observation of the pond is reliable, then go to the pond

n  if the visual observation of the enemy is reliable, then stay in the centre of the play field

Dog Eat Dog: Weighting the criteria importances

n  fuzzy criterion Ci has a weight wi ∈ [0, 1] n  a greater value wi corresponds to a greater importance

n  the weighted value from the implication wi → Ci n  classical definition (A → B ⇔ ¬A ∨ B): min{ (1 − wi ), Ci } n  Yager’s weighting scheme: the weighted membership value:

⎧ 1, if μ(x) = 0 and w = 0 μC

w(x) = ⎨ ⎩ (μC(x))w, otherwise

Dog Eat Dog: Aggregating the criteria

n  aggregator should have compensatory properties n  the effect of a poorly satisfied criterion is not so

drastic n  mean-based operators instead of conjunction

n  ordered weighted averaging (OWA)

Ordered weighted averaging (OWA)

n  weight sequence W = (w0, w1,…,wn – 1)T n  ∀wi ∈ [0, 1] and Σwi = 1

n  F(a0, a1,…,an – 1) = Σwjbj n  bj is the (j+1)th largest element of the sequence

A = 〈a0, a1,…,an – 1〉

n  by setting the weight sequence we can get n  conjunction: W = { 0, 0,…, 1} = min{A} n  disjunction: W = { 1, 0,…, 0} = max{A} n  average: W = {1/n, 1/n,…, 1/n}

n  soft-and operator: wi = 2(i + 1) / (n(n + 1)) n  example: n = 4, W = { 0.1, 0.2, 0.3, 0.4 }

Outroduction

§1 Introduction §2 Random Numbers §3 Tournaments §4 Game Trees §5 Path Finding §6 Decision-Making §7 Modelling Uncertainty

The intention, huh?

n  to provide a glance into the world of computer games as seen from the perspective of a computer scientist

Examinations

n  examination dates 1.  October 12 2009 2.  November 16, 2009 3.  December 14, 2009

n  check the exact times and places at http://www.it.utu.fi/opetus/tentit/

n  remember to enrol! https://ssl.utu.fi/nettiopsu/

source: The New Yorker, Sep. 17, 2007

Examinations (cont’d)

n  questions n  based on both lectures and the textbook n  two questions, à 5 points n  to pass the examination, at least 5 points (50%) are

required n  grade: g = ⎡p − 5⎤ n  questions are in English, but you can answer in

English or in Finnish

Follow-up course: Multiplayer Computer Games

n  focus: networking in computer games n  credits: 5 cp (3 cu) n  schedule:

n  October 27 – November 26, 2009 n  Tuesdays 10–12 a.m., and Thursdays 10–12 p.m.

n  web page: http://www.iki.fi/smed/mcg

top related