IBMDecisionOptimization / docplex-examples

These samples demonstrate how to use the DOcplex library to model and solve optimization problems.
https://ibmdecisionoptimization.github.io/
Apache License 2.0
392 stars 228 forks source link

Error while writing optimization constraint using cplex.mp.model module #56

Open HengChheang opened 3 years ago

HengChheang commented 3 years ago

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 @vlkong

vberaudi commented 3 years ago

@HengChheang sum(s[k=t-j+2] from k to t) is not clear to me. Isn't it something like:

sums = { (t,j) : clsp.sum(s_indicator[k-j+2] for k in range(j,t))  for t in period_list for j in range(1,t+1)}
m_8 = clsp.add_constraints( p_indicator[t,j] >= (s_indicator[t-j+1] - sums[t,j]) for t in period_list for j in range(1,t+1) )
HengChheang commented 3 years ago

@vberaudi It is actually P[t,j] >= s[t-j+1] - sum(s[t-j+2] from t-j+2 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 )