我在用纸浆解决LP问题。我想在我的模型中添加约束。最友好的方式是:
model += lhs == rhs当我们想使用apply方法在pandas.DataFrame中添加约束时,因为lambda方法中没有+=符号,所以我们需要这样做:
df['variable'].apply(lambda x: model.__iadd__(lhs==rhs))现在,我不想直接使用__iadd__()方法,而是想为此创建一个函数。我想要实现的是这样的东西:
`expression is our constraint`: lhs == rhs
def add_to_model(model, expression):
return model.__iadd__(expression)举个例子:
def add_to_model(model, x==0)
return model.__iadd__(x==0)因为在上面的函数中,expression变量将被视为Boolean,这并不能达到目的。对于如何做到这一点,有什么建议吗?
发布于 2022-02-22 21:59:54
我怀疑您应该使用LpProblem.addConstraint()来完成这个任务。
https://github.com/coin-or/pulp/blob/d1ab490c156a7932aca44cfd346ec754fdda96bc/pulp/pulp.py#L1643
https://stackoverflow.com/questions/71196892
复制相似问题