Skip to content
Snippets Groups Projects
Commit 2a4091d8 authored by Kai Sellschopp's avatar Kai Sellschopp
Browse files

cleanup of the standalone fps method

parent dc5fdb49
No related branches found
No related tags found
No related merge requests found
import numpy as np
from itertools import combinations
def greedy(k, dist_matrix):
# initialize some arrays
indices = np.arange(len(dist_matrix))
mask = np.ones(len(dist_matrix),dtype='bool')
# select two indices with largest distance
select = np.array(np.unravel_index(np.argmax(dist_matrix), dist_matrix.shape))
while len(select)<k:
# mask selected indices
mask[select] = False
# find the point with the largest minimum distance to already selected points
maxmin = np.argmax(np.amin(dist_matrix[select][:,mask], axis=0))
index_maxmin = indices[mask][maxmin]
# add the index to the selection
select = np.append(select, index_maxmin)
# return resulting selection (approximate solution)
return select
def brute_force(k, dist_matrix):
# initialize indices and selection
indices = np.arange(len(dist_matrix))
select = np.arange(k)
mask = np.logical_not(np.eye(k,dtype='bool')) # ignore diagonal elements
mindist_select = np.amin(dist_matrix[select][:,select][mask])
# iterate over all possible combinations
for combo in combinations(indices, k):
# convert to array
combo = np.array(combo)
# calculate the minimal distance between points
mindist_combo = np.amin(dist_matrix[combo][:,combo][mask])
# replace the selected combination if mindist is larger
if mindist_combo>mindist_select:
select = combo
mindist_select = mindist_combo
# return selection with maximum mindist (exact solution)
return select
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment