Just as you should use StringBuilder for string concatenation in C # and Java, there is a similar technique for adding variables in a mathematical model.
I will summarize it in a table.
package | How to write | Yes / No |
---|---|---|
sum | × | |
PuLP | lpSum | ○ |
PuLP | lpDot | ○ |
GUROBI | quicksum | ○ |
lpSum and quicksum are total calculations, and lpDot is inner product calculation. The equivalent of lpDot (a, b) in GUROBI can be done with quicksum (i * j for i, j in zip (a, b)).
Those with "○" are in the linear order, but those with "×" are in the square order.
Let's check with PuLP.
python3
from pulp import LpVariable, value
for i in [1000, 2000, 5000]:
v = [LpVariable('v%d'%i) for i in range(i)]
print(i)
%timeit lpSum(v)
%timeit sum(v)
>>>
1000
1000 loops, best of 3: 1.44 ms per loop
1 loop, best of 3: 403 ms per loop
2000
100 loops, best of 3: 2.89 ms per loop
1 loop, best of 3: 1.58 s per loop
5000
100 loops, best of 3: 7.11 ms per loop
1 loop, best of 3: 10 s per loop
that's all
--Graph drawing
python3
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot([0,1000,2000,5000], [0,1.44,2.89,7.11], label='lpSum')
ax2.plot([0,1000,2000,5000], [0,403,1580,10000], label='sum', color='red')
ax1.legend(loc='center left')
ax2.legend(loc='center right');
Recommended Posts