User ddofborg posted on Stack Overflow a programming question which hides a combinatorial optimisation problem.
The idea is the following: given a list of URLs with their respective domain names, he wants to find the permutation which makes the domain names "as unsorted as possible". By this he means that he does not want URLs with the same domain name to appear close to each other but, rather, he would like them to be as spaced apart as possible.
For example, let the following be the input data to the problem:
('host1.com', 'https://host1.com/id/4'),
('host1.com', 'https://host1.com/id/6'),
('host2.com', 'https://host2.com/abc'),
('host3.com', 'https://host3.com/x7'),
('host3.com', 'https://host3.com/f2'),
('host3.com', 'https://host3.com/l3')
In the above list all URLs associated with the same domain are close to each other, so the user wouldn't like this arrangement. The idea is that he will go through the URLs of this list and make some network request. He wants to space requests to the same domain apart, so that the corresponding server doesn't get overloaded. A solution the user would like is, e.g., the following:
('host3.com', 'https://host3.com/x7'),
('host1.com', 'https://host1.com/id/4'),
('host2.com', 'https://host2.com/abc'),
('host3.com', 'https://host3.com/l3'),
('host1.com', 'https://host1.com/id/6'),
('host3.com', 'https://host3.com/f2')
To quantify his "unsortedness" criterion, the user proposes to assign an objective value to each possible permutation of URLs. Before introducing the objective function, however, let me propose some notation which will make the discussion proceed more quickly within our community.
Let $B = \{1, \ldots, m\}$ be the set of bases (in our case, domain names) and let $I = \{1, \ldots, n\}$ be the set of items (in our case, URLs). Denote with $\alpha_b \in \mathbb{N}$ the number of items with base $b$, and with $\beta_i$ the base of item $i$. Let $x_{bj} \in \{0,1\}$ be a binary variable with value 1 iff an item of base $b$ is placed in position $j$ in the permutation.
For each base $b$ the user proposes to measure its "unsortedness" summing all the pairwise distances between items of base $b$ (without repetitions). For example, if URLs associated with host3.com take positions 4, 5 and 6 (as in the first example), then the unsortedness score of host3.com is $|4-5| + |4-6| + |5-6| = 4$. However if, as in the second example, the URLs take positions 1, 4 and 6, then the score is $|1-4| + |1-6| + |4-6| = 10$.
Formally, the objective function is: $$\sum_{b = 1}^m \sum_{j_1 = 1}^{n-1} \sum_{j_2 = j_1 + 1}^n (j_2 - j_1) x_{b j_1} x_{b j_2}$$ I.e., a quadratic function of variables $x_{bj}$.
I would like to investigate further this problem, see if it's possible to model it using a classical OR problem and devise some efficient algorithm.
Another Stack Overflow user has proposed a Constraint Programming approach (although I am not 100% convinced of its correctness).
I will mark this question as community wiki, so that everyone whose curiosity was picked by this problem can contribute.