简体   繁体   English

如何根据公式在LPP纸浆中设置LpVariable和目标函数?

[英]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 使用以下线性规划问题(LPP)方程式既可以实现兴趣也可以实现人气

在此处输入图片说明

using pulp package in python3.7. 在python3.7中使用纸浆包装。

I have 4 lists 我有4个清单

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

POPULARITY = [4,8,12,16,20] 热门度= [4,8,12,16,20]

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

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

and 2 variable values as 和2个变量值

e=0.5 ; e = 0.5; e may take (0 or 1 or 0.5) e可能取(0或1或0.5)

budget=20 预算= 20

and

i=0 to n ; i = 0至n; n is length of the list n是列表的长度

means, the summation want to perform for all list values. 表示要对所有列表值执行求和。

Here, if e==0 means Interest will 0 ; 在这里,如果e == 0表示兴趣将为0; if e==1 means Popularity will 0 ; 如果e == 1表示人气将为0; if e==0.5 means Interest and Popularity will be consider for Max Value 如果e == 0.5,则表示将考虑“兴趣和人气”作为最大值

Also xi takes 0 or 1; xi也取0或1; if xi==1 then the user will be consider else if xi==0 then the user will not be consider. 如果xi == 1,则将考虑用户;如果xi == 0,则将不考虑用户。

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 现在我收到2个错误,因为

1) line 714, in addInPlace for e in other: 1)在其他的e的addInPlace中的第714行:

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 2)第23行,概率为+ = lpSum(INTEREST中i的INTERS [i] e)+ lpSum(POPULARITY中i的POPULARITY [i] (1-e)))IndexError:列表索引超出范围

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. 如果我正确理解了您的需求,那么您的python变量ebudget将被视为线性程序的参数常量

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. 它应返回以下内容,即,使用这些特定参数,只会选择最后一个用户作为收录对象-但此决定会改变-例如,如果将预算增加到100,则将选择所有用户。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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