Here’s a recipe that turns your “competing apples” into a smooth, differentiable clustering problem—basically a soft‐k-means. Each apple (j) sits at (\mathbf{a}_j\in\mathbb{R}^2), each dot (i) at (\mathbf{x}i). We introduce a “soft‐assignment” weight (w{ij}) that says how much dot (i) belongs to apple (j), then write each apple’s loss so that it
• Pulls the apple toward the dots it “owns” most
• Indirectly competes with the other apples via those weights
1. Soft‐assignment weights
For each dot (i) and apple (j) define
[
d_{ij} ;=;|\mathbf{x}_i - \mathbf{a}j|^2
]
[
w{ij}
\frac{\exp\bigl(-\beta,d_{ij}\bigr)}%
{\displaystyle\sum_{k=1}^4 \exp\bigl(-\beta,d_{ik}\bigr)}
]
– β > 0 is an “inverse‐temperature” controlling how crisp the assignments are.
– As β→∞ you recover hard assignments (“each dot picks its nearest apple”).
2. Per-apple loss
Now let apple (j) minimize the expected squared‐distance to the dots it owns:
[
L_j(\mathbf{a}_j)
\sum_{i=1}^{N} ; w_{ij};d_{ij}
\sum_{i=1}^N w_{ij},|\mathbf{x}_i - \mathbf{a}j|^2
]
Because (w{ij}) depends on all ({\mathbf{a}_k}), each apple “feels” the others (they compete for the same pool of dots).
3. Putting it all together
- Initialize your 4 apples at random positions (\mathbf{a}_1,\dots,\mathbf{a}_4).
- Repeat until convergence:
a. Recompute all (d_{ij}) and (w_{ij}).
b. For each apple (j), take a gradient step
[
\mathbf{a}j ;\leftarrow;
\mathbf{a}j ;-;\eta;\nabla{\mathbf{a}j}L_j
\quad\text{where}\quad
\nabla{\mathbf{a}j}L_j
= -2\sum{i} w{ij},(\mathbf{x}_i - \mathbf{a}j);
+;\text{(terms from }\nabla w{ij}\text{)}.
]
c. (Optionally) Anneal β upward so assignments go from soft → hard.
In practice you don’t have to hand‐derive the full gradient of the (w_{ij}) term—just let autograd handle it if you’re in PyTorch/TensorFlow.
4. Tuning and extensions
• β schedule
– Start with a small β (soft, overlapping coverage) and slowly raise it so the apples “snap” to 25 dots each.
• Repulsion
– If you still see collapse, add a small repulsive penalty
[
R_j ;=;\lambda\sum_{k\neq j}\exp\bigl(-|\mathbf{a}_j-\mathbf{a}_k|^2/\sigma^2\bigr)
]
and minimize (L_j + R_j).
This setup is exactly a differentiable version of k-means (“soft k-means”), and will give you four competing cluster‐centers that each end up covering roughly 25 of your 100 dots.