python - 在项目选择问题中创建约束
问题描述
这就是我正在处理“项目选择”问题的数据框:
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"
我的限制是:
- 总“投资”限额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)
我得到的结果:
Project_CGH1 = 6.4549114
Project_Eolico1 = 84.982196
Project_Solar1 = 0.60163909
我需要的结果是:在给定约束的情况下,我会选择哪些项目(Project_name)? 就像是:
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
同样对于您想要限制百分比的其他部门。
推荐阅读
- docker - 无法通过 localhost 连接到 kubernetes pod 中的其他容器
- django - Django 胖模型更新字段参考模型
- pandas - Pandas - 如何为除我指定的列之外的所有列指定默认数据类型?
- java - 无法运行 uiautomatorviewer
- node.js - 哈巴狗不显示本地图像
- excel - 找不到有关使用用户选择的单元格的信息
- java - Micronaut 中的异常处理
- dart - 如何转换流
流式传输 - google-cloud-platform - 查找 Google 控制台访问权限?
- java - VM 初始化期间发生错误无法为对象堆 Android Studio 保留足够的空间