Decision Optimization

 View Only
Expand all | Collapse all

mod.add_mip_start(initial_solution) is giving error: AttributeError: 'dict' object has no attribute 'as_mip_start'

  • 1.  mod.add_mip_start(initial_solution) is giving error: AttributeError: 'dict' object has no attribute 'as_mip_start'

    Posted Sat May 18, 2024 07:40 AM
    I am creating a model and defining the variables as:

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

    z_vars={(i,j,k,d+dth):trial_mod.binary_var(name='z_{0}_{1}_{2}_{3}'.format(i,j,k,d+dth)) for d in range(len(p_id)) for i in n_id for j in ([0]+p_id[d]) for k in (p_id[d]+['t']) if j!=k}

    y_vars={(j,d+dth):trial_mod.continuous_var(name='y_{0}_{1}'.format(j,d+dth)) for d in range(len(p_id)) for j in p_id[d]}

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

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

    Then I define the dictionary for the initial solution as P1_soln={}.
    Then add the initial solution as 

    #----u_vars, v_vars, and x_vars----#
        for d in range(len(p_id)):
            for i in n_id:
                P1_soln[u_vars[i,d+dth]]=u_vars[i,d+dth]'s initial_value
                P1_soln[v_vars[i,d+dth]]=v_vars[i,d+dth]'s initial_value
                for j in p_id[d]:
                    P1_soln[x_vars[i,j,d+dth]]=x_vars[i,j,d+dth]'s initial_value
        #----z_vars----#
        for d in range(len(p_id)):
            for i in n_id:
                for j in [0]+p_id[d]:
                    for k in p_id[d]+['t']:
                        if j!=k:
                            P1_soln[z_vars[i,j,k,d+dth]]=z_vars[i,j,k,d+dth]'s initial_value
        #----y_vars----#
        for d in range(len(p_id)):
            for j in p_id[d]:
                P1_soln[y_vars[j,d+dth]]=y_vars[j,d+dth]'s initial_value


    Then I feed the initial solution using the command:
    trial_mod.add_mip_start(P1_soln)

    And, it gives the error as 
    AttributeError: 'dict' object has no attribute 'as_mip_start'

    -------------------------------------------------------------------------------------------------------

    I request the community please help me to resolve this issue.

    Thanks in Advance.


       


  • 2.  RE: mod.add_mip_start(initial_solution) is giving error: AttributeError: 'dict' object has no attribute 'as_mip_start'
    Best Answer

    Posted Mon May 20, 2024 10:48 AM

    Tiny example at https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoowarmstartapi.py

    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)
    
    warmstart=mdl.new_solution()
    warmstart.add_var_value(nbbus40,8)
    warmstart.add_var_value(nbbus30,0)
    mdl.add_mip_start(warmstart)
    
    
    sol=mdl.solve(log_output=True)
    
    for v in mdl.iter_integer_vars():
        print(v," = ",v.solution_value)


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------