Kaggle competition: Traveling Santa 2018 - Prime Paths
<SOS> metric: TravelingSanta2 Kaggle data source: traveling-santa-2018-prime-paths<categorify>
def score_tour(tour):
df = cities.reindex(tour + [0] ).reset_index()
df['prime'] = df.CityId.isin(primes ).astype(int)
 df['dist'] = np.hypot(df.X - df.X.shift(-1), df.Y - df.Y.shift(-1))
df['penalty'] = df['dist'][9::10] *(1 - df['prime'][9::10])* 0.1
return df.dist.sum() + df.penalty.sum()
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv')
 primes = list(primerange(0, len(cities['CityId'].values)))
 primes += [0,1]
cities['NotPrime'] = np.abs(cities.CityId.isin(primes ).astype(int)- 1)* 0.1 + 1
dPaths = {v:{'X': x, 'Y': y, 'penalty': z} for v, x, y, z in cities[['CityId','X','Y','NotPrime']].values}
cities = pd.read_csv('.. /input/traveling-santa-2018-prime-paths/cities.csv', index_col=['CityId'])
 tour = pd.read_csv('.. /input/dp-shuffle/submission.csv')['Path'].tolist()
score_tour(tour )
def mono_to_color(X, mean=None, std=None, norm_max=None, norm_min=None, eps=1e-6):
X = np.stack([X, X, X], axis=-1)
 mean = mean or X.mean()
std = std or X.std()
Xstd =(X - mean)/(std + eps)
 _min, _max = Xstd.min() , Xstd.max()
norm_max = norm_max or _max
norm_min = norm_min or _min
if(_max - _min)> eps:
V = Xstd
V[V < norm_min] = norm_min
V[V > norm_max] = norm_max
V = 255 *(V - norm_min)/(norm_max - norm_min)
 V = V.astype(np.uint8)
 else:
V = np.zeros_like(Xstd, dtype=np.uint8)
 return V
def convert_wav_to_image(df, source, img_dest):
X = []
for i, row in tqdm_notebook(df.iterrows()):
x = read_as_melspectrogram(conf, source/str(row.fname), trim_long_data=False)
 x_color = mono_to_color(x)
 X.append(x_color)
 return X
X_train = convert_wav_to_image(df, source=TRN_CURATED, img_dest=IMG_TRN_CURATED)
 X_test = convert_wav_to_image(test_df, source=TEST, img_dest=IMG_TEST )<define_variables>
def bscore(cand, is_tenth):
s = 0.0
for j in range(len(cand)-1):
p = np.hypot(dPaths[cand[j]]['X'] - dPaths[cand[j+1]]['X'], dPaths[cand[j]]['Y'] - dPaths[cand[j+1]]['Y'])
 if is_tenth[j]:
p *= dPaths[j]['penalty']
s += p
return s
def riffle2(batch, i):
is_tenth = [(j+1)%10==0 for j in range(i,i+n)]
best = batch
for per in permutations(batch[1:-1]):
perm = [batch[0]]+list(per)+[batch[-1]]
if bscore(perm, is_tenth)< bscore(best, is_tenth):
best = perm
if best != batch:
return best
else:
return None
def riffle(split_tour1):
order, batch = split_tour1
for i in range(0, len(batch)-n+1):
r = riffle2(batch[i:i+n],i)
 if r:
print(r,i)
 batch = batch[:i] + r + batch[i+n:]
return [order, batch]
def multi_riffle(t):
ret_d = {}
p = Pool(cpu_count())
 t = np.array_split(t, 10000)
 t = [[i,list(t)] for i, t in enumerate(t)]
ret = p.map(riffle, t)
 for i in range(len(ret)) :
ret_d[ret[i][0]] = ret[i][1]
ret = []
for i in range(len(ret_d)) :
ret += ret_d[i]
return ret
<define_variables><EOS> 
%%time
n=8
tour = multi_riffle(tour)
 pd.DataFrame({'Path': list(tour)} ).to_csv('submission.csv', index=False)
 print(score_tour(tour))