Error with DOCplex CP objective function expression

40 views Asked by At

I am trying to replicate a CPLEX ILOG objective function in DOcplex but keep running into some errors. I am not too sure on how the type_of_next function() works with indexing, making it difficult for me to debug the code.

CPLEX ILOG CODE: dexpr int totDistance = (sum(j in Jobs, v in Vehicles) Dist[j.id][typeOfNext(seqVeh[v], itvJ2V[j][v], j.id, j.id)]) ;

DOcplex CODE: totDistance = DARP_cp.sum(distance_matrix[i][DARP_cp.type_of_next(x[k], zeta[(i,k)], i, i)] for i in range(nrows) for k in range(1, no_vehicles + 1)) Error Message:

Traceback (most recent call last):

File "C:\Users\bsraf3\OneDrive - Loughborough University\CPLEX TUTORIALS\DOcplex Tutorials\DARP\DARP_CP.py", line 115, in for i in range(nrows) for k in range(1, no_vehicles + 1))

File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\cp\modeler.py", line 567, in sum_of arr = build_cpo_expr(x)

File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\cp\expression.py", line 2390, in build_cpo_expr return build_cpo_expr_array(val)

File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\cp\expression.py", line 2420, in build_cpo_expr_array cpval = _CPO_VALUES_FROM_PYTHON.get_or_create('array', val, normalize_value, _create_cpo_array_expr)

File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\cp\expression.py", line 2300, in get_or_create kval = kbldr(pexpr)

File "C:\ProgramData\Anaconda3\lib\site-packages\docplex\cp\expression.py", line 2409, in normalize_value return _CacheKeyTuple(val)

File "C:\Users\bsraf3\OneDrive - Loughborough University\CPLEX TUTORIALS\DOcplex Tutorials\DARP\DARP_CP.py", line 115, in for i in range(nrows) for k in range(1, no_vehicles + 1))

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

The idea was to have an objective function expression which could later be added to the model. that is:** DARP_cp.add(DARP_cp.minimize(totDistance))**

1

There are 1 answers

0
Chris Bracchi On

It is not possible to put a decision expression into "[]" in Python.

Try to use "element" instead.

Maybe something like that:

DARP_cp.sum(element(DARP_cp.type_of_next(x[k], zeta[(i,k)], i, i), distance_matrix[i]) for i in range(nrows) for k in range(1, no_vehicles + 1))

I hope this helps