Decision Optimization

 View Only
Expand all | Collapse all

Getting error in callback (lazy constraints) for docplex with Python to solve MILP

  • 1.  Getting error in callback (lazy constraints) for docplex with Python to solve MILP

    Posted Mon May 27, 2024 06:16 AM

    from docplex.mp.model import Model

    from cplex.callbacks import LazyConstraintCallback

    from docplex.mp.callbacks.cb_mixin import *

    -----------

    p_id is the list of lists of customers,

    n is the set of vehicles,

    dth is the fixed parameter, and

    q_prob is the dictionary representing the probability of availability of vehicles (day-wise)

     

    # Creating Model

    trial_mod=Model('My Trial Model',log_output=True)

    # Defining decision variables

    x_ind={(i,d+dth):trial_mod.binary_var(name='x_{0}_{1}'.format(i,d+dth)) for i in n_id for d in range(len(p_id))}

    Other decision variables are also defined but they are not relevant here.

    # Adding constraints using Callback (lazy constraints)

    At integer node (means whenever we obtain the candidate solution), I want to check that \parod_{i\in n} (1-q_i^dx_i^d) >= threshold \forall d. If not then I want to add a cut as

    \sum(x_ind[i,d+dth] for i in n_id if x_^*ind[i,d+dth]==0)+ \sum(1-x_ind[i,d+dth] for i in n_id if x_^*ind[i,d+dth]==1) >= 1 \forall d.

    For that, I have written the callback code as

    class NonLinearConstraintCallback(ConstraintCallbackMixin, LazyConstraintCallback):

            def __init__(self, env)

                LazyConstraintCallback.__init__(self, env)

                ConstraintCallbackMixin.__init__(self)

           def __call__(self):

                for d in range(1,len(p_id)):

                    prob_product = 1.0

                    for i in n_id:

                        ind_vars_value = self.get_values(self.x_ind[i,d+dth])

                        prob_product *= (1 -self.q_prob['nurse_{}_day_{}'.format(i,d+dth)])*ind_vars_value)                   

                    if product < threshold:

                        lhs = self.trial_mod.sum(self.x_ind[i,d+dth] for i in n_id if self.get_values(self.x_ind[i,d+dth])==0)+self.trial_mod.sum(1 - self.x_ind[i,d+dth] for i in n_id if self.get_values(self.x_ind[i,d+dth])==1)

                        self.add(lhs >= 1)

     

    cb = trial_mod.register_callback(NonLinearConstraintCallback)

    cb.x_ind = x_ind

    cb. q_prob   = q_prob

    trial_mod.lazy_callback = cb

     

    when I solve using

    trial_mod.solve(log_output=True)

    It gives errors as

    CPLEX Error  1006: Error during callback.

    Error: Internal error in CPLEX solve: TypeError: expecting name or index.

     

    I request the community please help me to fix this callback issue.


    Thanks in Advance,

    Samiullah.



  • 2.  RE: Getting error in callback (lazy constraints) for docplex with Python to solve MILP
    Best Answer

    IBM Champion
    Posted Mon May 27, 2024 11:11 AM

    Cross-posted at https://or.stackexchange.com/questions/12094/getting-error-in-callback-lazy-constraints-for-docplex-with-python-to-solve-mi.



    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------