Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Artificial Intelligence

LD Smith
October 14, 2018

Artificial Intelligence

Levi D. Smith covers various topics in the AI field such as identifying and evaluating alternatives, data structures and algorithms for decision making, computation time, conditioning, and expert systems. These topics are presented with a focus on how they can be applied to game design.

LD Smith

October 14, 2018
Tweet

More Decks by LD Smith

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. Other Trees • Red black trees • Extra bit •

    Self-balancing • Minimax decision rule • Pruning • Alpha, beta values
  7. 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
  8. 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
  9. 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
  10. 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
  11. Conditioning CPU Player Result Rock Scissors Rock Rock Rock Paper

    Rock Paper Scissors Paper Rock, Paper, Scissors Rock Paper Scissors beats beats beats
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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(); }
  18. 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<int> listChoices = new List<int>(); 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<int> 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)); } }
  19. 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--; }
  20. 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