Decision Optimization

 View Only
  • 1.  Using indicator constraints in CPLEX (Python)

    Posted Thu June 04, 2020 11:48 AM
    Hello,

    I am having some troubles implementing indicator-constraints in my mixed integer linear program ( MILP ). I am using the following code where K and J are some integers:

    z = m.binary_var_matrix( J , K , 'z' )
    t = m.binary_var_list( K , name = 't' )

    for k in K:
         m.add_indicator( t [ k ] , ( m.sum ( z [ j , k ] for j in J ) >= 1 ) , active_value = 1 )

    I want to check whether the sum over the z variables is greater or equal to 1 for each k. When I run m.number_of_indicator_constraints I do see that the indicator constraints are added to my model but when I solve the problem all values of t ( my indicator ) are equal to 0, while for some k the sum over z [ j , k ] >= 1, such that the some values of t should equal 1. Is there something I am missing in my implementation or do I have to use a different function?

    Thank you in advance!

    Lourens

    ------------------------------
    Lourens
    ------------------------------

    #DecisionOptimization


  • 2.  RE: Using indicator constraints in CPLEX (Python)

    Posted Thu June 04, 2020 12:20 PM
    Edited by System Test Fri January 20, 2023 04:44 PM
    Hi,

    could 

    https://community.ibm.com/community/user/datascience/communities/community-home/digestviewer/viewthread?MessageKey=badfe15c-f7d2-46ff-a989-78c7930df811&CommunityKey=ab7de0fd-6f43-47a9-8261-33578a231bb7&tab=digestviewer#bmbadfe15c-f7d2-46ff-a989-78c7930df811

    help ?

    or you prefer

    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*460 + nbbus30*360)
    
    mdl.solve()
    
    for v in mdl.iter_integer_vars():
        print(v," = ",v.solution_value)
    
    print()
    print("with indicator")
    
    notusebus40 = mdl.binary_var(name='notuseBus40')
    notusebus30 = mdl.binary_var(name='notuseBus30')
    
    mdl.add_indicator(notusebus40,(nbbus40==0),active_value = 1)
    mdl.add_indicator(notusebus30,(nbbus30==0),active_value = 1)
    
    nbKindOfBuses = mdl.integer_var(name='nbKindOfBuses')
    mdl.add(nbKindOfBuses==2-notusebus40-notusebus30)
    
    mdl.minimize(nbbus40*460 + nbbus30*360+(nbKindOfBuses-1)*(500))
    
    mdl.solve()
    
    for v in mdl.iter_integer_vars():
        print(v," = ",v.solution_value)
    
    for v in mdl.iter_binary_vars():
        print(v," = ",v.solution_value) 
    ​

    which gives

    nbBus40  =  6.0
    nbBus30  =  2.0
    
    with indicator
    nbBus40  =  0
    nbBus30  =  10.0
    nbKindOfBuses  =  1.0
    notuseBus40  =  1.0
    notuseBus30  =  0​


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



  • 3.  RE: Using indicator constraints in CPLEX (Python)

    Posted Fri June 05, 2020 07:39 AM
    Hi Alex,

    Thanks a lot! I managed to do it with the logical constraint from the link!

    ------------------------------
    Lourens Vale
    ------------------------------