如何在Sage混合整数线性程序的目标函数中指定乘法?

时间:2018-10-07 05:56:00

标签: python linear-programming sage mixed-integer-programming

我想在Sage中将其表示为mixed integer linear program

的目标函数

sum i sum j of c i j x i j

但是当我键入:

p = MixedIntegerLinearProgram()  
x = p.new_variable(integer=True, nonnegative=True)
c = p.new_variable(integer=True,nonnegative=True)
p.set_objective(sum(c[(i,j)]*x[(i,j)] for i in range(3) for j in range(4) ))

我收到此错误:

Error in lines 1-1
Traceback (most recent call last):
  File "/usr/local/sage/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute
    flags=compile_flags) in namespace, locals
  File "", line 1, in <module>
  File "/usr/local/sage/local/lib/python2.7/site-packages/sage/misc/functional.py", line 577, in symbolic_sum
    return sum(expression, *args)
  File "", line 1, in <genexpr>
  File "sage/structure/element.pyx", line 1532, in sage.structure.element.Element.__mul__ (build/cythonized/sage/structure/element.c:12188)
    return (<Element>left)._mul_(right)
  File "sage/structure/element.pyx", line 1576, in sage.structure.element.Element._mul_ (build/cythonized/sage/structure/element.c:12602)
    raise bin_op_exception('*', self, other)
TypeError: unsupported operand parent(s) for *: 'Linear functions over Real Double Field' and 'Linear functions over Real Double Field'

奇怪的是,这似乎可以正常工作(用+代替*):

p.set_objective(sum(c[(i,j)]+x[(i,j)] for i in range(3) for j in range(4) ))

有任何线索吗?混合整数线性程序的目标函数在Sage中是否不支持乘法

1 个答案:

答案 0 :(得分:0)

发现了我自己的错误,我仅有的变量是xc 不是决策变量。因此,应将其指定为矩阵。此c[i,j]*x[i,j]不会导致错误:

x = p.new_variable(integer=True, nonnegative=True)
c=matrix([[10,15],[24,25],[87,12]])
p.set_objective(sum(c[i,j]*x[i,j] for i in range(3) for j in range(2) ))