Доступ к элементам LPDictionary PulP - PullRequest
0 голосов
/ 12 февраля 2020

У меня есть следующий фрейм данных:

     Year_Month Name    Commit  Consumption Cost         Max   Min
      2019_01   B   506579.7943 401985.76   707494.9376  0.85  0.1
      2019_01   F   6143.392467 401985.76   317568.7504  0.65  0.1
      2019_01   O   27353.38501 401985.76   534641.0608  0.75  0.1
      2019_01   A   70173.42869 401985.76   586899.2096  0.40  0.1
      2019_02   B   489501.8602 363647.02   640018.7552  0.85  0.1
      2019_02   F   6290.480008 363647.02   287281.1458  0.65  0.2
      2019_02   O   10211.49053 363647.02   483650.5366  0.75  0.1
      2019_02   A   57892.78808 363647.02   530924.6492  0.40  0.2
      2019_03   B   560754.2634 417201.97   734275.4672  0.65  0.1
      2019_03   F   6392.157406 417201.97   329589.5563  0.10  0.05
      2019_03   O   11516.44708 417201.97   554878.6201  0.75  0.28
      2019_03   A   68380.52444 417201.97   609114.8762  0.60  0.30

Я хочу запустить оптимизацию для каждого месяца отдельно, и я использую следующий код:

 Monthly_Opt = pd.DataFrame({
"Year_Month": ["2019_01","2019_02","2019_03"]})

cols = ["Year_Month","Name","Cost","Max","Commit"]
YearMonthList=list(zip(Monthly_Opt['Year_Month']))

AllMonths_Optimization=[]
for i,year_month in enumerate(YearMonthList):
    subset=Data_Vols_Jan2019_Dec2019[(Data_Vols_Jan2019_Dec2019['Year_Month']==year_month[0])   

     ]
    subset=subset[cols]

# Create the 'prob' variable to contain the problem data
prob = LpProblem("TheProblem",LpMinimize)



#create data variables and dictionary
Partners = list(subset['Name'])
commit = dict(zip(Partners,subset['Commit']))
totcost = dict(zip(Partners,subset['Cost']))
network = dict(zip(Partners,subset['Max']))

Partner_vars =LpVariable.dicts("Partner",Partners,lowBound=0,cat='Continuous')
#Building the LP problem by adding the main objective function.
prob += lpSum([totcost[i]*Partner_vars[i] for i in Partners])


#Adding constraints
prob += lpSum([1 * Partner_vars[f] for f in Partners])==1



# The problem data is written to an .lp file
prob.writeLP("Model.lp")

# The problem is solved using PuLP's choice of Solver
prob.solve()

# The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])


# Each of the variables is printed with it's resolved optimum value

for v in prob.variables():
    save=[v.name, "=", v.varValue]
    AllMonths_Optimization.append(save)

 s=pd.DataFrame(AllMonths_Optimization)
 s

Я хочу добавить Еще несколько ограничений для каждого из имен, например:

  prob += B <= 0.85
  prob += F <= 0.65

и так далее для других имен

Для этого мне потребуется доступ к каждому элементу Lpdictionary. Как это сделать наилучшим образом?

1 Ответ

1 голос
/ 12 февраля 2020

Я использовал следующие строки кода для доступа к каждому элементу:

    for f in Partners:
         prob += Partner_vars[f] <= Max[f]
         prob += Partner_vars[f] <= Min[f]
...