Python 在项目选择问题中创建约束

Python 在项目选择问题中创建约束,python,pandas,dictionary,constraints,pulp,Python,Pandas,Dictionary,Constraints,Pulp,这就是我正在处理“项目选择”问题的数据框架: 我的目标是: Maximize the "Return" 我的限制是: 总“投资”限额为916000000 “太阳能行业”不能超过总投资的60% “风电行业”不得超过总投资的60%;及 “CGHPCHBIO部门”不能超过总投资的25% 这就是我迄今为止所尝试的: from pulp import * import pandas as pd import xlrd #First, we create a LP problem with the

这就是我正在处理“项目选择”问题的数据框架:

我的目标是:

Maximize the "Return"
我的限制是:

  • 总“投资”限额为916000000
  • “太阳能行业”不能超过总投资的60%
  • “风电行业”不得超过总投资的60%;及
  • “CGHPCHBIO部门”不能超过总投资的25%
这就是我迄今为止所尝试的:

from pulp import *
import pandas as pd
import xlrd

#First, we create a LP problem with the method LpProblem in PuLP
prob = LpProblem("Selecao de Projetos",LpMaximize)

#Read the first rows dataset in a Pandas DataFrame
df = pd.read_excel("df.xlsx", encoding = 'unicode_escape')

#Create a list of the projects names
projects = list(df['Project_name'])

#Create a dictionary of investments for all the projects
investments = dict(zip(projects,df['Investment']))

#Create a dictionay of sectors for all the projects
sectors = dict(zip(projects,df['Sector']))

#Create a dictionary of Returns for all the projects
returns = dict(zip(projects,df['Return']))

#Create a dictionary of projects with lower bound = 0 and category continuous
project_vars = LpVariable.dicts("Project",projects,lowBound =0,cat='Continuous')

#Built the LP problem by assing the main objective function
prob += lpSum([returns[i]*project_vars[i] for i in projects])

#Add the constraints
prob += lpSum([investments[f] * project_vars[f] for f in projects]) <= 916000000
prob += lpSum([investments[f] * project_vars[f] for f in projects if sectors[f]=="Solar"]) <= lpSum([investments[f] * project_vars[f] for f in projects])*0.6
prob += lpSum([investments[f] * project_vars[f] for f in projects if sectors[f]=="Wind"]) <= lpSum([investments[f] * project_vars[f] for f in projects])*0.6
prob += lpSum([investments[f] * project_vars[f] for f in projects if sectors[f]=="CGHPCHBIO"]) <= lpSum([investments[f] * project_vars[f] for f in projects])*0.25
prob.solve()

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

for v in prob.variables():
    if v.varValue>0:
        print(v.name, "=", v.varValue)
我需要的结果是:给定约束条件,我会选择哪些项目(项目名称)? 比如:

Projects chosen   Investment
Solar1            228376120
Wind1             6467237
Wind3             377494250
CGH2              60671402
PCH4              300677487
Biomassa3         61835863
欢迎来到SO

假设您希望将这些部门的比例限制为所选总投资的百分比,那么您缺少的约束应如下所示:

prob += lpSum([investments[f] * project_vars[f] for f in projects if sectors[f]=="Solar"]) <= lpSum([investments[f] * project_vars[f] for f in projects])*0.6

prob+=lpSum([investments[f]*project_vars[f]for f in projects if sections[f]==“Solar”])是否要查找满足限制的最高回报行(project)?
Projects chosen   Investment
Solar1            228376120
Wind1             6467237
Wind3             377494250
CGH2              60671402
PCH4              300677487
Biomassa3         61835863
prob += lpSum([investments[f] * project_vars[f] for f in projects if sectors[f]=="Solar"]) <= lpSum([investments[f] * project_vars[f] for f in projects])*0.6