Loading
Feedback

Tile-Slider

Baseline for Tile Slider Challenge

A getting started code for the TILE SLIDER challenge

By ninja_28


Getting Started Code for Tile-Slider Challenge

Authors :

๐Ÿ‘พKanish Anand

๐Ÿš€Animesh Sinha

Download Necessary Packages 📚

In [1]:
! pip install git+https://gitlab.aicrowd.com/yoogottamk/aicrowd-cli.git >/dev/null
%load_ext aicrowd.magic
  Running command git clone -q https://gitlab.aicrowd.com/yoogottamk/aicrowd-cli.git /tmp/pip-req-build-ark7ofec

Download data

The first step is to download out data. We will be training a classifier on the data and make predictions on data. We submit our predictions

In [2]:
API_KEY = "" #Please enter your API Key [https://www.aicrowd.com/participants/me]
%aicrowd login --api-key $API_KEY
API Key valid
Saved API Key successfully!
In [3]:
%aicrowd dataset download -c tile-slider -j 3
%aicrowd dataset download --challenge tile-slider -j 3


In [4]:
! unzip -q dataset.zip

Import packages

In [5]:
import os
import random
import tqdm
import pandas as pd
import numpy as np

Load Data

In [6]:
DATA_PATH = "dataset/" #path where data is stored

Visualize the data 👀

In [7]:
file = open(os.path.join(DATA_PATH,os.listdir(DATA_PATH)[0]), "r")
print(file.read())
40 40
#.#...#..#.....##...#.....##..#......###
.#.#...#..###..#.#..#.......#....#.#..#.
#......##.....##......##........#..##..#
##........#.#..#.#...#....#.##.....#....
.......#.#..#..###...#....#.####.#.....#
#...#.......#..#.#.##.#...#.#........#..
#...##.#.#.#...#.....#..#.##.#...#..#.##
.#.#......###...#.....#.###...#..#.....#
.#...............#....##.....#........##
.#...#......#..#.#.##....#..#...#.#...#.
..#....##....####...#.###......##.#..#.#
#...#.#.##.#....#..#...#.#.#..#...#.##.#
..#.#..#....###.##.###...............##.
..#........#....#.....#......#.#.......#
#..###.##..#...#..#..#..#..#..#....#.#.#
..#.........##....#..#..#....#....#....#
#...#....##.##.#..#....#..###......#.#.#
.#...#.......#..#..#.#.##.#...#...#.#.#.
.#..##....#.........#..#........#....##.
..............#.#....#.##...........##..
.##....#........##..#.#..#..##..#.......
#....###..#.#....##.......#..#..###.....
.#......#..##..........##....##....##...
...###...#.##.....#.#...###......#....#.
#.....##.##.#.....#....#.#..##...#...##.
#...##.....#...#..#.###...##..#..#....##
..#.......#..##...##...#..........#.....
..#...#....####.#.....#...#.####....#...
.....##..#..###...###...#..#.....#..##..
.#.#..............#.#.##..#...#.#.#....#
.......#.....#..#..##...#...#.....##...#
##...#...#.###....##.#...#.###.......#.#
..#.#.###..#.........####.#....#.....#..
.........#.##.#....#.#.#...#...#....#...
#.#....#.....##...#...##.##.###.#...#...
....##..............##..####.#.##....#.#
#........##...................##........
##..##..#.##..#......##.##.#.#.#..#..###
.#..#...#.#....#..#.#.......##.#....#.##
..#.#...........##...#............#..#.#
5
8 35 9 37
39 12 33 0
0 14 0 10
33 6 39 18
16 20 19 17

Helper Functions

Below class provides helper functions to load grid from given text files and make moves in the grid.

In [8]:
class GridState:
    def __init__(self, input_file):
        self.UP = (-1, 0)
        self.DOWN = (1, 0)
        self.LEFT = (0, -1)
        self.RIGHT = (0, 1)
        self.load(input_file)

    def load(self, input_file):
        file = open(input_file, 'r')
        # Get the Grid
        self.n, self.m = list(map(int, file.readline().strip().split()))
        self.grid = np.array(
            [[cell == "." for cell in file.readline().strip()] for _ in range(self.n)])
        k = int(file.readline().strip())
        self.tiles, self.targets = [], []
        for _ in range(k):
            line = list(map(int, file.readline().strip().split()))
            self.tiles.append((line[0], line[1]))
            self.targets.append((line[2], line[3]))
        file.close()

    def move_value(self, direction):
        if direction == 'U':
            return self.UP
        elif direction == 'D':
            return self.DOWN
        elif direction == 'L':
            return self.LEFT
        elif direction == 'R':
            return self.RIGHT

    def move(self, move):
        delta_r, delta_c = self.move_value(move)
        flag = 0
        for _ in range(max(self.n, self.m) + 1):
            for idx, tile in enumerate(self.tiles):
                next_r, next_c = tile[0] + delta_r, tile[1] + delta_c
                if 0 <= next_r < self.n and 0 <= next_c < self.m and \
                        self.grid[next_r, next_c] and (next_r, next_c) not in self.tiles:
                    flag = 1
                    tile = tile[0] + delta_r, tile[1] + delta_c
                self.tiles[idx] = tile
            # no change in any tile
            if flag == 0:
                break

TRAINING PHASE 🏋️

Here we are applying a random set of moves for each given grid and checking if that set of moves solves puzzle or not.

In [9]:
def create_random_moves(moves_length):
    seq = ['U', 'D', 'L', 'R']
    moves = ''.join(random.choices(seq, k=moves_length))
    return moves
In [10]:
solved = 0
moves_length = 20
lst = sorted(os.listdir(DATA_PATH))
total = len(lst)
df = pd.DataFrame(columns = {'filename', 'moves'})

for file in tqdm.tqdm(lst):
  path = os.path.join(DATA_PATH, file)
  obj = GridState(path)
  moves = create_random_moves(moves_length)
  for direction in moves:
      obj.move(direction)
      if obj.tiles == obj.targets:
          # print(obj.tiles, obj.targets)
          solved += 1
          # print("Puzzle Solved")
          break
  df = df.append({'filename':file, 'moves': moves}, ignore_index = True)
100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 2000/2000 [00:08<00:00, 234.71it/s]
In [11]:
df.head()
Out[11]:
moves filename
0 RDDRRLRRDDDDRUUDURRR 0001.txt
1 LULRRDDLRRDLLLULRURR 0002.txt
2 LRDDURRULRUDLLRRLRDU 0003.txt
3 DUURDLDDDDDLRUURLUUR 0004.txt
4 RURDLULLUURUUUUDDRUU 0005.txt

Evaluate the Performance

In [12]:
print("Number of puzzles solved is :" ,solved)
print("Percentage of puzzles solved :" , (solved/total)*100)
Number of puzzles solved is : 34
Percentage of puzzles solved : 1.7000000000000002

Save the prediction to csv

In [13]:
submission = pd.DataFrame(df)
submission.to_csv('submission.csv',index=False)

๐Ÿšง Note :

  • Do take a look at the submission format.
  • The submission file should contain a header.
  • Follow all submission guidelines strictly to avoid inconvenience.

Well Done! 👍 We are all set to make a submission and see your name on leaderborad.

In [14]:
%aicrowd submission create -c tile-slider -f submission.csv
                                       โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ                                       
                                       โ”‚ Successfully submitted! โ”‚                                       
                                       โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ                                       
                                             Important links                                             
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  This submission โ”‚ https://www.aicrowd.com/challenges/ml-battleground/submissions/123258              โ”‚
โ”‚                  โ”‚                                                                                    โ”‚
โ”‚  All submissions โ”‚ https://www.aicrowd.com/challenges/ml-battleground/submissions?my_submissions=true โ”‚
โ”‚                  โ”‚                                                                                    โ”‚
โ”‚      Leaderboard โ”‚ https://www.aicrowd.com/challenges/ml-battleground/leaderboards                    โ”‚
โ”‚                  โ”‚                                                                                    โ”‚
โ”‚ Discussion forum โ”‚ https://discourse.aicrowd.com/c/ml-battleground                                    โ”‚
โ”‚                  โ”‚                                                                                    โ”‚
โ”‚   Challenge page โ”‚ https://www.aicrowd.com/challenges/ml-battleground                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
{'submission_id': 123258, 'created_at': '2021-02-25T11:51:34.527Z'}
โ†•๏ธ  Read More


Comments

You must login before you can post a comment.