I want to represent this in Sage as an objective function of a mixed integer linear program
But when I type:
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) ))
I get this error:
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'
Strangely this seems to work fine ( +
instead of *
):
p.set_objective(sum(c[(i,j)]+x[(i,j)] for i in range(3) for j in range(4) ))
Any clues? Is multiplication not supported in Sage for objective functions of mixed integer linear programs?
Found my own mistake, the only variable I have is x
. c
is not a decision variable . So it should be specificed as a matrix. This c[i,j]*x[i,j]
doesn't result in an error:
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) ))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.