Memorandum @ Python OR Seminar: Pulp

Library for mathematical optimization modeling. Solvers such as Gurobi, CBC, and GLPK can be used. I don't know much about optimization, so I plan to study in the future.

Mathematical model creation procedure

The procedure for solving a problem with a mathematical model is as follows.

  1. Create a mathematical model (LpProblem)
  2. Add a variable (LpVariable)
  3. Add the objective function expression (LpAffineExpression)
  4. Add a constraint (LpConstraint)

Mathematical model: LpProblem

LpProblem(name='NoName', sense=LpMinimize)

--name: Mathematical model name. Default is NoName. --sense: Either minimize (LpMinimize) or maximize (LpMaximize).

Variable: LpVariable

LpVariable(name, lowBound=None, upBound=None, cat='Continuous', e=None)

--name: Variable name -- lowBound: Lower limit --ʻUpBound: Upper limit --cat`: Variable type

Objective function: LpAffineExpression

Can be created like " x + 2 * y "using variables It is also possible to add terms to the expression later Specify the objective function as "m + = expression" (however, m is a mathematical model)

Constraint: LpConstraint

Can be created like " x + y <= 1 "using variables It is also possible to add a term later on the left side of the constraint Constraints are specified as " m + = expression <= right side "

Other

Functions that seem to be used often value (): Fetch the value of a variable lpSum (): Find the sum of terms lpDot (): Find the dot product of two lists

The cheat sheet is here

Try to solve the knapsack problem

Common optimization problems

Put as many items as possible in a knapsack then:

--The maximum weight that can be put in a knapsack is 600 grams.

There are restrictions like. So what do you do?

Formulate

Insert the item $ s_i $ → $ v_i = 1 $ Do not put item $ s_i $ → $ v_i = 0 $

\begin{array}{cl}
\max & \sum_i{s_i \ v_i} \\
\mbox{subject to} & \sum_i{s_i \ v_i} \leq C \\
                 & v_i \in \{0, 1\} ~ \forall i
\end{array}

Implement with pulp

>>> #The weight of the item
>>> s = [128, 108, 34, 53, 71, 224, 299, 181, 336, 15]
>>> #Maximum load capacity of knapsack
>>> C = 600
>>> #Library preparation
>>> from pulp import LpProblem, LpVariable, LpMaximize, LpBinary
>>> rn = range(len(s))
>>> #Model preparation
>>> m = LpProblem('knapsack', LpMaximize)
>>> #variable(0 whether to include the i-th item/1)
>>> v = [LpVariable('v%d' % i, cat = LpBinary) for i in rn]
>>> #Objective function
>>> m += lpDot(s, v)
>>> #Constraint
>>> m += lpDot(s, v) <= C
>>> #solve
>>> m.solve()
>>> #output
>>> print(LpStatus[m.status], sum(s[i] * value(v[i]) for i in rn))
>>> print([s[i] for i in rn if value(v[i]) > 0.5])
Optimal 600.0
[108, 34, 53, 224, 181]

Other optimization problems

Other optimization problems can be solved by formulating (like)

Facility placement problem (p-median)

The problem of selecting p from the facility candidates and minimizing the total distance from the demand point to the facility

Example

Formulation

\begin{array}{cl}
\min & \sum_i{\sum_j{d_{ij} \ x_{ij}}} ~ ~ ~ ~ (1) \\
\mbox{subject to} & \sum_i{y_i} = p ~ ~ ~ ~ (2) \\
                 & \sum_i{x_{ij}} = 1 ~ \forall j ~ ~ ~ ~ (3) \\
                 & x_{ij} \leq y_i ~ \forall i, j ~ ~ ~ ~ (4) \\
                 & x_{ij} \in \{0, 1\} ~ \forall i, j
\end{array}

Sudoku

That is common.

Formulation

\begin{array}{cl}
\min & \mbox{no objective function} \\
\mbox{subject to} & \sum_k{v_{ijk}} = 1 ~ \forall i, j ~ (1)\\
                 & \sum_k{v_{ikj}} = 1 ~ \forall i, j ~ (2) \\
                 & \sum_k{v_{kij}} = 1 ~ \forall i, j ~ (3) \\
                 & 3\The same applies to the squares of times3~ (4) \\
                 & v_{ijk} \in \{0, 1\} ~ \forall i, j, k
\end{array}

Other optimization problems

Kakuro Nonogram Museum Numberlink Verbal arithmetic
Inequalities Building puzzle Wall logic Spillover effect Number skeleton
Slitherlink Cut into squares Masyu Build a bridge Nori glue
Block puzzle Tile paint Factor room Where black Detective puzzle
Leave me alone Heyawake Paint area A few rollers Pipe link
Creek ice burn Thumbline country road Kanaole
Philmat Shaka Shaka Yajilin Nurikabe Firefly beam
Stained glass Satogaeri skeleton Name quote from Nikoli

Recommended Posts

Memorandum @ Python OR Seminar: Pulp
Memorandum @ Python OR Seminar
Memorandum @ Python OR Seminar: matplotlib
Memorandum @ Python OR Seminar: Pandas
Memorandum @ Python OR Seminar: scikit-learn
Python memorandum
Python Memorandum 2
Python memorandum
python memorandum
python memorandum
Python memorandum
python memorandum
Python memorandum
Python basics memorandum
Python pathlib memorandum
Python memorandum (algorithm)
Python memorandum [links]
Python> list> extend () or + =
Python memorandum numbering variables
python memorandum (sequential update)
Python from or import
python autotest or sniffer
Python memorandum (personal bookmark)
Python basic memorandum part 2
Python optimization library Pulp
[Python] Iterative processing_Personal memorandum
python memorandum super basic
Effective Python Learning Memorandum Day 15 [15/100]
Cisco Memorandum _ Python config input
Python 3.4 or later standard pip
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
ABC memorandum [ABC163 C --managementr] (Python)
About python beginner's memorandum function
A memorandum about correlation [Python]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Python bitwise operator and OR
Effective Python Learning Memorandum Day 13 [13/100]
A memorandum about Python mock
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
[python] Random number generation memorandum
Effective Python Learning Memorandum Day 4 [4/100]
Ruby's `` like Python. 2.6 or later
Python or and and operator trap
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
python parallel / asynchronous execution memorandum
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Python pywin32 (win32com) Excel operation memorandum
[Python] A memorandum of beautiful soup4
python dict object memorandum (mysterious document)
Run mruby with Python or Blender
Which is better, PyPy or Python?
Git & Github & python & VScode Personal memorandum
PIL (Python Imaging Library) installation memorandum
ABC memorandum [ABC158 C --Tax Increase] (Python)
[Python] return A [or / and] B