我正在使用cplex做一个优化模型,以优化饮食结构。现在我想添加一个额外的变量/KPI,它是模型中其他KPI的总和。然而,这个总和是一个有最大值的分数,这意味着当每个KPI超过某个上限时,总和不应该考虑超过这个值。分数只是对每种食物成分的衡量,因此,上限不是对模型的约束,只是计算分数的限制,所以应该不会影响模型的结果。最后,我想使总分最小化。
我曾尝试用mdl.sum对每个指标进行求和,并将其添加到一个列表中,然后我试图在超过上限的情况下用另一个列表中的值来替换求和。见下面的例子。
# Decision variables, limited to be >= Food.qmin and <= Food.qmax
ftype = mdl.integer_vartype if ints else mdl.continuous_vartype
qty = mdl.var_dict(foods, ftype, lb=lambda f: f.qmin,ub=lambda f: f.qmax, name=lambda f:
"q_%s" % f.name)
# Limit range of nutrients, and mark them as KPIs
for c in constraints:
amount = mdl.sum(qty[f] * food_constraints[f.name, c.name] for f in foods)
mdl.add_range(c.qmin, amount, c.qmax)
mdl.add_kpi(amount, publish_name="Total %s" % c.name)
# add sum of indicators with max values:
score1 = mdl.sum(qty[f] * f.veg for f in foods)
score2 = mdl.sum(qty[f] * f.fruit for f in foods)
score3 = mdl.sum(qty[f] * f.fish for f in foods)
# add sum of indicators without max values:
score4 = mdl.sum(qty[f] * f.sugar for f in foods)
score5 = mdl.sum(qty[f] * f.fats for f in foods)
Score_sum_A = [score1, score2, score3]
max_scores = [10.6, 3.3, 9.7]
# cap values of Score_sum_A to max scores:
for i in range(len(max_scores)):
if Score_sum_A[i] >= max_scores[i]:
Score_sum_A[i] = max_scores[i]
else:
Score_sum_A[i] = Score_sum_A[i]
Score_sum_B = [score4, score5]
total_score_sum = sum(Score_sum_A + Score_sum_B)
mdl.add_kpi(total_score_sum , 'Total food score')
mdl.minimize(total_score_sum)
然而,当运行该模型时,我得到了错误。"TypeError:不能将线性约束转换为布尔值",这是因为if语句的原因。因此,似乎模型不能在模型内部操作这种if语句。
有谁知道,在cplex中是否有可能添加这样一个带有最大值的指标,而不限制模型结果?
如果有任何提示,我将非常感激。