Slide 1

Slide 1 text

AI (Artificial Intelligence) for Game Development 2018 Levi D. Smith

Slide 2

Slide 2 text

Overview • Decisions, Evaluating Alternatives • Tree Structures • Computation Time • Conditioning • Expert Systems

Slide 3

Slide 3 text

Identifying Number of Outcomes • How many ways to fill a tic tac toe board with X’s and O’s • Two alternatives • Nine cells • Total number of ways to fill X’s and O’s on the board = 29 = 512 • Similar to truth table used in computer science and computer engineering • 252 valid outcomes (alternating turns) • 255,168 game tree states • How many ways to arrange the letters in MISSISSIPPI? • 11! / (4! * 4! * 2!) = 34,650

Slide 4

Slide 4 text

Tic Tac Toe – Assigning decision weights 4 2 2 3 3 2 3 2 3 • Calculate how many ways to win with each square • Calculate how many way to lose with each square

Slide 5

Slide 5 text

Tic Tac Toe • Calculate how many ways you can win with each square • Play to win or not to lose? X X O O O X X O O O X X O O O X X O O O X X X X X O O O X P(Xwin ) = 2 P(Owin ) = 2 P(Xwin ) = 2 P(Owin ) = 3 P(Xwin ) = 2 P(Owin ) = 2 P(Xwin ) = 2 P(Owin ) = 2 P(Xwin ) – P(Owin ) = 0 = -1 = 0 = 0

Slide 6

Slide 6 text

Chess • Pieces have capture values • Is capturing the highest valued piece always the best move? • Queen captures bishop • Knight captures queen Source: https://en.wikipedia.org/wiki/Chess_piece_relative_value

Slide 7

Slide 7 text

Trees • Binary Search Trees • Structure with two pointers • Left child is always less than parent • Right child is always greater than parent 20 14 32 9 18 57 40 99 root node

Slide 8

Slide 8 text

Tree Traversal • Pre Order • In Order • Data is sorted • Post Order

Slide 9

Slide 9 text

Other Trees • Red black trees • Extra bit • Self-balancing • Minimax decision rule • Pruning • Alpha, beta values

Slide 10

Slide 10 text

Computation Complexity • “Big O” notation • O(1) – constant time • Accessing element in an array • O(log n) – logarithmic time • Binary search • O(n) – linear time • Add all items in a list, find largest number in an unsorted list • O(n log n) • Quicksort • O(n2) – polynomial time • O(2n) – exponential time • Solvable for simple problems, but years as the input grows

Slide 11

Slide 11 text

Problem complexity • P problems – answer can be provided in polynomial time • NP problems – (non deterministic polynomial) answer can be verified in polynomial time, but solution can’t be generated in polynomial time • NP hard – at least as hard as the hardest in NP • Prime factorization • Quantum Computing, Schrödinger • NP-complete – hardest problems in NP • Traveling salesman

Slide 12

Slide 12 text

An Action Game Example • Easter Egg Hunt • Carrot gives speed boost • Ice slows others • Bonus eggs have extra values • Decisions • Move in straight line toward the closest egg • Move in straight line toward the bonus egg • Move towards an ice powerup • Move towards a speed powerup • Factors • What the current score? • What is the minimum number of points guarantee win? • Score > Total number of points / 2 • Time to get to the closest egg / powerup • Calculate the time required to get to each powerup / egg

Slide 13

Slide 13 text

Dijkstra’s Algorithm • Difference between a graph and a tree • Shortest path between two specified nodes • Run time: O(n2), polynomial (quadratic) time • Best-first search • Difference with traveling salesman • No start and end points • Must visit all nodes

Slide 14

Slide 14 text

Conditioning • Skinner box • Pavlov’s Dog

Slide 15

Slide 15 text

Conditioning CPU Player Result Rock Scissors Rock Rock Rock Paper Rock Paper Scissors Paper Rock, Paper, Scissors Rock Paper Scissors beats beats beats

Slide 16

Slide 16 text

Fighting Games Standing Guard Low Attack beats Standing Guard Throw beats Street Fighter Tekken Crouching Guard Mid Attack beats Crouching Guard Air Attack beats Low Attack Parry beats • Otherwise, faster attack beats slower attack • Generally, slower attacks do more damage High Attack Crouch avoids General

Slide 17

Slide 17 text

Prolog – Logic Programming • Alain Colmerauer • “Expert System” • Inference Engine, Knowledge Base • Query relationships • Results are only as good as the input information • Problem of transferring knowledge from Subject Matter Experts to the Knowledge Base • Tools for capturing knowledge required • SWI-Prolog • Other functional languages (LISP, Scheme) • Neural Network – Emulating the human brain

Slide 18

Slide 18 text

Running SWI-Prolog • Command line • -f to load a knowledge base • Windows interface • Use “consult” to load a knowledge base • Semicolon to see more results

Slide 19

Slide 19 text

Prolog with Unity • Install Prolog.NET using NuGet • NuGet packages don’t work correctly with Unity projects • Find the installed DLLs and copy them into the Unity project (not plugins folder!) • Packages\Prolog.NET.1.0.2\tools • Set API level settings to 4.0 • Build Settings > Player Settings • Use .NET Framework 4.x instead of .NET Core 2.x? • “Prolog” or “SwiPlC” should appear under references • Working demo with CSProlog

Slide 20

Slide 20 text

Other considerations • Processing time • Each frame in 1.667 hundredths of a second (16.67 milliseconds) • More AI time may be okay in puzzle games where time is not a factor • Give the player a visual cue that the CPU is “thinking” • Consider Big O running time • Enemy follow patterns • Less processing time • More predictable • Mega Man, Punch-Out, Zelda

Slide 21

Slide 21 text

Distributing AI Choices • Simplest method • Can be “streaky” Attack 50% Defend 30% Special Power 20% iAttackDist = 50 / 100; iDefendDist = 30 / 100; iSpecialDist = 20 / 100; iRandom = Random.Range(0, 10) if (iRandom < iAttackDist) { doAttack(); } else if (iRandom < (iAttackDist + iDefendDist) ) { doDefend(); } else if (iRandom < (iAttackDist + iDefendDist + iSpecialDist) ) { doSpecial(); }

Slide 22

Slide 22 text

Distributing AI Choices • Queue method • Keep actions in a list • Ensures that all actions are performed at least once for the set • Good for puzzle game block distribution ATTACK = 0; DEFEND = 1; SPECIAL = 2; List listChoices = new List(); if (listChoices.Count <= 0) { addChoices(listChoices); } iChoice = listChoices[0] listChoices.RemoveAt(0); if (iChoice == ATTACK) { doAttack(); } else if (iChoice == DEFEND) { doDefend(); } else if (iChoice == SPECIAL) { doSpecial(); } void addChoices(List listChoices) { int i; iAttackDist = 5 iDefendDist = 3 iSpecialDist = 2 for (i = 0; < iAttackDist; i++) { listChoices.InsertAt(ATTACK, Random.Range(0, listChoices.Count)); } for (i = 0; < iDefendDist; i++) { listChoices.InsertAt(DEFEND, Random.Range(0, listChoices.Count)); } for (i = 0; < iSpecialDist; i++) { listChoices.InsertAt(SPECIAL, Random.Range(0, listChoices.Count)); } }

Slide 23

Slide 23 text

Distributing AI Choices • Track number of actions remaining as integer variables • More memory efficient than list method? iAttackDist = 5 iDefendDist = 3 iSpecialDist = 2 if ((iAttackRemain + iDefendRemain + iSpecialRemain) <= 0) { iAttackRemain = iAttackDist; iDefendRemain = iDefendDist; iSpecialRemain = iSpecialDist } iChoice = Random.Range(0, iAttackRemain + iDefendRemain + iSpecialRemain); if (iChoice < iAttackRemain) { doAttack(); iAttackRemain--; } else if (iChoice < iAttackRemain + iDefendRemain) { doDefend(); iDefendRemain--; } else if (iChoice < iAttackRemain + iDefendRemain + iSpecialRemain) { doSpecial(); iSpecialRemain--; }

Slide 24

Slide 24 text

Other AI Strategies • Rubberband AI • Gives advantage to AI falling behind • Penalizes human too far ahead • Racing games • Strength in Numbers • Beat em ups, RTS • Humans are limited to input devices • AI can alter the game itself • Overpowered bosses