0
1

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②)

Posted at

いろいろ見てると、python-lulp ではなく python-MIP の方が良いとの記述を見かけたので調べてみた。

python3.8(32) では使用できないようなので思い切って、python3.8(64)に入れ替えてました。ついでに、VSCに変更しました。

sample.py
# coding: UTF-8
import sys

def GenPlan():
    try:
        Output = [100, 150, 0, 300, 0, 400, 200]
        Before = 200
        After = 100
        MaxGen = 300
        MinGen = 0
        MAXHld = 500

        # 最適計算
        from mip import Model, minimize, xsum, INTEGER, OptimizationStatus

        problem = Model()

        #製造量
        make = [problem.add_var('make%d'%i, lb = MinGen, ub = MaxGen,  var_type=INTEGER)  for i in range(0,7,1)]
        hold = [problem.add_var('hold%d'%i, lb = 0, ub = MAXHld,  var_type=INTEGER)  for i in range(0,7,1)]

        #目的関数
        problem.objective = minimize(xsum(make))

        #等式制約
        problem += Before + make[0] - Output[0] - hold[0] == 0
        for i in range(1,7,1):
            problem += hold[i-1] + make[i] - Output[i] - hold[i] == 0

        #不等式制約
        for i in range(0,6,1):
            problem += hold[i] >= Output[i+1]
        problem += hold[6] >= After

        problem.optimize()
        print(problem.status == OptimizationStatus.OPTIMAL)
        print(problem.objective_value)
        print([int(problem.vars['hold%d'%i].x)  for i in range(0,7,1)])
        print([int(problem.vars['make%d'%i].x)  for i in range(0,7,1)])

    except:
        print(__name__)
# main
if __name__ == "__main__":
    GenPlan()

image.png

上側が貯蔵量、下側が製造量

Lulpの場合と最小値は同じですが、説明変数の値が異なることが分かります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?