Decision Optimization

 View Only
  • 1.  docplex clone vs copy

    Posted Mon January 25, 2021 06:02 AM
    Edited by System Test Fri January 20, 2023 04:11 PM
    Hi,

    I am using docplex and I want to know the difference between model.copy() and model.clone() .

    I am implementing a branch and price algorithm, and at each new node visited in the search tree, I need to recreate the model without the columns I have generated for the previous node. Thus, I need an efficient way to either clean the model used in the previous node from the new columns (and some rows), or recreate the initial model at each node,  or  (deep) copy the initial model at each node.

    would model.copy() or model.clone() do the job? if so what is the difference ? if not what do you suggest?
    I will also need to add constraint to the new model, however these constraint are defined using another model. how is it possible to add these constraints to the new model?

    Thanks in advance!
    #DecisionOptimization


  • 2.  RE: docplex clone vs copy

    Posted Mon January 25, 2021 01:26 PM
    Edited by System Test Fri January 20, 2023 04:50 PM
    Hi

    from docplex.mp.model import Model
    
    mdl = Model(name='buses')
    nbbus40 = mdl.integer_var(name='nbBus40')
    nbbus30 = mdl.integer_var(name='nbBus30')
    mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
    mdl.minimize(nbbus40*500 + nbbus30*400)
    
    mdl.solve(log_output=False,)
    
    mdl2=mdl.clone()
    mdl3=mdl.copy()
    mdl4=mdl;
    
    
    
    nbbus40.ub=0
    mdl2.solve(log_output=False,)
    mdl3.solve(log_output=False,)
    mdl4.solve(log_output=False,)
    
    print("clone")
    for v in mdl2.iter_integer_vars():
        print(v," = ",v.solution_value)
    
    print("copy")
    for v in mdl3.iter_integer_vars():
        print(v," = ",v.solution_value)
    
    print("= operator")
    for v in mdl4.iter_integer_vars():
        print(v," = ",v.solution_value)​


    gives

    clone
    nbBus40  =  6.0
    nbBus30  =  2.0
    copy
    nbBus40  =  6.0
    nbBus30  =  2.0
    = operator
    nbBus40  =  0
    nbBus30  =  10.0






    #DecisionOptimization