【问题标题】:PuLP: casting LpVariable or LpAffineExpression to integerPuLP:将 LpVariable 或 LpAffineExpression 转换为整数
【发布时间】:2017-03-14 09:01:30
【问题描述】:

在我的优化问题中,我有一个条件,即特定组中的项目数量(LpInteger)不得超过项目总数的百分比。为此,我编写了以下代码:

total = lpSum([num[i].varValue for i in ind])
for d in length:
    # get list of items that satisfy the conditional
    items_length_d = list(compress(items,[work[i]==work_group[d] for i in items])) 
    # use that list to calculate the amount of items in the group (an item can occur multiple times) 
    amount[d] = lpSum([num[dl] for dl in items_length_d])
    max_d[d] = total*perc_max[d] + 1 
    min_d[d] = total*perc_min[d] - 1 
    prob += max_d[d] >= amount[d]
    prob += min_d[d] <= amount[d] 

这种方法的问题是我的最大值和最小值变成了浮点数(LpContinuous)。这反过来使解决方案infeasible

我怎样才能确保每个 max_d 和 min_d 值都是整数?最好,我还想将 max_d 向上取整,同时截断 min_d。

编辑

我通过将total = lpSum([num[i].varValue for i in ind]) 更改为total = lpSum([num[i] for i in ind]) 解决了infeasible 解决方案的问题。但是,最小值和最大值仍然是浮点数。如果有人知道如何将这些转换为整数,仍然会非常感谢您的回答。

【问题讨论】:

  • 这会是线性的吗?
  • @ErwinKalvelagen 问题是线性问题(最小化项目*工作)。这只是制约因素之一。 Afaik,这仍然满足线性问题。我可以想象,截断和限制条件可能会使其线性度降低。

标签: python casting linear-programming pulp coin-or-cbc


【解决方案1】:

您似乎误解了构建和解决线性规划问题的工作原理。

应该设置整个问题,然后解决并提取解决方案值。

在设置问题时,您无法获取变量的 LpVariable.varValue。

因此,对于分数约束,如果我们将组定义为 i /in G,然后将总数定义为 i /in T

我们得到 f 是所需的分数

如果重新排列这个方程。

所以在你的代码中

prob += perc_max[d] * lpSum([num[i] for i in ind]) <= lpSum([num[dl] for dl in items_length_d])

【讨论】:

  • 感谢您的澄清。我指定的约束与您的相似,除了我将它们分配给一个变量(以允许我在最后得到最小值、最大值和数量)。因此问题是我无法获取或设置值(更不用说强制转换了),因为我只是在那个时候定义了问题,而值还不存在?
  • yup 变量值在问题解决之前是未定义的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 2011-05-30
相关资源
最近更新 更多