15
UNO (Card Game) Austin Gray, Dat Tran, and Zachary DeForge

UNO (Card Game)people.uncw.edu/tagliarinig/Courses/380/S2019 papers and presenta… · Genetic Search Algorithm . UNO (Ca rd G a me ) 5 Genetic Search is an optimization technique

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

UNO (Card Game)

Austin Gray, Dat Tran, and Zachary DeForge

UNO (Card Game) 1

Abstract

In this study we examine multiple different algorithms aimed to aid in the card selection

process of the popular card game UNO. While most humans take a more trivial approach to

card selection this study aims to have an algorithm find the most optimal card solution route in

order to maximize win rate while also keeping the time it takes to make this selection to a

minimum. The algorithms used are a Genetic Search, Simulated Annealing Search, Play

Anything, Heuristic Search, and Random Search.

UNO (Card Game) 2

Introduction

In the game of UNO, the deck consists of 108 cards. These cards are broken up into 4

colors with each color having 19 cards (0 to 9) along with 24 action cards, and 8 wild cards.

The aim of this project is to optimize the order in which the cards are played.

Formal Problem Statement

Given a set of N cards in a hand, label each with a distinct counting number from the set

S = {0, …, N-1}. For X being the top discarded card, find a permutation P of the cards in S

that minimizes the cost, given by the function below, of the permutation with the start of the

permutation being X

Context

Uno is an American card game invented by Merle Robbins in 1971. Then, it becomes

more popular not only in the past but also nowaday because of its enjoyable manner for friends

and family. The view of point of this game is no longer normal game, it is a humankind of

society in the future by connecting human together. A paper written by Erik D. Demaine titled

“The complexity of UNO” found that single-player version of UNO is NP-complete, but in some

cases are in P, while the uncooperative two-player version is in P. This was interpreted that any

number of player versions will be in polynomial time solving.

Experimental Procedure

Heuristic Search Algorithm

UNO (Card Game) 3

Our heuristic algorithm takes each of the cards in hand and finds the best grade for the

starting card in the route only. The reason we choice to include an algorithm such as this is to

show what a typical human player would do when playing Uno.

The Big-O of this algorithm is O(n) where n is the number of cards in hand.

Play Anything Algorithm

Play Anything is an algorithm that selects the very first card that can be played. This

algorithm is the fastest among the others due to its simplicity. From the data collected, this

algorithm will always have a winning chance below the baseline. The O(1) is the complexity

time here.

Random Search Algorithm

The Random search algorithm is helpful for ill-structured global optimization problems

with consecutive and discontinuous variables. Typically, random search algorithm doesn’t

guarantee the optimality for finding a good solution quickly with convergence results in

probability.

In our program, we start this algorithm by a constant number “c” which is the needed

number of permutations based on the number of cards in hand. By running a loop, we randomly

mix the cards to get a new range of cards until a “c” number of permutations. According to that

loop, we keep comparing route by route to get the best permutation (the least cost route) and

return that best one.

The big-O, complexity time of this random search algorithm is O(c).

Simulated Annealing Search Algorithm

UNO (Card Game) 4

Annealing is a process in which metal or glass is heated and then allowed to cool at a

controlled rate. Annealing changes the properties of a metal making it more ductile and

workable. Simulated annealing uses the idea of slowly cooling metal in an optimization

algorithm an initial guess is taken. Then another solution is randomly guessed near the previous

solution. This new solution is evaluated to see if it is better than the previous solution. Initially

some bad solutions are accepted to allow the algorithm to explore. Perhaps finding its way out of

a valley towards a better solution. As time goes on, the temperature is reduced and fewer bad

solutions are accepted. Eventually, only solutions that are better than the previous one is

accepted. It's a bit like a ball bouncing on a vibrating surface. As the vibration decreases, the ball

bounces less and less. Eventually it will come to a stop hopefully at the minimum point.

In the game, we set up a temperature t, a cooling rate c, a current route equal to the

current player’s cards in hand, and a best permutation (or the least cost). While the temperature t

is still greater than 0.000001, we create two random indexes to swap two cards and make them

into a new route. Then, if the current route’s cost is greater than new route’s cost, the new route

becomes the current route. If the current route is better than the best permutation, the current

route becomes the best route. Otherwise, the temperature t will be cooled down by t*(1.0-c). The

while loop will continue to run until temperature t is no longer greater than 0.000001x10-6, or

the current player has left only 1 card.

The complexity time of this algorithm is O(n*m) where n is the number of times to check

the cards on hand before getting 1 card and m is the number of loops the while loop will run.

Genetic Search Algorithm

UNO (Card Game) 5

Genetic Search is an optimization technique used to solve nonlinear or non-differentiable

optimization problems. They use concepts from evolutionary biology to search for a global

minimum. The name genetic algorithm comes from the fact that they're mimicking evolutionary

biology and it’s techniques. Genetic algorithms work by starting with an initial generation of

candidate solutions that are tested against the objective function. Then we generate subsequent

generations of points from the first generation through things such as selection rules (select the

individuals, called parents, that contribute to the population at the next generation), crossover

rules (combine two parents to form children for the next generation), and mutation rules (apply

random changes to individual parents to form children).

In our program’s algorithm, we create a limit of populations (n) needed and a number of

generations (m). Then we create randomly n different permutations based on the current player’s

cards and add them to the route list. Therefore, by looping of n populations and m generations,

we randomly pick two permutations of Route list as parents, use mutation rules to create a next

generation, and add them to the route list. By sorting the route list based on their costs, we just

accept first n populations and pick the first permutation to be the best permutation.

Based on the code, the Big-O time complexity of Genetic Algorithm is g • (m • n +

nlog(n) + m). g = generations, m = max population, n = number of cards in hand.

Implementation

Using Java, we created a replica of Uno following a simple version of the game where

some of the more difficult rules do not apply. The data was collected on a turn by turn basis and

each match was ran 5000 times with each match set having the same seed so that variability does

not play too much of a role in the data collection process. The hardest part of optimizing Uno

UNO (Card Game) 6

was figuring out what kind of grading function will show the best results. In order to not have an

algorithm select both a card, and a color if it was needed was to make a simple rule that a card

farther down the route found by the TSP algorithms would be used as the color. If there were no

cards that had color, a random color would be chosen. If there was a valid card, but the

algorithm did not put that card as the card to play, a random valid card would be played. This

did not happen as our implementation of the grading system made sure that cards that could not

be played were weighed more which caused them to be pushed to the back of the route. Routes

in our implementation are not just a simple city to city TSP problem. In our implementation, a

route is a set of instructions that are created every turn that the algorithm creates to tell our

virtual Uno game what card should be played. The card that is chosen should always be the card

that is first in the route, while the other cards are used as a weight of what cards might be played

in the future in the given order by the algorithm. An example of this would be the algorithm

selecting to play a +4 card and changing the color to the next card that was in the route which

would be red.

Red was chosen as the color due to the rule discussed above, which allows all future cards to

have a chance to be played especially if the top discard was not red.

Evidence

UNO (Card Game) 7

Genetic Search, Random Search, and Simulated annealing always had, on average, a smaller cost

compared to the other two algorithms. With Genetic Search and Simulated Annealing doing

slightly better than Random Search, we were able to make the connection that the cost is

important in determining whether a player has a higher chance at winning or not. These

algorithms are given a slight increase in the percentage chance of winning a game. We also

found that the position that the algorithm starts at also increases or decreases the chance of

winning by a decent percentage.

For each algorithm that was used in 2 player matches, we found that the ones to win the

majority of the games were the ones that always kept a low grading cost. Looking at the winning

percentages, we can find that the player that goes first has a higher chance of winning compared

to those that go after. In the 2’s matches, Simulated Annealing does the best overall whether it

goes first or second with a 54.79% chance of winning.

2 player matches Genetic Search

Heuristic Search Play Anything

Random Search

Simulated Annealing

UNO (Card Game) 8

1st Player 54.57% 48.92% 46.28% 54.08% 54.79%

2nd Player 50.76% 46.16% 43.13% 50.19% 51.14%

Overall 54.57% 48.92% 46.28% 54.08% 54.79% In the 3’s matches, while Genetic Search had a higher chance at winning when going first,

Simulated Annealing had the best percentage chance overall at winning in any position sitting at

a 35.10% chance.

3 player matches Genetic Search

Heuristic Search Play Anything

Random Search

Simulated Annealing

1st Player 37.26% 33.71% 32.51% 37.18% 37.16%

2nd Player 34.04% 31.01% 29.19% 33.71% 34.39%

3rd player 33.54% 30.56% 28.65% 33.30% 33.72%

Overall 34.95% 31.76% 30.12% 34.73% 35.10%

In the 4’s matches, Random Search had the highest chance of winning when going first, while

Simulated Annealing dominates the other positions with an overall chance of winning at 26.42%.

4 player matches Genetic Search

Heuristic Search Play Anything

Random Search

Simulated Annealing

1st Player 28.79% 26.07% 25.14% 28.97% 28.96%

2nd Player 26.00% 23.29% 22.18% 25.79% 26.06%

3rd Player 25% 22.90% 21.58% 24.96% 25.35%

4th Player 24.93% 22.55% 21.40% 24.78% 25.30%

Overall 26.18% 23.70% 22.58% 26.13% 26.42%

UNO (Card Game) 9

We can find that while the algorithm that goes first has a higher chance of winning, There is not

just a single algorithm that can be guaranteed to win unless the amount of players in the game is

also taken into account. But, if there is no control over the position that the algorithm starts in,

Simulated Annealing is the best choice in order to have a higher chance of winning overall.

The graph above shows that as the position of the algorithm in the game increases, the chances of

winning decreases.

Looking at individual matchups, it can be found that Simulated Annealing, Genetic

Search, and Random Search all won at higher percentages when facing Play Anything and

Heuristic Search.

UNO (Card Game) 10

But when played against each other, they are close to being evenly matched. With Genetic

Search vs Random Search, Genetic Search has a 52% chance of winning, while with Random

Search vs Genetic Search, Random Search has a 52.36% chance of winning. Also for Genetic

Search vs Simulated Annealing, Genetic Search has a 51.64% chance of winning while with

Simulated Annealing vs Genetic Search, Simulated Annealing has a 52.38% chance of winning.

Finally with Random Search vs Simulated Annealing, Random Search has a 52.36% chance of

winning while with Simulated Annealing vs Random Search, Simulated Annealing has a 52.9%

chance of winning. These values are all extremely similar with each other. Those slight

percentage differences give a small, but very noticeable effect on the overall chance of winning

for their respective algorithms.

UNO (Card Game) 11

When looking at the time complexity for each algorithm. We find that Genetic Search

took the longest amount of time to run. With 75% of the total amount of time was spent on

Genetic Search alone. Simulated Annealing was the second slowest, but way faster than Genetic

Search.

Random Search, Play Anything, and Heuristic Search were all simple algorithms that did not

take up too much time, with Play Anything being the fastest due to it having a Big(O) of O(1).

With this, we can find that Genetic Search is highly inefficient in terms of time, but is able to

find lower cost routes, and have a slightly higher percent chance of winning than Random

Search. Due to time not really being an issue, We can find that Genetic Search is better in terms

of winning when compared to Random Search. But if time was a factor, Random Search would

be the better choice, if choosing between the two.

UNO (Card Game) 12

Holes in Work

The biggest hole in our work that needs to be discussed is the use of a human made

grading system. While this grading system is shown to have an impact on how well a game

plays, we cannot deny that it may have affected the outcomes of the win percentages by a fair

margin. We found that the win percentage can be correlated to an overall lower cost of routes,

but we cannot fully assume that it was the cause. In “Future Work”, we mention about having a

neural network may fix this issue and if we ever have a similar problem like this in the future, we

may try to implement one in order to fix the possible biases.

Interpretation/Conclusions

We can conclude that the Simulated Annealing algorithm wins on average more games when

compared to the other algorithms. The Random Search algorithm wins slightly less games on

average, but has a faster time complexity when compared to the Genetic Search algorithm. The

Genetic Search algorithm wins more games on average than the Random Search algorithm but

has a way longer time complexity. Heuristic Search stays below the baseline for win percentages

for all games, but has a better chance of winning if matched off with the Play Anything

algorithm. Finally, the Play Anything algorithm is the worst at winning, but has the fastest time

complexity.

Overall, we can conclude that the algorithm that goes first has a better chance of winning

compared to other algorithms that go after it. We can also conclude that when playing Uno, it is

important to have some sort of grading system in place for the cards in hand, as that will play a

role at increasing the chances of winning even if the increase is very slight.

UNO (Card Game) 13

Future Work

Our future work would include adding in all different rule versions to the game instead of

the single version we have currently. Also we would use a neural network or a different

algorithm for the grading system instead of a human hard coded one. Also we would look to

create an Uno game in which humans can play against our algorithms.

UNO (Card Game) 14

Sources

Demaine, Erik D., et al. “The Complexity of UNO.” ArXiv.org, 2 Dec. 2013,

arxiv.org/abs/1003.2851.

“Simulated Annealing.” Wikipedia, Wikimedia Foundation, 30 Apr. 2019,

https://www.mathworks.com/help/gads/what-is-simulated-annealing.html.

“Travelling Salesman Problem.” Wikipedia, Wikimedia Foundation, 25 Apr. 2019,

https://en.wikipedia.org/wiki/Travelling_salesman_problem.

“Uno (Card Game).” Wikipedia, Wikimedia Foundation, 15 Apr. 2019,

https://en.wikipedia.org/wiki/Uno_(card_game).

“What Is Simulated Annealing?” MATLAB & Simulink,

www.mathworks.com/help/gads/what-is-simulated-annealing.html.