微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Python 中的 MIP 求解器 OR 工具的约束中使用 If else 语句

如何解决在 Python 中的 MIP 求解器 OR 工具的约束中使用 If else 语句

我正在使用 Python 的 OR 工具的 MIP 解算器,并且在声明约束时遇到了一个问题。有问题的约束,如下图所示,是关于雄性和雌性动物之间的比例(a):

constraint description

到目前为止,我尝试了两种方法来做到这一点:

for t in times:
  solver.Add(
      solver.Sum(
          if a in animais_male:
            [n_animals[(a,t)]*sell[(a,t)]
           elif a in animais_female:
             -proportion_max*n_animals[(a,t)]
             for a in animais
             ]) <= 0)

这个返回了这个错误

  File "<ipython-input-20-95a1db1c4418>",line 5
    if a in animals_male:
     ^
SyntaxError: invalid Syntax

我尝试的另一个解决方案是:

for t in dias_considerados:
  solver.Add(
      solver.Sum(
          [n_animals[(a,t)]
           for a in animals_male
           -proportion_max*n_animals[(a,t)]
            for a in animals_female
            ]) <= 0)

我收到了这个错误

TypeError                                 Traceback (most recent call last)
<ipython-input-21-2dfedfd19682> in <module>()
      4           [n_animals[(a,t)]
      5            for a in animals_male
----> 6            -proportion_max*n_animals[(a,t)]
      7             for a in animals_female
      8             ]) <= 0)

TypeError: 'SumArray' object is not iterable

n_animals[(a,t)] 是动物的数量,在图像中用 n_(a,t) 表示,sell[(a,t)] 是图像中的 I_(a,t) 二元决策变量。

因此,如果有人可以帮助我,我将不胜感激!如果我需要更好地解释我的情况,我可以这样做。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。