-1

I have a minimize problem and its objective function need to get absolute of a linear equation. But when I run these code, it raise this error TypeError: bad operand type for abs(): 'LinearExpr'. How can I fix this?

I searched this error on Internet but I can't find any solutions

from docplex.mp.model import Model
model = Model()
x = model.integer_var_list(9,lb=0, name='x')
y = sum(var for var in x[0:9]) # ->output: x_0+x_1+x_2
obj = abs(y)
model.set_objective('min', obj)

1 Answers1

0

You should use model.abs() instead of abs().

Full example at https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooabs.py

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')

#absolute value of nbBus40 - nbBus30
mdl.add_constraint(mdl.abs(nbbus40-nbbus30)<=2)

mdl.minimize(nbbus40*500 + nbbus30*400)

mdl.solve(log_output=True,)



for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15