简体   繁体   中英

How to set LpVariable and Objective Function in pulp for LPP as per the formula?

I want to calculate the Maximised value of the particular user based on his Interest | Popularity | both Interest and Popularity using following Linear Programming Problem(LPP) equation

在此处输入图片说明

using pulp package in python3.7.

I have 4 lists

INTEREST = [5,10,15,20,25]

POPULARITY = [4,8,12,16,20]

USER = [1,2,3,4,5]

cost = [2,4,6,8,10]

and 2 variable values as

e=0.5 ; e may take (0 or 1 or 0.5)

budget=20

and

i=0 to n ; n is length of the list

means, the summation want to perform for all list values.

Here, if e==0 means Interest will 0 ; if e==1 means Popularity will 0 ; if e==0.5 means Interest and Popularity will be consider for Max Value

Also xi takes 0 or 1; if xi==1 then the user will be consider else if xi==0 then the user will not be consider.

and my pulp code as below

from pulp import  *

INTEREST = [5,10,15,20,25]
POPULARITY = [4,8,12,16,20]
USER = [1,2,3,4,5]
cost = [2,4,6,8,10]

e=0.5    
budget=10

#PROBLEM VARIABLE
prob = LpProblem("MaxValue", LpMaximize)

# DECISION VARIABLE
int_vars = LpVariable.dicts("Interest", INTEREST,0,4,LpContinuous)

pop_vars = LpVariable.dicts("Popularity", 
           POPULARITY,0,4,LpContinuous)

user_vars = LpVariable.dicts("User", 
           USER,0,4,LpBinary)

#OBJECTIVE fUNCTION
prob += lpSum(USER(i)((INTEREST[i]*e for i in INTEREST) + 
        (POPULARITY[i]*(1-e)  for i in POPULARITY)))

#  CONSTRAINTS

prob += USER(i)cost(i) <= budget

#SOLVE
prob.solve()
print("Status : ",LpStatus[prob.status])

# PRINT OPTIMAL SOLUTION
print("The Max Value = ",value(prob.objective))

Now I am getting 2 errors as

1) line 714, in addInPlace for e in other:

2) line 23, in prob += lpSum(INTEREST[i] e for i in INTEREST) + lpSum(POPULARITY[i] (1-e) for i in POPULARITY) IndexError: list index out of range

What I did wrong in my code. Guide me to resolve this problem. Thanks in advance.

I think I finally understand what you are trying to achieve. I think the problem with your description is to do with terminology. In a linear program we reserve the term variable for those variables which we want to be selected or chosen as part of the optimisation.

If I understand your needs correctly your python variables e and budget would be considered parameters or constants of the linear program.

I believe this does what you want:

from pulp import  *
import numpy as np

INTEREST = [5,10,15,20,25]
POPULARITY = [4,8,12,16,20]
COST = [2,4,6,8,10]
N = len(COST)
set_user = range(N)

e=0.5    
budget=10

#PROBLEM VARIABLE
prob = LpProblem("MaxValue", LpMaximize)

# DECISION VARIABLE
x = LpVariable.dicts("user_selected", set_user, 0, 1, LpBinary)

# OBJECTIVE fUNCTION
prob += lpSum([x[i]*(INTEREST[i]*e + POPULARITY[i]*(1-e)) for i in set_user])

# CONSTRAINTS
prob += lpSum([x[i]*COST[i] for i in set_user]) <= budget

#SOLVE
prob.solve()
print("Status : ",LpStatus[prob.status])

# PRINT OPTIMAL SOLUTION
print("The Max Value = ",value(prob.objective))

# Show which users selected
x_soln = np.array([x[i].varValue for i in set_user])
print("user_vars: ")
print(x_soln)

Which should return the following, ie with these particular parameters only the last user is selected for inclusion - but this decision will change - for example if you increase the budget to 100 all users will be selected.

Status :  Optimal
The Max Value =  22.5
user_vars:
[0. 0. 0. 0. 1.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM