Python Practical Data Analysis After reading 100 knocks, I became interested in Pulp, an optimization library, so I also used it as a memorandum.
I want to easily perform optimization using variables that take integers Of course, if you use a For statement for brute force, you can easily apply it, or you can search by shaking the parameters one by one, but it takes time and it is troublesome to set constraint conditions.
From the console in Anaconda
conda install -c conda-forge pulp
pip install ortoolpy
Can be installed with
sample.py
from pulp import LpVariable,lpSum,value
from ortoolpy import model_max,addvars,addvals
m=model_max()
v1={"x":LpVariable("vx",lowBound=0,cat='Integer'),
"y":LpVariable("vy",lowBound=0,cat='Integer')}
m+=lpSum(v1["x"]+v1["y"])
m+=lpSum(v1["x"]*2+v1["y"])<=10
m+=lpSum(v1["x"]+v1["y"]*2)<=7
m.solve()
for k,x in v1.items():
print(k,value(x))
print(value(m.objective))
x 4.0
y 1.0
5.0
Recommended Posts