0

My goal is to write the following constraint with the doxplex.mp.model with Python API: P[t,j] >= s[t-j+1] - sum(s[k=t-j+2] from k to t) for t = 1,....,N, j = 1,...,t

my code.

from docplex.mp.model import Model
clsp = Model(name = 'capacitated lot sizing problem')
no_of_period = 8
period_list = [t for t in range(1, no_of_period+1)]
s_indicator = clsp.binary_var_dict(period_list, name = 's_indicator')
p_indicator = clsp.binary_var_matrix(period_list, period_list, name = 'p_indicator')
m_8 = clsp.add_constraints(p_indicator[t,j] >= s_indicator[t-j+1] - clsp.sum(s_indicator[t-j+2]  for j in range(1,t+1))  
                           for j in t for t in period_list )

Output: Keyerror 0 Any help would be appreciated

Philippe Couronne
  • 826
  • 1
  • 5
  • 6
DasPlaies
  • 5
  • 1
  • Just to clarify, the sum is from t-j+2 to t. What happens when j >= t+2? – Josh W Aug 20 '21 at 02:12
  • So you mean i should use the If.. condition? – DasPlaies Aug 20 '21 at 05:03
  • I'm only asking what it is you want. As with all programming, if you use an index that is outside of the range of acceptable you will get a key error. – Josh W Aug 20 '21 at 13:35
  • Ohh what I want is Example: p[1,1] >= s1 - s2 as t = 1, j = 1,,t which is j also = 1 and so on. But i got an error and struggle to continue. – DasPlaies Aug 20 '21 at 14:15

1 Answers1

0

Your code extract is not correct Python (for j in t). I had to modify it to make it run:

m_8 = clsp.add_constraints\
    (p_indicator[t,j] >= s_indicator[t-j+1] - clsp.sum(s_indicator[t-j+2]  for j in range(1,t+1))
                           for j in period_list for t in period_list )

Which gives ma a KeyError: 9 exception.

To fix this, remember that binary_var_matrix creates a Python dictionary, whose keys are the first argument, here period_list, ranging from 1 to 8.

To investigate this I wrote this small code to investigate all possible keys generated by your code:

ke = 0
for t in period_list:
    for j in period_list:
        ix = t-j+2
        if ix not in period_list:
            ke += 1
            print(f"** Key error[{ke}], t={t},j={j}, t-j+2={t-j+2}")

Prints 22 key errors, for example t=8, j=1 computes (t-j+2)=9 which is outside the dictionary key set.

To summarize: check your indices w.r.t the keys of the variable dictionaries in your model.

Philippe Couronne
  • 826
  • 1
  • 5
  • 6
  • The code above is really helpful but I wonder if I could the use the following code, too?for t in period_list: for j in range(1,t+1): s_indicator[9] = 0 m_09 = clsp.add_constraint(p_indicator[t,j] >= s_indicator[t-j+1] - clsp.sum(s_indicator[t-j+2] for j in range(1,t+1))) – DasPlaies Aug 30 '21 at 11:21
  • I cannot read your code snippet with two `=` on same line. I suggest you build your constraint with custom code for each value of `j`: build lhs, build set of indices for the sum, then build the rhs and add each constraint `lhs==rhs` one at a time. – Philippe Couronne Aug 30 '21 at 11:29
  • Really appreciate your help! That was really helpful. – DasPlaies Aug 30 '21 at 15:41