def greedytravelling(points, startingpoint = 0): points = np.ma.array(points, mask=False) finalgrouping = np.zeros((points.shape[0],1)) chosenpoint = startingpoint counter = 0 for i in range(points.shape[0]-1): counter += 1 chosenpointcoords = points[chosenpoint].data points[chosenpoint] = np.ma.masked closeindices = np.argsort(nphaversine(chosenpointcoords,points))[0] finalgrouping[counter] = closeindices chosenpoint = closeindices return finalgrouping
def greedytravelling(points, startingpoint=0): points = np.ma.array(points, mask=False) finalgrouping = np.zeros((points.shape[0], 1)) chosenpoint = startingpoint counter = 0 for i in range(points.shape[0] - 1): counter += 1 chosenpointcoords = points[chosenpoint].data points[chosenpoint] = np.ma.masked closeindices = np.argsort(nphaversine(chosenpointcoords, points))[0] finalgrouping[counter] = closeindices chosenpoint = closeindices return finalgrouping
members = np.where(finalgrouping==i) # grpmemberdist = haversine(origin,points[members[0]]) # this is sorting by who nearest pole # order = np.argsort(grpmemberdist) # this is sorting by who nearest pole finalgroupingrearrange = np.squeeze(greedytravelling(points[members[0],:])).astype(int) # DO A MULTIPLE START GREEDY TRAVEL EVAL THE DIST? # OR JUST START WITH THE ONE NEAREST THE POLE? a = int(finalgroupingrearrange[0]) z = int(finalgroupingrearrange[-1]) if nphaversine(points[members[0]][a],origin)>nphaversine(points[members[0]][z],origin): finalgroupingrearrange = finalgroupingrearrange[::-1] tripid[counter:counter+len(members[0])] = tripcounter giftid[counter:counter+len(members[0])] = members[0][finalgroupingrearrange] counter += len(members[0]) tripcounter += 1 giftid = giftid+1 # NO 0 GIFTID, DONT RUN THIS TWICE, CHECK WITH giftid.min() output = pd.DataFrame( data={"GiftId":giftid.astype(int), "TripId":tripid.astype(int)} ) # Use pandas to write the comma-separated output file
print 'Group: ' + str(i) members = np.where(finalgrouping == i) # grpmemberdist = haversine(origin,points[members[0]]) # this is sorting by who nearest pole # order = np.argsort(grpmemberdist) # this is sorting by who nearest pole finalgroupingrearrange = np.squeeze(greedytravelling( points[members[0], :])).astype(int) # DO A MULTIPLE START GREEDY TRAVEL EVAL THE DIST? # OR JUST START WITH THE ONE NEAREST THE POLE? a = int(finalgroupingrearrange[0]) z = int(finalgroupingrearrange[-1]) if nphaversine(points[members[0]][a], origin) > nphaversine( points[members[0]][z], origin): finalgroupingrearrange = finalgroupingrearrange[::-1] tripid[counter:counter + len(members[0])] = tripcounter giftid[counter:counter + len(members[0])] = members[0][finalgroupingrearrange] counter += len(members[0]) tripcounter += 1 giftid = giftid + 1 # NO 0 GIFTID, DONT RUN THIS TWICE, CHECK WITH giftid.min() output = pd.DataFrame(data={ "GiftId": giftid.astype(int), "TripId": tripid.astype(int) })