Showing posts with label Algorithms. Show all posts
Showing posts with label Algorithms. Show all posts

Wednesday, 26 March 2014

An Artificial Intelligence for the 2048 game

Lately the 2048 game reached a good notoriety on the internet.


A discussion about possible algorithms which solve the 2048 game arose on StackOverflow.

The main discussed algorithms are:

The solution I propose is very simple and easy to implement. Although, it has reached the score of 131040. Several benchmarks of the algorithm performances are presented.


Algorithm

Heuristic scoring algorithm

The assumption on which my algorithm is based is rather simple: if you want to achieve higher score, the board must be kept as tidy as possible. In particular, the optimal setup is given by a linear and monotonic decreasing order of the tile values.

This intuition will give you also the upper bound for a tile value: $2^{n} \rightarrow 2^{16} = 65536$ where $n$ is the number of tile on the board. (There's a possibility to reach the 131072 tile if the 4-tile is randomly generated instead of the 2-tile when needed)

Two possible ways of organizing the board are shown in the following images



To enforce the ordination of the tiles in a monotonic decreasing order, the score si computed as the sum of the linearized values on the board multiplied by the values of a geometric sequence with common ratio $r<1$ .

$p_n \in Path_{0 \cdots N-1}$

$score = \sum_{n=0}^{N-1} value(p_n) * r^n$

Several linear path could be evaluated at once, the final score will be the maximum score of any path.

Decision rule

The decision rule implemented is not quite smart, the code in Python is presented here:

An implementation of the minmax or the Expectiminimax will surely improve the algorithm. Obviously a more
sophisticated decision rule will slow down the algorithm and it will require some time to be implemented.I will try a minimax implementation in the near future. (stay tuned)

Benchmark


  • T1 - 121 tests - 8 different paths - $r = 0.125$ 
  • T2 - 122 tests - 8-different paths - $r = 0.25$ 
  • T3 - 132 tests - 8-different paths - $r = 0.5$
  • T4 - 211 tests - 2-different paths - $r = 0.125$
  • T5 - 274 tests - 2-different paths - $r = 0.25$
  • T6 - 211 tests - 2-different paths - $r = 0.5$



In case of T2, four tests in ten generate the 4096 tile with an average score of $\sim 42000$

Code

The code can be found on GiHub at the following link: https://github.com/Nicola17/term2048-AI
It is based on term2048 and it's written in Python. I will implement a more efficient version in C++ as soon as possible.

StackOverflow

You can upvote my answer on StackOverflow here :)

Monday, 20 January 2014

Single nm linewidth measurement

My girlfriend Marijke Scotuzzi is a PhD candidate at the Delft university of technology.

Her main goal is to deposit small structures in the sub-10nm range.

She deposited some lines next to each other and she needs to calculate the width of those lines: the figure below shows an example of those lines. You may noticethat it is affected by an heavy high frequency noise.
Furthermore the lines can hardly be distinguished from the back spots which lies on the surface around them.

I've tried to help her out by writing a C++ program to enhance the original image.
Luckily I can make some assumpions on the input image: the lines are always vertical an they cross the top and the bottom limit edge of the image.

I've developed the following algorithm:
  1. Apply a median blur to the image (optional) \[Image[j][i] = medianBlur(Image[j][i])\]
  2. Compute a per-column weight \[W[i] = \sum_{j=0}^J MedianImage[j][i]^2\]
  3. Apply a bilateral filter on vector W (optional) \[W[i] = bilateralFilter(W[i])\]
  4. Compute the enhanced image \[EnhancedImage[j][i] = Image[j][i]*\min((satO+W[i])*satM,255) \]

Here below some results are shown for a couple of images:

Input

Output

Median blur
Weights
Bilateral weights





Wednesday, 13 November 2013

Algorithms - Part II

I'm attending the online course "Algorithms - Part II" on Coursera which is held by Kevin Wayne and Robert Sedgewick from Princeton University. 


I already have a good knowledge of most of the Algorithms which are dealt in this course. Despite that, the course is very interesting and funny because of the weekly assignments: they consist of the application of the algorithms presented in lectures applied to real and sharp problems. 

In the second week the Ariel Shamir's Seam Carving algorithm is proposed as an application of a shotest path algorithm.


According to Coursera Honor Code I will not post any code of my algorithms, but I will show you some results instead. In fact I find this algorithm very interesting and smart! :)

PS: I already read about Ariel Shamir and expecially one of his amazing work. Have a look at this!