Optimizing Competition Strategies

A Mathematical Analysis of Resource Competition

Today, I’ll explore the fascinating world of competition strategy optimization through the lens of mathematical modeling. We’ll examine how species compete for limited resources and find optimal strategies using Python. This is a classic problem in evolutionary game theory and ecology that has applications in economics, biology, and business strategy.

The Problem: Resource Competition Model

Let’s consider a scenario where two species (or strategies) compete for a limited resource. Each species must decide how much energy to invest in competition versus other activities like reproduction or maintenance. This creates an interesting optimization problem where the payoff depends not only on your own strategy but also on your competitor’s strategy.

We’ll model this using the following framework:

  • Resource availability: $R$
  • Species 1 investment in competition: $x_1$
  • Species 2 investment in competition: $x_2$
  • Cost of competition: $c$
  • Resource acquisition function based on competitive investment

The payoff function for species $i$ can be expressed as:

$$\pi_i(x_i, x_j) = \frac{x_i}{x_i + x_j} \cdot R - c \cdot x_i^2$$

Where the first term represents the proportion of resources obtained and the second term represents the quadratic cost of competition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns

# Set up the competition model parameters
class CompetitionModel:
def __init__(self, R=100, c=0.5):
"""
Initialize the competition model

Parameters:
R (float): Total resource availability
c (float): Cost coefficient for competition investment
"""
self.R = R # Total resource pool
self.c = c # Cost of competition per unit squared

def payoff_species1(self, x1, x2):
"""
Calculate payoff for species 1

π₁(x₁, x₂) = (x₁/(x₁ + x₂)) * R - c * x₁²
"""
if x1 + x2 == 0:
return -self.c * x1**2
return (x1 / (x1 + x2)) * self.R - self.c * x1**2

def payoff_species2(self, x1, x2):
"""
Calculate payoff for species 2

π₂(x₁, x₂) = (x₂/(x₁ + x₂)) * R - c * x₂²
"""
if x1 + x2 == 0:
return -self.c * x2**2
return (x2 / (x1 + x2)) * self.R - self.c * x2**2

def find_nash_equilibrium(self):
"""
Find Nash equilibrium using optimization
At Nash equilibrium, each species maximizes its payoff given the other's strategy
"""
def objective(vars):
x1, x2 = vars
# We minimize the negative sum of squared deviations from best response
# Best response for species 1: ∂π₁/∂x₁ = 0
# Best response for species 2: ∂π₂/∂x₂ = 0

# Partial derivatives
if x1 + x2 > 0:
dpi1_dx1 = (x2 * self.R) / ((x1 + x2)**2) - 2 * self.c * x1
dpi2_dx2 = (x1 * self.R) / ((x1 + x2)**2) - 2 * self.c * x2
else:
dpi1_dx1 = -2 * self.c * x1
dpi2_dx2 = -2 * self.c * x2

return dpi1_dx1**2 + dpi2_dx2**2

# Initial guess
x0 = [5, 5]

# Constraints: x1, x2 >= 0
bounds = [(0, None), (0, None)]

result = minimize(objective, x0, bounds=bounds, method='L-BFGS-B')
return result.x

def analytical_nash_equilibrium(self):
"""
Calculate Nash equilibrium analytically

From first-order conditions:
∂π₁/∂x₁ = x₂R/(x₁+x₂)² - 2cx₁ = 0
∂π₂/∂x₂ = x₁R/(x₁+x₂)² - 2cx₂ = 0

By symmetry in this model, x₁* = x₂* at equilibrium
Solving: R/(4x*) - 2cx* = 0
Therefore: x* = √(R/(8c))
"""
x_star = np.sqrt(self.R / (8 * self.c))
return x_star, x_star

# Initialize the model
model = CompetitionModel(R=100, c=0.5)

# Find Nash equilibrium
nash_numerical = model.find_nash_equilibrium()
nash_analytical = model.analytical_nash_equilibrium()

print("Competition Strategy Optimization Results")
print("=" * 50)
print(f"Model Parameters:")
print(f" Resource Pool (R): {model.R}")
print(f" Competition Cost (c): {model.c}")
print()
print(f"Nash Equilibrium (Numerical): x₁* = {nash_numerical[0]:.3f}, x₂* = {nash_numerical[1]:.3f}")
print(f"Nash Equilibrium (Analytical): x₁* = {nash_analytical[0]:.3f}, x₂* = {nash_analytical[1]:.3f}")
print()

# Calculate payoffs at equilibrium
payoff1_nash = model.payoff_species1(nash_analytical[0], nash_analytical[1])
payoff2_nash = model.payoff_species2(nash_analytical[0], nash_analytical[1])
print(f"Equilibrium Payoffs:")
print(f" Species 1: π₁* = {payoff1_nash:.3f}")
print(f" Species 2: π₂* = {payoff2_nash:.3f}")
print(f" Total Payoff: {payoff1_nash + payoff2_nash:.3f}")

# Create strategy space for visualization
x1_range = np.linspace(0, 20, 100)
x2_range = np.linspace(0, 20, 100)
X1, X2 = np.meshgrid(x1_range, x2_range)

# Calculate payoff surfaces
Payoff1 = np.zeros_like(X1)
Payoff2 = np.zeros_like(X2)

for i in range(len(x1_range)):
for j in range(len(x2_range)):
Payoff1[j, i] = model.payoff_species1(X1[j, i], X2[j, i])
Payoff2[j, i] = model.payoff_species2(X1[j, i], X2[j, i])

# Create comprehensive visualization
fig = plt.figure(figsize=(20, 15))

# 1. 3D Payoff Surface for Species 1
ax1 = fig.add_subplot(2, 3, 1, projection='3d')
surf1 = ax1.plot_surface(X1, X2, Payoff1, cmap='viridis', alpha=0.8)
ax1.scatter([nash_analytical[0]], [nash_analytical[1]], [payoff1_nash],
color='red', s=100, label='Nash Equilibrium')
ax1.set_xlabel('Species 1 Investment (x₁)')
ax1.set_ylabel('Species 2 Investment (x₂)')
ax1.set_zlabel('Species 1 Payoff (π₁)')
ax1.set_title('Species 1 Payoff Surface')
plt.colorbar(surf1, ax=ax1, shrink=0.5)

# 2. 3D Payoff Surface for Species 2
ax2 = fig.add_subplot(2, 3, 2, projection='3d')
surf2 = ax2.plot_surface(X1, X2, Payoff2, cmap='plasma', alpha=0.8)
ax2.scatter([nash_analytical[0]], [nash_analytical[1]], [payoff2_nash],
color='red', s=100, label='Nash Equilibrium')
ax2.set_xlabel('Species 1 Investment (x₁)')
ax2.set_ylabel('Species 2 Investment (x₂)')
ax2.set_zlabel('Species 2 Payoff (π₂)')
ax2.set_title('Species 2 Payoff Surface')
plt.colorbar(surf2, ax=ax2, shrink=0.5)

# 3. Contour plot with Nash equilibrium
ax3 = fig.add_subplot(2, 3, 3)
contour1 = ax3.contour(X1, X2, Payoff1, levels=20, colors='blue', alpha=0.5)
contour2 = ax3.contour(X1, X2, Payoff2, levels=20, colors='red', alpha=0.5)
ax3.clabel(contour1, inline=True, fontsize=8, fmt='%.1f')
ax3.clabel(contour2, inline=True, fontsize=8, fmt='%.1f')
ax3.plot(nash_analytical[0], nash_analytical[1], 'ko', markersize=10, label='Nash Equilibrium')
ax3.set_xlabel('Species 1 Investment (x₁)')
ax3.set_ylabel('Species 2 Investment (x₂)')
ax3.set_title('Payoff Contours (Blue: Species 1, Red: Species 2)')
ax3.legend()
ax3.grid(True, alpha=0.3)

# 4. Best Response Functions
def best_response_1(x2):
"""Best response function for species 1 given species 2's strategy"""
if x2 == 0:
return 0
# From ∂π₁/∂x₁ = 0: x₂R/(x₁+x₂)² = 2cx₁
# This gives us a cubic equation that we solve numerically
def objective(x1):
if x1 + x2 <= 0:
return float('inf')
return abs((x2 * model.R) / ((x1 + x2)**2) - 2 * model.c * x1)

result = minimize(objective, [1], bounds=[(0, None)], method='L-BFGS-B')
return result.x[0] if result.success else 0

def best_response_2(x1):
"""Best response function for species 2 given species 1's strategy"""
if x1 == 0:
return 0
def objective(x2):
if x1 + x2 <= 0:
return float('inf')
return abs((x1 * model.R) / ((x1 + x2)**2) - 2 * model.c * x2)

result = minimize(objective, [1], bounds=[(0, None)], method='L-BFGS-B')
return result.x[0] if result.success else 0

ax4 = fig.add_subplot(2, 3, 4)
x_br = np.linspace(0.1, 15, 50)
br1 = [best_response_1(x) for x in x_br]
br2 = [best_response_2(x) for x in x_br]

ax4.plot(x_br, br1, 'b-', linewidth=2, label='BR₁(x₂)')
ax4.plot(br2, x_br, 'r-', linewidth=2, label='BR₂(x₁)')
ax4.plot(nash_analytical[0], nash_analytical[1], 'ko', markersize=10, label='Nash Equilibrium')
ax4.plot([0, 15], [0, 15], 'k--', alpha=0.5, label='45° line')
ax4.set_xlabel('Investment Level')
ax4.set_ylabel('Best Response')
ax4.set_title('Best Response Functions')
ax4.legend()
ax4.grid(True, alpha=0.3)

# 5. Payoff comparison across different strategies
strategies = np.linspace(1, 15, 15)
payoffs_coop = [] # Both species use same strategy
payoffs_defect = [] # One species increases investment

for s in strategies:
# Cooperative strategy (both use s)
p1_coop = model.payoff_species1(s, s)
p2_coop = model.payoff_species2(s, s)
payoffs_coop.append(p1_coop + p2_coop)

# Defection strategy (one uses s, other uses Nash)
p1_defect = model.payoff_species1(s, nash_analytical[1])
p2_defect = model.payoff_species2(nash_analytical[0], s)
payoffs_defect.append(p1_defect + p2_defect)

ax5 = fig.add_subplot(2, 3, 5)
ax5.plot(strategies, payoffs_coop, 'g-', linewidth=2, label='Cooperative (same strategy)')
ax5.plot(strategies, payoffs_defect, 'r-', linewidth=2, label='Mixed strategies')
ax5.axhline(y=payoff1_nash + payoff2_nash, color='k', linestyle='--',
label=f'Nash Equilibrium Total = {payoff1_nash + payoff2_nash:.2f}')
ax5.axvline(x=nash_analytical[0], color='k', linestyle=':', alpha=0.5)
ax5.set_xlabel('Investment Level')
ax5.set_ylabel('Total Payoff')
ax5.set_title('Strategy Comparison')
ax5.legend()
ax5.grid(True, alpha=0.3)

# 6. Sensitivity Analysis
costs = np.linspace(0.1, 1.0, 20)
nash_investments = []
total_payoffs = []

for c in costs:
temp_model = CompetitionModel(R=100, c=c)
x_nash = np.sqrt(temp_model.R / (8 * c))
nash_investments.append(x_nash)
payoff = temp_model.payoff_species1(x_nash, x_nash) + temp_model.payoff_species2(x_nash, x_nash)
total_payoffs.append(payoff)

ax6 = fig.add_subplot(2, 3, 6)
ax6_twin = ax6.twinx()

line1 = ax6.plot(costs, nash_investments, 'b-', linewidth=2, label='Nash Investment')
line2 = ax6_twin.plot(costs, total_payoffs, 'r-', linewidth=2, label='Total Payoff')

ax6.set_xlabel('Competition Cost (c)')
ax6.set_ylabel('Nash Investment Level', color='b')
ax6_twin.set_ylabel('Total Payoff', color='r')
ax6.set_title('Sensitivity to Competition Cost')

# Combine legends
lines1, labels1 = ax6.get_legend_handles_labels()
lines2, labels2 = ax6_twin.get_legend_handles_labels()
ax6.legend(lines1 + lines2, labels1 + labels2, loc='center right')

ax6.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

# Print additional analysis
print("\nDetailed Analysis:")
print("=" * 50)
print(f"At Nash equilibrium, each species invests {nash_analytical[0]:.3f} units")
print(f"This results in each species getting {model.R/2:.1f} units of resource")
print(f"Total competition investment: {2*nash_analytical[0]:.3f} units")
print(f"Total cost incurred: {2 * model.c * nash_analytical[0]**2:.3f} units")
print(f"Efficiency loss compared to no competition: {2 * model.c * nash_analytical[0]**2:.3f} units")

Code Explanation

Let me break down the key components of this competition strategy model:

1. Model Setup

The CompetitionModel class encapsulates our competition framework. The core insight is that each species faces a trade-off: investing more in competition increases their share of resources but comes at a quadratic cost.

2. Payoff Functions

The payoff function $\pi_i(x_i, x_j) = \frac{x_i}{x_i + x_j} \cdot R - c \cdot x_i^2$ captures two key elements:

  • Resource acquisition: The fraction $\frac{x_i}{x_i + x_j}$ represents how competitive investment translates to resource share
  • Competition cost: The term $c \cdot x_i^2$ represents increasing marginal costs of competition

3. Nash Equilibrium Calculation

I implemented both numerical and analytical solutions. The analytical solution comes from solving the first-order conditions:

$$\frac{\partial \pi_1}{\partial x_1} = \frac{x_2 R}{(x_1 + x_2)^2} - 2cx_1 = 0$$

By symmetry, at equilibrium $x_1^* = x_2^* = \sqrt{\frac{R}{8c}}$.

4. Visualization Components

3D Payoff Surfaces: These show how each species’ payoff varies with both investment levels. The red dot marks the Nash equilibrium.

Contour Plots: Provide a 2D view of the payoff landscapes, making it easier to see the strategic interactions.

Best Response Functions: Show how each species should optimally respond to their opponent’s strategy. The intersection point is the Nash equilibrium.

Strategy Comparison: Compares total welfare under different strategic scenarios.

Sensitivity Analysis: Shows how the equilibrium changes with different cost parameters.

Results

Competition Strategy Optimization Results
==================================================
Model Parameters:
  Resource Pool (R): 100
  Competition Cost (c): 0.5

Nash Equilibrium (Numerical): x₁* = 5.000, x₂* = 5.000
Nash Equilibrium (Analytical): x₁* = 5.000, x₂* = 5.000

Equilibrium Payoffs:
  Species 1: π₁* = 37.500
  Species 2: π₂* = 37.500
  Total Payoff: 75.000

Detailed Analysis:
==================================================
At Nash equilibrium, each species invests 5.000 units
This results in each species getting 50.0 units of resource
Total competition investment: 10.000 units
Total cost incurred: 25.000 units
Efficiency loss compared to no competition: 25.000 units

Key Insights from the Results

  1. Strategic Complementarity: The best response functions slope upward, indicating that higher competition from one species incentivizes higher competition from the other.

  2. Inefficiency of Competition: The Nash equilibrium results in both species investing in competition, reducing total welfare compared to a no-competition scenario.

  3. Cost Sensitivity: As competition costs increase, equilibrium investment levels decrease, but total payoffs don’t necessarily increase due to the reduced competitive pressure.

  4. Symmetry: In this symmetric game, both species end up with identical strategies and payoffs at equilibrium.

Mathematical Formulation

The complete mathematical model can be expressed as:

Maximize: $\pi_1(x_1, x_2) = \frac{x_1}{x_1 + x_2} \cdot R - c \cdot x_1^2$

Subject to: $x_1 \geq 0$

Where: Species 2 simultaneously solves the analogous problem

Nash Equilibrium: $(x_1^*, x_2^*) = \left(\sqrt{\frac{R}{8c}}, \sqrt{\frac{R}{8c}}\right)$

This model demonstrates fundamental principles in competition theory and has applications ranging from evolutionary biology to business strategy and economic competition. The key takeaway is that competitive markets often lead to over-investment in competition relative to social optimum, a classic result in game theory known as the “tragedy of competition.”