访问lps的元素

2024-05-16 00:23:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

     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的每个元素。怎样才能以最好的方式做到这一点


Tags: thenameinforpartneriszipyear