# Decision Optimization

View Only

## Issue with docplex syntax definition #### Abbas OmidiMon December 06, 2021 09:00 AM #### Vincent BeraudierMon December 06, 2021 09:20 AM • #### 1.  Issue with docplex syntax definition

Posted Mon December 06, 2021 09:00 AM
Dear support team,

I am trying to write a simple optimization problem (generalized assignment problem) by using docplex on the google colab. As I am very new to use this, I have some issues with defining its syntax to read the problem data. The MP model is as follows:

``````# Set
machines = ["m1", "m2"];

# Data
b = [13, 11];
c = [[9, 2],[1, 2],[3, 8]];
a = [[6, 8],[7, 5],[9, 6]];

# create the variables
mdl = Model('GAP')
idx_x = [(i,j) for i in tasks for j in machines]
x = mdl.binary_var_dict(idx_x, name="x");

# MP
mdl.minimize(mdl.sum(x[i,j]*c[i,j] for i in tasks for j in machines));

mdl.solve()​``````

When I run the engine, it shows the following error:

``````TypeError                                 Traceback (most recent call last)
<ipython-input-83-21876a9e517b> in <module>()
17
18 # MP
---> 19 mdl.minimize(mdl.sum(x[i,j]*c[i,j] for i in tasks for j in machines));
20 mdl.add_constraints(mdl.sum(x[i,j] for j in machines) == 1 for i in tasks);
21 mdl.add_constraints(mdl.sum(x[i,j]*a[i,j] for i in tasks) <= b[j] for j in machines);

3 frames
<ipython-input-83-21876a9e517b> in <genexpr>(.0)
17
18 # MP
---> 19 mdl.minimize(mdl.sum(x[i,j]*c[i,j] for i in tasks for j in machines));
20 mdl.add_constraints(mdl.sum(x[i,j] for j in machines) == 1 for i in tasks);
21 mdl.add_constraints(mdl.sum(x[i,j]*a[i,j] for i in tasks) <= b[j] for j in machines);

TypeError: list indices must be integers or slices, not tuple``````
I tried different syntaxes based on the existing example in this like, but I could not get any reasonable output.
Would you say how can I fix that, please?

Best regards

------------------------------
Abbas Omidi
------------------------------

• #### 2.  RE: Issue with docplex syntax definition

Posted Mon December 06, 2021 09:20 AM
Hello, it comes from the way you access your data in a,b and c.
Theses are integer arrays, that is are indexed from 0 to N, not by as in OPL where they are indexed by objects.
So you need to access them via their indexes, such as

`mdl.minimize(mdl.sum(x[(i,j)]*c[i1][j1] for i1, i in enumerate(tasks) for j1, j in enumerate(machines)));mdl.add_constraints(mdl.sum(x[(i,j)] for j in machines) == 1 for i in tasks);mdl.add_constraints(mdl.sum(x[(i,j)]*a[i1][j1] for i1, i in enumerate(tasks)) <= b[j1] for j1,j in enumerate(machines));`
for example or you need to use dictionaries to store a,b,c with something like

``b = {"m1": 13, "m2: 11};​​``

------------------------------
Vincent Beraudier
------------------------------

• #### 3.  RE: Issue with docplex syntax definition

Posted Mon December 06, 2021 03:09 PM
Dear Vincent,

Many thanks for your useful answer. Both mentioned methods have worked well.

Regards

------------------------------
Abbas Omidi
------------------------------