def greedy2(df): """ Try to get 2 gifts every time :param df: data :return: DataFrame with trip id """ df = df.sort(['Longitude', 'Latitude']) trip_id = -1 df['TripId'] = 0 last_pos = santa.NORTH_POLE for i, row in df.iterrows(): pos = row['Longitude'], row['Latitude'] if i % 2 == 0 or santa.haversine(pos, last_pos) > santa.haversine(pos, santa.NORTH_POLE)/3: trip_id += 1 last_pos = pos df.set_value(i, 'TripId', trip_id) return df
def find_trips(df): df = df.sort(['Longitude']) trip_id = 0 weight = 0 for i, row in df.iterrows(): weight += row.Weight if weight > MAX_LOAD: trip_id += 1 weight = row.Weight df.set_value(i, 'TripId', trip_id) dist = santa.haversine((row.Latitude, row.Longitude), santa.NORTH_POLE) df.set_value(i, 'dist', dist) return df.sort(['TripId', 'dist'])
def distance_from_polar(row): return santa.haversine((row['Latitude'], row['Longitude']), santa.NORTH_POLE)