Download Necessary Packages 📚¶
In [1]:
! pip install git+https://gitlab.aicrowd.com/yoogottamk/aicrowd-cli.git >/dev/null
%load_ext aicrowd.magic
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
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())
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)
In [11]:
df.head()
Out[11]:
Evaluate the Performance¶
In [12]:
print("Number of puzzles solved is :" ,solved)
print("Percentage of puzzles solved :" , (solved/total)*100)
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
Content
Comments
You must login before you can post a comment.