When I was reading "Python Practical Data Analysis 100 Knock", I came across an optimization problem using ortoolpy and pulp. It was a module I had never used, so I made a simple example for my own understanding.
See the comments in the code for details.
#Import required modules
from pulp import LpVariable, lpSum, value
from ortoolpy import model_min
#Model definition
model = model_min()
#Variable definition
# v_0 and v_Define two variables called 1. A container that does not contain a specific value.
v_0 = LpVariable("V0")
v_1 = LpVariable("V1")
#Objective function setting
# v_0 + v_I want to minimize 1.
model += lpSum([v_0, v_1])
#Add constraints
# v_0 is 2 or more
model += v_0 >= 2
# v_1 is 3 or more
model += v_1 >= 3
# v_0 and v_1 is the same value
model += v_0 == v_1
#From the above conditions, v_0 = v_1 =Expect to be 3.
#Solve the problem
model.solve()
#View results
# value(Variable name)Then, you can read the value of the calculation result.
print("v_0 : ", value(v_0))
print("v_1 : ", value(v_1))
# v_0 : 3.0
# v_1 : 3.0
#The expected value was obtained.
Recommended Posts