[PYTHON] Sum of variables in a mathematical model

what is this

How to write the sum of variables when creating a mathematical model for optimization

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.

Verification

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

image

that's all


reference

--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

Sum of variables in a mathematical model
Mathematical model of infectious disease epidemics
Display a list of alphabets in Python 3
Use a scikit-learn model trained in PySpark
[python] Get a list of instance variables
Project Euler # 16 "Sum of Powers" in Python
Benefits of using slugfield in Django's model
Implement a Custom User Model in Django
Draw a graph of a quadratic function in Python
Python scikit-learn A collection of predictive model tips often used in the field
Project Euler # 10 "sum of prime numbers" in Python
Get the caller of a function in Python
We have released a trained model of fastText
Make a copy of the list in Python
Find the number of days in a month
Rewriting elements in a loop of lists (Python)
A proposal for versioning of features in Kedro
Implement the mathematical model "SIR model" of infectious diseases in OpenModelica (see the effect of the vaccine)
Create a simple momentum investment model in Python
Make a joyplot-like plot of R in python
Output in the form of a python array
Project Euler # 13 "Sum of Large Numbers" in Python
Get a glimpse of machine learning in Python
Project Euler # 6 "Difference in sum of squares" in Python
Find a mathematical model of experience points required to level up DQ Walk (1)
A well-prepared record of data analysis in Python
Python scikit-learn A collection of predictive model tips often used in the field
Derivation of certainty of effect in A / B testing
Test Mathematical Part 2 (Mathematical model of item reaction theory)
[Python] Implementation of clustering using a mixed Gaussian model
Solving mathematical models of infectious disease epidemics in Python
This is a sample of function application in dataframe.
Specify the lighting Model of SCN Material in Pythonista
Maximum likelihood estimation implementation of topic model in python
A list of stumbling blocks in Django's image upload
Introduction of mathematical prediction model for infectious diseases (SIR model)
Count the number of parameters in the deep learning model
[Python] How to expand variables in a character string
[PyTorch] A little understanding of CrossEntropyLoss with mathematical formulas
A collection of commands frequently used in server management
A shell program that becomes aho in multiples of 3
A note on handling variables in Python recursive functions
Group by consecutive elements of a list in Python
Display a histogram of image brightness values in python
A collection of Excel operations often used in Python
Variational Bayesian inference implementation of topic model in python
A reminder about the implementation of recommendations in Python
A one-year history of operating a chatbot in a muffled manner
[Day 9] Creating a model
dict in dict Makes a dict a dict
Model changes in Django
Implement sum in Python
Implement the mathematical model "SIR model" of infectious diseases in OpenModelica (reflecting mortality rate and reinfection rate)
Implementation of VGG16 using Keras created without using a trained model
A note on the default behavior of collate_fn in PyTorch
Save the pystan model and results in a pickle file
Find out the apparent width of a string in python
[Django] Manage settings like writing in settings.py with a model
Make a table of multiplication of each element in a spreadsheet (Python)
Implement the mathematical model "SIR model" of infectious diseases with OpenModelica
The story of a Django model field disappearing from a class