minimax in the darkai/projects/old/minimax... · 2018-11-20 · in this project we will explore the...

14
Minimax In The Darkness A.I Final Project Assaf Yoel

Upload: others

Post on 25-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Minimax In The Darkness

A.I Final Project

Assaf

Yoel

Page 2: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Overview

In this project we will explore the Mini-max algorithm under a special condition :

Mini-max algorithm with one of the players is a partial viewer. In our case, the "MIN

player" randomly loses visibility of parts of the world , hence 'Minimax in the dark' .

In order to explore the algorithm uder these conditions, we created a simple two

player game and a GUI environment to demonstrate it.

The Minimax Algorithm – short overview

Minimax is used in zero-sum deterministic games to denote minimizing the

opponent's maximum payoff, assuming the opponent is a rational player and has full

visibility.

In order to explore the Minimax algorithm visibility factor influence, we decided to

take a “real world” problem – Cartman tries to chase down Kenny who tries to run

away.

Cartman, as our Min player will have a partial visibility, which means he will

randomly see only percentage of the grid in each turn.

Each player assumes the other plays optimally , therefore in the example above MAX

will choose A1 since A2 and A3 will lead MIN to choose moves A21 or A33 (that lead

to position value of 2) and A1 will yield a value of 3 . The tree's height equals to the

recursion depth in our algorithm.

Page 3: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Minimax attributes:

Optimal (if the other player plays optimally)

Complete – if the tree is finite.

Run time: O(bm) (b = legal moves, m = maximum depth of tree)

Minimax assumptions:

Full visibility (each player can see what his opponent did the last turn).

Each player assumes his opponent plays in an optimal way

Our game: Minimax In The Darkness

Game flow:

Two players (Cartman and Kenny) are randomly placed on a board . Kenny's mission

is to escape (reach the first row) without being caught by Cartman . Cartman's

mission is to catch Kenny before he reaches the first row.

In order to explore the visibility factor, Cartman's visibility is randomly limited to

some percentage of the board each turn (hence Minimax in the dark) .Kenny, on the

other hand enjoying full visibility and completely aware of Cartman position.

Kenny leaves 'footprints' wherever he steps that remain for up to 4 game turns, and

the footprints receive values between 1 to 4 (4 is the most recent footprint and 1 the

least). Using these footprints, Cartman could guess kenny's position whenever Kenny

is not visible to him.

The main difference in our version of Minimax is that one player (Cartman, which

has a partial visibility) will sometimes make non optimal moves. This will affect both players since the second player will assume the partial viewer is optimal.

Our project will examine Cartman's performance under various visibilities and

allowed calculation steps (recursion depth).

Page 4: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

The Evaluation functions

We scored each state of board according to the players positions as follows:

Space gManeuverin

),(tan

3

1

Space gManeuverin

),(tan

7

1)(

FirstRowKennyceDisKennyCartmanceDisboardEVAL

The evaluation's function idea is very simple. Kenny, the max player, wants to

increase the distance between himself and Cartman (otherwise he loses) and this is

more urgent than reaching the goal (hence the weights 0.7 and 0.3). So Kenny will

prefer positive values. Cartman, who wants the exact opposite, will prefer negative

values.

The maneuvering space is the actual squares both players can step. So both equation

fractions values are between 0-1 and the whole EVAL function can get values

between -1 to 1.

We implemented 2 versions of the distance between Cartman and Kenny (thus we

have two evaluation functions, one for each distance). The first uses a relaxation –

the distance is simply the Manhattan distance and doesn't take into account any

blocks in the path.

The second is the exact (shortest) distance considering the blocks on the road,

implemented by searching for possible paths using BFS and returning the shortest

considering blocks. In big recursion depth numbers with real distance the run time

becomes very long therefore the Manhattan evaluation will be used in such cases.

Page 5: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

The Algorithm

Kenny Moves: (max player)

Kenny chooses the move which minimizing the maximum payoff of Cartman using

the Minimax algorithm while considering Cartman as a rational opponent. The

ground is really dirty so without knowing he is leaving traces of his four last moves.

Cartman Moves: (min player)

In each turn, percentage of the board is invisible for cartman.

Two scenarios:

Kenny is located on the visible part of the board Cartman chooses the move which minimizing the maximum payoff of Kenny using

the Minimax algorithm.

Kenny is located on the invisible part of the board Cartman

will scan for the freshest trace of Kenny, if none of them is visible he will consider the

last place he saw Kenny as the freshest trace. Now, Cartman will calculate all the

places Kenny might progress from this trace and the possibility of that.

Page 6: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

For each Kenny's possible position, Cartman will have calculate the move which

minimizing the maximum payoff of Kenny using the Minimax algorithm, and multiply

the move payoff by the possibility of Kenny being in that position.

In the end of the process, Cartman will choose the move with the highest

expectancy.

Exponential growing algorithm

Because the algorithm has an exponential growth which leads to heavy run-time, we

decided to let the algorithm work until some depth in the recursion (1-5). That's

mean the players will not always know the exact outcome of their move, and only

the outcome of the board which we got from highest recursion depth.

Page 7: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Cartman Move function (pseudo code):

Vector Cartman_moves = list of all possible moves of Cartman

If (Kenny visible)

Update Kenny's last seen position

For each cMove in Carman_moves

Calculate Minimax(cMove) ;

Choose minimal cMove ;

else

latest_trace= findKennyTraceFunc()

Vector KennyPoss_moves= list of all the possible positions

Kenny might be and their probabilities.

For each cMove in Carman_moves

totalcMoveVal=0;

For each kMove KennyPoss_moves

moveVal=Calculate Minimax(cMove,kMove);

totalcMoveVal+=moveVal*kMove_possibily;

Choose minimal cMove which have minimal totalcMoveVal

Page 8: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Minimax function (pseudo code):

minimax(BOARD,TURN,RECURSION_DEPTH){

if kenny reached first row

return 1

if Cartman reaches Kenny

return -1

if RECURSION_DEPTH==0

retrun the board score using the EVAL function

if TURN=KENNY

Vector Kenny_moves= list of all the possible moves

Kenny might take.

For each kMove in Kenny_moves

newBoard= make new Board with kMove

moveVal=minmax(newBoard,CARTMAN,RECURSION-1)

return minimal moveVal.

if TURN=Cartman

Vector Cartman_moves= list of all the possible moves

Cartman might take.

For each cMove in Cartman_moves

newBoard= make new Board with cMove

moveVal=minmax(newBoard,Kenny,RECURSION-1)

return maximal moveVal.

}

Page 9: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Shots From The Game

Screen visibility

selection tab

Recursion Depth

selection tab

Picture from the game: Cartman

(center) will try to prevent

Kenny (upper left) from

reaching the first row. The

squares with "Satan" are

'blocks' – squares that are

impossible to step on

Page 10: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Results

We wanted to examine our algorithm behavior under two parameters:

Cartman's visibility

Recursion's depth

In order to do so, we let Cartman and Kenny play against each other 100 games with

different visibilities and depths and repeat this process for both evaluation functions.

Note that Cartman mission is much easier, the only thing he needs to do is get to

Kenny. Kenny however needs to get to the top of the screen without being caught.

So we were not expected to both players to get equals as the visibility rises.

Partial visibility was critical

We could not test both players behavior under full visibility because they have reach

very fast to aquarium point, which both stayed in their positions. only when the

visibility factor took place the game could be tested.

Game Statistics

First evaluation function (relexation)

Visibility (%)

Recursion Depth

Cartman Wins Kenny Wins

0.125 1 33 67

0.25 1 42 58 0.5 1 62 38

0.75 1 70 30

0.125 2 35 65

0.25 2 45 55 0.5 2 60 40

0.75 2 73 27

0.125 3 38 62

0.25 3 54 46 0.5 3 70 30

0.75 3 65 35

0.125 4 43 57 0.25 4 45 55

0.5 4 43 57

0.75 4 46 54

Page 11: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Second evaluation function (exact calculation)

Visibility (%) Recursion Depth

Cartman Wins Kenny Wins

0.125 1 25 75

0.25 1 31 69

0.5 1 41 59

0.75 1 52 48

0.125 2 22 78

0.25 2 32 68 0.5 2 38 62

0.75 2 60 40

0.125 3 24 76

0.25 3 31 69

0.5 3 51 49

0.75 3 65 35

0.125 4 18 82 0.25 4 27 73

0.5 4 40 60

0.75 4 58 42

Graphs

Evaluation function 1 (relaxation)

cartman's performance as a function of visibility

0

10

20

30

40

50

60

70

80

0.125 0.225 0.325 0.425 0.525 0.625 0.725 0.825

visibility

cart

man

perf

orm

an

ce

depth 1

depth 2

depth 3

depth 4

Page 12: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Cartman's performance as a function of recursion depth

0

10

20

30

40

50

60

70

80

1 2 3 4

recursion depth

Cart

man

's p

erf

orm

an

ce

visibility 12.5%

visibility 25%

visibility 50%

visibility 75%

Evaluation function 2 (accurate distance)

cartman's performance as a function of visibility

0

10

20

30

40

50

60

70

0.125 0.225 0.325 0.425 0.525 0.625 0.725 0.825

visibility

cart

man

perf

orm

an

ce

depth 1

depth 2

depth 3

depth 4

Page 13: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Cartman's performance as a function of recursion depth

0

10

20

30

40

50

60

70

1 2 3 4

recursion depth

Cart

man

's p

erf

orm

an

ce

visibility 12.5%

visibility 25%

visibility 50%

visibility 75%

Discussion

There is a slight differences between the results we have got using the relaxation

evolution function over the accurate evaluation function. The results on both

function indicates an improvement of Cartman's performance as results of an

increase of the visibility and of the recursion depth, with more tendentious results

using the accurate evaluation function.

Function 1 (relaxation)

Visibility factor The results show's a different size of growth in Cartman's performance in all

recursion depth beside recursion depth 4 which is almost unaffected. Furthermore,

we can se that the growth in the improvement tends to grow lower or even become

smaller (as in case of depth 3).

We didn't expected these result but it can be settled if we take into consideration

the fact that we used inaccurate evolution function that might of caused this

behavior.

Page 14: Minimax In The Darkai/projects/old/Minimax... · 2018-11-20 · In this project we will explore the Mini-max algorithm under a special condition : Mini-max algorithm with one of the

Recursion Depth factors The results shows the performance tends to converge to some value between 42 to

48. This evaluation function does not considering unreachable places or blocked

squares. So the players move only affected by the players' positions and not by the

board terrain. As a result of this, as much as the recursion depth is getting deeper

the general direction of each player is getting clearer and the players take the same

tactic in each play.

Function 2 (accurate evaluation)

Visibility factor The results show's almost the same linear growth in all recursion depths. It is clear

that Cartman visibility changes the game statistics to Cartman's benefit. We are

expecting Cartman improvement will continue to raise nearly to 95% performance.

notice that in 100% visibility the game can't take place, as we mentioned before.

Recursion Depth factor The

results shows that the recursion depth in this case is not a influence factor of

Cartman's performance. We think that using the accurate function there isn't much

need in going much deeper in the recursion because there is slight recursion hidden

in the evaluation function that uses BFS to calculate the distances. So the results we

get from the low depth recursions are sufficiently accurate.

Conclusions

The visibility factor has decisive influence of the Mini-max algorithm, even

when little percentage of it is taken.

Recursion Depth factor can influence the balance between the players when

the evaluation function is not accurate enough and more calculations are

needed.