Search code examples
pythoncplexdocplex

Duplicate variable name warning in Python


I have developed a code for an optimization problem in Python and defined a binary variable in the model. Then I used the Docplex module to solve the model through the Cplex engine. I've mentioned a part of the code as follows.

from docplex.mp.model import Model
model = Model(name='Logistics')

T = 3
A = 3
I = [3200, 2600, 2530]

LI = [[[model.binary_var(name="LI" + str(t) + str(i) + str(a)) for a in
range(A)] for i in range(I[t])] for t in range(T)]

After running the code, I got an optimal solution depicted below.

  • Model: Reverse Logistics
  • number of variables: 24990
    • binary=24990, integer=0, continuous=0
  • number of constraints: 0
    • linear=0
  • parameters: defaults
  • objective: maximize
  • problem type is: MILP
  • profit: 2499000.0

But I got some warning messages once I tried to run the code for A = 11. As you can see, I've mentioned some of these warning messages. These messages appeared before the optimal solution.

. . . . . . Warning: Duplicate variable name: LI225110 already used for docplex.mp.Var(type=B,name='LI225110')

Warning: Duplicate variable name: LI225210 already used for docplex.mp.Var(type=B,name='LI225210')

  • Model: Reverse Logistics
  • number of variables: 91630
    • binary=91630, integer=0, continuous=0
  • number of constraints: 0
    • linear=0
  • parameters: defaults
  • objective: maximize
  • problem type is: MILP
  • profit: 9163000.0

I want to know what these warnings are for and how should I fix them. I need your help! Thank you!


Solution

  • The names should probably be unique.

    A simple solution could be to add the t, i and a variable into the name:

    LI = [[[model.binary_var(name=f"LI_{t}_{i}_{a}") for ...