Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  DOcplexException: Expecting iterable

    Posted 07/06/22 05:39 AM
    Dear All,
    Good day to you.

    I have an error: DOcplexException: Expecting iterable for my coding below.
    However, when I tried to look-up for the available resources, it mentioned to add for l in range(Total_P) after the constraint statement and it still did not work.
    Anyone could suggest any solution, please? Thank you in advance.

    ###​

    mdl = Model(name='Scheduling')inf = docplex.cp.expression.INFINITY

    bigM= 1000000

     

    url = None

    key = None

     

    Total_P = 8 

    Total_T = 6 

     

    r1 = 100 

    r2 = 200 

     

    v = 1

    w = 1

     

    places = ['p1','p2','p3','p4','p5','p6','p7','p8']

    h = [v+w,w+r1,0,v+w,w+r2,0,w+v,0] 

    h = np.array(h)

    M0 = np.array([0,0,1,0,0,1,0,1]) 

     

    numbers = range(math.factorial(Total_T))

    K = [number for number in range(math.factorial(Total_T))] 

    Total_K = len(K)

     

    T = [i for i in range(Total_T)]

    P = [l for l in range(Total_P)]

     

    # Decision var (continuous var)

    x = np.empty((Total_T),dtype = object)

    x = x.tolist() 

    for k in range(Total_K):

      for i in range(Total_T):

        x_prev = x[:].copy()

        xi = x_prev

        xj = x 

     

    xj_temp = np.empty((Total_P), dtype = object)

    xj_temp = xj_temp.tolist()

     

    xi_temp = np.empty((Total_P), dtype = object)

    xi_temp = xi_temp.tolist()

     

    #PTN Index Rule

    for a in range(Total_T):

      if xj[a] != None:

        xj_temp[0] = xj[0]

        xj_temp[1] = xj[1]

        xj_temp[2] = xj[2]

        xj_temp[3] = xj[2]

        xj_temp[4] = xj[3]

        xj_temp[5] = xj[4]

        xj_temp[6] = xj[4]

        xj_temp[7] = xj[1] + xj[5]

        xj_temp

     

      if xi[a] !=  None:

     

        xi_temp[0] = xi[0]

        xi_temp[1] = xi[1]

        xi_temp[2] = xi[2]

        xi_temp[3] = xi[2]

        xi_temp[4] = xi[3]

        xi_temp[5] = xi[4]

        xi_temp[6] = xi[4]

        xi_temp[7] = xi[1] + xi[5]

        xi_temp

     

    #Decision variable

    for k in range(Total_K):

      for l in range(Total_P):

        xj_temp = mdl.continuous_var_list(keys= Total_P, lb= 0, ub= inf, name='xj' + str(k+1))

        xi_temp = mdl.continuous_var_list(keys= Total_P, lb= 0, ub= inf, name='xi' + str(k+1))

     

    # Decision var (continuous var)

    obj_lambda = np.empty((Total_T),dtype = object)

    if obj_lambda.any() is not None:

      value_obj_lambda = sum(obj_lambda)

     

    for k in range(Total_K):

      for l in range(Total_P):

        obj_lambda = mdl.continuous_var_list(keys= Total_P, lb= 0, ub= inf, name='obj_lambda' + str(k+1))

     

    #objective function

    mdl.minimize(obj_lambda[l])

     

    #constraint 1 

    for k in range(Total_K):

      for l in range(Total_P):

        mdl.add_constraints(xj_temp[l] - xi_temp[l] >= h[l] - M0[l]*obj_lambda[l]) 

     

    ---------------------------------------------------------------------------

    DOcplexException                          Traceback (most recent call last)

    <ipython-input-105-60b2680185c2> in <module>()

          2 for k in range(Total_K):

          3   for l in range(Total_P):

    ----> 4     mdl.add_constraints(xj_temp[l] - xi_temp[l] >= h[l] - M0[l]*obj_lambda[l])

          5              #mdl.add_constraints(xj_temp - xi_temp >= h - M0*obj_lambda)

     

    3 frames

    /usr/local/lib/python3.7/dist-packages/docplex/mp/error_handler.py in fatal(self, msg, args)

        208         resolved_message = resolve_pattern(msg, args)

        209         docplex_error_stop_here()

    --> 210         raise DOcplexException(resolved_message)

        211

        212     def fatal_limits_exceeded(self, nb_vars, nb_constraints):

     

    DOcplexException: Expecting iterable, got: xj720_0-xi720_0 >= 2

     ###




    ------------------------------
    Nicholas Nicholas
    ------------------------------

    #DecisionOptimization


  • 2.  RE: DOcplexException: Expecting iterable

    Posted 07/06/22 05:52 AM
    Dear Nicholas,

    The issue is that you are using the statement `mdl.add_constraints` whose purpose is to add a list of constraints (hence, the argument must be an iterable).
    You may just fix this by using instead `mdl.add_constraint` (singular), or reformulating as follows:

    mdl.add_constraints([xj_temp[l] - xi_temp[l] >= h[l] - M0[l] * obj_lambda[l] for l in range(Total_P) for k in range(Total_K)]

    Kind regards,
    Hugues

    ------------------------------
    Hugues Juille
    ------------------------------



  • 3.  RE: DOcplexException: Expecting iterable

    Posted 07/06/22 08:39 AM
    Thank you so much, Hugues. It works now.

    ------------------------------
    Nicholas Nicholas
    ------------------------------