pythonpandasdictionaryconstraintspulp

Creating a constraint in a project selection problem


That is the dataframe I'm working on my "project selection" problem:

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

My objective is:

Maximize the "Return"

My constraints are:

That's what I tried so far:

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)

The result I got:

Project_CGH1 = 6.4549114
Project_Eolico1 = 84.982196
Project_Solar1 = 0.60163909

The result I need is: which projects (Project_name) would I choose given the constraints? Something like:

Projects chosen   Investment
Solar1            228376120
Wind1             6467237
Wind3             377494250
CGH2              60671402
PCH4              300677487
Biomassa3         61835863

Solution

  • Assuming that you want to limit the ratio of those sectors, to the percentages of total investment that are selected, then your missing constraints should be as follows:

    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
    

    And similarly for the other sectors you want to limit percentages of.