1

This is the Docplex code

from docplex.mp.model import Model

car=[1, 2, 3]
myorder=[391, 392, 393, 448, 449, 450]

mdl = Model(name='planning')
Assignment = mdl.binary_var_matrix(myorder, car, name='Assignment')


CarLoaded = mdl.integer_var_dict(car, name='CarLoaded')
for f in car:
    CarLoaded[f] = mdl.sum(Assignment[o, f] * 20 for o in myorder)

CarUtilized = mdl.binary_var_dict(car, name='CarUtilized')
for f in car:
    mdl.add_constraint(
        mdl.if_then(CarLoaded[f] / 123 >= 0.8, CarUtilized[f] == 1))

LoadSum = mdl.sum(CarUtilized[f] * CarLoaded[f] / 30 for f in car)
mdl.maximize(LoadSum)

And it gives exception

Traceback (most recent call last):
  File "D:\Dev\my_test.py", line 17, in <module>
    mdl.if_then(CarLoaded[f] / 123 >= 0.8, CarUtilized[f] == 1))
  File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\mp\model.py", line 3971, in if_then
    StaticTypeChecker.typecheck_discrete_constraint(logger=self, ct=if_ct, msg='Model.if_then()')
  File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\mp\sttck.py", line 66, in typecheck_discrete_constraint
    logger.fatal('{0}, {1!s} is not discrete', msg, ct)
  File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\mp\model.py", line 1078, in fatal
    self._error_handler.fatal(msg, args)
  File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\mp\error_handler.py", line 210, in fatal
    raise DOcplexException(resolved_message)
docplex.mp.utils.DOcplexException: Model.if_then(), 0.163Assignment_391_1+0.163Assignment_392_1+0.163Assignment_393_1+0.163Assignment_448_1+0.163Assignment_449_1+0.163Assignment_450_1 >= 0.8 is not discrete

May I know what is the problem and how should I rectify it?

william007
  • 17,375
  • 25
  • 118
  • 194

1 Answers1

1

Change

for f in car:
    mdl.add_constraint(
        mdl.if_then(CarLoaded[f] / 123 >= 0.8, CarUtilized[f] == 1))

into

for f in car:
    mdl.add_constraint(
        mdl.if_then(CarLoaded[f]  >= math.ceil(0.8*123), CarUtilized[f] == 1))

and your model will work fine

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Thanks Alex, is that due to only for if..then we need discrete constraints? and do you know why do we need that for if..then and not others? – william007 Jul 22 '22 at 08:20
  • The negation of a constraint with continuous decision variable is not as trivial as with integer ones. So if you want to do the same as https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooifthen.py you should use indicator https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooindicator.py and write https://github.com/AlexFleischerParis/zoodocplex/blob/master/zooifthencontinuous.py – Alex Fleischer Jul 22 '22 at 10:55