## Tuesday, December 10, 2013

### Packing Santa's Sleigh (Python Code for MATLAB Benchmark)

`````` #
# This is a translation of the MATLAB benchmark code
# given by Kaggle in "Packing Santa's Sleigh" competition
#
# This file will give a score same as the MATLAB benchmark score
#
import numpy as np
import scipy as sp
import pandas as pd
def getData():
#print data
print "converting data to numpy array"
data = np.asarray(data)
#print data
return data
def pack(data):
#data = data[:100, :]
# the number of presents
presents = data[:,1:]
numPresents = data.shape
print "total presents : ", numPresents
# width and length are 1000 units. Height is not fixed for the packing
width = 1000
length = 1000
# Initial coordinates
xs = 1
ys = 1
zs = -1
lastRowsInd = np.zeros((100, 1)) # temp array for storing indexes of last few rows
lastShelfInd = np.zeros((100,1)) # temp array for storing indexes of last few shelves
numInRow = 0     # Store the number of presents in current row
numInShelf = 0     # Store the number of presents in current shelf
presentCoordinates = np.zeros((numPresents, 25))
tempPresentLenRow = []
tempPresentHeightShelf = []
for i in range(numPresents):
# check if there is room in the row, else increase the row
if (xs + presents[i,0] > width + 1):
ys = ys + np.max(tempPresentLenRow)
xs = 1
numInRow = 0
tempPresentLenRow = []
# check if there is room in shelf, else increase the height
if (ys + presents[i,1] > length + 1):
zs = zs - np.max(tempPresentHeightShelf)
xs = 1
ys = 1
numInShelf = 0
tempPresentHeightShelf = []
presentCoordinates[i,0] = data[i,0]
presentCoordinates[i,[1,7,13,19]] = xs
presentCoordinates[i,[4,10,16,22]] = xs + presents[i,0] - 1
presentCoordinates[i,[2,5,14,17]] = ys
presentCoordinates[i,[8,11,20,23]] = ys + presents[i,1] - 1
presentCoordinates[i,[3,6,9,12]] = zs
presentCoordinates[i,[15,18,21,24]] = zs - presents[i,2] + 1
xs = xs + presents[i,0]
numInRow = numInRow + 1
numInShelf = numInShelf + 1
tempPresentLenRow.append(presents[i,1])
tempPresentHeightShelf.append(presents[i,2])
if i%1000 == 0: print i
zCoords = presentCoordinates[:,3::3]
minZ = np.min(zCoords.ravel())
presentCoordinates[:,3::3] = zCoords - minZ + 1
return presentCoordinates
def saveCSV(predictions):