0
1

More than 3 years have passed since last update.

数理計画(python①)

Last updated at Posted at 2021-08-21

pythonで”EXCEL①”で記載したモデルで求解してみる。

【問題:条件】
  一週間の製造計画について考えます。(土曜日から金曜日)
  出荷は翌日朝から実施するため、前日に必要量を製造する。
  翌週のために金曜日の貯蔵量を守る。

  目的:製造量を最小化
  説明変数:製造量
  制約条件:
   ・製造量 ≦ 一日の最大製造量
   ・製造量 ≧ 一日の最小製造量
   ・貯蔵量 ≦ 貯蔵できる最大量
   ・貯蔵量 ≧ 翌日の出荷量
   ・最終貯蔵量 ≧ 翌週のための貯蔵量

  貯蔵量 = 前日の貯蓄量+製造量-出荷量
sample.py
# coding: UTF-8
import sys

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

        import pandas as pd

        index = [i for i in range(1,8,1)]

        # 最適計算
        import pulp

        problem = pulp.LpProblem('make_plan', pulp.LpMinimize) # 最小化

        # 変数
        X = pd.DataFrame(index=index)

        #製造量
        X['make'] = [pulp.LpVariable('make%d'%i, lowBound = MinGen, upBound = MaxGen,  cat="Integer")  for i in range(1,8,1)]
        X['hold'] = [pulp.LpVariable('hold%d'%i, lowBound = 0, upBound = MAXHld,  cat="Integer")  for i in range(1,8,1)]


        #目的関数
        problem +=  pulp.lpSum([X.loc[i,'make'] for i in range(1,8,1)])

        #等式制約
        problem += Before + X.loc[1,'make'] - Output[1] - X.loc[1,'hold'] == 0
        for i in range(2,8,1):
            problem += X.loc[i-1,'hold'] + X.loc[i,'make'] - Output[i] - X.loc[i,'hold'] == 0

        #不等式制約
        for i in range(1,7,1):
            problem += X.loc[i,'hold'] >= Output[i+1]
        problem += X.loc[7,'hold'] >= After


        status = problem.solve(pulp.PULP_CBC_CMD(msg = False))
        for i in range(1,7,1):
            print(int(X.loc[i,'hold'].value())  ,end=",")
        print(int(X.loc[7,'hold'].value()))
        for i in range(1,7,1):
            print(int(X.loc[i,'make'].value())  ,end=",")
        print(int(X.loc[7,'make'].value()))

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

image.png

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

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

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