2016-10-18 21 views
2

私は次の問題を最小限に抑えようとしています: ラップトップ、電話、タブレットの生産には在庫(1アイテムあたり1ドル) 10ドル/時間)。特定の月のガジェットの最小数の制約として機能する、満たされるべき需要スキームがあります。これに加えて、最大20000時間の生産と月間3,000時間の労働時間があります。PuLP最小化されたLpVariableのUpperBound値を表示

問題はpython/pulpが私に(1つの例外を除いて)LpVariablesに挿入されているすべての上限値であることを示しています:最小限のコストではありません!

from pulp import * 


# Define the LP problem: minimize costs 
prob = LpProblem("Minimize costs of production and inventory", LpMinimize) 


# Demand schemes 
demand_laptops = [75, 125, 1000, 1500, 1000, 500, 1250, 1500, 1000, 500, 500, 400, 300]   # Demand laptops 
demand_phones = [120, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000] # Demand phones 
demand_tablets = [50, 2000, 3000, 2000, 3000, 4000, 5000, 2000, 3000, 4000, 5000, 4000, 5000]  # Demand tablets 


# Defining variables: normal production hours and overtime production hours. 
production_laptop = {x: LpVariable("Production hours for laptop in month {}".format(x), 0, 20000) 
        for x in range(1, 13)} 
production_phone = {x: LpVariable("Production hours for phone in month {}".format(x), 0, 20000) 
        for x in range(1, 13)} 
production_tablet = {x: LpVariable("Production hours for tablet in month {}".format(x), 0, 20000) 
        for x in range(1, 13)} 

overtime_laptop = {x: LpVariable("Overtime hours for laptop in month {}".format(x), 0, 3000) 
        for x in range(1, 13)} 
overtime_phone = {x: LpVariable("Overtime hours for phone in month {}".format(x), 0, 3000) 
        for x in range(1, 13)} 
overtime_tablet = {x: LpVariable("Overtime hours for tablet in month {}".format(x), 0, 3000) 
        for x in range(1, 13)} 


# defining a list of names for the inventory of products 
inventory_laptops = {x: "inventory of laptops in month {}".format(x) 
        for x in range(1, 13)} 
inventory_phones = {x: "inventory of phones in month {}".format(x) 
        for x in range(1, 13)} 
inventory_tables = {x: "inventory of tablets in month {}".format(x) 
        for x in range(1, 13)} 


# Inventory (to be minimized) 
inventory_laptops[1] = demand_laptops[0] + (1/5) * production_laptop[1] + (1/5) * overtime_laptop[1] - demand_laptops[1] 
inventory_laptops[2] = inventory_laptops[1] + (1/5) * production_laptop[2] + (1/5) * overtime_laptop[2] - demand_laptops[2] 
inventory_laptops[3] = inventory_laptops[2] + (1/5) * production_laptop[3] + (1/5) * overtime_laptop[3] - demand_laptops[3] 
inventory_laptops[4] = inventory_laptops[3] + (1/5) * production_laptop[4] + (1/5) * overtime_laptop[4] - demand_laptops[4] 
inventory_laptops[5] = inventory_laptops[4] + (1/5) * production_laptop[5] + (1/5) * overtime_laptop[5] - demand_laptops[5] 
inventory_laptops[6] = inventory_laptops[5] + (1/5) * production_laptop[6] + (1/5) * overtime_laptop[6] - demand_laptops[6] 
inventory_laptops[7] = inventory_laptops[6] + (1/5) * production_laptop[7] + (1/5) * overtime_laptop[7] - demand_laptops[7] 
inventory_laptops[8] = inventory_laptops[7] + (1/5) * production_laptop[8] + (1/5) * overtime_laptop[8] - demand_laptops[8] 
inventory_laptops[9] = inventory_laptops[8] + (1/5) * production_laptop[9] + (1/5) * overtime_laptop[9] - demand_laptops[9] 
inventory_laptops[10] = inventory_laptops[9] + (1/5) * production_laptop[10] + (1/5) * overtime_laptop[10] - demand_laptops[10] 
inventory_laptops[11] = inventory_laptops[10] + (1/5) * production_laptop[11] + (1/5) * overtime_laptop[11] - demand_laptops[11] 
inventory_laptops[12] = inventory_laptops[11] + (1/5) * production_laptop[12] + (1/5) * overtime_laptop[12] - demand_laptops[12] 

inventory_phones[1] = demand_phones[0] + (1/2) * production_phone[1] + (1/2) * overtime_phone[1] - demand_phones[1] 
inventory_phones[2] = inventory_phones[1] + (1/2) * production_phone[2] + (1/2) * overtime_phone[2] - demand_phones[2] 
inventory_phones[3] = inventory_phones[2] + (1/2) * production_phone[3] + (1/2) * overtime_phone[3] - demand_phones[3] 
inventory_phones[4] = inventory_phones[3] + (1/2) * production_phone[4] + (1/2) * overtime_phone[4] - demand_phones[4] 
inventory_phones[5] = inventory_phones[4] + (1/2) * production_phone[5] + (1/2) * overtime_phone[5] - demand_phones[5] 
inventory_phones[6] = inventory_phones[5] + (1/2) * production_phone[6] + (1/2) * overtime_phone[6] - demand_phones[6] 
inventory_phones[7] = inventory_phones[6] + (1/2) * production_phone[7] + (1/2) * overtime_phone[7] - demand_phones[7] 
inventory_phones[8] = inventory_phones[7] + (1/2) * production_phone[8] + (1/2) * overtime_phone[8] - demand_phones[8] 
inventory_phones[9] = inventory_phones[8] + (1/2) * production_phone[9] + (1/2) * overtime_phone[9] - demand_phones[9] 
inventory_phones[10] = inventory_phones[9] + (1/2) * production_phone[10] + (1/2) * overtime_phone[10] - demand_phones[10] 
inventory_phones[11] = inventory_phones[10] + (1/2) * production_phone[11] + (1/2) * overtime_phone[11] - demand_phones[11] 
inventory_phones[12] = inventory_phones[11] + (1/2) * production_phone[12] + (1/2) * overtime_phone[12] - demand_phones[12] 

inventory_tables[1] = demand_tablets[0] + (1/4) * production_tablet[1] + (1/4) * overtime_tablet[1] - demand_tablets[1] 
inventory_tables[2] = inventory_tables[1] + (1/4) * production_tablet[2] + (1/4) * overtime_tablet[2] - demand_tablets[2] 
inventory_tables[3] = inventory_tables[2] + (1/4) * production_tablet[3] + (1/4) * overtime_tablet[3] - demand_tablets[3] 
inventory_tables[4] = inventory_tables[3] + (1/4) * production_tablet[4] + (1/4) * overtime_tablet[4] - demand_tablets[4] 
inventory_tables[5] = inventory_tables[4] + (1/4) * production_tablet[5] + (1/4) * overtime_tablet[5] - demand_tablets[5] 
inventory_tables[6] = inventory_tables[5] + (1/4) * production_tablet[6] + (1/4) * overtime_tablet[6] - demand_tablets[6] 
inventory_tables[7] = inventory_tables[6] + (1/4) * production_tablet[7] + (1/4) * overtime_tablet[7] - demand_tablets[7] 
inventory_tables[8] = inventory_tables[7] + (1/4) * production_tablet[8] + (1/4) * overtime_tablet[8] - demand_tablets[8] 
inventory_tables[9] = inventory_tables[8] + (1/4) * production_tablet[9] + (1/4) * overtime_tablet[9] - demand_tablets[9] 
inventory_tables[10] = inventory_tables[9] + (1/4) * production_tablet[10] + (1/4) * overtime_tablet[10] - demand_tablets[10] 
inventory_tables[11] = inventory_tables[10] + (1/4) * production_tablet[11] + (1/4) * overtime_tablet[11] - demand_tablets[11] 
inventory_tables[12] = inventory_tables[11] + (1/4) * production_tablet[12] + (1/4) * overtime_tablet[12] - demand_tablets[12] 


# Constraints to meet demand scheme 
prob += demand_laptops[0] + (1/5) * production_laptop[1] + (1/5) * overtime_laptop[1] >= demand_laptops[1] 
prob += inventory_laptops[1] + (1/5) * production_laptop[2] + (1/5) * overtime_laptop[2] >= demand_laptops[2] 
prob += inventory_laptops[2] + (1/5) * production_laptop[3] + (1/5) * overtime_laptop[3] >= demand_laptops[3] 
prob += inventory_laptops[3] + (1/5) * production_laptop[4] + (1/5) * overtime_laptop[4] >= demand_laptops[4] 
prob += inventory_laptops[4] + (1/5) * production_laptop[5] + (1/5) * overtime_laptop[5] >= demand_laptops[5] 
prob += inventory_laptops[5] + (1/5) * production_laptop[6] + (1/5) * overtime_laptop[6] >= demand_laptops[6] 
prob += inventory_laptops[6] + (1/5) * production_laptop[7] + (1/5) * overtime_laptop[7] >= demand_laptops[7] 
prob += inventory_laptops[7] + (1/5) * production_laptop[8] + (1/5) * overtime_laptop[8] >= demand_laptops[8] 
prob += inventory_laptops[8] + (1/5) * production_laptop[9] + (1/5) * overtime_laptop[9] >= demand_laptops[9] 
prob += inventory_laptops[9] + (1/5) * production_laptop[10] + (1/5) * overtime_laptop[10] >= demand_laptops[10] 
prob += inventory_laptops[10] + (1/5) * production_laptop[11] + (1/5) * overtime_laptop[11] >= demand_laptops[11] 
prob += inventory_laptops[11] + (1/5) * production_laptop[12] + (1/5) * overtime_laptop[12] >= demand_laptops[12] 

prob += demand_phones[0] + (1/2) * production_phone[1] + (1/2) * overtime_phone[1] >= demand_phones[1] 
prob += inventory_phones[1] + (1/2) * production_phone[2] + (1/2) * overtime_phone[2] >= demand_phones[2] 
prob += inventory_phones[2] + (1/2) * production_phone[3] + (1/2) * overtime_phone[3] >= demand_phones[3] 
prob += inventory_phones[3] + (1/2) * production_phone[4] + (1/2) * overtime_phone[4] >= demand_phones[4] 
prob += inventory_phones[4] + (1/2) * production_phone[5] + (1/2) * overtime_phone[5] >= demand_phones[5] 
prob += inventory_phones[5] + (1/2) * production_phone[6] + (1/2) * overtime_phone[6] >= demand_phones[6] 
prob += inventory_phones[6] + (1/2) * production_phone[7] + (1/2) * overtime_phone[7] >= demand_phones[7] 
prob += inventory_phones[7] + (1/2) * production_phone[8] + (1/2) * overtime_phone[8] >= demand_phones[8] 
prob += inventory_phones[8] + (1/2) * production_phone[9] + (1/2) * overtime_phone[9] >= demand_phones[9] 
prob += inventory_phones[9] + (1/2) * production_phone[10] + (1/2) * overtime_phone[10] >= demand_phones[10] 
prob += inventory_phones[10] + (1/2) * production_phone[11] + (1/2) * overtime_phone[11] >= demand_phones[11] 
prob += inventory_phones[11] + (1/2) * production_phone[12] + (1/2) * overtime_phone[12] >= demand_phones[12] 


prob += demand_tablets[0] + (1/4) * production_tablet[1] + (1/4) * overtime_tablet[1] >= demand_tablets[1] 
prob += inventory_phones[1] + (1/4) * production_tablet[2] + (1/4) * overtime_tablet[2] >= demand_tablets[2] 
prob += inventory_phones[2] + (1/4) * production_tablet[3] + (1/4) * overtime_tablet[3] >= demand_tablets[3] 
prob += inventory_phones[3] + (1/4) * production_tablet[4] + (1/4) * overtime_tablet[4] >= demand_tablets[4] 
prob += inventory_phones[4] + (1/4) * production_tablet[5] + (1/4) * overtime_tablet[5] >= demand_tablets[5] 
prob += inventory_phones[5] + (1/4) * production_tablet[6] + (1/4) * overtime_tablet[6] >= demand_tablets[6] 
prob += inventory_phones[6] + (1/4) * production_tablet[7] + (1/4) * overtime_tablet[7] >= demand_tablets[7] 
prob += inventory_phones[7] + (1/4) * production_tablet[8] + (1/4) * overtime_tablet[8] >= demand_tablets[8] 
prob += inventory_phones[8] + (1/4) * production_tablet[9] + (1/4) * overtime_tablet[9] >= demand_tablets[9] 
prob += inventory_phones[9] + (1/4) * production_tablet[10] + (1/4) * overtime_tablet[10] >= demand_tablets[10] 
prob += inventory_phones[10] + (1/4) * production_tablet[11] + (1/4) * overtime_tablet[11] >= demand_tablets[11] 
prob += inventory_phones[11] + (1/4) * production_tablet[12] + (1/4) * overtime_tablet[12] >= demand_tablets[12] 


# Objective function: inventory costs and overtime costs (10 per hour) 
prob += sum(inventory_laptops) + sum(inventory_phones) + sum(inventory_tables) + (10 * (sum(overtime_laptop) + sum(overtime_phone) + sum(overtime_tablet))) 

# Solve the problem 
prob.solve() 

print("Status:", LpStatus[prob.status]) 

for v in prob.variables(): 
    print(v.name, "=", v.varValue) 

print("total costs:", value(prob.objective)) 

これは私に次のような結果が得られます。

Status: Optimal 
Overtime_hours_for_laptop_in_month_1 = 3000.0 
Overtime_hours_for_laptop_in_month_10 = 3000.0 
Overtime_hours_for_laptop_in_month_11 = 3000.0 
Overtime_hours_for_laptop_in_month_12 = 3000.0 
Overtime_hours_for_laptop_in_month_2 = 3000.0 
Overtime_hours_for_laptop_in_month_3 = 3000.0 
Overtime_hours_for_laptop_in_month_4 = 3000.0 
Overtime_hours_for_laptop_in_month_5 = 3000.0 
Overtime_hours_for_laptop_in_month_6 = 3000.0 
Overtime_hours_for_laptop_in_month_7 = 3000.0 
Overtime_hours_for_laptop_in_month_8 = 3000.0 
Overtime_hours_for_laptop_in_month_9 = 3000.0 
Overtime_hours_for_phone_in_month_1 = 3000.0 
Overtime_hours_for_phone_in_month_10 = 3000.0 
Overtime_hours_for_phone_in_month_11 = 3000.0 
Overtime_hours_for_phone_in_month_12 = 3000.0 
Overtime_hours_for_phone_in_month_2 = 3000.0 
Overtime_hours_for_phone_in_month_3 = 3000.0 
Overtime_hours_for_phone_in_month_4 = 3000.0 
Overtime_hours_for_phone_in_month_5 = 3000.0 
Overtime_hours_for_phone_in_month_6 = 3000.0 
Overtime_hours_for_phone_in_month_7 = 3000.0 
Overtime_hours_for_phone_in_month_8 = 3000.0 
Overtime_hours_for_phone_in_month_9 = 3000.0 
Overtime_hours_for_tablet_in_month_1 = 0.0 
Overtime_hours_for_tablet_in_month_10 = 3000.0 
Overtime_hours_for_tablet_in_month_11 = 3000.0 
Overtime_hours_for_tablet_in_month_12 = 3000.0 
Overtime_hours_for_tablet_in_month_2 = 3000.0 
Overtime_hours_for_tablet_in_month_3 = 3000.0 
Overtime_hours_for_tablet_in_month_4 = 3000.0 
Overtime_hours_for_tablet_in_month_5 = 3000.0 
Overtime_hours_for_tablet_in_month_6 = 3000.0 
Overtime_hours_for_tablet_in_month_7 = 3000.0 
Overtime_hours_for_tablet_in_month_8 = 3000.0 
Overtime_hours_for_tablet_in_month_9 = 3000.0 
Production_hours_for_laptop_in_month_1 = 20000.0 
Production_hours_for_laptop_in_month_10 = 20000.0 
Production_hours_for_laptop_in_month_11 = 20000.0 
Production_hours_for_laptop_in_month_12 = 20000.0 
Production_hours_for_laptop_in_month_2 = 20000.0 
Production_hours_for_laptop_in_month_3 = 20000.0 
Production_hours_for_laptop_in_month_4 = 20000.0 
Production_hours_for_laptop_in_month_5 = 20000.0 
Production_hours_for_laptop_in_month_6 = 20000.0 
Production_hours_for_laptop_in_month_7 = 20000.0 
Production_hours_for_laptop_in_month_8 = 20000.0 
Production_hours_for_laptop_in_month_9 = 20000.0 
Production_hours_for_phone_in_month_1 = 20000.0 
Production_hours_for_phone_in_month_10 = 20000.0 
Production_hours_for_phone_in_month_11 = 20000.0 
Production_hours_for_phone_in_month_12 = 20000.0 
Production_hours_for_phone_in_month_2 = 20000.0 
Production_hours_for_phone_in_month_3 = 20000.0 
Production_hours_for_phone_in_month_4 = 20000.0 
Production_hours_for_phone_in_month_5 = 20000.0 
Production_hours_for_phone_in_month_6 = 20000.0 
Production_hours_for_phone_in_month_7 = 20000.0 
Production_hours_for_phone_in_month_8 = 20000.0 
Production_hours_for_phone_in_month_9 = 20000.0 
Production_hours_for_tablet_in_month_1 = 7800.0 
Production_hours_for_tablet_in_month_10 = 20000.0 
Production_hours_for_tablet_in_month_11 = 20000.0 
Production_hours_for_tablet_in_month_12 = 20000.0 
Production_hours_for_tablet_in_month_2 = 20000.0 
Production_hours_for_tablet_in_month_3 = 20000.0 
Production_hours_for_tablet_in_month_4 = 20000.0 
Production_hours_for_tablet_in_month_5 = 20000.0 
Production_hours_for_tablet_in_month_6 = 20000.0 
Production_hours_for_tablet_in_month_7 = 20000.0 
Production_hours_for_tablet_in_month_8 = 20000.0 
Production_hours_for_tablet_in_month_9 = 20000.0 
__dummy = None 
total costs: None 

誰かが私が間違っているのを教えすることはできますか?

答えて

3

いくつかの構文的な不具合があり、ソルバーが期待するものとは異なるモデルを取得します。さらに、私はいくつかの構文上の提案を指摘したいと思います。

どうしたのですか?

最初に、production変数は辞書として定義され、その値はLpVariablesです。このジョブ用に設計されたpulpデータ構造LpVariable.dictsを使用することをお勧めします。定義は次のようになります:これはあなたが数ヶ月(あるいは数週間後にコードを再利用する必要がある場合は多くのことができます良い習慣、次のとおりです。私は明示的に下限と上限を表し

production_laptop = LpVariable.dicts(
    "Production hours for laptop in month ", range(1, 13), 
    lowBound = 0, upBound = 20000) 

注意)。

以降では、変数名を表す辞書を定義します。変数はinventory_です。次に、これらのディクショナリの値を、変数と制約の組み合わせを指すように再定義します。

inventory_laptops = LpVariable.dicts(
    "inventory of laptops in month ", range(1,13), lowBound = 0) 

変数間の関係が存在する場合

は、あなたが制約でそれらを表現することができますので、我々は変数定義の段階でこのことを心配する必要はありません。私は以前のものとしてこれらの変数を定義します。

変数が定義された後、目的関数を定義する必要があることに注意してください。 sum(inventory_laptops)の代わりにsum(inventory_laptops[i] for i in range(1,13))がなければ、その定義は正しくありません。そうでなければ、という辞書でsumになります。

構文

DRY:あなたは簡単にループに合うことができる反復コードの多くを書きます。 not repeat yourselfを試してください。コードが不必要に長くて複雑で、エラーが発生しやすいからです。実際には、理由はあなたがすでに持っていたコピー・貼り付けコードであるかもしれないあなたのコードに誤りがあります:

prob += inventory_phones[1] + (1/4) * production_tablet[2] + (1/4) * overtime_tablet[2] >= demand_tablets[2] 
コピー・貼り付けたので、おそらく

と:制約の最後のブロックは、このような携帯電話やタブレットを、ミックスphonestabletsに置き換えます。あなたは簡単にも、あなたがしたい場合は、後でそれをトレースすることができるようにあなたは、あなたの制約名を与える追加の利点を持っている

for i in range(1,13): 
    prob += (demand_tablets[i - 1] if i == 1 else inventory_tables[i - 1]) + \ 
     (1/4) * production_tablet[i] + (1/4) * overtime_tablet[i] >= \ 
     demand_tablets[i], "demand tablets {}".format(i) 

としてこれらの行を書くことができます。

コメント:役に立つコメントを使用してください。これらは、があなたが実際に何を意図するのではなく、を記述するコメントです。あなたの変数がdemand_laptopsと呼ばれるとき、なぜ# Demand laptopsとコメントするのですか?

整合性:これは大きなものです。変数名に一貫性がないので、コードを扱うのに少し時間がかかります。demand_tablets, inventory_tables, overtime_tabletproduction_tabletはすべて非常によく似ていて混同しやすいです。より一貫した表記に固執してください。

行の長さ:必要ではありませんが、Python開発者は任意の行の長さを使用しません。良いIDE(私はPycharm、時にはSublime Textを使う)を使うようにしてください。通常のPythonの規則を使うように指示します(変数、関数などの名前付けにも使えます)。これにより、コードはPythonのように見えます。

数学最適化モデルのデバッグ:小さな問題では非常に便利な習慣ですが、ソルバに渡す式を印刷することです。これは多くのバグや問題を捕らえるのに役立ちます。たとえば、誤って実行された_dummyという変数を定義していることが明らかです。これはprob.writeLP("DebugThis.lp")で行われます。私はまた、より短い長さの変数を使用して、制約と目的関数で何が起こっているのか理解できるようにします。最後の言葉として、あなたのモデルの難しい数字を避けてみてください。現在のインスタンスの在庫コストは10ですが、将来はこれが変更される可能性があります(非割り当て環境ではに変更されます)。したがって、在庫コストのリストを定義し(各製品および/または期間ごとに1つ)、そのリストのみを更新する方がよい。これは、制約や変数が自動的に生成されるため、製品を追加したい場合に役立ちます。

改訂コードは、私はここにあなたのコードの作業バージョンを実装しました。それをオリジナルに近づけておくと、(あなた自身のために)わかりやすいように私は提案したものすべてを実装していませんでした。下の行をさらに改善するために、製品のリストを作成し、製品と月のペアを自動的に生成してみてください。これを行うにはいくつかの方法があり、おそらくthis exampleが役に立ちます。 (変数名/値をプリントアウトせず)

from pulp import * # I would not import the entire workspace - you might end up shadowing variables names that you need 

# Problem formulation starts here 
prob = LpProblem("Minimize costs of production and inventory", LpMinimize) 

# Problem data: Product Demands 
demand_laptops = [75, 125, 1000, 1500, 1000, 500, 1250, 1500, 1000, 500, 500, 400, 300] 
demand_phones = [120, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000] 
demand_tablets = [50, 2000, 3000, 2000, 3000, 4000, 5000, 2000, 3000, 4000, 5000, 4000, 5000]  


# Defining variables: normal production, overtime, and inventories 
production_laptop = LpVariable.dicts(
    "Production hours for laptop in month ", range(1, 13), 
    lowBound = 0, upBound = 20000) 
production_phone = LpVariable.dicts(
    "Production hours for phone in month ", range(1, 13), 
    lowBound = 0, upBound = 20000) 
production_tablet = LpVariable.dicts(
    "Production hours for tablet in month ", range(1, 13), 
    lowBound = 0, upBound = 20000) 

overtime_laptop = LpVariable.dicts(
    "Overtime hours for laptop in month ", range(1, 13), 
    lowBound = 0, upBound = 3000) 
overtime_phone = LpVariable.dicts(
    "Overtime hours for phone in month ", range(1, 13), 
    lowBound = 0, upBound = 3000) 
overtime_tablet = LpVariable.dicts(
    "Overtime hours for tablet in month ", range(1, 13), 
    lowBound = 0, upBound = 3000) 

inventory_laptops = LpVariable.dicts(
    "inventory of laptops in month ", range(1,13), lowBound = 0) 
inventory_phones = LpVariable.dicts(
    "inventory of phones in month ", range(1,13), lowBound = 0) 
inventory_tables = LpVariable.dicts(
    "inventory of tables in month ", range(1,13), lowBound = 0) 

# Objective function: inventory costs and overtime costs 
prob += (sum(
    inventory_laptops[i] + inventory_phones[i] + inventory_tables[i] 
     for i in range(1,13)) + \ 
    10 * sum(overtime_laptop[i] + overtime_phone[i] + overtime_tablet[i] 
     for i in range(1,13))) 

# Constraint definition part 
for i in range(1,13): 

    # Inventory definition for laptops, phones and tablets 
    prob += inventory_laptops[i] == (demand_laptops[i - 1] if i == 1 else \ 
    inventory_laptops[i - 1]) + \ 
    (1/5) * production_laptop[i] + (1/5) * overtime_laptop[i] - \ 
    demand_laptops[i], "inventory_laptops definition {}".format(i) 

    prob += inventory_phones[i] == (demand_phones[i - 1] if i == 1 else \ 
    inventory_phones[i - 1]) + \ 
    (1/2) * production_phone[i] + (1/2) * overtime_phone[i] - \ 
    demand_phones[i], "inventory_phones definition {}".format(i) 

    prob += inventory_tables[i] == (demand_tablets[i - 1] if i == 1 else \ 
    inventory_tables[i - 1]) + \ 
    (1/4) * production_tablet[i] + (1/4) * overtime_tablet[i] - \ 
    demand_tablets[i], "inventory_tables definition {}".format(i) 

    # Demand-covering constraints for laptops, phones, tablets 
    prob += (demand_laptops[i - 1] if i == 1 else inventory_laptops[i - 1]) + \ 
    (1/5) * production_laptop[i] + (1/5) * overtime_laptop[i] >= \ 
    demand_laptops[i], "demand laptops {}".format(i) 

    prob += (demand_phones[i - 1] if i == 1 else inventory_phones[i - 1]) + \ 
    (1/2) * production_phone[i] + (1/2) * overtime_phone[i] >= \ 
    demand_phones[i], "demand phones {}".format(i) 

    prob += (demand_tablets[i - 1] if i == 1 else inventory_tables[i - 1]) + \ 
    (1/4) * production_tablet[i] + (1/4) * overtime_tablet[i] >= \ 
    demand_tablets[i], "demand tablets {}".format(i) 

# Solve the problem 
prob.solve() 

# Take a look at what was solved 
prob.writeLP("SO40113557.lp") 

print("Status:", LpStatus[prob.status]) 

for v in prob.variables(): 
    print(v.name, "=", v.varValue) 

print("total costs:", value(prob.objective)) 

出力

('Status:', 'Optimal') 
('total costs:', 0.0) 

私はこのことができます願っています!

+1

これはこれと今後の問題に多大な影響を与えます。どうもありがとう! – Jeroen

関連する問題