这是我正在处理“项目选择”问题的数据框:
$args = array(
'timeout' => 10,
'sslverify' => false
);
我的目标是:
Return Sector Investment Project_name
0.290 Solar 228376120 Solar1
0.07 Solar 70021891 Solar2
0.25 Wind 6467237 Eolico1
0.3 Wind 417713440 Eolico2
0.16 Wind 377494250 Eolico3
0.28 Wind 230345712 Eolico4
0.29 CGHPCHBIO 35476862 CGH1
0.26 CGHPCHBIO 60671402 CGH2
0.07 CGHPCHBIO 349544333 PCH1
0.12 CGHPCHBIO 425442985 PCH2
0.29 CGHPCHBIO 66292734 PCH3
0.15 CGHPCHBIO 300677487 PCH4
0.25 CGHPCHBIO 409144798 Biomassa1
0.19 CGHPCHBIO 184123496 Biomassa2
0.08 CGHPCHBIO 61835863 Biomassa3
我的约束是:
那是我到目前为止所尝试的:
Maximize the "Return"
我得到的结果:
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)
我需要的结果是:给定约束,我将选择哪个项目(Project_name)? 像这样:
Project_CGH1 = 6.4549114
Project_Eolico1 = 84.982196
Project_Solar1 = 0.60163909
答案 0 :(得分:0)
欢迎您!
假设您想将这些部门的比例限制为所选总投资的百分比,那么缺少的约束应该如下:
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
对于其他要限制百分比的部门也是如此。