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?

More than 5 years have passed since last update.

Gurobi風の最適化記述方法

Last updated at Posted at 2020-05-12

1. 環境設定

・pip install mypulpでライブラリをインストール

2. 実装方法

# ①最適化ライブラリを呼び出す
from mypulp import *

# ②モデルを定義する
model = Model("名前は何でも良い")

# ③変数を宣言する
X =model.addVar(lb=0,ub=GRB.INFINITY,vtype=C,name=‘’)
# C:Continuous,B:Binary,I:Inter

# ④モデルをアップデートする(変数宣言後)
model.update()
# gurobiでは必要だが、pulpでは不要

# ⑤制約を定義する
model.addConstr(左辺の式 ==  右辺の式, name=名前 ) 

# ⑥目的関数を定義する
model.setObjective(目的関数の式 , GRB.MINIMIZE)

# ⑦モデルを解く
model.optimize()

# ⑧結果を出力する
print('Opt. Value=', model.ObjVal)
print('x=', x.X)

# 最適化の結果をstatusに格納
status = model.Status()
# If status == GRB.Status.OPTIMAL:
#    ~~で処理


# 定式結果を確認する方法
model.write("名前.lp")

for v in model.getVars():
	print(v, v.X)#vに変数、v.Xに最適解が入る

for c in model.getConstr():
	print(c.ConstrName, c.Slack, c.Pi)
	#C.ConstrNameに制約名、c.Slackにスラック変数、
	#c.Piにラグランジュ乗数が入る

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?