0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python】PuLPを用いた最適化

Posted at

PuLPの概要とサンプルコードの実行

PuLPの概要

PuLPはPythonで実装された混合整数線形計画問題(MILP; Mixed Integer Linear Programming)などを解くにあたって用いられるライブラリです。

PuLPのインストール

下記のコマンドを実行することでPyPIからPuLPをダウンロードすることができます。

$  pip install PuLP

PuLPのサンプルコードの実行

PuLPのインストール確認にあたって下記のコードを実行します。

from pulp import *


x = LpVariable("x", 0, 3)
y = LpVariable("y", cat="Binary")


prob = LpProblem("myProblem", LpMinimize)
prob += x + y <= 2
prob += -4*x + y


status = prob.solve()
status = prob.solve(GLPK(msg = 0))

print(LpStatus[status])
print(value(x))

・実行結果

Result - Optimal solution found

Objective value:                -8.00000000
Enumerated nodes:               0
Total iterations:               0
Time (CPU seconds):             0.00
Time (Wallclock seconds):       0.00

Option for printingOptions changed from normal to all
Total time (CPU seconds):       0.00   (Wallclock seconds):       0.00

Optimal
2.0

上記の問題では下記のような最適化問題について解いています。

\begin{align}
\mathrm{Objective} \quad &: -4x+y \quad \longrightarrow \quad \mathrm{Minimize} \\
\mathrm{Constraint} \quad &: x+y \leq 2, \, 0 \leq x \leq 3
\end{align}

この問題の最適解はx=2,y=0のとき-4x+y=-8であるので、最適な結果の出力が得られていることが確認できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?