2016-09-07 72 views
4

我必須採用他們使用PuLP程序包的現有腳本。我需要知道下列行的結果如何:瞭解Python/PuLP代碼的片段

unit = ["one", "two", "three"] 
time = range(10) 

status=LpVariable.dicts("status",[(f,g) for f in unit for g in time],0,1,LpBinary) 

鍵/值的外觀如何?

status["one"] = [0,1,1,1...]? 

非常感謝您的幫助!

+0

以下是['LpVariable.dicts']的文檔(http://www.coin-or.org/PuLP/pulp.html#pulp.LpVariable.dicts)。該代碼的哪一部分是你特別困惑的,列表理解? – CoryKramer

+0

嘿!謝謝您的回覆。我查看了文檔 - 但不幸的是,我不確定「狀態」是什麼樣子的(文檔中也沒有結果的例子) – Matias

+0

你的意思是狀態,而不是「狀態」,因爲後者只是一個字符串,右鍵? – Dschoni

回答

2
from pulp import * 
unit = ["one", "two"] 
time = range(2) 

status=LpVariable.dicts("status",[(f,g) for f in unit for g in time],0,1,LpBinary) 

引出

>>> status 

{('two', 1): status_('two',_1), 
('two', 2): status_('two',_2), 
('one', 2): status_('one',_2), 
('one', 0): status_('one',_0), 
('one', 1): status_('one',_1), 
('two', 0): status_('two',_0)} 

因此,不存在與關鍵 「一」 的條目。

+0

非常感謝您的回覆!還有一個問題:那個「0,1,LpBinary」是什麼意思?值必須是二進制文件? – Matias

+0

如上所述,字典(名稱,索引,lowBound = None,upBound = None,cat = 0,indexStart = [])。與貓 - 此變量的類別,整型,二進制或連續(默認)。 – Dschoni

+0

status_unit = [status [「one」,i] .value()for my time]給我[None,None] - >這是否意味着該值(例如「status _(」two「,_ 1)爲0或1? – Matias