Informatik II Tutorial 9 Mihai Bce mihai.bace@inf.ethz.ch Mihai - - PowerPoint PPT Presentation

informatik ii
SMART_READER_LITE
LIVE PREVIEW

Informatik II Tutorial 9 Mihai Bce mihai.bace@inf.ethz.ch Mihai - - PowerPoint PPT Presentation

Informatik II Tutorial 9 Mihai Bce mihai.bace@inf.ethz.ch Mihai Bce | | 22-Nov-19 1 Overview Debriefing Exercise 8 Briefing Exercise 9 Mihai Bce | | 22-Nov-19 2 U8.A1 Binary Search (a & b) 0 1 2 3 4 5 6 7 8 9


slide-1
SLIDE 1

| |

Mihai Bâce

mihai.bace@inf.ethz.ch

22-Nov-19 1

Informatik II

Tutorial 9

Mihai Bâce

slide-2
SLIDE 2

| | 22-Nov-19 Mihai Bâce 2

Overview

§ Debriefing Exercise 8 § Briefing Exercise 9

slide-3
SLIDE 3

| | 22-Nov-19 Mihai Bâce 3

U8.A1 Binary Search (a & b)

92 89 78 70 68 66 65 62 56 47 33 25 17 7 3

mi = (re-li)/2 + li;

14 13 12 11 10 9 8 7 6 5 4 3 2 1

li mi re

slide-4
SLIDE 4

| | 22-Nov-19 Mihai Bâce 4

U8.A1 Binary Search (c)

92 89 78 70 68 66 65 62 56 47 33 25 17 7 3

li mi re

mi = (re-li)/3 + li;

14 13 12 11 10 9 8 7 6 5 4 3 2 1

Conclusion: Small numbers? Faster! Large numbers? Slower? Overall? Better to split in half!

slide-5
SLIDE 5

| | 22-Nov-19 Mihai Bâce 5

U8.A1 Binary Search: How to implement?

public Value find(ArrayList<Unit<Key, Value>> haystack, Key needle) { } public Value find(ArrayList<Unit<Key, Value>> haystack, Key needle) { return findRecursive(haystack, needle, 0, haystack.size()); } private Value findRecursive( ArrayList<Unit<Key, Value>> haystack, Key needle, int begin, int end) { ... }

The interface defines the method like this: But, we can do better:

slide-6
SLIDE 6

| | 22-Nov-19 Mihai Bâce 6

U8.A1 Binary Search: find

Find division point Go left on the tree Or right

slide-7
SLIDE 7

| | 22-Nov-19 Mihai Bâce 7

U8.A2 Knapsack problem

§ Does the simple strategy deliver the optimal solution?

§ Yes, why? § We go through all the possible solutions § However, this is quite unlikely in most scenarios (time, memory, etc.)

§ Is there only one optimal solution?

§ No § How do you prove this? § By counter-example

§

Item <Weight, Value> [ <1,1>, <2,1>, <3,2> ]

§

Wmax = 3

§

Selection_1 = [1, 1, 0]

§

Selection_2 = [0, 0, 1]

slide-8
SLIDE 8

| | 22-Nov-19 Mihai Bâce 8

U8.A2 Brute force approach

public Selection findBest( ArrayList<Integer> values, ArrayList<Integer> weights, int maxWeight) { int last = (int) Math.pow(2, values.size()); int bestsum = 0; Selection bestsel = new Selection(values.size()); Selection sel = new Selection(values.size()); for(int i = 0; i < last; i++) { sel.setBits(i); if( sel.sum(weights) <= maxWeight ){ if( sel.sum(values) > bestsum ){ bestsum = sel.sum(values); bestsel.setBits(i); } } } return bestsel; }

slide-9
SLIDE 9

| | 22-Nov-19 Mihai Bâce 9

U8.A2 Backtracking (1)

private class FindResult { public int value; public Selection selection; FindResult(int val, Selection sel){ value = val; selection = sel; } } public Selection findBest(ArrayList<Integer> values, ArrayList<Integer> weights, int maxWeight) { if( values.size() != weights.size() ) throw new IllegalArgumentException("sizes of values and weights vectors are not equal"); // give initial selection and weight 0 FindResult result = find(new Selection(0), 0, values, weights, maxWeight); return result.selection; }

slide-10
SLIDE 10

| | 22-Nov-19 Mihai Bâce 10

U8.A2 Backtracking (2)

// find(new Selection(0), 0, values, weights, maxWeight); private FindResult find(Selection selection, int weight, ArrayList<Integer> values, ArrayList<Integer> weights, int maxWeight) { final int depth = selection.size(); if(depth == values.size()) return new FindResult( selection.sum(values), selection ); Selection without = new Selection(depth + 1, selection.bits()); without.set(depth, false); FindResult resultWithout = // recursion without current item find(without, weight, values, weights, maxWeight); if( weight + weights.get(depth) <= maxWeight) { Selection with = new Selection(depth + 1, selection.bits()); with.set(depth, true); FindResult resultWith = // recursion with current item find(with, weight + weights.get(depth), values, weights, maxWeight); if(resultWith.value > resultWithout.value) return resultWith; } return resultWithout; }

the actual Backtracking

slide-11
SLIDE 11

| | 22-Nov-19 Mihai Bâce 11

U8.A2 Some conclusions

§ Backtracking vs. Brute force

§ Backtracking is much faster § Backtracking: cuts early many solutions that are not possible § Both find the optimal solution

slide-12
SLIDE 12

| | 22-Nov-19 Mihai Bâce 12

U8.A3 Reversi [Part II] – Some useful commands

§ checkMove is declared in reversi.GameBoard § boolean reversi.GameBoard.checkMove(int, Coordinates) § Other useful methods § boolean reversi.GameBoard.isMoveAvailable(int) § boolean reversi.GameBoard.validCoordinates(Coordinates) § int reversi.GameBoard.countStones(int) § int reversi.Utils.other(int) § … § Summary: JavaDoc is cool! Use it! https://www.vs.inf.ethz.ch/edu/I2/reversi/javadoc/

slide-13
SLIDE 13

| | 22-Nov-19 Mihai Bâce 13

U8.A3 Reversi – check move

§ boolean checkMove(GameBoard …,Coordinates c) § Field must be free! § Check all directions § Until at least one direction is “valid”…

for( int i = -1; i <= 1; ++i ) for( int j = -1; j <= 1; ++j ) if( i != 0 && j != 0 ) if( checkDirection(gb, player, c, new Coordinates(i, j) ) return true; //would be a possible move return false; //not a possible move

§ follow( gb, player, pos, dir )

§ Follow this new direction § If it’s a stone from the other player, call follow again on the same direction § If it’s my stone, we stop. The move is valid!

slide-14
SLIDE 14

| | 22-Nov-19 Mihai Bâce 14

U8.A3 checkMove

public public boolean boolean checkMove checkMove(GameBoard GameBoard gb gb, , int int player player, Coordinates , Coordinates coord coord) { try try { if if (gb gb.getOccupation .getOccupation(coord coord) != ) != GameBoard. GameBoard.EMPTY EMPTY) ) return return false false; } catch catch (OutOfBoundsException OutOfBoundsException e) { ) { return return false false; } for for (int int x=-1; 1; x<=1; <=1; x++) { ++) { for for (int int y=-1; 1; y<=1; <=1; y++) { ++) { if if (x == 0 && == 0 && y == 0) == 0) continue continue; if if (checkDirection checkDirection(gb gb, , player player, , coord coord, , x, , y)) )) return return true true; } } return return false false; }

slide-15
SLIDE 15

| | 22-Nov-19 Mihai Bâce 15

U8.A3 checkMove - checkDirection

follow: move along a certain direction, given the rules from the previous slides

private private boolean boolean checkDirection checkDirection(GameBoard GameBoard gb gb, , int int player player, Coordinates , Coordinates coord coord, , int int x, , int int y) { Coordinates c = new new Coordinates( Coordinates(coord coord.getRow .getRow() + () + x, , coord coord.getCol .getCol() + () + y); ); try try { if if (gb gb.getOccupation .getOccupation(c) != ) != Utils. Utils.other

  • ther(player

player)) )) return return false false; } catch catch (OutOfBoundsException OutOfBoundsException e) { ) { return return false false; } return return follow( follow(gb gb, , Utils. Utils.other

  • ther(player

player), ), c, , x, , y); ); }

slide-16
SLIDE 16

| | 22-Nov-19 Mihai Bâce 16

U8.A3 Follow

private private boolean boolean follow( follow(GameBoard GameBoard gb gb, , int int player player, Coordinates , Coordinates coord coord, , int int x, , int int y) { Coordinates c = new new Coordinates( Coordinates(coord coord.getRow .getRow() + () + x, , coord coord.getCol .getCol() + () + y); ); int int occupation

  • ccupation;

try try {

  • ccupation = gb.getOccupation(c);

} catch catch (OutOfBoundsException OutOfBoundsException e) { ) { return return false false; } if if (occupation

  • ccupation ==

== player player) ) return return follow( follow(gb gb, , player player, , c, , x, , y); ); if if (occupation

  • ccupation ==

== Utils. Utils.other

  • ther(player

player)) )) return return true true; return return false false; }

slide-17
SLIDE 17

| | 22-Nov-19 Mihai Bâce 17

U8.A3 Reversi – Greedy player

§ For all possible moves § Simulate the move

§ Make a copy of the gameboard § Make the move § Evaluate your move (how?)

§ Choose the best move

slide-18
SLIDE 18

| | 22-Nov-19 Mihai Bâce 18

slide-19
SLIDE 19

| | 22-Nov-19 Mihai Bâce 19

Overview

§ Debriefing Exercise 8 § Briefing Exercise 9

slide-20
SLIDE 20

| | 22-Nov-19 Mihai Bâce 20

U9.A1 Game theory

§ Components of a game tree

§ Root à Beginning (state before any move) § Node à Possible state of the game § Edge à Move § Leaf à End of the game (final state)

slide-21
SLIDE 21

| | 22-Nov-19 Mihai Bâce 21

U9.A1 Minmax Algorithm

+1

  • 1

+1

  • 1
  • 1

+1

+1 ?

slide-22
SLIDE 22

| | 22-Nov-19 Mihai Bâce 22

U9.A1 Alpha-Beta Algorithm (1)

Beta-Cut: MAX will give a vealue of at least 20. It does not matter what is in the rest of the tree as the MIN player will select 10. beta 10 20 10

slide-23
SLIDE 23

| | 22-Nov-19 Mihai Bâce 24

U9.A1 Alpha-Beta Algorithm (3)

Alpha-Cut: MIN will choose 8 or less. Since on the other branch we have a 10, MAX will always choose the larger value. beta alpha 10 20 10 8 8 10

slide-24
SLIDE 24

| | 22-Nov-19 Mihai Bâce 25

U9.A2 Reversi [Part 3]

nextMove() Waits for entry from command line HumanPlayer nextMove() Chooses a random (but valid) next move RandomPlayer nextMove() Chooses the next move by means

  • f an easy and

non-recursive evaluation function GreedyPlayer nextMove() Chooses the next move by means

  • f a Minimax

analysis through a new evaluation function MinMaxPlayer

slide-25
SLIDE 25

| | 22-Nov-19 Mihai Bâce 26

U9.A2a Reversi MinMax Player

§ Evaluation of the game trees § Implement a method that evaluates the game tree through Minmax until the depth d is reached (alternating Max and Min) § Depth of research can be configured § Recursive approach § Build the game tree recursively § Evaluate the state at depth d § Minmax on the obtained evaluation yields the strategy § Incorporate all special cases (e.g. pass)!

slide-26
SLIDE 26

| | 22-Nov-19 Mihai Bâce 27

U9.A2b Reversi Timeout

§ The timeout value is an input parameter (initialize method) § Your nextMove() method should return a valid move before the time-out in milliseconds § Plan a time buffer (in the order of 10 ms) so that cancellation and delivery of result do not happen straight away! § Possible approach: throw an out-of-time exception

slide-27
SLIDE 27

| | 22-Nov-19 Mihai Bâce 28

U9.A2c Evaluation function (I) (optional)

§ Improve game state evaluation § You can find a “source of inspiration” in the following article: § „The Development of a World Class Othello Program“, Kai-Fu Lee and Sanjoy Mahajan, 1990 § To download from the Reversi website § username: i2bib § password: reversi § Artificial Intelligence: A Modern Approach § Stuart Russell and Peter Norvig (2nd Edition, 2003)

slide-28
SLIDE 28

| | 22-Nov-19 Mihai Bâce 29

U9.A2c Evaluation function (II) (optional)

§ Possible „evaluation functions“ § How many counters are flipped? § Where are the flipped counters located (center/border)? § .... § A few pieces of advice concerning the tournament § Start with writing the idea for the evaluation function in the pseudo-code § Keep developing the pseudo-code § The pseudo-code yields hints about how the information about the next move should be computed § Keep implementing the different versions of the pseudo-code for the tournament player

slide-29
SLIDE 29

| | 22-Nov-19 Mihai Bâce 30

Have Fun!

Image