Solving Minesweeper Using CSP

Similar documents
Problem A: Collatz Conjecture

CPSC 217 Assignment 3

COMP 110 Programming Exercise: Simulation of the Game of Craps

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

The Dynamic Typing Interlude

MIT Programming Contest Team Contest 1 Problems 2008

Analysis of Algorithms. 5-Dec-16

Millionaire. Input. Output. Problem limit seconds

Games and Adversarial Search II Alpha-Beta Pruning (AIMA 5.3)

Project: Minesweeper Online EDA095

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Analysis of algorithms

Using Microsoft Excel

Divisibility Rules and Their Explanations

Repetition Through Recursion

Ideally your algorithms for both parts should run in linear time. You will receive partial credit for a polynomial-time algorithm.

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

CS61B Lecture #35. [The Lecture #32 notes covered lectures #33 and #34.] Today: Enumerated types, backtracking searches, game trees.

Your First Windows Form

Constraint Satisfaction Problems

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

10/11/2017. Constraint Satisfaction Problems II. Review: CSP Representations. Heuristic 1: Most constrained variable

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

CMSC 201 Spring 2018 Project 3 Minesweeper

Pacific Northwest Region Programming Contest Division 2

UNM - PNM STATEWIDE MATHEMATICS CONTEST XLI. February 7, 2009 Second Round Three Hours

Graph theory. Po-Shen Loh. June We begin by collecting some basic facts which can be proved via bare-hands techniques.

Optimization and least squares. Prof. Noah Snavely CS1114

What Every Programmer Should Know About Floating-Point Arithmetic

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Error Analysis, Statistics and Graphing

Sorting and Selection

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

XVIII Open Cup named after E.V. Pankratiev Stage 1: Grand Prix of Romania, Sunday, September 17, 2017

Function Call Stack and Activation Records

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

EASY

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Lecture 16. Reading: Weiss Ch. 5 CSE 100, UCSD: LEC 16. Page 1 of 40

static String usersname; public static int numberofplayers; private static double velocity, time;

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Constraint Satisfaction Problems

Week 8: Constraint Satisfaction Problems

ASSIGNMENT 2. COMP-202A, Fall 2013, All Sections. Due: October 20 th, 2013 (23:59)

Navigating and Managing Files and Folders in Windows XP

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

Section Marks Pre-Midterm / 32. Logic / 29. Total / 100

UNC Charlotte 2010 Comprehensive

Balanced Search Trees

Introduction to Domain Testing

SPRITES Moving Two At the Same Using Game State

Propositional logic (Ch. 7)

Constraint Satisfaction. AI Slides (5e) c Lin

These are notes for the third lecture; if statements and loops.

CPSC 320: Intermediate Algorithm Design and Analysis. Tutorial: Week 3

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

CS2500 Exam 2 Fall 2011

Assignment #4 Minesweeper

Excel Basics: Working with Spreadsheets

Welfare Navigation Using Genetic Algorithm

Module 4. Constraint satisfaction problems. Version 2 CSE IIT, Kharagpur


Clustering. (Part 2)

AP Computer Science Unit 3. Programs

Slide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Algorithms and Data Structures

Outline. Best-first search

15-780: Graduate AI Homework Assignment #2 Solutions

Simple Graph. General Graph

Outline. Best-first search

Copyright 2000, Kevin Wayne 1

Background. $VENDOR wasn t sure either, but they were pretty sure it wasn t their code.

Constraint Satisfaction Problems. Chapter 6

Midterm I Exam Principles of Imperative Computation André Platzer Ananda Gunawardena. February 23, 2012

(Refer Slide Time: 01.26)

15-451/651: Design & Analysis of Algorithms November 4, 2015 Lecture #18 last changed: November 22, 2015

Maximum Density Still Life

Constraint Satisfaction

Week 12: Running Time and Performance

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

COMP : Practical 9 ActionScript: Text and Input

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Constraint Satisfaction Problems: A Deeper Look

Outline. Informed Search. Recall: Uninformed Search. An Idea. Heuristics Informed search techniques More on heuristics Iterative improvement

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Section 4.1: Introduction to Trigonometry

Algorithm Analysis. Big Oh

1.00 Lecture 32. Hashing. Reading for next time: Big Java Motivation

Excel Spreadsheets and Graphs

/ Approximation Algorithms Lecturer: Michael Dinitz Topic: Linear Programming Date: 2/24/15 Scribe: Runze Tang

ASYMPTOTIC COMPLEXITY

Correlation. January 12, 2019

Type Checking and Type Equality

CSE 326: Data Structures Splay Trees. James Fogarty Autumn 2007 Lecture 10

CS 231: Algorithmic Problem Solving

CS 3 Midterm 1 Review

CMPSCI611: Approximating SET-COVER Lecture 21

18-642: Code Style for Compilers

Transcription:

Solving Minesweeper Using CSP AI Course Final Project Gil & Chai

Usage (After using Makefile) java player/aiplayer <height> <width> <#mines> <method> <flag: trivial> <flag: subtract> <flag: sectors> <flag: gui> <flag: sleep> <optional: trivial-sleep> <optional: backtracking-sleep> <optional: guesssleep> <height> and <width> are the dimensions of the board (must be positive). <#mines> is the number of mines on board (between 0 and height*width). <method> is the method for selecting the variable in the backtracking algorithm (see explanation in the backtracking algorithm part). 1=random, 2=least-constrained, 3=most-constrained, 4=smallest-constraint, 5=biggest-constraint. the trivial, subtract and sectors flags are optional algorithms that should increase the efficiency of the program (explanations will follow). enter 1 for 'true' or 0 for 'false'. <flag: gui>: enter 1 ('true') unless you want to measure performance time, etc. <flag: sleep>: enter 1 to enter some sleep values, in order to see the progress of the game.

the rest are the sleep values. Recommended values: 60 60 600 (when making a guess, the program will hold for 600 milliseconds). Output: output to stdout: <flag: won> <time> <trivialcounter> <sutractcounter> <backtrackcounter> <guesscounter> <crapshotcounter> <flag:won> is 1 if the game has been won, and 0 otherwise. <time> is the running time of the program, in seconds. the next values are counters: how many times in this game we have used the trivial algorithm, the subtracting operation, the backtracking algorithm, how many times we've made an educated guess and how many times we have made a crap shot (explanations will follow), respectively. The Minesweeper Game Minesweeper is a simple computer game. It has many graphical versions and was integrated in some popular operation systems. The game is played on a square-based board. Squares' content is hidden. Each square can either contain a mine or not. The goal: opening all squares that doesn t contain a mine. When opening a hidden square (left mouse click): if the square contains a mine, the game is lost. If the square doesn t contain a mine, a number appears on the square- the number of mines in the

squares around it. When it is known that a square contains a mine, it is marked with a flag (right mouse click). A short demonstration on how the game is played suppose we are in the next game state: In this state, there are four 1-squares that has only one hidden square near them. Therefore these squares must be mines (or there will be 1-squares with no mines around them). After we've flagged those 1-squares, we are in this game state. Now, we have five 1-squares near flagged-squares (mines). This means that all other squares near those 1-squares (beside the mines) must be non-mine squares

(or the 1-squares would have at least 2 mines around them). After we've opened those squares, we are in this state. notice we've pressed on an "air pocket" - a 0-square. In this case all squares around it are safe and are being opened automatically. now, there are four 2-squares that obligate 4 squares to be mines. We keep using the same considerations and progress in the game.

And the game is won. Notice that this was the "beginner level" (of the

Microsoft Windows version), that only rarely requires making hard-guesses. Other configurations might give the best player in the world less than 50% chance of success (for example, Microsoft Windows "expert level"). Constraint Satisfaction Problem (CSP) A special search problem. Defined by a set of variables V, their domains D, and a set of constraints on the variables values C. A state is defined by an assignment of values to some or all variables in V, each value from the according domain. A goal state is an assignment of values to all variables in V, which satisfies all constraints in C. Solving Minesweeper Using CSP In order to solve Minesweeper, it is impossible to use regular CSP. The reason is that regular CSP finds one complete and satisfying assignment, while there are more than one assignment possible (i.e., there are multiple goal states). However, only one of these goal states matches the true (and hidden) mine locations, while the other ones will cause a game loss. Another important point is the fact that Minesweeper is dynamic: When a square is opened, assuming it's not a mine a number appears. This number is a new information about the mine locations, and therefore a new constraint. From those reasons we needed a modified, new type of CSP. In our CSP, each game-state is considered one CSP (Thus, we would probably need to solve many CSPs in order to solve a single Minesweeper game. Moreover, we will soon see that we might have several CSPs per one game state). The variables are all hidden squares we have information about, which are all hidden squares adjacent to an open square. Each variable can be assigned 1 (mine) or 0 (safe square). The constraints are originated in the open squares, one constraint for each open square. for an open square with number n on it (0<=n<=8), the constraint would be: [<sum of all unassigned variables around this square> = n]. Comments: 1. Unassigned variables are all hidden and unflagged squares adjacent to an

open square. 2. We would use the next form to describe a constraint: [v 1 + v 2 +... + v k = n] for 0 <= n,k <= 8. Example: In this example we have five hidden square which are adjacent to an open square, marked with letters a to e. The CSP for this state would be: V = {a,b,c,d,e} D = {{0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1}} C = {(a+b=2),(a+b+c+d+e=3),(d+e=1),(e=1)} Modeling The Constraint Class: The constraint object consists of an origin square (Point origin), a list of variables around it (maximal number of variables: 8, Each variable is represented simply by a Point object indicating it s location) and the desired sum of those variables. Main data structure: a hash map with key Point (origin square) and value Constraint. The variables aren t saved in any other data structure, and can be extracted using our hash map. The Algorithm "The Layer Model": Each layer describes an algorithm or some operation, and we try those operations in the given order. Whenever an operation fails, we try the next operation. Whenever an operation succeeds, we try the operations all over again from

the beginning. Notice we would never use more than one layer on one CSP. Once we've assigned a value to at least one integer, we try the first layer again for the new CSP we obtained. Finding Trivial Constraints: When a constraint has n variables and it s sum is either 0 or n, it has only one possible assignment and is considered a trivial constraint. In this layer we find all trivial constraints, and open/flag each constraint s variables according to the constraint s sum. Example: In the figure above, (a+b=2) and (e=1) are trivial constraints. Subtracting Constraints: We say that constraint c contains constraint c if each variable in c is also in c. In this layer, we find all pairs of constraints (c,c ) such that c contains c, and subtract the variables in c from the variables in c. Consider the following situation: c has 7 variables and sum 4, c has 5 variables and sum 2. If c contains c, after subtracting c from c, c becomes a trivial constraint! Example: In the figure above, subtracting (a+b+c+d+e=3) from (a+b=2) turns the first constraint into (c+d+e=1). Subtracting (e=1) from (d+e=1) turns the first constraint into (d=1) - a trivial constraint!

Backtracking: In this layer we get to the main algorithm we use for solving our CSP. In the backtracking algorithm we find all of the safe moves (unlike the previous layers, where we find only some of them). If we fail here, we re going to have to make a guess in order to progress in the game. The backtracking we use is different than the one we ve seen in class, because we want to find all possible assignments (and not only one). We use a function to assign the variables in a certain order (more about this function soon). The algorithm for finding all possible assignments: assign 0 to the first variable and recursively find all possible assignments for the rest of the variables, assign 1 to the first variable and do the same thing, return the union of the two groups (of assignments) you ve found. For each variable that has the same value in all assignments, assign this value to it. Order of Selecting Variables: Well, in which order should we select the variables? Does it even matter? Unlike classical CSP, we go over all of the solutions. What difference does it make? In the course we ve learned about the least constrained first order, which means: select the variable that has the least constraints on it first. Will it be useful in our CSP? In the time-measures part of this presentation we will see that selecting the least constrained variable first is even worse than selecting a random variable. However, selecting the most constrained variable first gives excellent results, and makes a huge different in the program s running time. When we assign value to the least constrained variable first, we keep most options available. This is good for classical CSP, because we want to find one assignment, and this way we increase the chances for finding it. But in our CSP we go over all of the possible assignments. If an assignment to a variable violates some constraint, it is not a valid assignment and we want to know about this as soon as possible. When we assign value to the most constrained variable first, we increase the chances of finding this violation earlier. Another order of assigning the variables is biggest constraint first : selecting a random variable from the constraint that has (currently) the biggest number of variables. smallest constraint first is defined in a similar way. Surprisingly (in our opinion), both "smallest constraint possible" and "biggest constraint possible" gives good results that are similar to the "most constrained" results - much better than random selection.

Out expectations were as follows: "Smallest constraint" will give good results while "Biggest constraint" will give bad ones. The reason: When you select a variable using the "smallest constraint" method, you select it from the smallest constraint - which will definitely be the smallest constraint on the next round (its size decreased by one!). Using this method we try to satisfy constraint by constraint, which will help killing bad assignments while keeping the constraints satisfied. We've expected "Biggest constraint" to give bad results from the same reason - it will never assign a whole constraint; it will always assign to the biggest constraint possible, and will completely assign a constraint only when they all have size 1 - and therefore the biggest constraint too. Why "biggest constraint" gives good results remains a mystery to us. Guessing If we couldn t find any trivial constraints, couldn t find any subtracting possible and couldn t find any safe assignment using backtracking, we have no choice but to take a risk, i.e., make a guess. But we don t have to make a blind guess, we can use the fact that we ve already found all possible assignments in the backtracking layer: In each possible assignment, the variable v gets either one or zero. If we look at the average of the values assigned to it in each assignment, we get the probability that v contains a mine. We find the variable v with the minimal average (denoted as a(v)), and we find the variable v with the maximal a(v ). If a(v) is smaller than 1-a(v ), we assign 0 to v. Else, we assign 1 to v. In other words: we look for the average that is the farthest from ½. By now we have considered only part of the squares on the board. What about all of the squares that aren t adjacent to any open square, and therefore aren t variables yet? We know how many hidden mines are left, but some of them are in the variable-squares and the rest are in the non-variable squares. We know all possible solutions for the variable-squares, so we have a possible range on the number of mines in it. Therefore, we have a possible range on the number of mines in the nonvariable squares. We know the initial number of squares, how many squares are open or flagged, and how many squares are currently variables so we can calculate how many hidden non-variable squares we have left. (#mines / #squares) in the non-variable squares will give as the probability that a random nonvariable square will contain a mine. Because we only have a range for #mines, we will get a range of probabilities.

We will take the worst-case probability, i.e., the probability from the range that is the closest to ½. If the probability is farther from ½ than all the probabilities for the variablesquares, we don t assign value to any variable, and we only open/flag a random non-variable square if the probability was smaller/bigger than ½, respectively. (if we open, we get a new constraint). "Crap Shot": When the best guess we could make have exactly probability of ½ to be a mine (and also to be safe), we have to make a 50-50 guess. This kind of guess is somewhat frequent, so we will use a special term ("crap shot") for it, and give it a special place in the analysis part. After making an assignment of one variable, we make no more guesses, and try all the layers from the beginning (recall: we now have a new CSP!). Measures and Analysis Games Distribution: In this part we have ran a few thousands of games on the three Microsoft- Windows configurations (beginner, intermediate and expert), using some bash scripts. The blue colors are wins, the red colors are losses. "First guess only" is when we win by making a guess only on the first move - which is a necessary guess in every game. "Crap shot" was explained in the "Guessing" part of the algorithm, and "educated guess only" is when winning by making at least one additional guess to the first (and necessary) guess but without any crap shots at all. "loss on first hit" is when losing on the first and necessary guess. Notice: in a perfect measure, "loss on first hit" should be exactly #mines/#squares. The distribution is as follows: Beginner: (9x9 board, 10 mines)

Intermediate: (16x16 board, 40 mines)

Expert: (16x30 board, 99 mines)

Order of Selecting Variables on Backtracking: Here are the results for a comparison between the 5 methods for selecting the variable in the backtracking algorithm. A deep analysis for those results appears in the backtracking algorithm itself. We have ran the game for a fixed mine density of 15%, and checked the performance of each method as the size of the board increases (we ran the game on boards with height=width). We will first show the results in a linear scale, and then in a logarithmic scale on the y axis. Notice: It was difficult to get a large number of results, because we were measuring time, and as a result - we could only compare games that have

been won. Some methods made the game run very slow, and we could get only a few results for each board size. This might be the explanation for the big hill around the 60 in the "leased constraint method". Linear Scale: We can see in this scale that the "least constrained" method is (by far) the worst method. We can also see that random selection can be unstable, and sometimes as bad as the least-constrained. Logarithmic Scale:

As we can see clearly in this scale, all of the good methods - "most constrained", "biggest constraint", "smallest constraint" are very similar in their performance. Moreover, there is no preferable method for a specific board size - they all give similar results in all board sizes. Success Rate: We have tested the success rate of our program as a function of the mine density, and as a function of the board size. Success Rate as a Function of the Mine density: We have tested this on four different board sizes.

The results are interesting: As expected, the success rate is a direct result of the mine density. However, the smaller the board is - the larger is your chance is to "get away" from a large mine density. Our explanation to the phenomenon is that when we have a board of size 2x and a board of size x (the 2x board has 4 times the number of squares in the x board), the mine density in the 4 sub-boards of the 2x board will probably not be even. The sub-board with the highest mine density will more likely to cause a loss. Success Rate as a Function of the Board Size: We have tested this on three different mine densities.

Our conclusion from this results is similar to the previous ones: The higher the mine density is, the more important the board size is. Again, large board means lower success rate. Final Conclusions: CSP-Minesweeper Regarding Conclusions: An adjusted CSP was required to solve the minesweeper problem: A new CSP for each game state, finding all solutions instead of one solution. The method for selecting the variable in the backtracking algorithm is very important: a ~1000 factor in the running time. Minesweeper Regarding Conclusions: Making guesses is sometimes inevitable. We try to make the guesses as educated as possible.

The chance of winning depends mostly on the mine density, but also on the board size. Minesweeper is fun!