0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python 最適化ライブラリ Pulp

Posted at

目的

Python実践データ分析100本ノックを読んで、最適化ライブラリのPulpに興味が湧いたので、備忘録もかねて

#達成したいこと
整数をとる変数を使った最適化の簡単に行いたい
もちろん、For文を使った総当たりなら簡単にかけるし、一つづつパラメータを振りながら探ることもできるが、時間がかかるし、拘束条件の設定が面倒だったりする

#用いるライブラリ Pulpとortoolpy
Anacondaでコンソールから
conda install -c conda-forge pulp
pip install ortoolpy
でインストールできる

#サンプル

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
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?