Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  DOcplexException: Model.sum() expects numbers/variables/expressions

    Posted 07/06/22 08:43 AM
    Dear All,
    Good day to you.

    When I create a constraint by using Cplex library (python), I got error message: DOcplexException: Model.sum() expects numbers/variables/expressions, got: [1 0 0 0 0 0].
    Is there anyone could help me, please? Thank you in advance.

    Here is the python code.


    ###
    AT =np.array([[1,-1,0,0,0,0],
                 [0,1,-1,0,0,0],
                 [0,-1,1,0,0,0],
                 [0,0,1,-1,0,0],
                 [0,0,0,1,-1,0],
                 [0,0,0,-1,1,0],
                 [0,0,0,0,1,-1],
                 [-1,1,-1,1,-1,1]])

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

    ATZ = np.empty((Total_P), dtype = object)
    for l in range(Total_P):
      ATZ[t] = np.matmul(AT,Z[t])

    M = np.empty((Total_P),dtype = 
    object
    for k in range(Total_K):
      M_prev = M[:].copy() 

    for k in range(720):
      for l in range(8):
        if M.any() != None and M_prev.any() != None:
          mdl.add_constraint(M = M_prev + ATZ)

    /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: Model.sum() expects numbers/variables/expressions, got: [1 0 0 0 0 0]


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

    #DecisionOptimization


  • 2.  RE: DOcplexException: Model.sum() expects numbers/variables/expressions

    Posted 07/06/22 10:05 AM
    Dear Nicholas,
    I couldn't reproduce  your issue from the above code. There are no `mdl.sum(...)` constraint in it. Maybe a bad copy / paste.
    Anyway, I noticed an error in the last constraint: the `equivalence` constraint uses `==`. In your formulation, an assignment is used, which is not correct.

    Kind regards,
    Hugues

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



  • 3.  RE: DOcplexException: Model.sum() expects numbers/variables/expressions

    Posted 07/06/22 10:38 AM
    Dear Hugues,
    Apology for the wrong copy.
    What I typed for the constraint is as follows.


       
    mdl.add_constraint(mdl.sum(z[i]) for i in range(6) for k in range(720)) ==1)

    What do you think?

    Thank you.

    Best regards,
    Nicholas






  • 4.  RE: DOcplexException: Model.sum() expects numbers/variables/expressions

    Posted 07/06/22 10:47 AM
    Don't you have a parenthesis issue?
    I mean: in `mdl.sum(z[i]) for i in range(6) for k in range(720))`,  `mdl.sum(z[i])` by itself will resolve to an integer, so you are something like `integer for ... for ...` which in fine is a list generator, which seems to resolve to [1 0 0 0 0 0]
    So it should instead be something like
    mdl.sum(z[i] for i in range(6) for k in range(720))
    ?

    ------------------------------
    Vincent BeraudierVincent Beraudier
    ------------------------------



  • 5.  RE: DOcplexException: Model.sum() expects numbers/variables/expressions

    Posted 07/06/22 09:37 PM
    Dear Vincent,

    I tried #Constraint 3
    mdl.add_constraint(mdl.sum(Z[i] for i in range(Total_T) for k in range(Total_K)) == 1) , however there is an error message as follows.

    AttributeError                            Traceback (most recent call last) 
    /usr/local/lib/python3.7/dist-packages/docplex/mp/aggregator.py in _sum_with_iter(self, args)     240                 try: --> 241                     expr = item.to_linear_expr()     242                     sum_of_nums += expr.get_constant()  
    AttributeError: 'list' object has no attribute 'to_linear_expr' 
    During handling of the above exception, another exception occurred:  
    DOcplexException                          Traceback (most recent call last) 

    5 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: Model.sum() expects numbers/variables/expressions, got: [1, 0, 0, 0, 0, 0]





  • 6.  RE: DOcplexException: Model.sum() expects numbers/variables/expressions

    Posted 07/07/22 05:18 AM
    Dear Nicholas,

    I'm not sure what is the intent of your constraint. Anyway, it contains only constant expressions (no decision variable is involved in the argument of `mdl.sum`).
    The issue is that the expression generates a list of 1-D numpy arrays that cannot be handled by `mdl.sum`.
    I'm afraid I can't help much more here. Sorry,
    Hugues

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