[PYTHON] A very simple example of an ortoolpy optimization problem

What i did

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.

environment

code

#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

A very simple example of an ortoolpy optimization problem
Pandas: A very simple example of DataFrame.rolling ()
A simple example of how to use ArgumentParser
Combinatorial optimization --Typical problem-- Partition of a set problem
A simple sample of pivot_table.
Implementation of a simple particle filter
Want to solve a simple classification problem?
I implemented NSGA-II, a multi-objective optimization problem.
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Machine learning of sports-Analysis of J-League as an example-②
Simple implementation example of one kind of data augmentation