I have a delivery app, and I want to group orders (each order has a lat and lng coordinates) by location proximity (linear distance) and constraints like max orders and max total products (each order has an amount of products) inside a group.
For proximity grouping I used DBSCAN
coordinates = [[lat,lng],[lat,lng]],[lat,lng]],[lat,lng]],[lat,lng]]]
distance_matrix = squareform(pdist(coordinates, (lambda u,v: haversine(u,v))))
#eps=0.1 => 100m radius, 50m linear
db = DBSCAN(eps=0.1, min_samples=2, metric='precomputed')
results = db.fit(distance_matrix)
How can I add constraints in this functionality?
Is there anyway of doing this by using something else than DBSCAN or HDBSCAN ?
Unfortunately, I think the model you want should be developed from the scratch.
Your question can be modeled as following optimization model.
Objective function: minimize the number of clusters, K Constraint (1) The size of every cluster is equal or smaller than S (parameter) (2) The number of orders in every cluster is equal or smaller than O (parameter) (3) For a sample x and its cluster Ck, dist(x, Ck) be the minimum among dist(x, C1), dist(x, C2), ..., dist(x, CK).
Solving this problem would require a lot of efforts...