如何将lpsum转换为lpconstraint?

发布于 2025-02-10 21:04:42 字数 548 浏览 2 评论 0原文

在纸浆中,我有形式的约束,

lpSum([decision_vars[an_id][item] for item in a_vector]) == count_req[an_id], f'constraint_{user_id}'

我想将其转换为使用lpconstraint作为制作这种约束弹性的垫脚石。 IE lpconstraint(...)。makeelasticsubproblem(...)

LpConstraint(
    e=pl.lpSum([decision_vars[an_id][item] for item in a_vector]), 
    sense=LpConstraintEQ,
    rhs=count_req[an_id],
    name=f'constraint_{an_id}'
)

这些等效吗?

是否有一些更干净的示例或文档将lpsum转换为lpconstraint

In Pulp I have constraints of the form,

lpSum([decision_vars[an_id][item] for item in a_vector]) == count_req[an_id], f'constraint_{user_id}'

and I want to convert this to use LpConstraint as a stepping stone to making this constraint elastic. i.e. LpConstraint(...).makeElasticSubProblem(...)

LpConstraint(
    e=pl.lpSum([decision_vars[an_id][item] for item in a_vector]), 
    sense=LpConstraintEQ,
    rhs=count_req[an_id],
    name=f'constraint_{an_id}'
)

Are these equivalent?

Is there some cleaner example or documentation for converting an lpSum to an LpConstraint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2025-02-17 21:04:42

也许不是您要寻找的答案,但我建议不要使用纸浆。 cvxpy 更易于学习和使用(免费变量而不是绑定到模型的变量),并允许您轻松扩展扩展您的模型to nonoflowl noreferrer“> nonlinear convex systems ,这给您带来了更多的意义。努力与获得的能力相比。

例如,在CVXPY中,您的问题可能会被抛弃为:

from collections import defaultdict

from cvxpy import cp

decision_vars = {}
for an_id in ids:
  for item in a_vector:
    decision_vars[an_id][item] = cp.Variable(pos=True)

constraints = []
for an_id in ids:
  my_sum = sum(x for x in decision_vars[an_id].values())
  constraints.append(count_req[an_id]*lower_proportion <= my_sum)
  constraints.append(my_sum <= count_req[an_id]*upper_proportion)

problem = cp.Problem(cp.Minimize(OBJECTIVE_HERE), constraints)
optimal_value = problem.solve()
for an_id in ids:
  for item in a_vector:
    print(f"{an_id} {item} {decision_vars[an_id][item].value}")

请注意,使用自由变量意味着我们可以使用标准的Python构造,例如字典,总和和比较运算符来构建我们的问题。当然,您必须手动构建弹性约束,但这并不具有挑战性。

It's maybe not the answer you're looking for, but I recommend against using PuLP. cvxpy is easier to learn and use (free variables instead of variables bound to models) and allows you to easily extend your models to nonlinear convex systems, which gives you much more bang for your buck in terms of effort put into learning versus capability obtained.

For instance, in cvxpy, your problem might be cast as:

from collections import defaultdict

from cvxpy import cp

decision_vars = {}
for an_id in ids:
  for item in a_vector:
    decision_vars[an_id][item] = cp.Variable(pos=True)

constraints = []
for an_id in ids:
  my_sum = sum(x for x in decision_vars[an_id].values())
  constraints.append(count_req[an_id]*lower_proportion <= my_sum)
  constraints.append(my_sum <= count_req[an_id]*upper_proportion)

problem = cp.Problem(cp.Minimize(OBJECTIVE_HERE), constraints)
optimal_value = problem.solve()
for an_id in ids:
  for item in a_vector:
    print(f"{an_id} {item} {decision_vars[an_id][item].value}")

Note that the use of free variables means that we can use standard Python constructs like dictionaries, sums, and comparison operators to build our problem. Sure, you have to construct the elastic constraint manually, but that's not challenging.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文