from pulp import *
import pandas as pd
import numpy as np
pd.read_excel('Example.xlsx', encoding='latin-1')
prob = pulp.LpProblem('Performance', pulp.LpMaximize)
#### Create Decision Variables:
decision_variables = []
for rownum, row in data.iterrows():
variable = str('x' + str(rownum))
variable = pulp.LpVariable(str(variable), lowBound= row['D']*0.7,
upBound= row['D']*1.3, cat='Continuous')
decision_variables.append(variable)
#### Define Objective Function
total_cost = ""
for rownum, row in data.iterrows():
for i, variable in enumerate(decision_variables):
if rownum == i:
formula = variable * row['C'] * row['F'] / row['D']
total_cost += formula
prob += total_cost
print("Optimization Function: " + str(total_cost))
#### Define Constraints
problem_spend = ""
for rownum, row in data.iterrows():
for i, variable in enumerate(decision_variables):
if rownum == i:
formula = variable * variable * row['C'] * row['F'] * row['E'] / row['D']
problem_spend += formula
prob += (total_spend == problem_spend)
[] 在运行#### Define约束部分后得到以下错误:'TypeError:非常数表达式不能相乘。这可能是因为我的约束条件包括非线性变量。
我的Objective函数是线性的:公式:Maximize [Variable * constant]
我的约束是二次方的:公式:[变量*变量*常数==常数值]
我是PULP的新手,并且遇到此错误遇到的困难。有什么方法可以使用CVXPy或其他方法解决?
答案 0 :(得分:0)
纸浆无法制定或解决QP问题。我建议您使用CVXpy,Gurobi或Cplex。或重新设计问题以使用线性约束