[英]How to build a correct linking constraint in Pyomo - Mixed Integer Linear program?
[英]How to specify a multiplication in the objective function of a Sage mixed integer linear program?
我想在Sage中将此表示为混合整数线性程序的目标函数
但是当我键入:
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中混合整数线性程序的目标函数不支持乘法吗?
发现我自己的错误,我仅有的变量是x
。 c
不是 决策变量 。 因此,应将其指定为矩阵。 此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) ))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.