Decision Optimization

Expand all | Collapse all

Using set set_starting_point with integer_var_dict in docplex.cp

  • 1.  Using set set_starting_point with integer_var_dict in docplex.cp

    Posted Wed June 17, 2020 09:13 AM

    Hello, 

    I have a cp problem where i use a  integer_var_dict. I would like to set a starting point of that variable. I'm new to this and don't know if this is a lack of python or cplex knowledge. 

    Basicly:

    import docplex.cp.model as cp 

    m = cp.CpoModel()

    take = m.integer_var_dict(((b, r) 
    for b in BatcheRange for r in Range), 0 , 3 , "take")

    sol = m.create_empty_solution()

    sol[take] = ???

    m.set_starting_point(sol)


    ------------------------------
    Johan Eriksson
    ------------------------------


  • 2.  RE: Using set set_starting_point with integer_var_dict in docplex.cp

    Posted Wed June 17, 2020 09:31 AM
    Edited by ALEX FLEISCHER Wed June 17, 2020 09:32 AM
    In https://www.linkedin.com/pulse/making-optimization-simple-python-alex-fleischer/
    you have many examples but not yet that one.

    from docplex.cp.model import CpoModel
    
    mdl = CpoModel(name='buses')
    nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
    nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
    mdl.add(nbbus40*40 + nbbus30*30 >= 300)
    mdl.minimize(nbbus40*500 + nbbus30*400)
    
    sol=mdl.create_empty_solution()
    sol[nbbus40]=8
    sol[nbbus30]=0
    
    mdl.set_starting_point(sol)
    msol=mdl.solve()
    
    print(msol[nbbus40]," buses 40 seats")
    print(msol[nbbus30]," buses 30 seats") ​
    works fine

    and gives

    ! -------------------------------------------------- CP Optimizer 12.10.0.0 --
     ! Minimization problem - 2 variables, 1 constraint
     ! Using starting point solution
     ! Initial process time : 0.00s (0.00s extraction + 0.00s propagation)
     !  . Log search space  : 19.9 (before), 19.9 (after)
     !  . Memory usage      : 267.0 kB (before), 267.0 kB (after)
     ! Using parallel search with 12 workers.
     ! ----------------------------------------------------------------------------
     !          Best Branches  Non-fixed    W       Branch decision
                            0          2                 -
     + New bound is 0
     ! Starting point is complete and consistent with constraints.
     *          4000        0  0.01s        1      (gap is 100.0%)
                4000        0          2    1            -
     + New bound is 3800 (gap is 5.00%)
     *          3800        0  0.01s        1      (gap is 0.00%)
     ! ----------------------------------------------------------------------------
     ! Search completed, 2 solutions found.
     ! Best objective         : 3800 (optimal - effective tol. is 0)
     ! Best bound             : 3800
     ! ----------------------------------------------------------------------------
     ! Number of branches     : 232
     ! Number of fails        : 188
     ! Total memory usage     : 5.3 MB (5.3 MB CP Optimizer + 0.0 MB Concert)
     ! Time spent in solve    : 0.01s (0.01s engine + 0.00s extraction)
     ! Search speed (br. / s) : 21090.9
     ! ----------------------------------------------------------------------------
    6  buses 40 seats
    2  buses 30 seats​


    regards

    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 3.  RE: Using set set_starting_point with integer_var_dict in docplex.cp

    Posted Wed June 16, 2021 12:58 PM
    Hi @ALEX FLEISCHER Can you please tell me how did you get that log printed? when I rant your code I just got the answer but not the log which you have put in your answer. Thanks ​

    ------------------------------
    SHanaka Perera
    ------------------------------



  • 4.  RE: Using set set_starting_point with integer_var_dict in docplex.cp

    Posted Thu June 17, 2021 12:11 PM
    Edited by ALEX FLEISCHER Thu June 17, 2021 12:20 PM
    Hi,

    I used Idle and saw



    which python tool have you tried ?

    You may also try

    from docplex.cp.model import CpoModel
    
    mdl = CpoModel(name='buses')
    nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
    nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
    mdl.add(nbbus40*40 + nbbus30*30 >= 300)
    mdl.minimize(nbbus40*500 + nbbus30*400)
    
    sol=mdl.create_empty_solution()
    sol[nbbus40]=8
    sol[nbbus30]=0
    
    mdl.set_starting_point(sol)
    msol=mdl.solve(LogPeriod=1)
    
    print(msol.get_solver_log())
    
    print(msol[nbbus40]," buses 40 seats")
    print(msol[nbbus30]," buses 30 seats")​


    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 5.  RE: Using set set_starting_point with integer_var_dict in docplex.cp

    Posted Thu June 17, 2021 12:25 PM
    Alright msol.get_solver_log() this did the trick. 

    Thanks a lot for helping.

    ------------------------------
    SHanaka Perera
    ------------------------------