[Python] Docplex: KeyError: (-1, 0) when attempting to add a constraint that makes the first...

Discussão em 'Python' iniciado por Stack, Março 31, 2025.

  1. Stack

    Stack Membro Participativo

    Part of my model states that the initial value of a time series should always equal the last. When modelling this for just one time series, the constraint is added no problem.

    m.add_constraint(var_1[-1] == var_1[0])


    But I am trying to add it to multiple time series with the following:

    var_1 = m.continuous_var_dict([(i, j) for i in timesteps for j in timeseries], name = 'var_1 ')

    m.add_constraints(var_1[-1, j] == var_1[0, j] for j in series)


    But this time, I run into the error:

    File "...\docplex\mp\model.py", line 4190, in add_constraints
    return self._lfactory._new_constraint_block1(cts)

    File "...\docplex\mp\mfactory.py", line 972, in _new_constraint_block1
    ctseq = list(cts)

    File "...\main.py", line 324, in <genexpr>
    m.add_constraints(var_1[-1, j] == var_1[0, j] for j in series)

    KeyError: (-1, 0)


    Could anyone offer any advice? All the best!

    Continue reading...

Compartilhe esta Página