Firstly, let me apologize for my unclear title. I don't know how to describe this. Here is more description.
Given a ratio
$$x = x_1 : x_2 : x_3 : \cdots : x_n$$
where $s = \sum x_i$ and $x_i, n \in \mathbb{Z}^+$
Given $t$, is there any way to find the following ratio?
$$y = y_1 : y_2 : y_3 : \cdots : y_n$$ Such that
- $y$ is closest to $x$ (see below)
- $t = \sum y_i$
- $y_i \in \mathbb{Z}^+$
I would say $y$ is closest to $x$, if and only if, there is no other ratio
$$z = z_1 : z_2 : z_3 : \cdots : z_n$$
where $\sum z_i = t$ and $z_i \in \mathbb{Z}^+$
Such that:
$$\sum|\frac{z_i}{t} - \frac{x_i}{s}| < \sum|\frac{y_i}{t} - \frac{x_i}{s}|$$
My intuitive possible solution is: $$y_i = t \times \operatorname{round}(x_i \div s)$$
However, this is obviously stupid. Take the ratio $1:1$, and let's say we are looking for $t=5$.
Then following my proposal:
$$ x = 1:1\\ y = (5 \times \operatorname{round}(0.5)):(5 \times \operatorname{round}(0.5)) = 5:5 $$
Obviously, since $5+5= 10 \neq 5$, it violates my requirement $\sum y_i = t$. The correct answer of this example should be $2:3$ or $3:2$.
Thanks in advance.
The first thing to do is make it $y_i=\operatorname{round}(tx_i/s)$ so you get more granularity in your rounding. A lot of work has been put into this for apportioning representatives. For example, the US House of Representatives is fixed at $435$ seats which are to be allocated to the states in proportion to population. Of course, if you just divide the population of the country by $435$ and say you get one representative for each set of that many people you get a lot of fractional representatives. The naive thing, and what is done above, is to round but that doesn't guarantee the sum is $435$. Basically what is done (after you guarantee each state at least one representative) is to set a breakpoint for rounding that may well not be $0.5$ but is chosen so the proper number of states round up to give the total $435$. The competing concept is to adjust the multiplier $t/s$ a bit so that rounding with a breakpoint of $0.5$ results in the proper sum. That impacts the large $x_i$ more than the small $x_i$ so if your initial distribution with multiplier $t/s$ and breakpoint $0.5$ leaves you short they will be more likely to be assigned to the larger $x_i$ compared to just shifting the breakpoint. I don't know a proof that either one of these approaches is optimal for the metric of closest you have selected.