2

Excelソルバーモデルをpythonパルプ構文に変換するのが難しいと思っています。 私のモデルでは、OT変数の合計を最小化する目的で、各部門のHCおよびOT変数を最適化しています。この制約により、HC変数の合計は92以下になり、総生産量(以下のスプレッドシートの=E2*C2*D2 + F2*C2)は部門ごとの要件(以下のExcelスプレッドシートの「入力」列)を満たしている必要があります。以下に示すExcelソルバーの定式化は完全に機能します。ExcelソルバーソリューションをPythonパルプに変換

  1. 問題は、どのように私は(:F6)エクセルF7 = SUM(F2に)パルプで私の目的関数を書くことができますか?

  2. 制約E7 < = 92
  3. 制約G2:G6> = B2:B6
  4. Iは、2つの決定変数HCOTを有します。以下のPythonコードでは、私は1つの変数しか作成しませんでした。

enter image description here

ソルバー

enter image description here

import pulp 
import numpy as np 
import pandas as pd 

idx = [0, 1, 2, 3, 4] 

d = {'Dept': pd.Series(['Receiving', 'Picking', 'PPicking', 'QC', 'Packing'], index=idx), 
    'Target': pd.Series([61,94,32,63,116], index=idx), 
    'Hrs/day': pd.Series([7.75, 7.75, 7.75, 7.75, 7.75], index=idx), 
    'Prod': pd.Series([11733, 13011, 2715, 13682, 14194], index=idx), 
    'HC': pd.Series([24,18,6,28,16], index=idx), 
    'OT': pd.Series([0,0,42,0,0], index=idx)} 

df = pd.DataFrame(d) 

# Create variables and model 
x = pulp.LpVariable.dicts("x", df.index, lowBound=0) 
mod = pulp.LpProblem("OTReduction", pulp.LpMinimize) 

# Objective function 
mod += sum(df['OT']) 


# Lower and upper bounds: 
for idx in df.index: 
    mod += x[idx] <= df['Input'][idx] 


# Total HC value should be less than or equal to 92 
mod += sum([x[idx] for idx in df.index]) <= 92 


# Solve model 
mod.solve() 

# Output solution 
for idx in df.index: 
    print idx, x[idx].value() 


# Expected answer 
# HC, OT 
# 19, 35.795 
# 18, 0 
# 11, 0 
# 28, 0 
# ---------------- 
# 92, 35.795 -> **note:** SUM(HC), SUM(OT) 

答えて

3

した後、あなたの投稿パルプ・コードを持ついくつかの問題があります前に、 。

1組の変数、xを宣言するだけですが、あなたは2組、すなわち、HCとOTをExcel表記で使用しています。あなたは、変数2つの別々のセットを宣言し、それらを適切に名前を付ける必要があります:あなたはmod += sum(df['OT'])として目的を追加すると、エラーが発生したモデルにデータフレームの列を追加しようとしている

HC = pulp.LpVariable.dicts("HC", df.index, lowBound=0) 
OT = pulp.LpVariable.dicts("OT", df.index, lowBound=0) 

。代わりに、あなたが達成することができOT変数の合計を追加したい:

mod += sum([OT[idx] for idx in df.index]) 

あなたが制約x[idx] <= df['Input'][idx]を追加すると、あなたはx変数は、上の入力データで囲まれていることを必要としています。しかし、現実には、より複雑な制約があります.Excelコードでは、入力欄の下限がE2*C2*D2 + F2*C2になります。ここにあなたの制約は、同じロジックを示すべきである:

import pulp 
import pandas as pd 

# Problem data 
idx = [0, 1, 2, 3, 4] 
d = {'Dept': pd.Series(['Receiving', 'Picking', 'PPicking', 'QC', 'Packing'], index=idx), 
    'Target': pd.Series([61,94,32,63,116], index=idx), 
    'Hrs/day': pd.Series([7.75, 7.75, 7.75, 7.75, 7.75], index=idx), 
    'Prod': pd.Series([11346, 13011, 2715, 13682, 14194], index=idx)} 
df = pd.DataFrame(d) 

# Create variables and model                         
HC = pulp.LpVariable.dicts("HC", df.index, lowBound=0) 
OT = pulp.LpVariable.dicts("OT", df.index, lowBound=0) 
mod = pulp.LpProblem("OTReduction", pulp.LpMinimize) 

# Objective function                           
mod += sum([OT[idx] for idx in df.index]) 

# Lower and upper bounds:                          
for idx in df.index: 
    mod += df['Target'][idx] * df['Hrs/day'][idx] * HC[idx] + df['Target'][idx] * OT[idx] >= df['Prod'][idx] 

# Total HC value should be less than or equal to 92                   
mod += sum([HC[idx] for idx in df.index]) <= 92 

# Solve model                             
mod.solve() 

# Output solution                            
for idx in df.index: 
    print(idx, HC[idx].value(), OT[idx].value()) 
# 0 24.0 0.0 
# 1 13.241236 35.795316 
# 2 10.947581 0.0 
# 3 28.022529 0.0 
# 4 15.788654 0.0 
:すべて一緒にこれを置く

for idx in df.index: 
    mod += df['Target'][idx] * df['Hrs/day'][idx] * HC[idx] + df['Target'][idx] * OT[idx] >= df['Prod'][idx] 

は、所望の出力を生成します

関連する問題