Maximizing Total Population with Python
Imagine a government that must divide a fixed development budget among three regions. Every unit of budget spent in one region improves living conditions there and lets its population grow, but that same unit is no longer available to the other two regions. How should the budget be split so that the total population ten years from now is as large as possible?
This is a classic constrained optimization problem. In this article we build a concrete model, solve it numerically with Python, verify the answer by brute-force search, and visualize the result in 3D.
1. Problem Setting
Three regions $i = 1, 2, 3$ (Region A, B, C) share a total budget $R$. Let $x_i \ge 0$ be the amount invested in region $i$.
$$
\max_{x_1,x_2,x_3}\ \sum_{i=1}^{3} P_i(T;,x_i)
\quad \text{subject to} \quad \sum_{i=1}^{3} x_i \le R,\quad x_i \ge 0
$$
Population model
Population in each region follows logistic growth:
$$
\frac{dP_i}{dt} = r_i P_i\left(1-\frac{P_i}{K_i}\right),\qquad P_i(0)=P_{i,0}
$$
The closed-form solution is
$$
P_i(t) = \frac{K_i P_{i,0}}{P_{i,0} + \left(K_i - P_{i,0}\right)e^{-r_i t}}
$$
Investment raises the carrying capacity of the region, but with diminishing returns:
$$
K_i(x_i) = K_{i,0} + \Delta K_i\left(1 - e^{-x_i/s_i}\right)
$$
Here $K_{i,0}$ is the capacity without investment, $\Delta K_i$ is the maximum additional capacity that investment can create, and $s_i$ is the scale of investment at which the benefit starts to saturate.
Optimality conditions
Because $P_i(T)$ is increasing and concave in $K_i$, and $K_i$ is increasing and concave in $x_i$, each $P_i(T;x_i)$ is an increasing concave function of $x_i$. The problem is therefore a concave maximization over a simplex, so any local optimum is the global optimum. Introducing a Lagrange multiplier $\lambda$ for the budget constraint gives the KKT conditions:
$$
\frac{\partial P_i(T;x_i)}{\partial x_i} = \lambda \quad (x_i > 0),\qquad
\frac{\partial P_i(T;x_i)}{\partial x_i} \le \lambda \quad (x_i = 0)
$$
In words: at the optimum, the marginal population gain per unit of budget is equal across all funded regions. The multiplier $\lambda$ is the shadow price of the budget, that is, the extra population obtained from one more unit of budget.
2. Example Problem
| Parameter | Region A | Region B | Region C |
|---|---|---|---|
| Initial population $P_{i,0}$ (thousand) | 50 | 30 | 20 |
| Capacity without investment $K_{i,0}$ (thousand) | 80 | 60 | 50 |
| Maximum capacity gain $\Delta K_i$ (thousand) | 120 | 100 | 90 |
| Investment scale $s_i$ | 40 | 25 | 20 |
| Growth rate $r_i$ (per year) | 0.30 | 0.45 | 0.60 |
The total budget is $R = 100$ units and the planning horizon is $T = 10$ years.
3. Source Code
1 | import numpy as np |
4. Code Walkthrough
Parameters and the population model
The arrays P0, K0, dK, s, and r hold the table of parameters from Section 2, one entry per region. The function final_population implements
$$
P_i(T) = \frac{K_i P_{i,0}}{P_{i,0}\left(1-e^{-r_iT}\right) + K_i, e^{-r_iT}}
$$
which is the logistic solution evaluated at $t=T$. It is written with NumPy broadcasting, so it accepts either a single allocation of shape (3,) or a whole batch of shape (N, 3). This vectorization is what makes the brute-force search below essentially instantaneous: about five thousand candidate allocations are evaluated in a single call without any Python loop. trajectory evaluates the same formula at many time points to draw growth curves, and population_region evaluates one region at a time for the marginal-return plot.
Numerical optimization
The optimization uses SLSQP, a sequential quadratic programming method suited to smooth problems with equality constraints and bounds. Because SciPy minimizes, the objective is the negative of the total population. The constraint $R - \sum_i x_i = 0$ forces the whole budget to be spent, which loses nothing because population is increasing in every $x_i$. The starting point is an equal split. Since the problem is concave, SLSQP converges to the global optimum regardless of the starting point.
Brute-force verification
To confirm that the solver has not been trapped somewhere unexpected, the code enumerates every integer allocation $(x_1, x_2, x_3)$ with $x_1 + x_2 \le R$ and $x_3 = R - x_1 - x_2$. The mask G1 + G2 <= R keeps only points on the budget simplex, and np.argmax picks the best one. The grid optimum should agree with the SLSQP optimum to within the one-unit grid spacing, and the SLSQP total should be equal to or slightly higher than the grid total.
Marginal returns
After the optimum is found, the code estimates $\partial P_{\text{total}}/\partial x_i$ by central-style finite differences. If the theory is correct, every region that receives funding shows the same marginal gain, and that common value is the shadow price $\lambda$ of the budget.
Comparison strategies
Three reference policies are evaluated alongside the optimal plan: splitting equally, splitting in proportion to the initial population (a natural “fairness” rule), and investing nothing. They show how much the optimization actually gains over intuitive rules of thumb.
5. Results
Result images

Console output
================================================================ Optimal resource allocation among three competing regions ================================================================ Solver status : Optimization terminated successfully Total budget : 100.0 Planning horizon : 10 years ---------------------------------------------------------------- Region Investment Final pop. Marginal gain Region A 35.986 137.355 0.95683 Region B 33.593 128.951 0.95683 Region C 30.420 118.858 0.95683 ---------------------------------------------------------------- Total population (SLSQP) : 385.1636 thousand Total population (grid search): 385.1561 thousand Grid-search allocation : A=36, B=34, C=30 ---------------------------------------------------------------- Strategy Total pop. vs Optimal Optimal 385.164 100.00% Equal split 384.874 99.92% Proportional to initial pop. 379.521 98.53% No investment 186.835 48.51% ================================================================
6. Interpreting the Graphs
Top left: 3D surface of total population. The horizontal axes are the investments in Regions A and B, and the investment in Region C is whatever remains of the budget. The surface is a single smooth dome with no competing peaks, which is the visual signature of the concavity proved earlier. The red marker sits at the top of the dome, the best allocation the budget allows.
Top center: contour map. The same surface seen from above. The triangular domain is the set of all feasible budget splits. The star marks the optimal plan and the white dot marks the equal split. The distance between them shows how far an intuitive “split it evenly” policy sits from the true optimum, and the contour lines show how quickly the total falls off as the allocation moves away from the best point.
Top right: allocation by strategy. Side-by-side bars compare the optimal split with the equal split and the split proportional to initial population. Differences in bar height show how the optimal plan deliberately favors regions whose investment converts into population most efficiently, rather than regions that are simply large today.
Bottom left: population trajectories. Solid curves show each region’s population over the ten years under the optimal plan, and dashed curves show what would happen with no investment. The gap between solid and dashed curves of the same color is the benefit of funding that region. Regions with higher growth rates $r_i$ approach their capacity sooner, while slower-growing regions are still climbing at $t = T$.
Bottom center: marginal returns. Each curve shows the extra population gained from one more unit of budget as a function of the amount already invested in that region. All curves decline because of diminishing returns. The dots mark the optimal allocation, and for every funded region they lie at the same height, the dotted horizontal line representing the shadow price $\lambda$. This is the KKT condition made visible: moving a unit of budget from one region to another cannot help, because the gain in one is exactly cancelled by the loss in the other.
Bottom right: total population by strategy. The optimal plan produces the largest population, with the equal split and the proportional rule below it and the no-investment baseline far behind. The gap between the optimal plan and the intuitive rules is the practical value of solving the allocation problem properly.
7. Conclusion
A competing allocation problem looks complicated because every investment decision affects every other region, yet its structure is simple. With diminishing returns in each region, the total population is a concave function of the allocation, so a single numerical solve finds the global optimum, and the optimal plan is characterized by one clean principle: equalize the marginal gain per unit of budget across all funded regions. The shadow price $\lambda$ then tells decision makers exactly how much additional population one extra unit of budget would buy, which is valuable information when negotiating the size of the budget itself.
The same framework extends naturally to more regions, additional constraints such as minimum guaranteed funding per region, or investment that is spread over time as a dynamic optimization problem.










