Global AI and Data Science

Global AI & Data Science

Train, tune and distribute models with generative AI and machine learning capabilities

 View Only
  • 1.  CPLEX Lazy Constraint callback - docplex

    Posted 19 hours ago
    Hello All,
    I am trying to implement lazy constraint callback. I looked at the usual TSP example on github
    and I found that only 6 out of 26 constraint were being unsatisfied and added. It turns out that this stills needs further constraints and the loop never stops or you know I see the same constraints getting added and added, again and again. 

    Only 6 of 26 are being added because of unsat
                   unsats = self.get_cpx_unsatisfied_cts([ct], sol, tolerance=1e-6)
                    for ct, cpx_lhs, sense, cpx_rhs in unsats:
                        print('Add violated subtour')
                        self.add(cpx_lhs, sense, cpx_rhs)
    So I am unable to come up with the internal ways in which data is represented which is given by  self.get_cpx_unsatisfied_cts([ct], sol, tolerance=1e-6)
    So..stuck in dilemna, I added all my cuts as self.add_constraint(ct)
    Now the below is happening
    *     0+    0                         2017.0000      171.8000            91.48%
          0     0      214.7501     7     2017.0000       Cuts: 5      315   89.35%
          0     0      264.3077     9     2017.0000       Cuts: 9      337   86.90%
          0     0      351.3789     9     2017.0000      Cuts: 19      356   82.58%
          0     0      628.3137     8     2017.0000      Cuts: 14      364   68.85%
          0     2      628.3137     8     2017.0000      664.7353      364   67.04%                        0             0
    Elapsed time = 1.53 sec. (287.14 ticks, tree = 0.01 MB, solutions = 1)
         56    37        cutoff           2017.0000     1119.5000      628   44.50%           x_6_4 D     56     55     13
        120    68     1380.0000     1     2017.0000     1380.0000      921   31.58%        xe_8_9_4 U    120    103      7
        189    92     1380.0000     3     2017.0000     1380.0000     1306   31.58%      xe_17_18_4 U    189    188     10
        267    88        cutoff           2017.0000     1380.0000     1662   31.58%           x_4_4 U    267    129     10
        353   126     1380.0000     2     2017.0000     1380.0000     2088   31.58%           x_4_4 D    353    352     11
        458   141     1380.0000     3     2017.0000     1380.0000     2583   31.58%      xe_17_19_4 D    458    443     11
        535   125     1511.2157     3     2017.0000     1380.0000     2884   31.58%           x_6_4 D    535    533     10
        622   152        cutoff           2017.0000     1380.0000     3242   31.58%           x_6_4 U    622    621      9
        690   157     1503.6667     3     2017.0000     1380.0000     3486   31.58%           x_6_4 D    690    688     11
        992   208     1445.8930     4     2017.0000     1380.0000     4625   31.58%      xe_17_18_4 U    992    819      9
    Elapsed time = 4.41 sec. (3403.92 ticks, tree = 3.37 MB, solutions = 1)
       1288   255        cutoff           2017.0000     1380.0000     5733   31.58%           x_6_4 U   1288   1287     13
       1600   302     1380.0000     2     2017.0000     1380.0000     6864   31.58%      xe_11_13_4 U   1600   1586      7
       1938   337        cutoff           2017.0000     1380.0001     8020   31.58%           x_6_4 D   1938   1936     11

    Implied bound cuts applied:  20
    Mixed integer rounding cuts applied:  7
    Gomory fractional cuts applied:  8

    Root node processing (before b&c):
      Real time             =    1.53 sec. (287.33 ticks)
    Sequential b&c:
      Real time             =    6.23 sec. (6900.99 ticks)
                              ------------
    Total (root+branch&cut) =    7.77 sec. (7188.32 ticks)
    Traceback (most recent call last):
      File "C:\Users\shari\OneDrive\Desktop\RamRam-TrainDISPLIB\Code\ProperCode\BranchAndCut\BandC_controller.py", line 244, in <module>
        process_all_instances()
      File "C:\Users\shari\OneDrive\Desktop\RamRam-TrainDISPLIB\Code\ProperCode\BranchAndCut\BandC_controller.py", line 236, in process_all_instances
        train_partitioner(f"Data\\{instance_name}")
      File "C:\Users\shari\OneDrive\Desktop\RamRam-TrainDISPLIB\Code\ProperCode\BranchAndCut\BandC_controller.py", line 205, in train_partitioner
        mdl_class,mdl = model_solve_bandc(job_dict_cloned, ob_list, filename, idtor_dict, rtoid_dict, min_duration_dict, release_times_dict, save_sol)
      File "C:\Users\shari\OneDrive\Desktop\RamRam-TrainDISPLIB\Code\ProperCode\BranchAndCut\helper_full.py", line 404, in model_solve_bandc
        print("Model solved with status:", mdl.solve_details.status, "and objective value:", mdl.objective_value)
      File "C:\Users\shari\anaconda3\envs\cplexenv\lib\site-packages\docplex\mp\model.py", line 5443, in objective_value
        self._check_has_solution()
      File "C:\Users\shari\anaconda3\envs\cplexenv\lib\site-packages\docplex\mp\model.py", line 5261, in _check_has_solution
        self.fatal("Model<{0}> did not solve successfully", self.name)
      File "C:\Users\shari\anaconda3\envs\cplexenv\lib\site-packages\docplex\mp\model.py", line 1093, in fatal
        self._error_handler.fatal(msg, args)
      File "C:\Users\shari\anaconda3\envs\cplexenv\lib\site-packages\docplex\mp\error_handler.py", line 210, in fatal
        raise DOcplexException(resolved_message)
    docplex.mp.utils.DOcplexException: Model<SINTEF-DISPLIB-Challenge> did not solve successfully

    Now I am not sure what is the error..please let me know asap!

    Thanks
    Hariharan


  • 2.  RE: CPLEX Lazy Constraint callback - docplex

    Posted 18 hours ago

    The code looks like below right now:

            print(f"🔍 Found {len(constraints)} lazy constraints from conflicts")
            # Step 6: Add them lazily
            unsats = self.get_cpx_unsatisfied_cts(constraints, sol, tolerance=1e-6)
            print(f"🔍 Found {len(unsats)} unsatisfied lazy constraints")
            for ct in constraints:
                self.mdl_class.mdl.add_constraint(ct)
            # for ct, cpx_lhs, sense, cpx_rhs in unsats:
            #     print(ct, cpx_lhs, sense, cpx_rhs)
            #     print('Add violated deadlock/resource conflict')
            #     self.add(cpx_lhs, sense, cpx_rhs)
    Notice all the unsat part is all commented out...
    The cpx_lhs, sense, cpx_rhs are difficult to find out by my own...and hence I am unable to figure out how to add all my constraints...for which even the representation is not there..
    Or maybe there is a way to get the representation?
    I think the above way is incorrect since I am using self.mdl_class.mdl.add_constraint(ct) which is the mdl object...whereas as per rule, I am supposed to use the callback object which is self?!!



    Please do reply as soon as possible - I am in a bit of tight hurry!
    THanks
    Hariharan


    ------------------------------
    HARIHARAN SUBRAMANIAN
    ------------------------------